The full answer
Adaptive query execution (AQE) is Spark SQL's runtime re-optimisation: it uses the actual statistics of data produced during a query to adjust the rest of the query plan. It solves the problem that plans made before execution rely on estimates that are often wrong, which leads to badly sized partitions, the wrong join strategy and skewed tasks. AQE has been on by default since Spark 3.2 (spark.sql.adaptive.enabled).
The problem it solves Before AQE, Spark fixed the whole plan up front:
- Partition count: one value of
spark.sql.shuffle.partitions(200 by default) for every shuffle in every query. Small data got thousands of tiny tasks; large data got huge partitions that spilled to disk. - Join strategy: chosen from estimates. A table that would shrink to 2 MB after a filter might still be joined with an expensive sort-merge join, because the planner thought it was large.
- Skew: one key with most of the rows created a single task that ran for hours while the others finished in minutes.
- Missing statistics: many tables have none, so estimates were guesses.
How it works The query is divided into stages at exchanges (shuffles and broadcasts). Spark runs the stages whose inputs are ready, collects the size of each output partition, then re-optimises and re-plans the remaining part of the query before continuing.
What it does at runtime
- Coalesces post-shuffle partitions into fewer, larger ones (
spark.sql.adaptive.coalescePartitions.enabled, target size 64 MB by default). - Converts sort-merge joins to broadcast hash joins when one side is small enough at runtime, reading shuffle output locally.
- Converts sort-merge joins to shuffled hash joins when all partitions are small enough (off by default; enabled with
spark.sql.adaptive.maxShuffledHashJoinLocalMapThreshold). - Splits skewed partitions in sort-merge joins: a partition is skewed if it is larger than 5 times the median and larger than 256 MB.
- Splits skewed partitions when rebalancing output (with the
REBALANCEhint).
Effect in practice
- Less manual tuning: set a generous initial partition number and let AQE coalesce.
- Faster joins when filters make one side small.
- Fewer straggler tasks from skew.
Example settings
spark.conf.set("spark.sql.adaptive.enabled", "true") # default
spark.conf.set("spark.sql.adaptive.coalescePartitions.initialPartitionNum", "2000")
spark.conf.set("spark.sql.adaptive.coalescePartitions.parallelismFirst", "false") # respect the 64 MB target on busy clusters
By default (parallelismFirst true), coalescing favours parallelism and only respects a 1 MB minimum partition size; the Spark documentation recommends setting it to false on a busy cluster so partitions reach the advisory size.
What it doesn't do It doesn't change how data is read in the first stage, doesn't apply to streaming queries, and doesn't fix skew in aggregations or in joins that aren't sort-merge joins. See the question on AQE's limitations.
Last updated 16 October 2026
Sources