The full answer
The DAG (directed acyclic graph) is the plan Spark builds of how each result depends on its inputs. It lets Spark see the whole computation before running it, group operations that don't need to move data into stages, run independent work in parallel, and recover lost data by recomputing only what depends on it.
How execution flows through the DAG
- Building: transformations add nodes to a logical plan; nothing runs. For DataFrames and SQL, the Catalyst optimizer rewrites this plan and chooses a physical plan.
- Action: an action (
count,write,collect) submits a job to the DAG scheduler. - Stage creation: the DAG scheduler walks back from the final result. Operations connected by narrow dependencies (
filter,select,map) are pipelined into one stage; each shuffle dependency (groupBy, shuffle joins,repartition) creates a stage boundary. - Ordering: stages are submitted when their parent stages have finished; independent branches, such as scanning two tables for a join, run at the same time.
- Tasks: each stage becomes one task per partition, sent to executors by the task scheduler.
- Recovery: if a task fails it is retried; if shuffle output is lost, the scheduler re-runs only the parent stage that produced it.
What the DAG enables
- Optimisation of the whole job: filters pushed to the source, projections pruned, operations fused into generated code.
- Pipelining: narrow operations run together on each partition with no intermediate data written.
- Minimal shuffles: the shuffle boundaries are explicit, which is where data moves.
- Fault tolerance: lineage edges say exactly what to recompute.
- Runtime re-planning: adaptive query execution changes the remaining stages once shuffle sizes are known.
Example
from pyspark.sql import functions as F
orders = spark.read.parquet("/data/orders").filter(F.col("amount") > 0)
customers = spark.read.parquet("/data/customers")
report = (orders.join(customers, "customer_id") # shuffle (or broadcast)
.groupBy("country") # shuffle
.agg(F.sum("amount").alias("revenue")))
report.write.mode("overwrite").parquet("/out/revenue_by_country")
The scans of orders and customers are separate stages that run in parallel; the join and the aggregation add stage boundaries, unless the customers table is broadcast, in which case the join happens inside the orders stage.
Seeing it The Spark UI shows a DAG visualisation for every job and stage, and the SQL tab shows the physical plan with metrics per operator. Reading it is the quickest way to see where shuffles happen and which stage is slow.
Last updated 16 October 2026
Sources