<?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=Narrow_and_wide_transformations</id>
	<title>Narrow and wide transformations - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Narrow_and_wide_transformations"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Narrow_and_wide_transformations&amp;action=history"/>
	<updated>2026-12-06T00:08:17Z</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=Narrow_and_wide_transformations&amp;diff=160&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=Narrow_and_wide_transformations&amp;diff=160&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:45Z</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            = Narrow and wide transformations&lt;br /&gt;
| also_known_as   = Narrow and shuffle dependencies&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = Narrow dependency, shuffle (wide) dependency, &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; operator, stage boundary&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Shuffle]], [[Job, stage and task]], [[Directed acyclic graph (Spark)]], [[Partition]], [[Broadcast join]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (core DAGScheduler.scala, EnsureRequirements.scala, docs/rdd-programming-guide.md, docs/sql-performance-tuning.md), the Apache Iceberg 1.12.0 Spark configuration page and physical plans of 18 DataFrame operations from a PySpark 4.3.0 run}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], a &amp;#039;&amp;#039;&amp;#039;narrow transformation&amp;#039;&amp;#039;&amp;#039; is one where each output [[partition]] depends on a single input partition, so it runs inside the same task as the step before it; a &amp;#039;&amp;#039;&amp;#039;wide transformation&amp;#039;&amp;#039;&amp;#039; needs data from many input partitions and therefore causes a [[shuffle]] and a new stage.&amp;lt;ref name=&amp;quot;dag&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/core/src/main/scala/org/apache/spark/scheduler/DAGScheduler.scala |title=DAGScheduler.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;rddguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html |title=RDD Programming Guide (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The distinction decides how Spark splits a job into stages. Narrow steps are cheap and are pipelined together; wide steps write shuffle files to disk and send them over the network, which makes them the main cost in most jobs.&lt;br /&gt;
&lt;br /&gt;
== How do narrow and wide transformations differ? ==&lt;br /&gt;
Spark&amp;#039;s scheduler states the rule directly. RDD operations with narrow dependencies, like &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, are pipelined into one set of tasks in each stage, while operations with shuffle dependencies need two stages: one to write a set of map output files and another to read them after a barrier.&amp;lt;ref name=&amp;quot;dag&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A wide transformation exists because a task works on one partition at a time. For &amp;lt;code&amp;gt;reduceByKey&amp;lt;/code&amp;gt;, the values of one key are usually spread over many partitions and machines, so Spark must read from all partitions and bring each key&amp;#039;s values together. This all-to-all step is the shuffle; it involves disk I/O, data serialization and network I/O.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Narrow transformation !! Wide transformation&lt;br /&gt;
|-&lt;br /&gt;
! Input partitions per output partition&lt;br /&gt;
| One&lt;br /&gt;
| Many&lt;br /&gt;
|-&lt;br /&gt;
! Data movement&lt;br /&gt;
| None; the task reads its own partition&lt;br /&gt;
| A shuffle across executors&lt;br /&gt;
|-&lt;br /&gt;
! Effect on stages&lt;br /&gt;
| Pipelined into the current stage&lt;br /&gt;
| Ends a stage and starts a new one&amp;lt;ref name=&amp;quot;dag&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! In a DataFrame physical plan&lt;br /&gt;
| No &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; node&lt;br /&gt;
| An &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; node (or a &amp;lt;code&amp;gt;BroadcastExchange&amp;lt;/code&amp;gt; for a [[broadcast join]])&lt;br /&gt;
|-&lt;br /&gt;
! Number of output partitions&lt;br /&gt;
| Same as the input (or fewer, for &amp;lt;code&amp;gt;coalesce&amp;lt;/code&amp;gt;)&lt;br /&gt;
| For DataFrame and SQL, &amp;lt;code&amp;gt;spark.sql.shuffle.partitions&amp;lt;/code&amp;gt; (default 200) unless set explicitly or changed by [[Adaptive Query Execution]]&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;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Which operators shuffle? ==&lt;br /&gt;
The Spark documentation lists repartition operations (such as &amp;lt;code&amp;gt;repartition&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;coalesce&amp;lt;/code&amp;gt;), most &amp;lt;code&amp;gt;ByKey&amp;lt;/code&amp;gt; operations except counting (such as &amp;lt;code&amp;gt;groupByKey&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;reduceByKey&amp;lt;/code&amp;gt;) and join operations (such as &amp;lt;code&amp;gt;cogroup&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt;) as operations that can cause a shuffle.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; &amp;quot;Can&amp;quot; matters: in the DataFrame plans below, &amp;lt;code&amp;gt;coalesce&amp;lt;/code&amp;gt; to fewer partitions merges partitions without an &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt;, and a join does not shuffle again an input that is already partitioned by its keys.&lt;br /&gt;
&lt;br /&gt;
The following table shows what Spark 4.3.0 actually planned for common DataFrame operations. Each row is the physical plan of the operation on a 100-row DataFrame in PySpark 4.3.0, with [[Adaptive Query Execution]] and automatic broadcast joins turned off so that the base plan is visible.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! DataFrame operation !! Narrow or wide !! &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; in the physical plan&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;where&amp;lt;/code&amp;gt; || Narrow || None&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;select&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;withColumn&amp;lt;/code&amp;gt; || Narrow || None&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;explode&amp;lt;/code&amp;gt; || Narrow || None&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;union&amp;lt;/code&amp;gt; || Narrow || None&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;sample&amp;lt;/code&amp;gt; || Narrow || None&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;coalesce(n)&amp;lt;/code&amp;gt; || Narrow || None&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;sortWithinPartitions&amp;lt;/code&amp;gt; || Narrow || None&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;limit(n)&amp;lt;/code&amp;gt; alone || Narrow || None&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;limit(n)&amp;lt;/code&amp;gt; followed by &amp;lt;code&amp;gt;groupBy&amp;lt;/code&amp;gt; || Wide || &amp;lt;code&amp;gt;Exchange SinglePartition&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;groupBy(...).agg(...)&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;count&amp;lt;/code&amp;gt; per key || Wide || &amp;lt;code&amp;gt;Exchange hashpartitioning(key, 200)&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;distinct&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt; || Wide || &amp;lt;code&amp;gt;Exchange hashpartitioning(...)&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;orderBy&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;sort&amp;lt;/code&amp;gt; || Wide || &amp;lt;code&amp;gt;Exchange rangepartitioning(... ASC NULLS FIRST, 200)&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;repartition(4)&amp;lt;/code&amp;gt; || Wide || &amp;lt;code&amp;gt;Exchange RoundRobinPartitioning(4)&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;repartition(&amp;quot;col&amp;quot;)&amp;lt;/code&amp;gt; || Wide || &amp;lt;code&amp;gt;Exchange hashpartitioning(col, 200), REPARTITION_BY_COL&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; (sort-merge) || Wide || One &amp;lt;code&amp;gt;Exchange hashpartitioning&amp;lt;/code&amp;gt; per side&lt;br /&gt;
|-&lt;br /&gt;
| Window with &amp;lt;code&amp;gt;partitionBy&amp;lt;/code&amp;gt; || Wide || &amp;lt;code&amp;gt;Exchange hashpartitioning(partition key, 200)&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;intersect&amp;lt;/code&amp;gt; || Wide || &amp;lt;code&amp;gt;Exchange hashpartitioning&amp;lt;/code&amp;gt; on all columns&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
A [[broadcast join]] replaces the two shuffles of a sort-merge join with a &amp;lt;code&amp;gt;BroadcastExchange&amp;lt;/code&amp;gt; of the small side. Spark plans one automatically when a side&amp;#039;s estimated size is below &amp;lt;code&amp;gt;spark.sql.autoBroadcastJoinThreshold&amp;lt;/code&amp;gt; (default 10 MB).&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Spark avoids shuffles it does not need ==&lt;br /&gt;
Spark adds an &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt; only when an operator&amp;#039;s input is not already distributed the way the operator requires. The planner rule that does this is &amp;lt;code&amp;gt;EnsureRequirements&amp;lt;/code&amp;gt;, which is why shuffles it adds are labeled &amp;lt;code&amp;gt;ENSURE_REQUIREMENTS&amp;lt;/code&amp;gt; in the plan.&amp;lt;ref name=&amp;quot;ensure&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/exchange/EnsureRequirements.scala |title=EnsureRequirements.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; In the PySpark 4.3.0 plan below, a &amp;lt;code&amp;gt;groupBy(&amp;quot;g&amp;quot;)&amp;lt;/code&amp;gt; is followed by a join on the same key. The aggregate&amp;#039;s output is already hash-partitioned by &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;, so the join reuses it and only the other side is shuffled:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
*(5) SortMergeJoin [g#5L], [g#7L], Inner&lt;br /&gt;
:- *(2) Sort [g#5L ASC NULLS FIRST], false, 0&lt;br /&gt;
:  +- *(2) HashAggregate(keys=[g#5L], functions=[count(1)])&lt;br /&gt;
:     +- Exchange hashpartitioning(g#5L, 200), ENSURE_REQUIREMENTS&lt;br /&gt;
:        +- *(1) HashAggregate(keys=[g#5L], functions=[partial_count(1)])&lt;br /&gt;
:           ...&lt;br /&gt;
+- *(4) Sort [g#7L ASC NULLS FIRST], false, 0&lt;br /&gt;
   +- Exchange hashpartitioning(g#7L, 200), ENSURE_REQUIREMENTS&lt;br /&gt;
      ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tables stored with a matching layout can avoid the shuffle entirely. Storage-partitioned join uses the partitioning reported by a compatible V2 data source to skip the shuffle phase of a join (&amp;lt;code&amp;gt;spark.sql.sources.v2.bucketing.enabled&amp;lt;/code&amp;gt;, default true, since 3.3.0).&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt; [[Apache Iceberg]] supports it through &amp;lt;code&amp;gt;spark.sql.iceberg.planning.preserve-data-grouping&amp;lt;/code&amp;gt; (default false), which groups scan tasks for the same partition into the same read split.&amp;lt;ref name=&amp;quot;icecfg&amp;quot;&amp;gt;{{Cite web |url=https://iceberg.apache.org/docs/latest/spark-configuration/ |title=Spark Configuration (Apache Iceberg 1.12.0 documentation) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to reduce the cost of wide transformations ==&lt;br /&gt;
* Filter and select columns before a wide step, so that less data is shuffled. The [[Catalyst optimizer]] pushes many filters down automatically (see [[Predicate pushdown]]).&lt;br /&gt;
* Use a [[broadcast join]] when one side is small.&lt;br /&gt;
* Prefer aggregations that combine on the map side. &amp;lt;code&amp;gt;reduceByKey&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;aggregateByKey&amp;lt;/code&amp;gt; build in-memory structures on the map side; in the DataFrame API, the &amp;lt;code&amp;gt;partial_count&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;partial_sum&amp;lt;/code&amp;gt; steps in the plan show the same map-side combine.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
* Let [[Adaptive Query Execution]] coalesce small shuffle partitions and split skewed ones.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Shuffle]]&lt;br /&gt;
* [[Job, stage and task]]&lt;br /&gt;
* [[Directed acyclic graph (Spark)]]&lt;br /&gt;
* [[Partition]]&lt;br /&gt;
* [[Broadcast join]]&lt;br /&gt;
* [[Sort-merge join]]&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>