Sub topic 7
A query is a plan: logical plans as trees
This chapter assumes you know the Polars expression API (pl.col, filter, group_by, agg) and have seen lazy() and collect(). It explains what a LazyFrame stores while it waits to run.
What a LazyFrame holds
A LazyFrame does not hold results. It holds a logical plan, which is a description of the work to do. Each method you call adds one step to that description and returns a new LazyFrame. No data is processed while you build it.
The plan is a tree. Each step is a node. A node takes the output of the node below it as its input. The data source, such as an in-memory table or a file scan, is at the bottom (the leaf). The last operation you wrote is at the top (the root).
import polars as pl
lf = (
pl.LazyFrame({
"city": ["Oslo", "Lima", "Oslo", "Cairo"],
"temp": [3.0, 19.5, 5.0, 28.0],
})
.filter(pl.col("temp") > 4)
.group_by("city")
.agg(pl.col("temp").mean().alias("avg_temp"))
.sort("city")
)
print(lf.explain(optimized=False))
print(lf.collect())The first print shows the plan without running anything. The second runs it with collect() and prints the result.
The plan as a tree
The query above becomes a chain of four nodes. Data flows from the source at the bottom up to the sort at the top.
This example is a straight chain because each node has one input. Operations such as joins and concatenations have two or more inputs, so their nodes branch. A query with a join has two subtrees feeding one join node.
Reading the printed plan
explain(optimized=False) prints the same tree as indented text. Polars prints the root first, so the last operation you wrote appears at the top. The source appears at the bottom, usually as a DF node for in-memory data or a Csv SCAN node for a file. The exact wording varies between Polars versions, so look for the order of the nodes rather than the labels.
The unoptimised plan matches your code step for step. That matters because the optimiser works by rewriting this tree into another tree that gives the same result. The next chapter reads these plans in more detail.
Why a plan is useful
A plan can be inspected before it runs. Polars can see that only city and temp are used, that a filter could be applied sooner, or that two branches compute the same thing. An eager DataFrame method runs at once and keeps nothing that could be reordered afterwards.
Common mistakes
- Expecting a LazyFrame method to change the frame. Methods return a new LazyFrame, so keep the result:
lf = lf.filter(...). - Printing a LazyFrame and expecting rows. Printing shows a description of the plan, not data. Call
collect()for the data. - Treating the plan order as a guarantee of execution order. The plan records what you asked for. The optimiser may run steps in a different order.
Exercise
In a plan that reads a CSV, filters rows, and then selects two columns, which node is at the leaf of the tree and which is at the root?
Show answer
The CSV scan is the leaf, because it is the data source at the bottom. The select of the two columns is the root, because it is the last operation written.