<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Adaptive_Query_Execution</id>
	<title>Adaptive Query Execution - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Adaptive_Query_Execution"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Adaptive_Query_Execution&amp;action=history"/>
	<updated>2026-12-06T00:07:25Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Adaptive_Query_Execution&amp;diff=118&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Adaptive_Query_Execution&amp;diff=118&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:32Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Adaptive Query Execution&lt;br /&gt;
| also_known_as   = AQE, adaptive execution&lt;br /&gt;
| field           = Query optimization&lt;br /&gt;
| introduced      = Apache Spark 3.0.0 (SPARK-31412); on by default since 3.2.0 (SPARK-33679)&lt;br /&gt;
| key_parts       = Query stages, runtime statistics, partition coalescing, skew join splitting, join strategy switching, local shuffle reader&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Catalyst optimizer]], [[Shuffle]], [[Broadcast join]], [[Sort-merge join]], [[Shuffle hash join]], [[Cost-based optimizer (Spark)]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the Spark 3.0.0 release notes, SPARK-33679, the v4.3.0 tag of apache/spark (docs/sql-performance-tuning.md, SQLConf.scala, AdaptiveSparkPlanExec.scala, InsertAdaptiveSparkPlan.scala, OptimizeSkewedJoin.scala, ShuffledJoin.scala), SPARK-31412 and EXPLAIN output and job counts from a PySpark 4.3.0 run}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Adaptive Query Execution&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;AQE&amp;#039;&amp;#039;&amp;#039;) is the part of Spark SQL in [[Apache Spark]] that re-optimizes a query while it runs, using statistics measured on completed [[shuffle]] stages instead of estimates made before the query started.&amp;lt;ref name=&amp;quot;perf&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-performance-tuning.html |title=Performance Tuning (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Its three main features are coalescing small shuffle partitions, splitting skewed partitions in joins, and switching a planned [[sort-merge join]] to a [[broadcast join]] or [[shuffle hash join]]. AQE was introduced in Spark 3.0.0 and has been on by default since Spark 3.2.0, controlled by &amp;lt;code&amp;gt;spark.sql.adaptive.enabled&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;r300&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-3-0-0.html |title=Spark Release 3.0.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;jira33679&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-33679 |title=SPARK-33679: Enable spark.sql.adaptive.enabled true by default |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does AQE work? ==&lt;br /&gt;
The [[Catalyst optimizer]] plans a query before it runs, from estimated sizes that may be missing or wrong. AQE splits the physical plan into &amp;#039;&amp;#039;&amp;#039;query stages&amp;#039;&amp;#039;&amp;#039; at exchange nodes. Each query stage materializes its output; when one completes, Spark re-optimizes and re-plans the rest of the query from the statistics of all materialized stages, then creates the next stages.&amp;lt;ref name=&amp;quot;aqeexec&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala |title=AdaptiveSparkPlanExec.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Partition coalescing, for example, uses the map output statistics of a finished shuffle.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The effect is visible in &amp;lt;code&amp;gt;explain()&amp;lt;/code&amp;gt;. Before an action, the physical plan starts with &amp;lt;code&amp;gt;AdaptiveSparkPlan isFinalPlan=false&amp;lt;/code&amp;gt;. After the action, the same call prints a &amp;lt;code&amp;gt;Final Plan&amp;lt;/code&amp;gt; with query stages, followed by the &amp;lt;code&amp;gt;Initial Plan&amp;lt;/code&amp;gt;. This aggregation from PySpark 4.3.0 shows the final plan after AQE coalesced the 200 shuffle partitions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
AdaptiveSparkPlan isFinalPlan=true&lt;br /&gt;
+- == Final Plan ==&lt;br /&gt;
   ResultQueryStage 1&lt;br /&gt;
   +- *(2) HashAggregate(keys=[_groupingexpression#26L], functions=[count(1)])&lt;br /&gt;
      +- AQEShuffleRead coalesced&lt;br /&gt;
         +- ShuffleQueryStage 0&lt;br /&gt;
            +- Exchange hashpartitioning(_groupingexpression#26L, 200), ENSURE_REQUIREMENTS&lt;br /&gt;
               +- *(1) HashAggregate(keys=[_groupingexpression#26L], functions=[partial_count(1)])&lt;br /&gt;
                  ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the same run, the reduce stage had 200 tasks with AQE off and 1 task with AQE on, and the single &amp;lt;code&amp;gt;collect()&amp;lt;/code&amp;gt; ran as two jobs, one for the map-side query stage and one for the result. See [[Job, stage and task]].&lt;br /&gt;
&lt;br /&gt;
== Coalescing post-shuffle partitions ==&lt;br /&gt;
A Spark SQL shuffle has &amp;lt;code&amp;gt;spark.sql.shuffle.partitions&amp;lt;/code&amp;gt; partitions (default 200), which is too many for small data and too few for large data. With AQE, Spark merges contiguous small shuffle partitions after the map side has run. The Spark documentation recommends setting a large enough initial number with &amp;lt;code&amp;gt;spark.sql.adaptive.coalescePartitions.initialPartitionNum&amp;lt;/code&amp;gt; and letting Spark pick the final number at run time.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Setting !! Default (4.3.0) !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.adaptive.coalescePartitions.enabled&amp;lt;/code&amp;gt; || true || Turn coalescing on or off&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.adaptive.advisoryPartitionSizeInBytes&amp;lt;/code&amp;gt; || 64 MB || Target partition size when coalescing or splitting&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.adaptive.coalescePartitions.parallelismFirst&amp;lt;/code&amp;gt; || true || Ignore the target size and only respect the minimum size, to keep parallelism; the docs recommend false on a busy cluster&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.adaptive.coalescePartitions.minPartitionSize&amp;lt;/code&amp;gt; || 1 MB || Smallest partition after coalescing&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;spark.sql.adaptive.coalescePartitions.initialPartitionNum&amp;lt;/code&amp;gt; || (none; uses &amp;lt;code&amp;gt;spark.sql.shuffle.partitions&amp;lt;/code&amp;gt;) || Number of shuffle partitions before coalescing&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Source: Spark 4.3.0 performance tuning guide.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Skew join handling ==&lt;br /&gt;
Data skew means a few partitions hold far more data than the rest, so their tasks run much longer. With &amp;lt;code&amp;gt;spark.sql.adaptive.skewJoin.enabled&amp;lt;/code&amp;gt; (default true), AQE splits a skewed partition into smaller ones and replicates the matching partition of the other side, so the pieces run as parallel tasks.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;skew&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/OptimizeSkewedJoin.scala |title=OptimizeSkewedJoin.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A partition counts as skewed when it is larger than both &amp;lt;code&amp;gt;spark.sql.adaptive.skewJoin.skewedPartitionFactor&amp;lt;/code&amp;gt; (default 5.0) times the median partition size and &amp;lt;code&amp;gt;spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes&amp;lt;/code&amp;gt; (default 256 MB).&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt; A join that AQE has split shows &amp;lt;code&amp;gt;(skew=true)&amp;lt;/code&amp;gt; after its name in the plan, for example &amp;lt;code&amp;gt;SortMergeJoin(skew=true)&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;shuffled&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/ShuffledJoin.scala |title=ShuffledJoin.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Join strategy switching ==&lt;br /&gt;
AQE can replace the join strategy chosen at planning time once it knows the real sizes:&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Sort-merge join to broadcast hash join.&amp;#039;&amp;#039;&amp;#039; If the runtime size of a join side is below &amp;lt;code&amp;gt;spark.sql.adaptive.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; (default: the value of &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt;, 10 MB), AQE switches to a [[broadcast join]]. The documentation notes that this is less efficient than planning a broadcast join from the start, but still avoids sorting both sides; with &amp;lt;code&amp;gt;spark.sql.adaptive.localShuffleReader.enabled&amp;lt;/code&amp;gt; (default true), the already written shuffle files are read locally to save network traffic.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Sort-merge join to shuffled hash join.&amp;#039;&amp;#039;&amp;#039; If every post-shuffle partition is smaller than &amp;lt;code&amp;gt;spark.sql.adaptive.maxShuffledHashJoinLocalMapThreshold&amp;lt;/code&amp;gt;, AQE uses a [[shuffle hash join]] regardless of &amp;lt;code&amp;gt;spark.sql.join.preferSortMergeJoin&amp;lt;/code&amp;gt;. The default threshold is 0, so this conversion is off unless the threshold is raised to at least the advisory partition size.&lt;br /&gt;
&lt;br /&gt;
== What can AQE not fix? ==&lt;br /&gt;
AQE only changes what it can measure, and only at stage boundaries. The Spark 4.3.0 documentation and source give these limits:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;No shuffle, no adaptation.&amp;#039;&amp;#039;&amp;#039; AQE is applied only to queries that contain or may need exchanges, or that contain subqueries. A query that is a single scan and filter gets no AQE plan.&amp;lt;ref name=&amp;quot;insert&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/InsertAdaptiveSparkPlan.scala |title=InsertAdaptiveSparkPlan.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Decisions made before the first stage stay.&amp;#039;&amp;#039;&amp;#039; The first stages run on the initial plan, so a scan that reads too much, or a bad join order chosen up front, is not undone. Better table statistics and the [[Cost-based optimizer (Spark)|cost-based optimizer]] act earlier.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Late broadcast is second best.&amp;#039;&amp;#039;&amp;#039; A sort-merge join converted to a broadcast join has already shuffled the large side; the documentation calls the conversion less efficient than a broadcast join planned at the start.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Skew handling is limited to joins and rebalances.&amp;#039;&amp;#039;&amp;#039; The skew join rule splits partitions only on the sides that a join type allows: the left side for inner, cross, left outer, left semi and left anti joins, and the right side for inner, cross and right outer joins. A full outer join cannot be split on either side.&amp;lt;ref name=&amp;quot;skew&amp;quot; /&amp;gt; Skewed aggregations are not split; &amp;lt;code&amp;gt;spark.sql.adaptive.optimizeSkewsInRebalancePartitions.enabled&amp;lt;/code&amp;gt; handles skew only in &amp;lt;code&amp;gt;REBALANCE&amp;lt;/code&amp;gt; repartitioning.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Stateful streaming is excluded.&amp;#039;&amp;#039;&amp;#039; In Spark 4.3.0, AQE is not applied to stateful streaming queries; stateless streaming queries use it when &amp;lt;code&amp;gt;spark.sql.adaptive.streaming.stateless.enabled&amp;lt;/code&amp;gt; is true (the default, since 4.1.0).&amp;lt;ref name=&amp;quot;insert&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
AQE is on by default, so most code needs no settings. These lines make the coalescing target explicit and raise the initial partition count for a large shuffle:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
spark.conf.set(&amp;quot;spark.sql.adaptive.enabled&amp;quot;, &amp;quot;true&amp;quot;)&lt;br /&gt;
spark.conf.set(&amp;quot;spark.sql.adaptive.coalescePartitions.parallelismFirst&amp;quot;, &amp;quot;false&amp;quot;)&lt;br /&gt;
spark.conf.set(&amp;quot;spark.sql.adaptive.advisoryPartitionSizeInBytes&amp;quot;, &amp;quot;128m&amp;quot;)&lt;br /&gt;
spark.conf.set(&amp;quot;spark.sql.adaptive.coalescePartitions.initialPartitionNum&amp;quot;, &amp;quot;2000&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Catalyst optimizer]]&lt;br /&gt;
* [[Shuffle]]&lt;br /&gt;
* [[Broadcast join]]&lt;br /&gt;
* [[Sort-merge join]]&lt;br /&gt;
* [[Shuffle hash join]]&lt;br /&gt;
* [[Cost-based optimizer (Spark)]]&lt;br /&gt;
* [[Job, stage and task]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>