Sub topic 7
Running a query with the streaming engine
This chapter assumes you can write basic Polars expressions (pl.col, filter, group_by) and know how scan_parquet creates a lazy query. It needs a recent Polars release that supports engine="streaming".
What the engine argument does
A lazy query does nothing until you call collect(). By default, collect() runs the query on the in-memory engine, which processes whole columns at once. Passing engine="streaming" runs the same query on the streaming engine. This engine reads the input in chunks, pushes each chunk through the query steps, and combines partial results as it goes.
Your query does not change. Only the way it executes changes.
A complete example
This query totals the amount per user from many Parquet files. The raw input can be far larger than RAM, because only one chunk at a time plus the running totals must fit.
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")
print(result.head())result is an ordinary DataFrame. Everything you do with it afterward runs eagerly, in memory.
What stays in memory
Streaming limits the memory used while the query runs. It does not shrink the answer. Three things still occupy memory:
- The chunks currently being processed.
- State kept by operations that must see many rows, such as the running totals in a
group_by. - The final
DataFramethatcollectreturns.
In the example above, the output has one row per distinct user_id. If you have a million users, that is a million rows, which is small. If the query had no group_by and kept most of the rows, the result could be as large as the input and collect could still run out of memory.
Choosing the engine for a session
If you want streaming to be the default for collect calls, you can set an engine affinity once instead of passing the argument each time.
import polars as pl
pl.Config.set_engine_affinity("streaming")
total = (
pl.scan_parquet("events/*.parquet")
.select(pl.col("amount").sum())
.collect()
)
print(total)An explicit engine= argument on a call still takes precedence over this setting.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Out-of-memory error, even with streaming | The result, or a `group_by`, `join` or `sort` state, is too large | Filter or aggregate earlier, or sink the result |
| Memory use is the same as before | You used `read_csv` or `read_parquet`, which load eagerly | Switch to `scan_csv` or `scan_parquet` |
| Query runs but is not faster | A step is not supported by the streaming engine | Check the plan, as described in later chapters |
| `collect` ignores streaming | An older Polars version | Upgrade Polars |
The second row is the most common mistake. The engine argument only exists on LazyFrame.collect. If you create the frame with an eager read_* function, the data is already in memory before streaming can help.
Exercise
A colleague writes pl.read_parquet("big/*.parquet").lazy().filter(pl.col("x") > 0).collect(engine="streaming") and still runs out of memory. Why, and what is the fix?
Show answer
read_parquet loads all files into memory eagerly before .lazy() is called, so streaming never gets a chance to process chunks. Replace it with pl.scan_parquet("big/*.parquet") so the data is read lazily, and then collect with engine="streaming".
Exercise
You run a streaming query that filters 500 million rows down to 400 million rows with no aggregation, and collect(engine="streaming") fails with an out-of-memory error. Why does streaming not prevent this?
Show answer
Streaming bounds the memory used during processing, but collect still has to assemble the full 400-million-row result as a DataFrame. Aggregate further so the result is small, or write the output with sink_parquet instead of collecting it.