<?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=Logical_plan_and_physical_plan</id>
	<title>Logical plan and physical plan - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Logical_plan_and_physical_plan"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Logical_plan_and_physical_plan&amp;action=history"/>
	<updated>2026-12-06T00:08:32Z</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=Logical_plan_and_physical_plan&amp;diff=158&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=Logical_plan_and_physical_plan&amp;diff=158&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            = Logical plan and physical plan&lt;br /&gt;
| also_known_as   = Query plan, execution plan, EXPLAIN output&lt;br /&gt;
| field           = Query optimization&lt;br /&gt;
| key_parts       = Parsed (unresolved) logical plan, analyzed logical plan, optimized logical plan, physical plan, &amp;lt;code&amp;gt;EXPLAIN&amp;lt;/code&amp;gt; modes&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Catalyst optimizer]], [[Whole-stage code generation]], [[Adaptive Query Execution]], [[Cost-based optimizer (Spark)]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/sql-ref-syntax-qry-explain.md, docs/web-ui.md, python/pyspark/sql/dataframe.py, WholeStageCodegenExec.scala), the SIGMOD 2015 Spark SQL paper and EXPLAIN output from a PySpark 4.3.0 run}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], a &amp;#039;&amp;#039;&amp;#039;logical plan&amp;#039;&amp;#039;&amp;#039; is a tree that describes what a query computes (which tables, filters, joins and aggregates) without saying how, and a &amp;#039;&amp;#039;&amp;#039;physical plan&amp;#039;&amp;#039;&amp;#039; is the tree of executable operators, such as &amp;lt;code&amp;gt;SortMergeJoin&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;HashAggregate&amp;lt;/code&amp;gt;, that Spark actually runs on the cluster.&amp;lt;ref name=&amp;quot;explain&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-syntax-qry-explain.html |title=EXPLAIN (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;paper&amp;quot;&amp;gt;{{Cite web |url=https://people.csail.mit.edu/matei/papers/2015/sigmod_spark_sql.pdf |title=Spark SQL: Relational Data Processing in Spark (SIGMOD 2015) |publisher=Armbrust et al., Databricks, MIT CSAIL and AMPLab UC Berkeley |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The [[Catalyst optimizer]] produces three logical plans and one physical plan for every SQL query or DataFrame. &amp;lt;code&amp;gt;EXPLAIN&amp;lt;/code&amp;gt; in SQL and &amp;lt;code&amp;gt;DataFrame.explain()&amp;lt;/code&amp;gt; in PySpark print them, and reading them is the main way to find out why a query is slow.&lt;br /&gt;
&lt;br /&gt;
== What are the four plans? ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Plan !! Produced by !! What it contains !! Marker in EXPLAIN EXTENDED&lt;br /&gt;
|-&lt;br /&gt;
| Parsed logical plan || The SQL parser or the DataFrame API || The query as written; table and column names are not yet checked. Unresolved names start with an apostrophe, for example &amp;lt;code&amp;gt;&amp;#039;sum(&amp;#039;id)&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;== Parsed Logical Plan ==&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Analyzed logical plan || The analyzer || Names resolved against the catalog and turned into typed attributes with IDs, such as &amp;lt;code&amp;gt;id#0L&amp;lt;/code&amp;gt;; the output schema is printed first || &amp;lt;code&amp;gt;== Analyzed Logical Plan ==&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Optimized logical plan || The logical optimizer || The analyzed plan after optimization rules: constants folded, filters pushed down, columns pruned || &amp;lt;code&amp;gt;== Optimized Logical Plan ==&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Physical plan || The planner, then [[Adaptive Query Execution]] at run time || Concrete operators, exchanges (shuffles) and code generation stages || &amp;lt;code&amp;gt;== Physical Plan ==&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Spark documentation describes the steps in the same terms: the parsed logical plan is an unresolved plan extracted from the query; analysis translates unresolved attributes and relations into fully typed objects; and the optimized logical plan goes through a set of optimization rules, resulting in the physical plan.&amp;lt;ref name=&amp;quot;explain&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do you read EXPLAIN EXTENDED? ==&lt;br /&gt;
This PySpark 4.3.0 example filters and sums a range of numbers:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
df = spark.range(1000).filter(&amp;quot;id % 2 = 0&amp;quot;).selectExpr(&amp;quot;sum(id)&amp;quot;)&lt;br /&gt;
df.explain(mode=&amp;quot;extended&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
== Parsed Logical Plan ==&lt;br /&gt;
&amp;#039;Project [unresolvedalias(&amp;#039;sum(&amp;#039;id))]&lt;br /&gt;
+- Filter ((id#0L % cast(2 as bigint)) = cast(0 as bigint))&lt;br /&gt;
   +- Range (0, 1000, step=1, splits=Some(2))&lt;br /&gt;
&lt;br /&gt;
== Analyzed Logical Plan ==&lt;br /&gt;
sum(id): bigint&lt;br /&gt;
Aggregate [sum(id#0L) AS sum(id)#5L]&lt;br /&gt;
+- Filter ((id#0L % cast(2 as bigint)) = cast(0 as bigint))&lt;br /&gt;
   +- Range (0, 1000, step=1, splits=Some(2))&lt;br /&gt;
&lt;br /&gt;
== Optimized Logical Plan ==&lt;br /&gt;
Aggregate [sum(id#0L) AS sum(id)#5L]&lt;br /&gt;
+- Filter ((id#0L % 2) = 0)&lt;br /&gt;
   +- Range (0, 1000, step=1, splits=Some(2))&lt;br /&gt;
&lt;br /&gt;
== Physical Plan ==&lt;br /&gt;
AdaptiveSparkPlan isFinalPlan=false&lt;br /&gt;
+- HashAggregate(keys=[], functions=[sum(id#0L)], output=[sum(id)#5L])&lt;br /&gt;
   +- Exchange SinglePartition, ENSURE_REQUIREMENTS, [plan_id=15]&lt;br /&gt;
      +- HashAggregate(keys=[], functions=[partial_sum(id#0L)], output=[sum#7L])&lt;br /&gt;
         +- Filter ((id#0L % 2) = 0)&lt;br /&gt;
            +- Range (0, 1000, step=1, splits=2)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Plans are trees printed with the root at the top; data flows from the bottom (the scan) up. Each line is one operator, and &amp;lt;code&amp;gt;+-&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;:-&amp;lt;/code&amp;gt; mark its children. The differences between sections show Catalyst&amp;#039;s work:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Parsed to analyzed.&amp;#039;&amp;#039;&amp;#039; &amp;lt;code&amp;gt;&amp;#039;sum(&amp;#039;id)&amp;lt;/code&amp;gt; became &amp;lt;code&amp;gt;sum(id#0L)&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;bigint&amp;lt;/code&amp;gt;, and the projection with an aggregate became an &amp;lt;code&amp;gt;Aggregate&amp;lt;/code&amp;gt; node. The number after &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt; is the attribute ID that the analyzer assigned.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Analyzed to optimized.&amp;#039;&amp;#039;&amp;#039; &amp;lt;code&amp;gt;cast(2 as bigint)&amp;lt;/code&amp;gt; was folded into the literal &amp;lt;code&amp;gt;2&amp;lt;/code&amp;gt;; this is the &amp;lt;code&amp;gt;ConstantFolding&amp;lt;/code&amp;gt; rule.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Optimized to physical.&amp;#039;&amp;#039;&amp;#039; The single logical &amp;lt;code&amp;gt;Aggregate&amp;lt;/code&amp;gt; became two &amp;lt;code&amp;gt;HashAggregate&amp;lt;/code&amp;gt; operators around an &amp;lt;code&amp;gt;Exchange&amp;lt;/code&amp;gt;: a &amp;lt;code&amp;gt;partial_sum&amp;lt;/code&amp;gt; in each task, a shuffle into one partition, then the final &amp;lt;code&amp;gt;sum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The physical plan starts with &amp;lt;code&amp;gt;AdaptiveSparkPlan isFinalPlan=false&amp;lt;/code&amp;gt; because [[Adaptive Query Execution]] is on by default; the plan printed before the query runs is the initial plan, and AQE may change it during execution. Calling &amp;lt;code&amp;gt;explain()&amp;lt;/code&amp;gt; after an action prints both a &amp;lt;code&amp;gt;Final Plan&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;Initial Plan&amp;lt;/code&amp;gt;, with the final plan showing query stages and nodes such as &amp;lt;code&amp;gt;AQEShuffleRead coalesced&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== EXPLAIN modes ==&lt;br /&gt;
&amp;lt;code&amp;gt;EXPLAIN&amp;lt;/code&amp;gt; takes one optional mode; &amp;lt;code&amp;gt;df.explain(mode=...)&amp;lt;/code&amp;gt; accepts the same names in lowercase.&amp;lt;ref name=&amp;quot;explain&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;pydf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/dataframe.py |title=dataframe.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! SQL !! PySpark &amp;lt;code&amp;gt;mode&amp;lt;/code&amp;gt; !! Output&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;EXPLAIN&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;&amp;quot;simple&amp;quot;&amp;lt;/code&amp;gt; (default) || Physical plan only&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;EXPLAIN EXTENDED&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;&amp;quot;extended&amp;quot;&amp;lt;/code&amp;gt; || Parsed, analyzed and optimized logical plans and the physical plan&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;EXPLAIN FORMATTED&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;&amp;quot;formatted&amp;quot;&amp;lt;/code&amp;gt; || Two sections: a short physical plan outline and the details of each node&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;EXPLAIN CODEGEN&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;&amp;quot;codegen&amp;quot;&amp;lt;/code&amp;gt; || Physical plan and the generated Java code, if any (see [[Whole-stage code generation]])&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;EXPLAIN COST&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;&amp;quot;cost&amp;quot;&amp;lt;/code&amp;gt; || Optimized logical plan with statistics, if available (see [[Cost-based optimizer (Spark)]])&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== How do you read EXPLAIN FORMATTED? ==&lt;br /&gt;
&amp;lt;code&amp;gt;EXPLAIN FORMATTED&amp;lt;/code&amp;gt; prints the physical plan as a short outline with a number in brackets for each node, followed by one block of details per number. Operators inside a [[Whole-stage code generation|whole-stage code generation]] stage are marked with &amp;lt;code&amp;gt;*&amp;lt;/code&amp;gt; in the outline and with &amp;lt;code&amp;gt;[codegen id : n]&amp;lt;/code&amp;gt; in their block.&amp;lt;ref name=&amp;quot;explain&amp;quot; /&amp;gt; This PySpark 4.3.0 example reads a Parquet file, filters it and counts rows per group, with [[Adaptive Query Execution]] turned off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
df = spark.read.parquet(&amp;quot;/data/pf&amp;quot;).filter(&amp;quot;id &amp;gt; 10&amp;quot;).groupBy(&amp;quot;g&amp;quot;).count()&lt;br /&gt;
df.explain(mode=&amp;quot;formatted&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
== Physical Plan ==&lt;br /&gt;
* HashAggregate (7)&lt;br /&gt;
+- Exchange (6)&lt;br /&gt;
   +- * HashAggregate (5)&lt;br /&gt;
      +- * Project (4)&lt;br /&gt;
         +- * Filter (3)&lt;br /&gt;
            +- * ColumnarToRow (2)&lt;br /&gt;
               +- Scan parquet  (1)&lt;br /&gt;
&lt;br /&gt;
(1) Scan parquet&lt;br /&gt;
Output [2]: [id#2L, g#3L]&lt;br /&gt;
Batched: true&lt;br /&gt;
Location: InMemoryFileIndex [file:/data/pf]&lt;br /&gt;
PushedFilters: [IsNotNull(id), GreaterThan(id,10)]&lt;br /&gt;
ReadSchema: struct&amp;lt;id:bigint,g:bigint&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(3) Filter [codegen id : 1]&lt;br /&gt;
Input [2]: [id#2L, g#3L]&lt;br /&gt;
Condition : (isnotnull(id#2L) AND (id#2L &amp;gt; 10))&lt;br /&gt;
&lt;br /&gt;
(6) Exchange&lt;br /&gt;
Input [2]: [g#3L, count#10L]&lt;br /&gt;
Arguments: hashpartitioning(g#3L, 200), ENSURE_REQUIREMENTS, [plan_id=48]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Blocks (2), (4), (5) and (7) are left out above, and the file path is shortened. The outline shows the shape at a glance: two code generation stages (1 and 2) separated by one shuffle. The detail blocks hold the long argument lists, such as the filters pushed to the Parquet reader (see [[Predicate pushdown]]) and the schema actually read, which would make a single-line tree hard to read.&lt;br /&gt;
&lt;br /&gt;
== Physical plan operators worth recognizing ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Operator !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;Exchange hashpartitioning(...)&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;rangepartitioning&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SinglePartition&amp;lt;/code&amp;gt; || A [[shuffle]], and therefore a stage boundary (see [[Narrow and wide transformations]])&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;BroadcastExchange&amp;lt;/code&amp;gt; || The small side of a [[broadcast join]], collected and sent to every executor&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;BroadcastHashJoin&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SortMergeJoin&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ShuffledHashJoin&amp;lt;/code&amp;gt; || The join strategy chosen (see [[Broadcast join]], [[Sort-merge join]], [[Shuffle hash join]])&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;HashAggregate&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;partial_&amp;lt;/code&amp;gt; functions || The map-side half of a two-phase aggregation&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;FileScan parquet&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;PushedFilters&amp;lt;/code&amp;gt; || A Parquet scan and the filters handed to the reader (see [[Predicate pushdown]])&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;*(n)&amp;lt;/code&amp;gt; prefix || The operator is compiled into whole-stage code generation stage &amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;lt;ref name=&amp;quot;wscg&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/WholeStageCodegenExec.scala |title=WholeStageCodegenExec.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ReusedExchange&amp;lt;/code&amp;gt; || The output of an identical exchange elsewhere in the plan is reused instead of being computed twice&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The Spark web UI shows the same plans. On the SQL / DataFrame tab, each query&amp;#039;s detail page draws the physical plan as a graph with runtime metrics, and a &amp;quot;Details&amp;quot; link expands the parsed, analyzed and optimized logical plans and the physical plan.&amp;lt;ref name=&amp;quot;webui&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/web-ui.html |title=Web UI (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Catalyst optimizer]]&lt;br /&gt;
* [[Whole-stage code generation]]&lt;br /&gt;
* [[Adaptive Query Execution]]&lt;br /&gt;
* [[Cost-based optimizer (Spark)]]&lt;br /&gt;
* [[Narrow and wide transformations]]&lt;br /&gt;
* [[Predicate pushdown]]&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>