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.

Rows flow from the files through stage 1 into shuffle files. Only once those files exist does AQE measure them and rewrite stage 2.
  • 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 groupSplitsRows read per splitDays per rowRows emitted per splitTask time
Ordinary3172,000,0003060,000,00040 s
Legacy import32,000,0003,6507,300,000,00081 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

SymptomWhy AQE misses itTool that reaches it
A few first-stage tasks run far longer after `explode` or a heavy functionThe skew forms before any exchange exists`repartition(n)` before the expensive step, or salting the expansion
Thousands of read tasks that each finish in millisecondsScan splits are fixed at planningCompaction, or a larger `spark.sql.files.maxPartitionBytes`
Every post-shuffle partition is 10GB, all about the sameCoalescing only merges, and skew splitting needs a partition 5 times the medianA higher `spark.sql.shuffle.partitions` or `initialPartitionNum`
One hot key stalls a `groupBy`Skew splitting covers joins and leaves aggregations aloneTwo-phase aggregation on a salted key
A read-filter-write job runs slowlyNo exchange means no re-planInput 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?

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.