Splitting Skewed Partitions
Data Skew: Intermediate fixed hot keys by salting and isolating them in your own code. AQE does a version of the same work inside the join: once both sides' map stages have finished, it knows every join partition's size before a single join task starts, and it can break the straggler into pieces.
The 2 skew tests
A partition on one side of the join counts as skewed when it passes both tests. It must be larger than skewedPartitionFactor times the median partition size on that side, 5 times by default. And it must be larger than skewedPartitionThresholdInBytes, 256MB by default. Both settings sit under spark.sql.adaptive.skewJoin., and spark.sql.adaptive.skewJoin.enabled turns the rule on, as it is by default.
| Partition | Side's median | 5 x median | Above 256MB? | Skewed? |
|---|---|---|---|---|
| 3.2GB | 40MB | 200MB | yes | yes |
| 180MB | 20MB | 100MB | no | no |
| 300MB | 80MB | 400MB | yes | no |
The factor alone would flag a 3MB partition beside a 0.5MB median in a tiny job, where splitting saves nothing. The byte threshold alone would flag every partition of a job that is large everywhere. Requiring both catches partitions that are big in absolute terms and out of line with their neighbours.
Splitting a skewed partition by map task
Partition 17 on the left side is made of blocks, a block from each left-side map task. AQE groups those blocks into contiguous runs of map tasks, each run close to a target size, and gives each run its own task. The target is the larger of the advisory partition size and the average size of that side's non-skewed partitions.
Because the split follows map tasks, a single hot key gets divided too: its rows reached partition 17 from many map tasks, so each run holds a share of them. The smallest piece AQE can make is a single map task's block, so a block that is huge on its own stays whole.
The board plays a sort-merge join in which customer 1 owns 9 of the 12 orders. Watch the task that receives customer 1 after the shuffle: that is the partition AQE would split. The customers block that lands beside it is the one each piece would need a copy of.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
orders.join(customers, "customer_id").count()| order_id | customer_id | amount |
|---|---|---|
| 101 | 1 | 20 |
| 102 | 1 | 35 |
| 103 | 2 | 12.5 |
| 104 | 1 | 8 |
| 105 | 1 | 41 |
| 106 | 3 | 17 |
| 107 | 1 | 9.5 |
| 108 | 1 | 22 |
| 109 | 4 | 30 |
| 110 | 1 | 14 |
| 111 | 1 | 27.5 |
| 112 | 1 | 11 |
| customer_id | country |
|---|---|
| 1 | US |
| 2 | DE |
| 3 | FR |
| 4 | JP |
The cost of copying the other side
Each piece of left partition 17 still has to meet every right-side row with the same keys, so every piece reads all of right partition 17. Take a left side of 200 partitions with a 40MB median and a non-skewed average of 40MB, partition 17 at 3.2GB, and a right partition 17 of 30MB. The target is the larger of 64MB and 40MB.
- about 50
- about 1.5GB
The slowest task drops from a 3.2GB read to about 64MB plus its copy of the right side. The price is paid in reads of the side that was not split, and it multiplies when both sides are skewed on the same partition: a left split into 4 and a right split into 3 run as 12 tasks, one for every pairing.
A sort-merge join's left side has 200 partitions with a 30MB median and a 30MB average outside one partition of 900MB. The matching right partition is 20MB, and every setting is at its default. What does AQE do with the 900MB partition?
- It splits it into about 15 pieces near 64MB, and each piece reads the whole 20MB right partition.
- It splits it into about 15 pieces, and cuts the 20MB right partition into 15 matching pieces.
- It leaves it alone, because 900MB is not more than 5 times the 256MB threshold.
- It merges it with its neighbours first, since coalescing runs before skew handling.
Which joins can be split
| Join type | Split the left side | Split the right side |
|---|---|---|
| Inner, cross | yes | yes |
| Left outer, left semi, left anti | yes | no |
| Right outer | no | yes |
| Full outer | no | no |
A left outer join emits a null-filled row for each left row with no match. If the right side were split, a left row would find its match in one piece and miss it in the others, producing null rows that should not exist. A groupBy on a hot key gets no help from this rule at all: it splits join partitions only. In the SQL tab, a split join shows as SortMergeJoin(skew=true), and the AQEShuffleRead above it reports the number of skewed partitions and the splits made from them.
Run the cell for the heaviest user_id values in user_events, with row counts standing in for bytes. The skew test compares partitions, and each of 200 partitions holds every key hashed into it. Change the last line to known.count() for the total: the hot key's partition holds its rows plus about total / 200, the median partition about total / 200. Is the first over 5 times the second? A real partition would also need to clear 256MB.
Capstone: reading a whole run
This question draws on every section: what each finished stage measured, and which rewrite that triggered.
A nightly job's final plan contains an AQEShuffleRead labelled coalesced above one exchange, a BroadcastHashJoin whose probe side reads through AQEShuffleRead local, and a SortMergeJoin(skew=true). Which statement about this run holds?
- The broadcast join was planned before the job started, which is why its probe side could use a local read.
- The coalesced partitions were made by rewriting the shuffle files into 64MB chunks before the next stage read them.
- The local read means the broadcast side was read from local disk on each executor instead of being sent to them.
- Each of the 3 rewrites was decided at a materialization point, from output that a finished stage had measured.
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.