Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Spark Developer Associate

Lazy Evaluation: Jobs, Stages, and Tasks

Databricks Spark Developer Associate · Lesson 4 of 18 · 20:38

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: transformations and actions, lazy evaluation, narrow and wide transformations, and how Spark splits the work into jobs, stages, and tasks. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Instant, then four minutes

A Coralpeak developer writes ten lines that read, filter, join, and group the game events, and the cell finishes instantly. Then a single call to show takes four minutes. This lesson explains why: transformations, actions, lazy evaluation, and how Spark splits the work into jobs, stages, and tasks.

Transformations and actions

A transformation describes a new DataFrame from an existing one, like select, filter, withColumn, join, or groupBy with an aggregation, and it returns immediately. An action asks for a result, so Spark has to compute: show, count, collect, take, and first return values to the driver. Writing data is an action too, since a write with save or saveAsTable runs the whole computation. So here is a quick way to tell them apart: a transformation gives back a DataFrame, while an action gives back data, or writes it somewhere.

Lazy evaluation

Spark evaluates transformations lazily: it builds a logical plan, and waits for an action before running anything. That lets the Catalyst optimizer see the whole query at once, and improve it, for example by pushing filters down to the source, and by reading only the columns it needs. You can see the plan with explain, which prints the physical plan, and in extended mode, the logical plans as well. And each action runs the whole plan from the source again, unless part of it is cached, so two actions on the same DataFrame mean two computations.

Narrow and wide transformations

In a narrow transformation, each output partition depends on a single input partition, like select, filter, or withColumn, so no data moves between machines. In a wide transformation, an output partition needs rows from many input partitions, like groupBy, join, distinct, orderBy, or repartition, so Spark has to shuffle the data. Spark chains narrow transformations together, and runs them in one pass over each partition, which is called pipelining. And wide transformations are where the shuffles, and most of the cost, come from.

Jobs, stages, and tasks

When an action runs, the driver creates a job: the parallel computation needed to produce that result. The job is split into stages at each shuffle boundary: within a stage, the transformations are narrow, and run as one pipeline. Each stage is made of tasks, one per partition, and the driver sends the tasks to the executors, which run them on their cores. So the hierarchy is: an application runs jobs, a job has stages, and a stage has tasks. And the Jobs tab of the Spark UI shows each job, with a graph of its stages, and the number of tasks in each.

Recap

Let's recap. Transformations return a new DataFrame and run nothing, while actions, like show, count, collect, and writes, compute a result. Spark is lazy: it builds a plan, optimizes it as a whole, and runs it only when an action asks. Narrow transformations stay within a partition, while wide ones, like groupBy and join, shuffle the data. Each action creates a job, split into stages at the shuffles. And each stage runs one task per partition, on the cores of the executors.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A cell reads the events with an explicit schema, filters the purchases, and groups them by country with a count. It finishes in under a second. Which statement is true?

    • Nothing was computed yet, since no action has run
    • The events were cached during the read
    • Spark computed the counts on the driver
    • The cluster processed every file in under a second
    Show the answer

    Nothing was computed yet, since no action has run

    Filter and groupBy with an aggregation are transformations, so Spark only recorded a plan, and no action has asked for a result. “The events were cached during the read” is wrong, since nothing is cached unless you call cache, and even then only an action fills it. “Spark computed the counts on the driver” is wrong, since nothing has been computed anywhere yet. And “The cluster processed every file in under a second” is wrong, since the work has not started, which is exactly why the cell was so fast.

  2. Which of these Coralpeak transformations needs a shuffle?

    • filter() on the event type
    • withColumn() that adds the event date
    • groupBy() on the player, then count()
    • select() of four columns
    Show the answer

    groupBy() on the player, then count()

    Grouping by player needs all the rows of each player in the same partition, so Spark shuffles the data. “filter() on the event type” is wrong, since filter keeps or drops rows inside each partition. “withColumn() that adds the event date” is wrong, since withColumn computes a value from the same row. And “select() of four columns” is wrong, since select only keeps some columns, partition by partition.

  3. An action reads the events, filters them, and counts them by country. How many stages does its job have?

    • One stage, since there is one action
    • Two stages, split at the shuffle of the groupBy
    • Three stages, one for each transformation
    • One stage for each partition that is read
    Show the answer

    Two stages, split at the shuffle of the groupBy

    The read and the filter are narrow, and run in the first stage, then the shuffle for the groupBy starts a second stage. “One stage, since there is one action” is wrong, since one action creates one job, but a shuffle splits it into stages. “Three stages, one for each transformation” is wrong, since narrow transformations share a stage, they do not get one each. And “One stage for each partition that is read” is wrong, since partitions become tasks, not stages.