Where AQE Skew-Join Stops

Concepts covered: Data Skew

Adaptive query execution (AQE) waits for a shuffle's map side to finish, reads the real size of every reduce partition it wrote, and re-plans the rest of the query with those sizes. It has been on by default since Spark 3.2, through spark.sql.adaptive.enabled. One of its rules, switched by spark.sql.adaptive.skewJoin.enabled (also on by default), targets the hot reduce task of a sort-merge join. The animation shows that task: the 7 orders from Two-Phase Aggregation join 3 customers on customer_id with broadcasting turned off, so both sides shuffle and every c7 row from both tables meets in 1 reduce task.

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
orders.join(customers, "customer_id").collect()
orders
order_idcustomer_idamount
1c740
2c725
3c260
4c735
5c750
6c320
7c745
customers
customer_idname
c2Ana
c3Ben
c7Dev

When a partition counts as skewed

AQE marks a reduce partition skewed when it is larger than skewedPartitionFactor (5) times the median partition size and also larger than skewedPartitionThresholdInBytes (256MB); both settings live under spark.sql.adaptive.skewJoin. It then cuts that partition into chunks of about spark.sql.adaptive.advisoryPartitionSizeInBytes (64MB), or of the average non-skewed partition if that is larger, and joins each chunk with its own copy of the matching partition from the other side.

StageMedian partitionLargest partitionAQE's decision
200 partitions80MB3GBSkewed: 3GB passes both 400MB and 256MB, so it splits into about 38 chunks near 80MB
20 partitions2GB8GBLeft alone: 8GB is 4 times the median, under the factor of 5

A sort-merge join stage has 20 partitions. The median is 2GB and the largest is 8GB. With default settings, what does AQE do with the 8GB partition?

Where the rule stops

SituationWhy AQE leaves itTake over with
Hot key on the side an outer join fills with nullsInner joins split either side, left joins only the left, right joins only the right, full outer joins neitherIsolate the hot keys and join them on their own
Hot rows written by 1 map taskChunks are cut at map-output block boundaries, and 1 block cannot be dividedA round-robin repartition of the input, or salting
Below the thresholdsUnder 5 times the median, or under 256MBLower the settings, or salt
Small inputs, huge outputSizes are input bytes, so a duplicate-key blowup on 100MB partitions looks normalPer-key salting sized on the rows the key produces
No exchange to measureBroadcast joins, bucketed joins and first stages read no shuffleRebalance the input
Split would need an extra shuffleA later operator needs the join's output partitioning, so AQE drops the split`spark.sql.adaptive.forceOptimizeSkewedJoin`, or salting
Skewed groupBy or windowThe rule covers joins onlyPartial aggregation or a two-phase split

The block rule ties back to Skew Before the First Shuffle. A reduce partition is a list of blocks, 1 from each map task, and AQE groups consecutive blocks into chunks. If 2,000 map tasks each wrote around 1.5MB of the hot key, the 3GB partition cuts into fine pieces. If 1 file held the hot key, so 1 map task wrote nearly all 3GB as a single block, there is nothing to cut, and the partition runs as 1 task.

To confirm what happened, read the plan after the action. A pre-run explain() prints AdaptiveSparkPlan isFinalPlan=false, because the decision waits for shuffle sizes. After the run, the final plan's AQEShuffleRead node reports the number of skewed partitions and the splits made from them, and a join that was split prints as SortMergeJoin(skew=true).

AQE is on with its default settings. A sort-merge left join keeps every session and attaches bot events. Of the bot_events rows, 90% belong to user_id 0, and that partition is 30 times the median. The stage still has 1 slow task. Which change lets AQE split it?

sessions.join(bot_events, "user_id", "left")

About this lesson

This section is part of the Data Skew: Advanced 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.