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

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()
orders
order_idcustomer_idamount
1130
2215.5
3342
418.25
5419.99
6260
7312
8425
customers
customer_idcountry
1US
2DE
3US
4JP

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.

StatisticWhere it comes fromWhich rewrite uses it
Bytes per shuffle partitionBlock sizes reported by each map task, summed per partitionCoalescing, skew splitting
Total data sizeThe exchange's data size metricSwitching a join to broadcast
Row countRecords written by the map tasksRe-planning, such as dropping a join whose input came back with 0 rows

Where AQE cannot re-plan

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?

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.