Sub topic 4
Reading the query plan
This chapter assumes you know basic Python and Polars expressions (pl.col, filter, select). It also assumes you have met lazy queries and scan_parquet. Here you will learn to read the plan Polars builds for a lazy query, so you can check what the query will do before it runs.
What a query plan is
A lazy query does not run when you write it. Polars records each step in a logical plan, a tree of operations. Before running anything, the optimizer rewrites that plan to do less work. You can print the result with explain().
import polars as pl
lf = (
pl.scan_parquet("events/*.parquet")
.filter(pl.col("status") == "ok")
.select("user_id", "amount")
)
print(lf.explain())explain() returns a string and does not read the data. It is cheap to call on a query over any file size.
Reading the output
The plan prints as indented text. The scan, which sits at the bottom of the tree, appears on the deepest line, and the operations above it run after it. Read from the bottom up to follow the order in which data flows.
For the query above, the output looks roughly like this. The exact wording differs between Polars versions.
Parquet SCAN [events/*.parquet]
PROJECT 2/5 COLUMNS
SELECTION: [(col("status")) == (String(ok))]Three details matter here:
- SCAN names the source. If you see a
DFnode instead, the data is already in memory and was not scanned from a file. - PROJECT 2/5 COLUMNS means the scan reads only the columns the query needs, out of the five in the file. This is projection pushdown.
- SELECTION shows the filter attached to the scan. The reader can skip data that cannot match. This is predicate pushdown.
The next chapter covers pushdown in detail. For now, treat these lines as a checklist.
Comparing the written and optimized plans
explain() shows the optimized plan by default. Pass optimized=False to see the plan as you wrote it.
import polars as pl
lf = (
pl.scan_parquet("events/*.parquet")
.select("user_id", "amount", "status")
.filter(pl.col("status") == "ok")
)
print(lf.explain(optimized=False))
print("---")
print(lf.explain())In the unoptimized plan, the filter sits above the select, which is the order you wrote. In the optimized plan, the filter moves into the scan. Comparing the two shows exactly what the optimizer changed.
Common mistakes
- Calling
explain()on a DataFrame. Only aLazyFramehasexplain(). If you get anAttributeError, you probably usedread_parquetinstead ofscan_parquet. Call.lazy()on the DataFrame to get aLazyFrame, but note that its data is already in memory. - Expecting a filter to be pushed down when it is not. A filter that depends on a computed column, or on an operation that changes row order meaning, may stay above the scan. If the
SELECTIONline is missing at the scan, look at what sits between the scan and the filter. - Reading the plan top-down. The top line is the last step. Start at the scan.
Summary of what to check
| Plan line | What it tells you | What to do if it is missing |
|---|---|---|
| `SCAN` | Data is read from files | Use `scan_*` instead of `read_*` |
| `PROJECT n/m COLUMNS` | Only needed columns are read | Select columns early, and avoid `select("*")` patterns |
| `SELECTION` at the scan | Filter is applied while reading | Move the filter earlier and use simple column comparisons |
Exercise
You run print(lf.explain()) on a query built from pl.scan_parquet("events/*.parquet"). The output says PROJECT */5 COLUMNS and shows no SELECTION at the scan, though your query filters on status. What does this tell you, and what should you check?
Show answer
The scan will read all five columns and apply the filter later, so neither projection pushdown nor predicate pushdown happened. Check the steps between the scan and the filter, and whether the query selects only the columns it needs. Then compare explain(optimized=False) with explain() to see what the optimizer did.