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()| order_id | customer_id | amount |
|---|---|---|
| 1 | c7 | 40 |
| 2 | c7 | 25 |
| 3 | c2 | 60 |
| 4 | c7 | 35 |
| 5 | c7 | 50 |
| 6 | c3 | 20 |
| 7 | c7 | 45 |
| customer_id | name |
|---|---|
| c2 | Ana |
| c3 | Ben |
| c7 | Dev |
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.
| Stage | Median partition | Largest partition | AQE's decision |
|---|---|---|---|
| 200 partitions | 80MB | 3GB | Skewed: 3GB passes both 400MB and 256MB, so it splits into about 38 chunks near 80MB |
| 20 partitions | 2GB | 8GB | Left 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?
- Leaves it as 1 task, because 8GB is 4 times the median and the factor is 5
- Splits it into 64MB chunks, because 8GB is far over the 256MB threshold
- Splits it into 4 chunks of 2GB, because the median sets the chunk size
- Flags it as skewed, then keeps 1 task because 20 partitions fill the cores
Where the rule stops
| Situation | Why AQE leaves it | Take over with |
|---|---|---|
| Hot key on the side an outer join fills with nulls | Inner joins split either side, left joins only the left, right joins only the right, full outer joins neither | Isolate the hot keys and join them on their own |
| Hot rows written by 1 map task | Chunks are cut at map-output block boundaries, and 1 block cannot be divided | A round-robin repartition of the input, or salting |
| Below the thresholds | Under 5 times the median, or under 256MB | Lower the settings, or salt |
| Small inputs, huge output | Sizes are input bytes, so a duplicate-key blowup on 100MB partitions looks normal | Per-key salting sized on the rows the key produces |
| No exchange to measure | Broadcast joins, bucketed joins and first stages read no shuffle | Rebalance the input |
| Split would need an extra shuffle | A later operator needs the join's output partitioning, so AQE drops the split | `spark.sql.adaptive.forceOptimizeSkewedJoin`, or salting |
| Skewed groupBy or window | The rule covers joins only | Partial 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")- Rewrite it as bot_events.join(sessions, "user_id", "right"), which moves the hot table to the left
- Raise spark.sql.shuffle.partitions from 200 to 2,000, so the hot partition falls under the factor
- Lower skewedPartitionFactor from 5 to 2, so the hot partition gets flagged even more readily
- If only sessions with bot events are needed, an inner join, which AQE can split on either side
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.