Sub topic 2
How the tools execute work: eager, lazy, single-node, distributed
This chapter assumes you can write basic Python, read simple SQL, and have loaded a dataset with pandas before. It explains two questions that shape how each tool behaves: when does work run, and where does it run?
Two axes of execution
Every tool in this tutorial can be placed on two axes.
- Eager or lazy. An eager tool runs each step as soon as you call it. A lazy tool records your steps as a plan and runs them later, usually when you ask for a result.
- Single-node or distributed. A single-node tool uses one machine, possibly with many CPU cores. A distributed tool splits work across several machines that coordinate over a network.
| Tool | When work runs | Where work runs |
|---|---|---|
| pandas | Eager | One machine, mostly one core |
| Polars | Eager or lazy | One machine, all cores |
| DuckDB | Lazy (SQL query is planned, then run) | One machine, all cores |
| Spark | Lazy | Cluster, or one machine in local mode |
Eager execution
In pandas, each line does its work immediately and holds the result in memory.
import pandas as pd
df = pd.read_parquet("sales.parquet", columns=["region", "revenue"])
result = df.groupby("region", as_index=False)["revenue"].sum()
print(result)read_parquet loads the selected columns into memory right away. groupby(...).sum() runs right away too. This is easy to debug, because you can inspect any intermediate variable. The cost is that pandas cannot see your whole pipeline. It cannot skip work you never use or reorder steps to be cheaper.
Lazy execution
A lazy tool builds a query plan first. Polars shows this with scan_parquet, which does not read the data. Nothing runs until collect().
import polars as pl
result = (
pl.scan_parquet("sales.parquet")
.group_by("region")
.agg(pl.col("revenue").sum())
.collect()
)
print(result)Because the engine sees the whole plan, it can optimize it, for example by reading only the columns the query needs. Chapter 9 covers this in detail. DuckDB works the same way from the SQL side: it parses your query, plans it, then executes it.
Spark is also lazy. Transformations such as groupBy and agg only describe the work. An action such as show() triggers execution.
from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.appName("sales").getOrCreate()
result = (
spark.read.parquet("sales.parquet")
.groupBy("region")
.agg(F.sum("revenue").alias("revenue"))
)
result.show()Single-node and distributed
pandas, Polars, and DuckDB run inside one Python process on one machine. Polars and DuckDB split work across CPU cores. pandas mostly uses one core.
Spark divides data into partitions and sends them to executors, which are worker processes, possibly on many machines. A driver process builds the plan and coordinates the executors. This lets Spark handle data larger than any one machine, but each stage adds coordination, startup, and network cost. Chapter 11 covers those costs.
Common errors
- Forgetting
collect()in Polars, then wondering why you hold aLazyFrameinstead of data. - Forgetting a Spark action, so nothing seems to run.
- Assuming a lazy tool is always faster. For tiny data, planning overhead can dominate.
- Assuming Spark is faster on one machine. Local mode still pays for its distributed design.
Exercise
You write a Polars pipeline starting with pl.scan_parquet("big.parquet"), add a filter and a group-by, and run the script. It finishes instantly and no result appears. What is the most likely cause, and how do you fix it?
Show answer
The pipeline is lazy, so it only built a plan. Nothing executed because no call such as .collect() was made. Add .collect() at the end to run the query and return a DataFrame.