Sub topic 13
Expression simplification and type coercion
This chapter assumes you can read explain() output and know what a LazyFrame is. Two quieter optimiser steps work on the expressions inside a plan. Expression simplification rewrites expressions into cheaper equivalents. Type coercion makes the data types in an expression agree.
Expression simplification
Before running a query, the optimiser looks for expressions it can compute ahead of time or replace with something simpler. The standard example is constant folding. If part of an expression depends only on literal values, Polars evaluates it once during planning. It does not evaluate it on every row.
import polars as pl
lf = pl.LazyFrame({"x": [1, 2, 3, 4]})
query = lf.with_columns(
y=pl.col("x") + (pl.lit(1) + pl.lit(2))
)
print(query.explain(optimized=False))
print(query.explain())
print(query.collect())In Python, 1 + 2 would be computed before Polars saw it. Wrapping the numbers in pl.lit keeps the addition inside the expression tree. The unoptimised plan shows the addition of two literals. The optimised plan should show a single literal 3. The exact text of the plan differs between Polars versions, so compare the two outputs and don't match strings.
The result is the same either way. Simplification only changes how much work is done, which is why you can leave it on without worrying about correctness.
Type coercion
Type coercion is a different kind of step. When an expression mixes types, Polars has to decide on a common type, and it inserts casts where needed.
import polars as pl
lf = pl.LazyFrame({"x": [1, 2, 3, 4]}) # x is Int64
query = lf.filter(pl.col("x") > 2.5)
print(query.explain(optimized=False))
print(query.explain())
print(query.collect())The column is Int64 and the literal 2.5 is a float. Comparing them requires a common type, so the optimised plan casts the column to Float64 for the comparison. Rows 3 and 4 pass the filter. The unoptimised plan has no such cast, because coercion hasn't run yet.
Why the distinction matters
| Step | Purpose | Changes the result? |
|---|---|---|
| Expression simplification | Do less work | No |
| Type coercion | Make mixed types valid | It decides the type used |
Simplification is a pure speed-up. Coercion is part of defining what the query means. For example, an integer column compared with a float literal is compared as floats.
Common mistakes
- Expecting a cast to be absent from the optimised plan. A cast in
explain()is usually coercion doing its job. - Relying on implicit coercion for precision-sensitive work. If you need a particular type, cast explicitly with
.cast(pl.Int64)so the intent is visible in the code. - Treating
explain()text as a stable format. Use it to learn what happened, not to build tests.
Exercise
A LazyFrame has an Int64 column "n". You write lf.filter(pl.col("n") > 1.5). Which part of the optimiser makes this comparison valid, and what do you expect to see in the optimised plan?
Show answer
Type coercion. It finds a common type for the integer column and the float literal. The optimised plan should show a cast of "n" to Float64 inside the filter condition, which the unoptimised plan lacks.