The Pre-Shuffle Blind Spot
AQE changes a plan at a single kind of moment: when an exchange finishes. A completed shuffle or broadcast hands the driver measured sizes, and Spark re-optimizes the stages that have not started. Everything that ran before the first exchange was compiled from estimates and executed as compiled. A read-filter-write job has no exchange at all, so it is never re-planned.
What the first stage holds
The first stage of a job contains the scan, every filter and projection, any explode or withColumn work, and the map side of the first shuffle: the partial aggregate and the shuffle write. Its task count comes from the input splits, which Spark sizes from the file listing and spark.sql.files.maxPartitionBytes, 128MB by default. AQE sets neither number, because it has measured nothing when they are chosen.
- Session files (source, 40GB on disk)
- Stage 1: scan, explode, partial agg (transform, planned from estimates); parallelism: 320 partitions
- Shuffle exchange (queue, first materialization point)
- Stage 2: final aggregate (transform, re-planned by AQE); parallelism: up to 200 partitions
- Daily totals (consumer, output table)
- Session files -> Stage 1: scan, explode, partial agg
- Stage 1: scan, explode, partial agg -> Shuffle exchange
- Shuffle exchange -> Stage 2: final aggregate
- Stage 2: final aggregate -> Daily totals
- Fixed at planning: Session files, Stage 1: scan, explode, partial agg
- Adapted at runtime: Shuffle exchange, Stage 2: final aggregate, Daily totals
A worked case: skew that grows inside a stage
A subscriptions job reads 40GB of account intervals, a row per account with a start day and an end day. It expands each row into a row for every active day, then counts active accounts per day. Most intervals cover 30 days. A legacy import of accounts spanning 3,650 days each sits in 3 splits, written together in a single batch.
| Split group | Splits | Rows read per split | Days per row | Rows emitted per split | Task time |
|---|---|---|---|---|---|
| Ordinary | 317 | 2,000,000 | 30 | 60,000,000 | 40 s |
| Legacy import | 3 | 2,000,000 | 3,650 | 7,300,000,000 | 81 min |
Every split is the same 128MB, so the scan looked balanced. The skew forms inside the tasks, as explode multiplies rows by 30 in most splits and by 3,650 in 3 of them. AQE first sees the data when stage 1's shuffle files are complete, 81 minutes in. By then the stragglers have set the job's runtime, and no rule that acts at runtime can rerun stage 1 with a better layout.
Routing each blind spot to a tool that reaches it
| Symptom | Why AQE misses it | Tool that reaches it |
|---|---|---|
| A few first-stage tasks run far longer after `explode` or a heavy function | The skew forms before any exchange exists | `repartition(n)` before the expensive step, or salting the expansion |
| Thousands of read tasks that each finish in milliseconds | Scan splits are fixed at planning | Compaction, or a larger `spark.sql.files.maxPartitionBytes` |
| Every post-shuffle partition is 10GB, all about the same | Coalescing only merges, and skew splitting needs a partition 5 times the median | A higher `spark.sql.shuffle.partitions` or `initialPartitionNum` |
| One hot key stalls a `groupBy` | Skew splitting covers joins and leaves aggregations alone | Two-phase aggregation on a salted key |
| A read-filter-write job runs slowly | No exchange means no re-plan | Input split settings and the output file layout |
The first row's fix changes the plan itself. A round-robin repartition(2000) placed before the expansion shuffles the compact 40GB of intervals and scatters the legacy rows over 2,000 tasks, so each task emits roughly 20 million rows. The price is a single extra shuffle of small rows, paid before they multiply.
from pyspark.sql import functions as F
subs = spark.table("subscriptions")
daily = (
subs.repartition(2000)
.withColumn("day", F.explode(F.sequence("start_day", "end_day")))
.groupBy("day")
.agg(F.count("*").alias("active_accounts"))
)
This program runs 2 shuffles and 3 stages: the round-robin exchange, then the hash exchange on day that the groupBy needs, because round-robin output is spread evenly with no regard to day. The count is written as a number, and AQE leaves an explicit count alone, so all 2,000 tasks reach the expansion.
AQE is on with default settings. Which of these slowdowns can it fix with no change to the code?
- A first-stage task runs 80 minutes because its split holds rows that a regex parses slowly
- A groupBy stalls because a single key holds 40% of the rows reaching its final stage
- A join stalls because a single post-shuffle partition is 3GB while the median is 90MB
- A job runs 200 shuffle partitions of 9GB each, all about the same size as each other
Pick the count from the rows the expansion will emit. Divide that total by the rows a task handles comfortably. The 3 legacy splits emit 7.3 billion rows each, so at around 20 million rows a task they need roughly 1,100 tasks on their own. The ordinary splits' 19 billion rows add about 950 more, and together they come to about 2,000. Place repartition immediately before the row-multiplying step. Placed after it, the rows have already multiplied inside the original tasks, and the stragglers are back.
About this lesson
This section is part of the Adaptive Query Execution: 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.