Sub topic 12
Slice pushdown: head and limit reach the scan
This chapter assumes you can build a LazyFrame, call collect(), and read basic explain() output. See the earlier chapters on those topics if not.
What slice pushdown does
A slice keeps only some rows of a result, for example head(5), limit(5) or slice(0, 5). Slice pushdown moves that row limit down the plan, as close to the data source as the semantics allow. If it reaches the scan, Polars can stop reading the file after it has enough rows.
The result stays the same. Only the amount of work changes.
Worked example
The script below writes a one-million-row CSV file to a temporary folder. It then scans the file, selects two columns and takes five rows.
import tempfile
from pathlib import Path
import polars as pl
path = Path(tempfile.mkdtemp()) / "numbers.csv"
n = 1_000_000
pl.DataFrame({"id": range(n), "value": [i * 0.5 for i in range(n)]}).write_csv(path)
lf = pl.scan_csv(path).select("id", "value").head(5)
print(lf.explain(optimized=False)) # head sits on top of the scan
print(lf.explain()) # the row limit appears in the scan node
print(lf.collect())In the unoptimised plan, the slice is the top node. It would run after the whole file had been read. In the optimised plan, the CSV scan node carries the row limit, so the reader can stop early. The exact label for the limit differs between Polars versions, so look for the row count in the scan line.
When the slice cannot move
A slice can only move past a step if doing so cannot change which rows end up in the result. Some common cases follow.
| Query shape | Slice reaches the scan? | Reason |
|---|---|---|
| `select(...)` then `head(n)` | Yes | Selecting columns does not change which rows come first. |
| `with_columns(...)` then `head(n)` | Usually | Row-wise expressions keep row order. |
| `filter(...)` then `head(n)` | No | The first n rows before the filter differ from the first n rows after it. |
| `sort(...)` then `head(n)` | No | Every row must be seen to know which come first. |
| `group_by(...)` then `head(n)` | No | Groups need all of their rows before the result exists. |
Common mistakes
- Calling
head()on a DataFrame you have already read in full. The slice then saves nothing, because the data is already in memory. Usescan_csvorscan_parquetand slice the LazyFrame. - Expecting a sampled result from
head(). It returns the first rows in file order, which may not represent the whole file. - Putting
sortbeforeheadand expecting a fast read. Polars must still scan all the data. - Placing a Python UDF between the scan and the slice. As the barriers chapter explains, opaque steps can stop the optimiser from moving the slice.
Exercise
You run pl.scan_csv("big.csv").filter(pl.col("score") > 90).head(10). Why is this not a plain case of slice pushdown, and which optimisation applies to the filter?
Show answer
The slice cannot move before the filter, because the first 10 rows of the file are not the first 10 rows that pass the filter. The filter itself is handled by predicate pushdown, which moves the condition into the scan node.