VOL. 01
← The learning path
FUNDAMENTALS / LESSON 04

Lazy evaluation & actions

Understand why a line of code is not a job.

THE IDEA TO KEEP

Transformations describe a computation. An action asks Spark to produce a result, triggering the work it needs.

Build a plan, then execute

Think of a transaction pipeline as a recipe: read, filter, select, aggregate. Describing a recipe is different from cooking it. Deferring execution lets Spark inspect the requested computation before running it.

Predict before you run

In this example, building even describes a filter. count() asks for a result. A second action can require work again unless an appropriate intermediate result has been persisted and materialized.

Use the plan as evidence

explain("formatted") helps you inspect execution operators without collecting the dataset. Distinguish planning from execution, and avoid claiming that every API call is free: schema inference and metadata operations can do work too.

◈ Make it visible — explore the shuffle lab →Connect the concepts: the complete Spark execution flow →

Try it on a small dataset

Run this in a PySpark notebook with an existing spark session. Create a local session if needed. Examples are illustrative; this website does not execute Spark.

PYTHON / PYSPARK
from pyspark.sql import functions as F
numbers = spark.range(0, 10)
even = numbers.filter(F.col("id") % 2 == 0)
even.explain("formatted")
print(even.count())
Expected result

count() returns 5. The plan contains a range and a filter.

Result reasoned through on the small input; not runtime-tested against a Spark cluster.

Watch the assumption

Do not say “one action always equals exactly one Spark job.” SQL execution, subqueries, and adaptive execution can make that relationship more involved.

EXPLAIN IT BACK

I wrote five transformations. How many jobs did I create?

Say your answer out loud before opening the notes.

Reveal the reasoning

Transformation count alone cannot tell you. Ask which actions run, inspect the executed plan, and confirm in the Spark UI. Lazy transformations primarily build a plan.

Sources & verification

Core claims checked against Apache Spark 3.5.7 documentation and source references on 2026-09-13. Explanations, scenarios, and diagrams are original teaching material. Other releases and managed runtimes can differ.

Find a concept