Sub topic 12
Unsupported operations and fallback behavior
This chapter assumes you can write a lazy Polars query with scan_parquet, filter, and group_by. It also assumes you have run one with collect(engine="streaming"). Here you learn what happens when a query contains an operation the streaming engine cannot run in chunks.
Streaming is not all-or-nothing
The streaming engine does not implement every Polars operation. When you request engine="streaming", Polars converts your query plan into a graph of streaming nodes. If a part of the plan has no streaming implementation, Polars does not raise an error. It runs that part with the regular in-memory engine and carries on.
This keeps your queries working. It also means a query can use far more memory than you expect, because the fallback part needs its whole input in RAM.
What tends to fall back
Support grows with each Polars release, so no fixed list is reliable. Operations that have needed fallback in some versions include:
- Some window expressions, such as
overon certain inputs. - Rolling and dynamic group-by operations.
- Reshaping operations such as
pivot. - Python user-defined functions, such as
map_elementsandmap_batches. These run Python code on whole columns or on single values.
Check the release notes and documentation for your installed version before you rely on any of these.
Detecting a fallback
collect(engine="streaming") does not tell you when it falls back. You can look at the plan instead.
import polars as pl
lf = pl.LazyFrame(
{
"user_id": [1, 1, 2, 2, 3],
"amount": [10.0, 20.0, 5.0, 15.0, 7.0],
}
).with_columns(
pl.col("amount").sum().over("user_id").alias("user_total")
)
# The optimized logical plan, as in the earlier chapter.
print(lf.explain())
# The physical plan the streaming engine would run.
# Nodes that appear as in-memory fallbacks are visible here.
print(lf.explain(engine="streaming"))
print(lf.collect(engine="streaming"))Read the streaming plan from the scan upward. Look for nodes marked as in-memory. Their position matters. A fallback node near the end of a query that has already filtered and aggregated the data is usually cheap. A fallback node right after the scan loads the full file.
You can also set the environment variable POLARS_VERBOSE=1 before running. Polars then logs extra details about execution, which may include fallback messages. The exact messages differ between versions.
POLARS_VERBOSE=1 python my_query.pyWorking around a fallback
Try these in order:
- Replace the expression. Many
map_elementscalls can be rewritten with native expressions such aspl.when, string functions, or arithmetic. Native expressions are faster and are more likely to stream. - Reduce the data first. Put
filter,select, andgroup_bybefore the unsupported step. The fallback then sees a smaller input. - Split the work. Use
sink_parquetto write an intermediate file with the streaming-friendly part of the query. Then scan that file and apply the unsupported step. - Upgrade Polars. An operation that falls back today may stream in a newer release.
import polars as pl
# Stage 1: streaming-friendly work, written to disk.
(
pl.scan_parquet("events/*.parquet")
.filter(pl.col("status") == "ok")
.select("user_id", "amount")
.sink_parquet("events_ok.parquet")
)
# Stage 2: the step that may fall back now sees much less data.
result = (
pl.scan_parquet("events_ok.parquet")
.with_columns(pl.col("amount").sum().over("user_id").alias("user_total"))
.collect(engine="streaming")
)Exercise
A query using engine="streaming" runs out of memory. The query ends with a map_elements call on a column read straight from a very large Parquet file. What is the likely cause, and what are two ways to fix it?
Show answer
map_elements has no streaming implementation, so that step probably fell back to the in-memory engine and loaded its full input. Two fixes: rewrite the step with native Polars expressions, or filter and select columns before it (or sink an intermediate Parquet file first) so the fallback sees much less data.