Sub topic 11
Projection pushdown: read only the columns you use
This chapter assumes you know the Polars expression API (select, filter, with_columns) and have seen scan_parquet and explain(). Projection pushdown is one of the optimiser rewrites described earlier in this tutorial.
The idea
A projection is the set of columns a query needs. Projection pushdown moves that set down to the scan node, so the reader loads only those columns. A file with 50 columns costs far less to read if the query touches three.
Eager code can't do this. pl.read_parquet loads every column before you choose any. A lazy query sees the whole plan first, so Polars can work out which columns are used anywhere in it.
A worked example
import polars as pl
lf = (
pl.scan_parquet("events.parquet")
.select("user_id", "ts", "country")
.filter(pl.col("country") == "NO")
.head(5)
)
print(lf.explain())
print(lf.collect())The select names three columns. In the optimised plan, the PARQUET SCAN node lists PROJECT 3/N COLUMNS, where N is the file's column count. The filter also appears inside the scan as a predicate, which is predicate pushdown working alongside projection pushdown.
Parquet stores each column in its own chunk. Polars can skip the chunks of unused columns completely, so the saving is in bytes read from disk, not only in memory.
Columns you never select
Pushdown also applies to columns used only inside a step. Suppose you filter on country but never return it:
import polars as pl
lf = (
pl.scan_parquet("events.parquet")
.filter(pl.col("country") == "NO")
.select("user_id")
)
print(lf.explain())The scan must still read country to evaluate the filter, and it reads user_id for the output. It does not read anything else. The optimiser works out the union of columns needed by every later step.
Where it helps and where it doesn't
Projection pushdown helps most with columnar formats such as Parquet. CSV is row-based, so Polars still has to parse each line, but it can skip materialising columns you don't use, which reduces memory.
Reading all columns is still correct. Pushdown changes the cost, not the result. A query that ends with collect() and no column restriction, such as a bare scan_parquet("x.parquet").collect(), reads every column.
Opaque steps can limit the optimiser. If a Python function receives a whole row, such as map_rows, Polars can't know which columns it reads, so it must keep them all. The chapter on optimisation barriers covers this.
Exercise
A Parquet file has columns a, b, c and d. You run pl.scan_parquet("f.parquet").filter(pl.col("b") > 0).select("a").collect(). Which columns does the scan read, and why?
Show answer
It reads a and b. Column a is needed for the output and column b is needed to evaluate the filter. Columns c and d are never used, so projection pushdown leaves them unread.
Exercise
How can you check that projection pushdown happened for a lazy query?
Show answer
Call explain() on the LazyFrame and look at the scan node. It shows how many columns are projected out of the total. Compare it with explain(optimized=False) to see the difference.