Materialization Points
AQE can change a plan only at a materialization point: a moment when a stage has finished and its output is complete, with its size known. Those moments are the exchanges: a shuffle exchange's output is its shuffle files, a broadcast exchange's is a table collected to the driver. Between 2 exchanges the plan is fixed, because nothing finished exists there to measure.
Query stages and the AQE loop
With AQE on, the physical plan sits under an AdaptiveSparkPlan node. Spark cuts that plan at every Exchange into query stages. A query stage below a shuffle exchange is the map side of that shuffle: it reads its input, runs every narrow operation pipelined into a task per partition, and writes shuffle files. A broadcast exchange forms a query stage too, one that collects a small table and sends it to every executor.
The loop the driver runs, once per finished stage
- Submit every query stage whose inputs are complete.
- Wait until a running stage finishes.
- Swap the estimates for that stage for its measured output.
- Re-run logical and physical planning over the stages not yet started, then apply AQE's own rules: coalescing, the local read, skew splitting.
- Repeat until the stage that produces the result has run.
The board below plays a sort-merge join followed by a count per country, with broadcasting switched off so both sides shuffle. Count the stages as it runs. The 2 scans each write shuffle files keyed on customer_id. The join stage reads them, counts per country inside each task, and writes a second shuffle keyed on country. A last stage finishes the count. That makes 4 stages and 3 exchanges: 3 moments where output is complete on disk before anything downstream reads it.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
by_country = orders.join(customers, "customer_id").groupBy("country").count()
by_country.collect()| order_id | customer_id | amount |
|---|---|---|
| 1 | 1 | 30 |
| 2 | 2 | 15.5 |
| 3 | 3 | 42 |
| 4 | 1 | 8.25 |
| 5 | 4 | 19.99 |
| 6 | 2 | 60 |
| 7 | 3 | 12 |
| 8 | 4 | 25 |
| customer_id | country |
|---|---|
| 1 | US |
| 2 | DE |
| 3 | US |
| 4 | JP |
The board draws the plan Spark builds before the job starts. AQE's rewrites are what the rest of this lesson covers, and the board does not show them.
The sizes a finished stage reports
When a map task finishes, it reports to the driver the size of every block it wrote, a block per shuffle partition. When the whole stage finishes, the exchange also holds its totals: the bytes and rows it wrote. Those numbers replace the planner's guesses for that branch of the plan.
| Statistic | Where it comes from | Which rewrite uses it |
|---|---|---|
| Bytes per shuffle partition | Block sizes reported by each map task, summed per partition | Coalescing, skew splitting |
| Total data size | The exchange's data size metric | Switching a join to broadcast |
| Row count | Records written by the map tasks | Re-planning, such as dropping a join whose input came back with 0 rows |
Where AQE cannot re-plan
- Inside a stage: narrow operations are pipelined into a single task, so nothing finishes between them.
- Scan stages before the first exchange: they run on the static plan, since nothing has been measured yet.
- A stage that has started: its tasks keep the plan they were handed.
- A query with no exchange: a filter and a select over one table form a single stage and give AQE nothing to measure.
Call explain() before the action and the top node of the plan reads AdaptiveSparkPlan isFinalPlan=false: that is the starting plan. Call it on the same DataFrame after the action and the node reads isFinalPlan=true, with the final plan printed above the initial one.
A job filters events, joins the result to users with a sort-merge join, and writes the output. When can AQE first replace the sort-merge join with a different strategy?
- While the filter on events runs, as soon as it sees how many rows pass.
- When the driver builds the physical plan, before any task has run.
- When a join side's map stage finishes and its measured output is known.
- After the join stage finishes, once the size of the joined output is known.
About this lesson
This section is part of the Adaptive Query Execution: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.