<?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=Lazy_evaluation</id>
	<title>Lazy evaluation - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Lazy_evaluation"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Lazy_evaluation&amp;action=history"/>
	<updated>2026-12-06T00:08:09Z</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=Lazy_evaluation&amp;diff=157&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=Lazy_evaluation&amp;diff=157&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:44Z</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            = Lazy evaluation&lt;br /&gt;
| also_known_as   = Lazy execution, deferred execution&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = Transformations (lazy), actions (eager), logical plan, analysis, optimization&lt;br /&gt;
| implementations = [[Apache Spark]] (RDD, DataFrame, SQL and [[Spark Connect]] APIs)&lt;br /&gt;
| related         = [[Directed acyclic graph (Spark)]], [[Catalyst optimizer]], [[Logical plan and physical plan]], [[SparkSession]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/rdd-programming-guide.md, docs/spark-connect-gotchas.md, docs/spark-connect-overview.md) and a PySpark 4.3.0 run (ANSI mode default, analysis and runtime errors)}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], &amp;#039;&amp;#039;&amp;#039;lazy evaluation&amp;#039;&amp;#039;&amp;#039; means that transformations such as &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;select&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; do not compute anything when they are called; Spark only records them in a plan, and runs the plan when an action such as &amp;lt;code&amp;gt;collect&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;count&amp;lt;/code&amp;gt; or a write needs a result.&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; Deferring the work lets Spark see the whole computation before running it, so it can combine steps and return only what the action needs. It also means that many errors appear at the action, far from the line that caused them.&lt;br /&gt;
&lt;br /&gt;
== Transformations vs actions ==&lt;br /&gt;
Spark divides operations into two kinds. A &amp;#039;&amp;#039;&amp;#039;transformation&amp;#039;&amp;#039;&amp;#039; creates a new dataset from an existing one; an &amp;#039;&amp;#039;&amp;#039;action&amp;#039;&amp;#039;&amp;#039; returns a value to the driver program or writes data after running a computation.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; All transformations are lazy: they remember the operations applied to a base dataset and are computed only when an action requires a result. The Spark guide&amp;#039;s example is a &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; followed by a &amp;lt;code&amp;gt;reduce&amp;lt;/code&amp;gt;, where Spark can return only the reduced result to the driver instead of the larger mapped dataset.&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;
! Kind !! DataFrame and SQL examples !! RDD examples !! When it computes&lt;br /&gt;
|-&lt;br /&gt;
| Transformation || &amp;lt;code&amp;gt;select&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;withColumn&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;groupBy().agg()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spark.sql(&amp;quot;SELECT ...&amp;quot;)&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;reduceByKey&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;join&amp;lt;/code&amp;gt; || At the next action that needs it&lt;br /&gt;
|-&lt;br /&gt;
| Action || &amp;lt;code&amp;gt;collect&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;count&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;show&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;take&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;toPandas&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;write...save&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;collect&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;count&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;reduce&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;saveAsTextFile&amp;lt;/code&amp;gt; || Immediately&lt;br /&gt;
|-&lt;br /&gt;
| SQL command || &amp;lt;code&amp;gt;spark.sql(&amp;quot;INSERT ...&amp;quot;)&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spark.sql(&amp;quot;CREATE ...&amp;quot;)&amp;lt;/code&amp;gt; || (none) || Immediately&amp;lt;ref name=&amp;quot;gotchas&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/spark-connect-gotchas.html |title=Eager vs Lazy: Spark Connect vs Spark Classic (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;
Each action runs the full chain of transformations again unless the data is cached. Without &amp;lt;code&amp;gt;persist()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;cache()&amp;lt;/code&amp;gt;, each transformed RDD may be recomputed every time an action runs on it.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Why do errors appear late? ==&lt;br /&gt;
Because transformations only build a plan, an error in the data or in a function passed to Spark is raised when an action runs, not on the line where the transformation was written. Two kinds of error behave differently in Spark Classic:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Analysis errors&amp;#039;&amp;#039;&amp;#039; (a missing column, a wrong type, an unknown table) are raised early. Spark Classic analyzes each DataFrame eagerly as it is built, so &amp;lt;code&amp;gt;spark.sql(&amp;quot;select 1 as a, 2 as b&amp;quot;).filter(&amp;quot;c &amp;gt; 1&amp;quot;)&amp;lt;/code&amp;gt; fails immediately because column &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; cannot be found.&amp;lt;ref name=&amp;quot;gotchas&amp;quot; /&amp;gt; In PySpark 4.3.0 this raises &amp;lt;code&amp;gt;AnalysisException&amp;lt;/code&amp;gt; with error class &amp;lt;code&amp;gt;UNRESOLVED_COLUMN.WITH_SUGGESTION&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Runtime errors&amp;#039;&amp;#039;&amp;#039; (bad values, an exception inside a UDF) are raised late, when the action runs the tasks. ANSI mode is on by default in Spark 4.3.0 (&amp;lt;code&amp;gt;spark.sql.ansi.enabled=true&amp;lt;/code&amp;gt;), so a division by zero is an error rather than a null, but it still surfaces only when the data is processed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
df = spark.range(3).selectExpr(&amp;quot;id&amp;quot;, &amp;quot;10 / (id - 1) AS x&amp;quot;)   # no error: only a plan&lt;br /&gt;
df.collect()   # raises SparkArithmeticException [DIVIDE_BY_ZERO] at the action&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Accumulators show the same effect. An accumulator updated inside a transformation such as &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; changes only when an action computes that RDD, so updates inside lazy transformations are not guaranteed to have run.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does Spark Connect change when analysis happens? ==&lt;br /&gt;
With [[Spark Connect]], execution is lazy in the same way, but analysis is lazy too. The client builds unresolved plans and sends them to the server only when an operation needs a resolved plan: an action, schema access (&amp;lt;code&amp;gt;df.columns&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;df.schema&amp;lt;/code&amp;gt;), &amp;lt;code&amp;gt;explain&amp;lt;/code&amp;gt; or persisting a DataFrame.&amp;lt;ref name=&amp;quot;gotchas&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Operation !! Spark Classic !! Spark Connect&lt;br /&gt;
|-&lt;br /&gt;
| Transformations: &amp;lt;code&amp;gt;filter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;select&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;limit&amp;lt;/code&amp;gt; || Executed lazily; analyzed eagerly || Executed lazily; analyzed lazily&lt;br /&gt;
|-&lt;br /&gt;
| Schema access: &amp;lt;code&amp;gt;df.columns&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;df.schema&amp;lt;/code&amp;gt; || Eager || Eager, and each access sends an analysis request to the server&lt;br /&gt;
|-&lt;br /&gt;
| Actions: &amp;lt;code&amp;gt;collect&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;show&amp;lt;/code&amp;gt; || Eager || Eager&lt;br /&gt;
|-&lt;br /&gt;
| Session state a DataFrame depends on (UDFs, temporary views, configs) || Captured when the DataFrame is defined || Resolved when the DataFrame executes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Source: the Spark 4.3.0 guide &amp;quot;Eager vs Lazy: Spark Connect vs Spark Classic&amp;quot;.&amp;lt;ref name=&amp;quot;gotchas&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This changes program behavior in three ways that the guide documents:&amp;lt;ref name=&amp;quot;gotchas&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* The example &amp;lt;code&amp;gt;spark.sql(&amp;quot;select 1 as a, 2 as b&amp;quot;).filter(&amp;quot;c &amp;gt; 1&amp;quot;)&amp;lt;/code&amp;gt; raises no error in Spark Connect until &amp;lt;code&amp;gt;df.columns&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;df.show()&amp;lt;/code&amp;gt; is called.&lt;br /&gt;
* A DataFrame that reads a temporary view by name sees the view&amp;#039;s current definition at execution time; replacing the view changes what earlier DataFrames return. Unique view names avoid this.&lt;br /&gt;
* A Python UDF is serialized when the query runs, not when it is defined, so it sees the latest value of any outer variable it reads.&lt;br /&gt;
&lt;br /&gt;
Code that calls &amp;lt;code&amp;gt;df.columns&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;df.schema&amp;lt;/code&amp;gt; many times, for example in a loop that adds columns one by one, sends many analysis requests over Spark Connect. The guide recommends building all column expressions first and applying them in a single &amp;lt;code&amp;gt;select&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;gotchas&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Directed acyclic graph (Spark)]]&lt;br /&gt;
* [[Catalyst optimizer]]&lt;br /&gt;
* [[Logical plan and physical plan]]&lt;br /&gt;
* [[Spark Connect]]&lt;br /&gt;
* [[SparkSession]]&lt;br /&gt;
* [[Narrow and wide transformations]]&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>