Sub topic 6
How the Streaming Engine Processes Data in Chunks
This chapter assumes you know basic Python, Polars expressions (pl.col, filter, group_by), and the difference between a DataFrame and a LazyFrame. You should also have seen scan_parquet and explain from earlier chapters.
The core idea: process a piece, then discard it
An eager pipeline holds the whole dataset in memory at each step. The streaming engine works differently. It splits the input into small pieces, pushes each piece through the query, and releases it before it reads too many more.
Polars calls these pieces morsels. A morsel is a chunk of rows, small enough that several can sit in memory at once without pressure. The engine reads morsels from the scan, passes them through the operations in your query, and sends results on to the next step.
A pipeline of nodes
The streaming engine turns the optimized query plan into a graph of nodes. Each node does one job: read a source, filter, compute columns, aggregate, or write output. Morsels flow along the edges between nodes.
Nodes run in parallel across CPU cores. While one morsel is being filtered, the scan can already be reading the next one. This is why streaming can be fast as well as memory-friendly.
Streaming versus blocking operations
Operations fall into two groups, and the difference explains most memory behavior.
| Kind | Examples | Memory behavior |
|---|---|---|
| Streaming (per morsel) | `filter`, `select`, `with_columns`, casts | Each morsel is handled alone and then dropped. Memory stays low. |
| Accumulating | `group_by`, `join`, `sort`, `unique` | The node must keep state across morsels, such as running totals, a hash table, or buffered rows. |
A filter needs only the current row to decide what to do. A group_by with a sum needs to remember one running total per group. The input rows can be discarded after each update, but the totals persist. If you have a few thousand groups, that state is tiny. If you have hundreds of millions of distinct keys, the state itself can exceed memory.
Running the query
The following example reads many Parquet files, filters, and aggregates. Only one morsel per worker is in flight at a time, plus the group totals.
import polars as pl
lf = (
pl.scan_parquet("events/*.parquet")
.filter(pl.col("status") == "ok")
.group_by("user_id")
.agg(pl.col("amount").sum().alias("total_amount"))
)
result = lf.collect(engine="streaming")Here the filter works per morsel. The group_by node holds one running sum for each user_id. The final result has one row per user, so it is much smaller than the input.
What this means in practice
- Peak memory depends on morsel size, the number of worker threads, and the state held by accumulating nodes. It does not depend on the input size alone.
- Query results are the same as with the in-memory engine. Only the execution strategy changes. Row order from a parallel run is not guaranteed unless you sort explicitly.
- A query made only of per-morsel operations can handle a file much larger than RAM.
Common mistakes
- Assuming that
collect(engine="streaming")bounds memory for any query. A join between two huge tables or a sort of a huge column still needs large buffers. - Reading with
pl.read_csvfirst and then calling.lazy(). The file is already fully loaded, so streaming has nothing to save. - Expecting identical row order across runs without a
sort.
Exercise
A query scans a 200 GB Parquet dataset, filters rows, and computes a sum per country (about 200 distinct values). Another query computes a sum per session_id (about 2 billion distinct values). Both run with collect(engine="streaming"). Which is more likely to run out of memory, and why?
Show answer
The session_id query. The streaming engine discards each morsel after updating the running totals, so the input size does not matter. The group-by node must keep one total per distinct key. With 200 countries that state is tiny. With about 2 billion session IDs the state can itself exceed available memory.