Polars internals
The last chapter of the book looks under the hood: how Polars stores data, how it optimizes a query, how to inspect expressions and profile queries, and how to test them. Nanuq runs the same engine, so all of it applies; this chapter keeps the parts with code.
Query optimization
Other optimizations
The optimizer finds a common subplan (here the doubled values, used by two branches) and computes it once:
import nanuq
let values = newDataFrame(series("value", [10, 20, 30, 40, 50, 60])).lazy
let commonSubplan = values.withColumns(col.value * 2)
let branch1 = commonSubplan.select((col.value * 4).alias("value2"))
let branch2 = commonSubplan.select((col.value * 2).alias("value3"))
let combined = concat([branch1, branch2])
echo combined.explain
UNION[maintain_order: true]
PLAN 0:
SELECT [(col("value") * 4).alias("value2")]
CACHE[id: afad8cce-867c-4b9d-af33-9c9155abd2c2]
WITH_COLUMNS:
[(col("value") * 2)]
DF ["value"]; PROJECT */1 COLUMNS
PLAN 1:
SELECT [(col("value") * 2).alias("value3")]
CACHE[id: afad8cce-867c-4b9d-af33-9c9155abd2c2]
END UNION[maintain_order: true]
import polars as pl
values = pl.LazyFrame({"value": [10, 20, 30, 40, 50, 60]})
common_subplan = values.with_columns(pl.col("value") * 2)
branch1 = common_subplan.select(value2=pl.col("value") * 4)
branch2 = common_subplan.select(value3=pl.col("value") * 2)
combined = pl.concat([branch1, branch2])
print(combined.explain())UNION[maintain_order: true]
PLAN 0:
SELECT [(col("value") * 4).alias("value2")]
CACHE[id: 901df4ee-16af-4643-9ff6-63b2096e7e3e]
WITH_COLUMNS:
[(col("value") * 2)]
DF ["value"]; PROJECT */1 COLUMNS
PLAN 1:
SELECT [(col("value") * 2).alias("value3")]
CACHE[id: 901df4ee-16af-4643-9ff6-63b2096e7e3e]
END UNION[maintain_order: true]Successive with_columns calls that don't depend on each other are merged into one, so
both versions of the BMI query below run as a single projection:
var bmi = newDataFrame(
series("weight_kg", [70, 80, 60, 90]),
series("length_cm", [175, 180, 160, 190])).lazy
bmi = bmi
.withColumns((col.weight_kg / col.length_cm).alias("weight_per_cm"))
.withColumns(lit(0).alias("weight_kg_average"))
.withColumns((col.length_cm / 100).alias("length_m"))
.withColumns(col.weight_kg.mean.alias("weight_kg_average"))
nbShow bmi.collect
| weight_kg | length_cm | weight_per_cm | weight_kg_average | length_m |
|---|---|---|---|---|
| i64 | i64 | f64 | f64 | f64 |
| 70 | 175 | 0.4 | 75.0 | 1.75 |
| 80 | 180 | 0.444444 | 75.0 | 1.8 |
| 60 | 160 | 0.375 | 75.0 | 1.6 |
| 90 | 190 | 0.473684 | 75.0 | 1.9 |
bmi = pl.LazyFrame(
{"weight_kg": [70, 80, 60, 90], "length_cm": [175, 180, 160, 190]}
)
bmi = (
bmi.with_columns(weight_per_cm=pl.col("weight_kg") / pl.col("length_cm"))
.with_columns(weight_kg_average=pl.lit(0))
.with_columns(length_m=pl.col("length_cm") / 100)
.with_columns(weight_kg_average=pl.col("weight_kg").mean())
)
bmi.collect()| weight_kg | length_cm | weight_per_cm | weight_kg_average | length_m |
|---|---|---|---|---|
| i64 | i64 | f64 | f64 | f64 |
| 70 | 175 | 0.4 | 75.0 | 1.75 |
| 80 | 180 | 0.444444 | 75.0 | 1.8 |
| 60 | 160 | 0.375 | 75.0 | 1.6 |
| 90 | 190 | 0.473684 | 75.0 | 1.9 |
Checking your expressions
meta namespace examples
The meta namespace answers questions about an expression itself, without running it:
var expr1 = col.name
var expr2 = lit("constant")
echo "Is ", expr1, " a column: ", expr1.meta.isColumn
echo "Is ", expr2, " a column: ", expr2.meta.isColumn
Is col("name") a column: true
Is "constant" a column: false
expr1 = pl.col("name")
expr2 = pl.lit("constant")
print(f"Is {expr1} a column: {expr1.meta.is_column()}")
print(f"Is {expr2} a column: {expr2.meta.is_column()}")Is col("name") a column: True
Is "constant" a column: Falseecho "Is ", expr1, " a literal: ", expr1.meta.isLiteral
echo "Is ", expr2, " a literal: ", expr2.meta.isLiteral
Is col("name") a literal: false
Is "constant" a literal: true
print(f"Is {expr1} a literal: {expr1.meta.is_literal()}")
print(f"Is {expr2} a literal: {expr2.meta.is_literal()}")Is col("name") a literal: False
Is "constant" a literal: Trueexpr1 = col.age * 2
expr2 = col.name.alias("username")
# Get output names
echo expr1, " output name: ", expr1.meta.outputName
echo expr2, " output name: ", expr2.meta.outputName
[(col("age")) * (dyn int: 2)] output name: age
col("name").alias("username") output name: username
expr1 = pl.col("age") * 2
expr2 = pl.col("name").alias("username")
# Get output names
print(f"{expr1} output name: {expr1.meta.output_name()}")
print(f"{expr2} output name: {expr2.meta.output_name()}")[(col("age")) * (dyn int: 2)] output name: age
col("name").alias("username") output name: usernamevar expr = col.original_name.alias("new_name")
let originalExpr = expr.meta.undoAliases
echo originalExpr.meta.outputName
original_name
expr = pl.col("original_name").alias("new_name")
original_expr = expr.meta.undo_aliases()
original_expr.meta.output_name()'original_name'
expr = col.origin.alias("destination")
echo expr.meta.rootNames
@["origin"]
expr = pl.col("origin").alias("destination")
expr.meta.root_names()['origin']
Profiling Polars
Tests in Polars
Comparing DataFrames and Series
Polars has assertions to compare frames and series in tests. In Nanuq they are in the
module nanuq/testing, and they raise Nim's AssertionDefect, which unittest reports as
a failed check.
import nanuq/testing
let floats = newDataFrame:
a: [1.0, 2.0, 3.0, 4.0]
let differentFloats = newDataFrame:
a: [1.001, 2.0, 3.0, 4.0]from polars.testing import (
assert_series_equal,
assert_frame_equal,
assert_series_not_equal,
assert_frame_not_equal,
)
floats = pl.DataFrame({"a": [1.0, 2.0, 3.0, 4.0]})
different_floats = pl.DataFrame({"a": [1.001, 2.0, 3.0, 4.0]})By default floats must be equal up to a tiny relative difference, so these two frames differ:
try:
assertFrameEqual(floats, differentFloats)
except AssertionDefect as e:
echo e.name
AssertionDefect
try:
assert_frame_equal(floats, different_floats)
except AssertionError as e:
print(type(e).__name__)AssertionError
With a relative tolerance of 1% they are equal:
assertFrameEqual(floats, differentFloats, rtol = 0.01)
echo "The DataFrames are equal."
The DataFrames are equal.
assert_frame_equal(floats, different_floats, rel_tol=0.01)
print("The DataFrames are equal.")The DataFrames are equal.
The data types are compared too: an Int8 frame equals only an Int8 frame.
block:
let result = newDataFrame(series("a", [1, 3]), series("b", [2, 4]))
.cast({"a": Int8, "b": Int8})
let expected = newDataFrame:
a: [1'i8, 3]
b: [2'i8, 4]
assertFrameEqual(result, expected)
echo "DataFrames are equal"
DataFrames are equal
result = pl.DataFrame({"a": [1, 3], "b": [2, 4]}).cast(
pl.Schema({"a": pl.Int8, "b": pl.Int8})
)
expected = pl.DataFrame(
{"a": [1, 3], "b": [2, 4]}, schema={"a": pl.Int8, "b": pl.Int8}
)
assert_frame_equal(result, expected)
print("DataFrames are equal")DataFrames are equal
import nanuqbook
bookInit()
nbText: "# Polars internals"
attribution definitiveGuide(18, "Polars Internals",
"Left out: the examples on the NYC taxi trips (several GB of Parquet files), `show_graph` " &
"(it draws plans with Graphviz; the printed plan of `explain` is shown instead), " &
"`pl.from_repr`, the timing of `%%time` cells and `profile`, which Polars 2 removed.")
nbText: """
The last chapter of the book looks under the hood: how Polars stores data, how it
optimizes a query, how to inspect expressions and profile queries, and how to test them.
Nanuq runs the same engine, so all of it applies; this chapter keeps the parts with code.
## Query optimization
### Other optimizations
The optimizer finds a *common subplan* (here the doubled values, used by two branches) and
computes it once:
"""
tabs """
import polars as pl
values = pl.LazyFrame({"value": [10, 20, 30, 40, 50, 60]})
common_subplan = values.with_columns(pl.col("value") * 2)
branch1 = common_subplan.select(value2=pl.col("value") * 4)
branch2 = common_subplan.select(value3=pl.col("value") * 2)
combined = pl.concat([branch1, branch2])
print(combined.explain())
""":
nbCode:
import nanuq
let values = newDataFrame(series("value", [10, 20, 30, 40, 50, 60])).lazy
let commonSubplan = values.withColumns(col.value * 2)
let branch1 = commonSubplan.select((col.value * 4).alias("value2"))
let branch2 = commonSubplan.select((col.value * 2).alias("value3"))
let combined = concat([branch1, branch2])
echo combined.explain
nbText: """
Successive `with_columns` calls that don't depend on each other are merged into one, so
both versions of the BMI query below run as a single projection:
"""
tabs """
bmi = pl.LazyFrame(
{"weight_kg": [70, 80, 60, 90], "length_cm": [175, 180, 160, 190]}
)
bmi = (
bmi.with_columns(weight_per_cm=pl.col("weight_kg") / pl.col("length_cm"))
.with_columns(weight_kg_average=pl.lit(0))
.with_columns(length_m=pl.col("length_cm") / 100)
.with_columns(weight_kg_average=pl.col("weight_kg").mean())
)
bmi.collect()
""":
nbCode:
var bmi = newDataFrame(
series("weight_kg", [70, 80, 60, 90]),
series("length_cm", [175, 180, 160, 190])).lazy
bmi = bmi
.withColumns((col.weight_kg / col.length_cm).alias("weight_per_cm"))
.withColumns(lit(0).alias("weight_kg_average"))
.withColumns((col.length_cm / 100).alias("length_m"))
.withColumns(col.weight_kg.mean.alias("weight_kg_average"))
nbShow bmi.collect
nbText: """
## Checking your expressions
### meta namespace examples
The `meta` namespace answers questions about an expression itself, without running it:
"""
tabs """
expr1 = pl.col("name")
expr2 = pl.lit("constant")
print(f"Is {expr1} a column: {expr1.meta.is_column()}")
print(f"Is {expr2} a column: {expr2.meta.is_column()}")
""":
nbCode:
var expr1 = col.name
var expr2 = lit("constant")
echo "Is ", expr1, " a column: ", expr1.meta.isColumn
echo "Is ", expr2, " a column: ", expr2.meta.isColumn
tabs """
print(f"Is {expr1} a literal: {expr1.meta.is_literal()}")
print(f"Is {expr2} a literal: {expr2.meta.is_literal()}")
""":
nbCode:
echo "Is ", expr1, " a literal: ", expr1.meta.isLiteral
echo "Is ", expr2, " a literal: ", expr2.meta.isLiteral
tabs """
expr1 = pl.col("age") * 2
expr2 = pl.col("name").alias("username")
# Get output names
print(f"{expr1} output name: {expr1.meta.output_name()}")
print(f"{expr2} output name: {expr2.meta.output_name()}")
""":
nbCode:
expr1 = col.age * 2
expr2 = col.name.alias("username")
# Get output names
echo expr1, " output name: ", expr1.meta.outputName
echo expr2, " output name: ", expr2.meta.outputName
tabs """
expr = pl.col("original_name").alias("new_name")
original_expr = expr.meta.undo_aliases()
original_expr.meta.output_name()
""":
nbCode:
var expr = col.original_name.alias("new_name")
let originalExpr = expr.meta.undoAliases
echo originalExpr.meta.outputName
tabs """
expr = pl.col("origin").alias("destination")
expr.meta.root_names()
""":
nbCode:
expr = col.origin.alias("destination")
echo expr.meta.rootNames
nbText: """
## Profiling Polars
"""
nanuq """
The book's `profile` runs a query and also returns when each node of the plan started and
ended. Polars 2.0 removed it, from Python and from the Rust crate that Nanuq builds on,
because its per-node timings mislead under the streaming engine. To see where a query
spends its time, read its plan with `explain` and time `collect`.
"""
nbText: """
## Tests in Polars
### Comparing DataFrames and Series
Polars has assertions to compare frames and series in tests. In Nanuq they are in the
module `nanuq/testing`, and they raise Nim's `AssertionDefect`, which `unittest` reports as
a failed check.
"""
tabs """
from polars.testing import (
assert_series_equal,
assert_frame_equal,
assert_series_not_equal,
assert_frame_not_equal,
)
floats = pl.DataFrame({"a": [1.0, 2.0, 3.0, 4.0]})
different_floats = pl.DataFrame({"a": [1.001, 2.0, 3.0, 4.0]})
""":
nbCode:
import nanuq/testing
let floats = newDataFrame:
a: [1.0, 2.0, 3.0, 4.0]
let differentFloats = newDataFrame:
a: [1.001, 2.0, 3.0, 4.0]
nbText: """
By default floats must be equal up to a tiny relative difference, so these two frames
differ:
"""
tabs """
try:
assert_frame_equal(floats, different_floats)
except AssertionError as e:
print(type(e).__name__)
""":
nbCode:
try:
assertFrameEqual(floats, differentFloats)
except AssertionDefect as e:
echo e.name
nbText: """
With a relative tolerance of 1% they are equal:
"""
tabs """
assert_frame_equal(floats, different_floats, rel_tol=0.01)
print("The DataFrames are equal.")
""":
nbCode:
assertFrameEqual(floats, differentFloats, rtol = 0.01)
echo "The DataFrames are equal."
nbText: """
The data types are compared too: an `Int8` frame equals only an `Int8` frame.
"""
tabs """
result = pl.DataFrame({"a": [1, 3], "b": [2, 4]}).cast(
pl.Schema({"a": pl.Int8, "b": pl.Int8})
)
expected = pl.DataFrame(
{"a": [1, 3], "b": [2, 4]}, schema={"a": pl.Int8, "b": pl.Int8}
)
assert_frame_equal(result, expected)
print("DataFrames are equal")
""":
nbCode:
block:
let result = newDataFrame(series("a", [1, 3]), series("b", [2, 4]))
.cast({"a": Int8, "b": Int8})
let expected = newDataFrame:
a: [1'i8, 3]
b: [2'i8, 4]
assertFrameEqual(result, expected)
echo "DataFrames are equal"
nanuq """
The book builds `expected` from the printed table with `pl.from_repr`; both tabs here
build it from values.
"""
bookSave()
On GitHub: book/definitive-guide/polars_internals.nim