What is lazy evaluation in Spark? What does it change about how you write a pipeline?
Why they ask this
It is the first idea in Spark, and the one that explains why a long chain of transformations costs nothing until the end.
Say this
Spark does not run a transformation when you write it. It records the step in a plan, and runs the whole plan only when an action asks for a result. That lets the optimizer see every step before it runs any of them.
The reasoning
Lazy evaluation means a transformation such as `filter`, `select` or `join` returns a new DataFrame that describes work, not data. Nothing is read and nothing is computed. Spark adds the step to a logical plan and waits.
An action such as `count()`, `collect()`, `show()` or `write` forces the plan to run. At that moment Catalyst sees the whole chain. It can push a filter below a join, drop columns nobody reads, and fold constants, because it knows the end before it starts.
For a pipeline this changes two habits. First, you can build a plan in small named steps without paying for each one. Second, every action re-runs the plan from the start unless you cache, so two actions on the same DataFrame read the source twice.
The cost is that errors surface late. A bad column name fails at the action, not at the line that named it. `explain()` shows the plan without running it, which is the cheap way to check.
The answer most people give
"Spark is lazy so it is slow to start." Laziness is not a delay. It is what lets Spark optimize the whole query and skip work, and it is why a transformation line never fails on its own.
They’ll ask next
Two actions on the same DataFrame. How many times is the source file read, and what changes that?
