Sub topic 5
Predicate and projection pushdown
This chapter assumes you know basic Polars expressions (pl.col, filter, select) and have seen scan_parquet and explain() from the earlier chapters. Here you will see how the query optimizer moves work down to the file scan, and how to confirm that it did.
What pushdown means
A lazy query is a plan, not a computation. Before running it, Polars rewrites the plan to do less work. Two rewrites matter most for large files:
- Projection pushdown reads only the columns the query uses. Columns that are never referenced are never loaded.
- Predicate pushdown applies filters at the scan. Rows that fail the filter are dropped as early as possible, and for Parquet whole chunks can be skipped.
Both reduce how much data enters memory at all. That is why they come before the streaming engine in this tutorial: streaming processes data in chunks, but pushdown decides how many chunks there are.
Projection pushdown in practice
Take this query:
import polars as pl
lf = (
pl.scan_parquet("events/*.parquet")
.filter(pl.col("status") == "ok")
.select("user_id", "amount")
)
print(lf.explain())The file may have 40 columns. The query touches three: status, user_id, and amount. In the optimized plan, the scan line lists only those columns, usually as a PROJECT 3/40 COLUMNS note. Parquet stores data by column, so the other 37 columns are never read from disk.
Predicate pushdown in practice
In the same plan, the filter on status appears inside the scan node, often as SELECTION: [(col("status")) == ("ok")], instead of as a separate FILTER step above it. Parquet files keep minimum and maximum values for each row group, which is a block of rows. If a row group's statistics show it cannot contain "ok", the reader skips it without decoding it.
CSV has no such statistics. For CSV, predicate pushdown still drops rows early, but every byte must still be read and parsed.
Checking the plan
Do not assume pushdown happened. Read the output of explain() and look for these signs:
| What you want | What to look for in the plan |
|---|---|
| Only needed columns are read | The scan shows `PROJECT n/m COLUMNS` with a small `n` |
| Filter is applied at the scan | A `SELECTION` entry on the scan, not a `FILTER` node above it |
The plan is read from the bottom up. The scan is the lowest node, and it should carry the work.
Writing queries that push down well
- Filter on raw columns with simple comparisons, such as
pl.col("status") == "ok". - Select only the columns you need, or leave selection implicit by referencing only those columns.
- Avoid
collect()midway and then continuing in eager mode. That ends the optimization. - Compare the unoptimized plan with the optimized one, using
lf.explain(optimized=False), to see what changed.
import polars as pl
lf = (
pl.scan_parquet("events/*.parquet")
.filter(pl.col("status") == "ok")
.select("user_id", "amount")
)
print(lf.explain(optimized=False))
print(lf.explain())The first plan shows a full scan with a separate filter and projection. The second shows both folded into the scan.
Common errors
- Reading the wrong plan.
explain()shows the optimized plan by default. Useoptimized=Falseonly when you want the original. - Expecting savings from CSV. Projection and predicate pushdown help less on CSV, because rows must be parsed to find column boundaries.
- Forgetting that pushdown is not streaming. A pushed-down query can still need more memory than you have if the remaining data is large. The next chapters cover the streaming engine for that case.
Exercise
You run explain() on a query over a 40-column Parquet dataset. The plan shows a FILTER node above a scan that reads "40/40 COLUMNS". Name two things the plan tells you, and one change you would try.
Show answer
The scan reads all columns, so projection pushdown did not apply, and the filter runs after the scan, so predicate pushdown did not apply either. Try selecting only the needed columns, and rewrite the filter as a simple comparison on a raw column. Also check that no eager step or computed expression sits between the scan and the filter. Then run explain() again to confirm.