Sub topic 10
Predicate pushdown: filter early, read less
This chapter assumes you can write Polars expressions with filter, select and with_columns, and that you know what scan_parquet and explain() do. A predicate is a condition that is true or false for each row, such as pl.col("country") == "NO". Predicate pushdown means the optimiser moves that condition as close to the data source as it can.
Why filter position matters
Suppose you write a query that scans a file, adds columns, and only then filters. Run exactly as written, it would load every row and compute the new columns for all of them. Most of that work is thrown away by the filter.
With a LazyFrame, the optimiser sees the whole plan. If the filter does not depend on the new columns, it moves the filter below them. If the source is a file scan, the filter can go into the scan itself.
Seeing it in explain()
This example creates its own Parquet file, so you can run it as shown.
import polars as pl
pl.DataFrame({
"user_id": [1, 2, 3, 4],
"country": ["NO", "SE", "NO", "DK"],
"amount": [10.0, 20.0, 5.0, 7.5],
}).write_parquet("events.parquet")
lf = (
pl.scan_parquet("events.parquet")
.with_columns(amount_eur=pl.col("amount") * 0.09)
.filter(pl.col("country") == "NO")
)
print(lf.explain(optimized=False)) # filter sits above with_columns
print(lf.explain()) # filter appears inside the scan node
print(lf.collect())In the unoptimised plan, the filter is the top node. In the optimised plan, the condition shows up on the scan node as its predicate (the label varies by Polars version, often SELECTION). The with_columns step then only processes rows that already passed.
What the scan does with the predicate
What the reader saves depends on the file format.
- Parquet: files store min and max statistics per row group. A row group whose statistics rule out the predicate can be skipped without being read.
- CSV: there are no such statistics, so the file is still read. Rows that fail the predicate are dropped early, before later steps run on them.
Polars applies a pushed predicate while it reads the data, so rows that fail it are not kept for later steps.
When a filter cannot move
A filter can only move below a step if it does not need that step's output. In the next query, the filter uses a column created by a Python function.
import polars as pl
lf = (
pl.LazyFrame({"x": [1, 2, 3, 4]})
.with_columns(y=pl.col("x").map_elements(lambda v: v * 2, return_dtype=pl.Int64))
.filter(pl.col("y") > 4)
)
print(lf.explain())The filter needs y, and y only exists after the function runs, so the filter stays above with_columns. A filter on x would not have this problem. Writing pl.col("x") * 2 as a native expression also lets the optimiser reason about it. The next chapters cover this in more detail.
Exercise
A query scans a CSV file, then runs .with_columns(total=pl.col("a") + pl.col("b")), then filters pl.col("region") == "EU". Where will the optimiser place the filter, and why can it?
Show answer
It moves the filter into the CSV scan node. The filter only uses region, which with_columns does not change, so the filter does not depend on the new total column and can run first.