Coalescing Small Partitions
Every shuffle in an application starts with the same number of reducers, spark.sql.shuffle.partitions, whether it moves 2TB or 300MB. On the small shuffle that means hundreds of tasks that each spend longer being scheduled than working, and a tiny file per partition if the stage writes its output. Sizing Shuffle Partitions had you pick that count per job. Coalescing picks it per shuffle, from sizes measured after the map stage.
Once the map stage has finished, Spark knows the bytes headed to every reducer. It merges neighbouring partitions until each merged one is near a target size, and the plan records the step as an AQEShuffleRead node labelled coalesced. The switch for it, spark.sql.adaptive.coalescePartitions.enabled, is on by default.
Merging neighbours toward a target
Take a shuffle of 12 partitions holding 276MB on a 4-core cluster, with a target of 64MB. Spark walks the partitions in ID order. It adds the next partition to the current group while the group stays at or under the target, and starts a new group when the next partition would push it over.
Partitions 6 and 7 stay apart because together they would reach 70MB. The task reading partition 7 (30MB) and the task reading partitions 9 to 11 (26MB) would fit in a single 56MB task, but Spark merges only neighbours, so they stay separate.
The merge moves no rows
No row moves to make a coalesced partition. Each map task already wrote a data file with its blocks laid out in partition order, plus an index of where each block starts. A task that reads partitions 0 to 2 asks every map output for blocks 0, 1 and 2, which sit side by side in the file. With spark.sql.adaptive.fetchShuffleBlocksInBatch on, its default, Spark fetches such a run in a single request when the codec allows it. The whole cost of coalescing is the driver adding up block sizes.
Keeping to neighbours is what keeps it cheap: merging partitions 7 and 10 would mean 2 separate reads from every map file. It also keeps joins correct. When a stage reads 2 shuffles, as a sort-merge join does, Spark adds partition i's size across both sides and applies one set of ranges to both, so partition i on the left still meets partition i on the right.
The settings behind the target
The target comes from 3 settings under spark.sql.adaptive.. advisoryPartitionSizeInBytes is the size Spark aims for, coalescePartitions.minPartitionSize is the smallest merged partition it will plan, and coalescePartitions.parallelismFirst decides whether the cluster's core count can lower the target.
- 64MB
- 1MB
def coalesce_target(total_bytes, cores, parallelism_first=True):
advisory = 64 * 1024 * 1024
minimum = 1024 * 1024
floor_count = cores if parallelism_first else 1
return max(minimum, min(advisory, total_bytes // floor_count))With parallelismFirst at its default, true, Spark will not plan fewer coalesced partitions than the cluster's default parallelism, normally its total core count, unless that pushes them under the minimum. The 12-partition shuffle holds 276MB on 4 cores: 276 / 4 is 69MB, so the advisory 64MB is smaller and wins. Put a 2,048MB shuffle on 40 cores and 2,048 / 40 is roughly 51MB, so Spark aims at 51MB and plans at least 40 tasks instead of at least 32. Setting parallelismFirst to false makes the advisory size the target, which the Spark configuration docs recommend on a busy shared cluster.
Run the cell. Each row is one event_type value, and every row with the same key hashes to the same shuffle partition. Work out how many of the default 200 reducers receive any data, then how many tasks AQE plans for this aggregation.
Answer: no more reducers than there are rows in the result receive data, and the rest are empty. The whole shuffle is far below the 1MB minimum, so the target is 1MB and every partition merges into a single task.
A 200-partition shuffle writes 1,200MB on a 12-core cluster with every AQE setting at its default. What target size does coalescing use?
- 100MB, because 1,200MB spread across 12 cores gives each core that much.
- 64MB, because the advisory size is smaller than 1,200MB divided by 12.
- 6MB, the 1,200MB divided across the original 200 shuffle partitions.
- 1MB, because the minimum partition size always becomes the target.
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.