Switching to Broadcast Mid-Run

Joins: Intermediate showed the 10MB threshold deciding a join's strategy at planning time, from Catalyst's estimate. With AQE on, Spark checks a threshold a second time, after a join side's map stage finishes, against the bytes that side actually produced. A side that looked large on paper and measured small is broadcast for the rest of the join.

Why the estimate misses

Without column statistics, Catalyst estimates a filter's output at the full size of its input, and a groupBy from the size of its input, scaled only for the columns it keeps. Both guesses can be off by orders of magnitude. Take a 4GB campaigns table filtered to the active campaigns of one advertiser: the planner sees 4GB, far above 10MB, and plans a sort-merge join against an 800GB page_views table. At runtime the filtered side writes 6MB of shuffle output.

Both Sort operators are gone, the small side now goes to every executor, and the large side arrives through a local read, the subject of the next section. The page_views exchange stays. Spark submitted both map stages together, the 6MB side finished first and triggered the re-plan, and the final plan reads the shuffle files the 800GB stage goes on to write.

The threshold that governs the switch

The runtime check reads spark.sql.adaptive.autoBroadcastJoinThreshold, added in Spark 3.2. Left unset, it takes the value of spark.sql.autoBroadcastJoinThreshold, so both checks use 10MB out of the box. The number it compares is the measured data size of that side's shuffle output.

Static thresholdAdaptive thresholdPlanning-time broadcastRuntime switch
10MBunsetunder 10MB estimatedunder 10MB measured
-1unsetnevernever
-110MBneverunder 10MB measured
10MB-1under 10MB estimatednever

The third row suits a team burned by bad estimates: Spark never broadcasts on a guess, yet still broadcasts a side it has measured. The fourth row keeps planning-time broadcasts and stops AQE from adding new ones.

That is why a planned broadcast beats a runtime one when you can get it: with good statistics or a broadcast() hint, the large side is never shuffled at all. The runtime switch covers the joins where the estimate was wrong.

Seeing the switch

In the SQL tab, a switched join shows BroadcastHashJoin in the final graph, where explain() before the run showed SortMergeJoin. The Exchange node under the small side shows its data size: compare it with 10MB to see why the switch happened, or why it did not.

Catalyst plans orders.filter(orders.status == "Completed") as if every row survived. Run the cell and work out the share of rows that carry Completed. Then suppose orders is 30MB, so Catalyst estimates the filtered side at 30MB: would the static plan broadcast it, and would AQE?

Answer: the static plan sees 30MB and never broadcasts. If under a third of the rows are Completed, the filtered side measures under 10MB and AQE switches the join.

campaigns is 4GB on disk and the filter keeps 3MB of it. With these settings, which join does this program run?

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
spark.conf.set("spark.sql.adaptive.autoBroadcastJoinThreshold", "10MB")
active = campaigns.filter(campaigns.status == "active")
result = page_views.join(active, "campaign_id")
result.write.parquet("/out/active_views")

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.