PySpark / PerformanceFig_14 · Lesson 14 / 14
Fig_14 · Query Plans · v1.1Catalyst pipeline

Query Plans

SQL TO LOGICAL TO PHYSICAL TO EXECUTION

Spark does not run your DataFrame code directly. It parses it, resolves it, optimizes it, chooses a physical plan, generates code where possible, and then runs distributed tasks.

EXPLAIN EXTENDEDEXPLAIN FORMATTEDEXPLAIN CODEGEN

01 · What it is

Read the plan as an assembly line

A Spark query plan is the path from what you wrote to what executors run. The order matters: Spark first understands the query, then improves the logical plan, then chooses a physical strategy, then executes it.

The one rule to rememberThe top of explain(True) shows how Spark understood and optimized your query. The bottom, Physical Plan, shows what Spark will actually run.
SQL AST / DataFrame
Unresolved logical
Analysis
Logical plan
Logical optimization
Optimized logical
Physical planning
Physical plans
Cost model
Selected physical
Codegen
RDDs

02 · Interactive pipeline

Step through the image left to right

Query-plan pipeline

Click each stage in the same order as the diagram.

SQL AST / DataFrame
current stage
What did I write?
question to ask
customers.filter(col("city") == "boston").select("cust_id", "name")
This is your starting point: SQL text or DataFrame transformations. Nothing has run yet.

03 · How to read explain

The four sections Spark prints

SectionPlain meaningWhat to notice
Parsed Logical PlanSpark parsed your code but names may still be unresolvedDoes this match what you wrote?
Analyzed Logical PlanCatalog/schema resolved columns, tables, functions, typesWrong column/type errors fail here
Optimized Logical PlanCatalyst removes unnecessary workColumn pruning, pushed filters, collapsed projections
Physical PlanExecutable operators Spark selectedExchange, joins, aggregates, scans, AQE
df.explain(True)
# == Parsed Logical Plan ==
# == Analyzed Logical Plan ==
# == Optimized Logical Plan ==
# == Physical Plan ==

04 · Notebook examples

Narrow vs wide plans

analogyNarrow operations are like editing each page in place. Wide operations are like collecting pages from every desk and redistributing them by topic.

ExamplePlan clueMeaning
filter / select / withColumnFilter, Project, no ExchangeLocal per-partition work
repartition(24)Exchange RoundRobinPartitioning(24)Full shuffle to create new partitions
coalesce(5)Coalesce 5Merge partitions without full shuffle
join with broadcast disabledSortMergeJoin + two Exchange nodesBoth sides shuffle by join key
groupBy().count()HashAggregate → Exchange → HashAggregatePartial aggregate, shuffle, final aggregate

05 · Physical node checklist

Start with expensive nodes

NodePlain meaningNext check
FileScan parquetRead filesReadSchema, PushedFilters, PartitionFilters
ExchangeShuffle / repartitionSpark UI shuffle read/write
CoalesceReduce partitions cheaplyPossible imbalance
SortMergeJoinShuffle, sort, mergeJoin size and skew
HashAggregateAggregate by keyPartial vs final aggregate
AQEShuffleReadAQE changed shuffle readFinal adaptive plan after action

06 · Pushdown note

Why a filter can appear twice

If Spark pushes a filter into Parquet/Delta, you may still see a Filter above the scan. That is not necessarily duplicate waste. Spark keeps a physical filter as a correctness check because not every source can fully apply every pushed predicate.

customers.filter(col("age").cast("int") > 50).explain(True)
# Casts and complex expressions may limit pushdown.
# Check PushedFilters and the physical Filter.

07 · Layer names

Catalyst chooses; engines execute

TermWhere it fitsSimple meaning
CatalystLogical + physical planningOptimizes and chooses query plans
Cost modelPhysical planningPicks from candidate physical plans
AQERuntime planningRevises after real shuffle stats
TungstenExecution internalsCompact memory and codegen
PhotonDatabricks execution engineNative execution for compatible operators