Menu

PySpark course · Lesson 12 of 12

Top 50 PySpark and Spark Interview Questions and Answers

50 PySpark interview questions with answers and code: shuffles, joins, skew, caching, memory tuning and Structured Streaming. For freshers and experienced.

  • Intermediate
  • 109 min read
  • Updated Oct 2026
On this page
  1. Question list
  2. Beginner PySpark and Spark interview questions (freshers)
  3. Intermediate PySpark and Spark interview questions
  4. Advanced PySpark and Spark interview questions (experienced)

These 50 PySpark and Spark interview questions cover the execution model, partitions and shuffles, join strategies, skew, caching, memory and resource tuning, AQE and Structured Streaming. They are grouped from beginner (freshers) to advanced (experienced). Each question has one clear answer in plain words, with an example where it helps and the mistakes interviewers watch for.

Question list

Show all 50 questions
  1. Spark architecture: driver, executors and cluster manager
  2. Jobs, stages, tasks and the DAG
  3. Lazy evaluation
  4. Transformations vs actions
  5. Narrow vs wide transformations
  6. RDD vs DataFrame vs Dataset
  7. repartition vs coalesce
  8. cache() vs persist() and storage levels
  9. Handling nulls in PySpark
  10. Reading and writing with an explicit schema
  11. Broadcast variables and accumulators
  12. Output modes: append, update, complete
  13. Coding: deduplicate to keep the latest record per key
  14. Coding: word count / frequency count
  15. Spark Connect (client-server)
  16. What happens during a shuffle, and why is it expensive?
  17. spark.sql.shuffle.partitions: default and tuning
  18. How Spark decides the number of input partitions
  19. Join strategies: broadcast hash, shuffle hash, sort-merge
  20. Broadcast joins: threshold, hints and limits
  21. How do you detect data skew?
  22. Adaptive Query Execution: what does it do?
  23. When caching hurts, and how to unpersist
  24. Python UDF vs pandas UDF vs built-in functions
  25. Dynamic allocation
  26. Reading the Spark UI
  27. Using explain() to read physical plans
  28. Predicate pushdown and partition pruning
  29. partitionBy vs bucketBy when writing
  30. Save modes and dynamic partition overwrite
  31. Window functions in PySpark
  32. explode, flattening nested JSON and arrays
  33. groupByKey vs reduceByKey (RDD API)
  34. Structured Streaming programming model
  35. Triggers: processingTime, availableNow, once, continuous
  36. foreachBatch for upserts and multiple sinks
  37. Coding: top-N per group and running totals
  38. Spark vs pandas vs Polars: when to use each
  39. Fix skew with salting (with code)
  40. Catalyst optimizer phases
  41. Project Tungsten and whole-stage code generation
  42. Executor memory model: unified, execution, storage, overhead
  43. Driver OOM vs executor OOM: causes and fixes
  44. How do you size executors (cores and memory)?
  45. Dynamic partition pruning
  46. Checkpointing vs caching (lineage truncation)
  47. Watermarks and late data in Structured Streaming
  48. Checkpoints and exactly-once in Structured Streaming
  49. Stream-stream joins
  50. What’s new in Spark 4.0 for data engineers?

Beginner PySpark and Spark interview questions (freshers)

1. Spark architecture: driver, executors and cluster manager

Beginner · Very Common · Architecture

A Spark application has one driver and many executors. The driver runs your main program. It turns your code into a plan, splits the work into stages and tasks, and sends those tasks out. Executors are processes on worker machines. They run the tasks, keep cached data and write shuffle files. A cluster manager (standalone, YARN, Kubernetes or Databricks) gives the driver and executors their CPU and memory.

Driver

  • Hosts the SparkSession / SparkContext.
  • Turns DataFrame and SQL code into a plan (Catalyst; see Catalyst optimizer phases), then into a DAG of stages and tasks (see Jobs, stages, tasks and the DAG).
  • The DAG scheduler splits work into stages at shuffle boundaries. The task scheduler sends tasks to executors, tracks progress and retries failures.
  • Holds results of actions like collect() and broadcast variables before distribution. A driver OOM usually means too much data was pulled to it (see Driver OOM vs executor OOM: causes and fixes).

Executors

  • Launched at application start (or dynamically; see Dynamic allocation). Each has a number of cores, which are task slots, and a memory allocation.
  • Run tasks: one task processes one partition of data.
  • Store cached partitions (see cache() vs persist() and storage levels) and write and serve shuffle files.
  • Report status and metrics back to the driver.

Cluster manager

Manager Notes
Standalone Spark’s built-in simple manager
YARN Hadoop clusters (EMR, on-prem)
Kubernetes Containers; driver and executors as pods
Databricks / managed services Platform-managed clusters or serverless

Deploy modes

  • Client mode: the driver runs where you submit (a laptop or edge node).
  • Cluster mode: the driver runs inside the cluster, which is more reliable in production.

Spark Connect (Spark 3.4+) decouples the client from the driver. A thin client sends unresolved plans to a remote Spark server (see Spark Connect (client-server)).

Example

# Runtime: PySpark 3.5
from pyspark.sql import SparkSession

spark = (SparkSession.builder
         .appName("orders-etl")
         .config("spark.executor.instances", "10")
         .config("spark.executor.cores", "4")
         .config("spark.executor.memory", "16g")
         .config("spark.driver.memory", "8g")
         .getOrCreate())

sc = spark.sparkContext
print(sc.master, sc.applicationId, sc.defaultParallelism)

On managed platforms such as Databricks or EMR Serverless, executor settings are usually defined by the cluster configuration rather than in code.

Common mistakes:

  • Thinking the driver processes the data.
  • Not knowing a task maps to a partition.
  • Confusing executors with worker nodes. One node can host several executors.

2. Jobs, stages, tasks and the DAG

Beginner · Very Common · Execution Model

Every action, such as count, write or collect, starts a job. Spark splits each job into stages wherever data has to be shuffled between machines. Each stage is made of tasks, one per partition, all running the same code in parallel. The DAG (directed acyclic graph) records how the stages depend on each other. Spark uses it to run stages in the right order and to rebuild lost data.

Hierarchy: Application → Jobs (per action) → Stages (split at shuffles) → Tasks (per partition).

Stages

  • Narrow transformations (filter, select, withColumn, map) are pipelined within one stage. No data moves between executors.
  • Wide transformations (groupBy, join without broadcast, distinct, repartition, orderBy) need a shuffle and create a new stage (see Narrow vs wide transformations, What happens during a shuffle, and why is it expensive?).
  • ShuffleMapStages write shuffle files. The ResultStage produces the action’s output.

Tasks

Fault tolerance through lineage: if a task fails, Spark retries it (by default up to 4 attempts). If shuffle output is lost with a dead executor, Spark recomputes the parent stage’s affected partitions from lineage.

Reading it in the Spark UI

  • Jobs tab: one entry per action. Stages tab: durations, shuffle read and write, spill, and the task time distribution.
  • The DAG visualisation shows stage boundaries (exchanges).
  • With AQE, the final plan can differ from the initial one (see Adaptive Query Execution: what does it do?).

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

orders = spark.read.parquet("s3://lake/silver/orders/")         # stage 1 (scan)
paid = orders.filter(F.col("status") == "paid")                  # narrow, same stage
by_region = paid.groupBy("region").agg(F.sum("amount").alias("revenue"))  # shuffle -> stage 2
by_region.write.mode("overwrite").parquet("s3://lake/gold/revenue_by_region/")  # action -> job

The write action triggers one job with two stages: scan, filter and partial aggregation; then shuffle, final aggregation and write.

Common mistakes:

  • Saying “each transformation is a stage”.
  • Not linking stage boundaries to shuffles.
  • Not knowing that each action triggers recomputation from the source unless cached.

In depth: Explain Spark jobs, stages and tasks.

3. Lazy evaluation

Beginner · Very Common · Execution Model

Spark does not run a transformation the moment you call it. It only records it in a plan. Nothing runs until you call an action, such as count, show, collect or write. Waiting lets Spark’s optimizer see the whole pipeline first. It can then push filters down, read only the columns you need, merge steps together and choose the best join before reading any data.

How it works

  • df.filter(...).select(...).join(...) returns new DataFrames immediately. Each one is just a plan.
  • On an action, Catalyst analyses, optimises and physically plans the entire chain (see Catalyst optimizer phases), then runs it as jobs, stages and tasks.

Benefits

  • Optimisation across steps: a filter written after a join can be pushed below the join or into the file scan (predicate pushdown; see Predicate pushdown and partition pruning). Unused columns are never read (column pruning).
  • Less I/O: only the necessary data is read.
  • Pipelining: narrow operations fuse into one stage, with no intermediate materialisation.
  • Fault tolerance: the plan (lineage) lets Spark recompute lost partitions.

Consequences to remember

  • Errors appear late. A bad cast or a missing file may only fail at the action, far from the line that caused it.
  • Recomputation. Every action recomputes the plan from the source unless you cache or persist (see cache() vs persist() and storage levels). Calling count() “for debugging” before a write doubles the work.
  • Non-deterministic sources (reading a changing table, using rand() or the current time) can give different results across actions if the data changes between them.
  • explain() shows the plan without running it (see Using explain() to read physical plans). show() runs a limited job, which may not exercise the whole dataset.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

df = spark.read.parquet("s3://lake/silver/events/")              # nothing read yet
recent = df.filter(F.col("event_date") >= "2026-03-01")           # still lazy
small = recent.select("user_id", "event_type")                     # still lazy

small.explain()            # shows pushed filters and pruned columns; no job runs
print(small.count())       # action: job runs now, reading only 2 columns of matching files

Common mistakes:

  • Believing each line executes immediately.
  • Calling multiple actions on an uncached DataFrame.
  • Being surprised that errors surface later.

Learn more: PySpark Transformations vs Actions

4. Transformations vs actions

Beginner · Very Common · Execution Model

Transformations, such as select, filter, withColumn, join, groupBy().agg() and union, describe a new DataFrame built from an existing one. They are lazy: nothing runs yet. Actions, such as count, collect, show, take, write and foreach, make Spark actually run the plan. They either bring results back to the driver or write data out. Every action starts at least one job.

Category Examples Effect
Narrow transformations select, filter/where, withColumn, drop, map (RDD), union, coalesce No shuffle; pipelined in one stage
Wide transformations groupBy().agg(), join (non-broadcast), distinct, orderBy, repartition, window functions with PARTITION BY Shuffle; new stage
Actions returning to driver collect, take(n), first, head, count, show, toPandas Results come to driver (watch memory)
Actions writing out write.save/saveAsTable/parquet/insertInto, foreach, foreachPartition Data written by executors
Streaming writeStream.start() Starts a continuous/incremental query

Subtleties

  • cache() and persist() are lazy markers, not actions.
  • df.rdd.getNumPartitions() doesn’t trigger a job. df.count() does.
  • Some “transformations” trigger eager work: schema inference on read, df.schema with inference, checkpoint() (eager by default), localCheckpoint(), and in some cases sort sampling jobs to compute range boundaries.
  • show() runs a small job and may read only a few partitions, so it isn’t a full validation.

Driver-safety guidance

  • Prefer take(n) or limit(n).toPandas() to collect() / toPandas() on large data.
  • Write large outputs with df.write rather than collecting them to the driver.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

clicks = spark.read.parquet("s3://lake/silver/clicks/")
daily = (clicks
         .withColumn("click_date", F.to_date("click_ts"))      # transformation (narrow)
         .groupBy("click_date", "campaign_id")                 # transformation (wide)
         .agg(F.count("*").alias("clicks")))

daily.show(5)                                                   # action (small job)
daily.write.mode("overwrite").partitionBy("click_date") \
     .parquet("s3://lake/gold/daily_campaign_clicks/")          # action (full job)

Common mistakes:

  • Calling cache() an action.
  • Using collect() on large data.
  • Not realising every action re-executes uncached lineage.

Learn more: PySpark Transformations vs Actions

In depth: What is the difference between a transformation and an action in Spark?

5. Narrow vs wide transformations

Beginner · Very Common · Execution Model

In a narrow transformation, each output partition needs data from only one input partition. Examples are filter, select and withColumn, and no data moves between machines. In a wide transformation, each output partition needs data from many input partitions. Examples are groupBy, join, distinct, orderBy and repartition. That forces a shuffle across the network and starts a new stage. Fewer and smaller shuffles is the key to fast Spark jobs.

Aspect Narrow Wide
Dependency One-to-one (or few-to-one, e.g. coalesce) Many-to-many
Data movement None (within executor) Shuffle across network
Stage boundary No Yes
Failure recovery Recompute one parent partition May need to recompute many parent partitions
Examples select, filter, withColumn, cast, union, coalesce, broadcast hash join (large side) groupBy().agg(), join (sort-merge/shuffle hash), distinct, dropDuplicates, orderBy, repartition, window PARTITION BY

Notes

  • Broadcast joins avoid shuffling the large side. The small side is sent to every executor, so the join itself is narrow for the big table (see Broadcast joins: threshold, hints and limits).
  • Aggregations do a partial (map-side) aggregation before the shuffle, so only per-partition partial results move. That greatly reduces the shuffle size for sum and count.
  • coalesce(n) reducing partitions is narrow. repartition(n) is wide (see repartition vs coalesce).
  • Pre-partitioned or bucketed data can let Spark skip a shuffle for joins and aggregations on the bucket keys (see partitionBy vs bucketBy when writing).

How to reduce wide work

  • Filter and project before joins and aggregations.
  • Broadcast small dimensions.
  • Combine several aggregations into one groupBy.
  • Avoid unnecessary distinct or orderBy in intermediate steps.
  • Reuse the same partitioning for consecutive operations on the same key.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

orders = spark.read.table("silver.orders")
stores = spark.read.table("silver.stores")          # small dimension

result = (orders
          .filter(F.col("order_date") >= "2026-03-01")          # narrow
          .join(F.broadcast(stores), "store_id")                 # broadcast: no shuffle of orders
          .groupBy("region")                                     # wide: shuffle (after partial agg)
          .agg(F.sum("amount").alias("revenue"),
               F.countDistinct("customer_id").alias("customers")))
result.explain()     # look for 'Exchange' nodes = shuffles

Common mistakes:

  • Calling coalesce wide.
  • Not knowing broadcast joins avoid a shuffle of the large table.
  • Ignoring map-side partial aggregation.

6. RDD vs DataFrame vs Dataset

Beginner · Very Common · APIs

An RDD is Spark’s low-level collection of objects spread across the cluster. It gives full control, but Spark cannot optimise it because it has no schema. A DataFrame is a distributed table with named columns and types. Spark’s Catalyst optimizer and Tungsten engine make it fast, and it is the standard API in Python, SQL, Scala and R. A Dataset is a typed DataFrame for Scala and Java only. In PySpark, you use DataFrames.

Aspect RDD DataFrame Dataset (Scala/Java)
Abstraction Distributed collection of objects Distributed table (rows + schema) Typed objects with schema
Optimisation None (you control execution) Catalyst + Tungsten Catalyst + Tungsten (lambdas less optimisable)
Type safety Compile-time (Scala) Runtime (column names checked at analysis) Compile-time
Languages Scala, Java, Python All, including SQL Scala, Java
Serialization Java/Kryo objects (Python: pickled) Compact binary (UnsafeRow) Encoders to binary
Python performance Slow (Python worker round-trips) Fast (work stays in JVM/Photon) N/A
Typical use today Legacy code, low-level control, unstructured processing Almost everything Typed Scala pipelines

Why DataFrames win for data engineering

When you still see RDDs

  • Older code using map/reduceByKey, mapPartitions for custom per-partition logic (DataFrames also have mapInPandas / mapInArrow), or very low-level partition control.
  • Not available on some platforms’ shared or serverless compute (for example Databricks standard access mode and serverless), which is another reason to avoid them.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

# RDD style (legacy): word count
lines_rdd = spark.sparkContext.textFile("s3://lake/raw/logs/2026-03-01/*.log")
counts_rdd = (lines_rdd.flatMap(lambda line: line.split())
                       .map(lambda w: (w.lower(), 1))
                       .reduceByKey(lambda a, b: a + b))

# DataFrame style (optimised)
lines_df = spark.read.text("s3://lake/raw/logs/2026-03-01/*.log")
counts_df = (lines_df.select(F.explode(F.split(F.lower("value"), r"\s+")).alias("word"))
                     .filter(F.col("word") != "")
                     .groupBy("word").count())

Common mistakes:

  • Saying Datasets are available in PySpark.
  • Writing new pipelines with RDDs without a reason.
  • Thinking DataFrames are “just pandas on Spark”.

7. repartition vs coalesce

Beginner · Very Common · Partitioning

repartition(n) does a full shuffle and produces n partitions of roughly equal size. You can also repartition by columns. It can raise or lower the number of partitions. coalesce(n) can only lower the number. It merges existing partitions without a shuffle, so it is cheaper, but the partitions can end up uneven. Be careful: Spark may apply the coalesce to earlier steps too, which can make the whole job less parallel.

Aspect repartition coalesce
Shuffle Yes (full) No (narrow)
Increase partitions Yes No (cannot increase without shuffle=True in RDD API)
Decrease partitions Yes Yes
Balance Even sizes (round-robin or hash) Possibly uneven (merges neighbours)
By column repartition(n, "col") hash-partitions by key No
Cost Higher Lower
Typical use Fix skewed/too few partitions, prepare for partitioned writes, increase parallelism Reduce output file count at the end of a job

The coalesce gotcha. Because coalesce is narrow, Spark can pipeline it into the preceding stage. df.<heavy transforms>.coalesce(1).write may run the heavy transforms with only 1 task, which is extremely slow. If upstream work is heavy, use repartition(n) before writing (accepting one shuffle), or make sure a shuffle boundary already exists before the coalesce.

Related functions

  • repartitionByRange(n, col): range partitioning, which gives sorted, non-overlapping ranges. Useful before writes that benefit from ordered files.
  • df.write.partitionBy("col"): directory partitioning at write time, a different concept (see partitionBy vs bucketBy when writing).
  • AQE can automatically coalesce small shuffle partitions after a shuffle (see Adaptive Query Execution: what does it do?), which often removes the need for a manual coalesce.
  • SQL hints: /*+ REPARTITION(200, col) */, /*+ COALESCE(10) */, /*+ REBALANCE */ (AQE-aware rebalancing that avoids small files).

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

big = spark.read.table("silver.events").filter(F.col("event_date") == "2026-03-01")

# Write ~16 files per date partition, keyed by user for locality
(big.repartition(16, "user_id")
    .write.mode("overwrite")
    .parquet("s3://lake/gold/events_by_user/event_date=2026-03-01/"))

# Small final result: reduce files cheaply (upstream already has a shuffle from groupBy)
summary = big.groupBy("event_type").count()
summary.coalesce(1).write.mode("overwrite").json("s3://lake/reports/event_type_counts/")

Common mistakes:

  • coalesce to increase partitions.
  • coalesce(1) directly after heavy narrow transformations.
  • repartition everywhere without need, adding shuffles.

Learn more: Spark Partitions, Shuffles and Data Skew

8. cache() vs persist() and storage levels

Beginner · Very Common · Caching

cache() and persist() both keep a DataFrame’s results after it is first computed, so later actions reuse them instead of reading the source again. For DataFrames, cache() is simply persist() with the default level, memory and disk. persist(level) lets you choose, for example MEMORY_ONLY or DISK_ONLY. Both are lazy: data is stored on the next action. Call unpersist() to free the memory when you are done.

Storage levels (common ones)

Level Memory Disk Serialized Replication Notes
MEMORY_ONLY Yes (deserialized for RDD) 1 Recompute partitions that don’t fit
MEMORY_AND_DISK (DataFrame default for cache()) Yes Yes DataFrames use columnar compressed format 1 Spill to disk if memory is full
MEMORY_ONLY_SER / MEMORY_AND_DISK_SER (Scala/Java RDD) Yes (Yes) Yes 1 Less memory, more CPU
DISK_ONLY Yes Yes 1 When memory is scarce
*_2 variants 2 Replicated for faster recovery
OFF_HEAP off-heap Yes 1 Requires off-heap config

For DataFrames, cached data is stored in an in-memory columnar format (compressed), which is efficient for scans.

When caching helps

  • A DataFrame is reused in multiple actions or branches (written to two outputs, used in several joins).
  • Iterative algorithms (ML training loops).
  • An expensive computation (heavy joins) feeding several downstream steps.

How to use it correctly

  1. df.cache() (lazy).
  2. Trigger materialisation with the first real action. A count() forces a full materialisation, which costs time. Often the first downstream write does the job.
  3. Reuse df.
  4. df.unpersist() when done (memory is otherwise held until evicted or the session ends).

Check it: the Spark UI Storage tab shows the fraction cached, memory and disk use. df.storageLevel shows the level.

Platform note: on Databricks, the disk cache (formerly Delta cache) automatically caches remote Parquet and Delta data on local SSDs, which is separate from Spark caching. Serverless and some shared compute modes restrict cache() or persist().

Example

# Runtime: PySpark 3.5
from pyspark import StorageLevel
from pyspark.sql import functions as F

enriched = (spark.read.table("silver.orders")
            .join(spark.read.table("silver.customers"), "customer_id")
            .withColumn("order_month", F.date_trunc("month", "order_ts")))

enriched.persist(StorageLevel.MEMORY_AND_DISK)

(enriched.groupBy("order_month", "segment").agg(F.sum("amount").alias("revenue"))
         .write.mode("overwrite").saveAsTable("gold.revenue_by_segment"))     # materialises cache
(enriched.groupBy("order_month", "country").agg(F.countDistinct("customer_id").alias("buyers"))
         .write.mode("overwrite").saveAsTable("gold.buyers_by_country"))      # reuses cache

enriched.unpersist()

Common mistakes:

Learn more: Spark Caching, Checkpointing, Broadcast Variables and Accumulators

9. Handling nulls in PySpark

Beginner · Common · Data Cleaning

Find nulls with isNull() and isNotNull(), never with == None. Replace them with fillna, F.coalesce(col, default) or when/otherwise. Drop rows that contain them with dropna, choosing which columns to check. Use eqNullSafe (<=> in SQL) when NULL should equal NULL. Remember two rules: NULL keys never match in a normal join, and most aggregates simply skip NULLs.

Key behaviours (three-valued logic; see Find the Nth highest salary (several approaches))

  • F.col("x") == None yields NULL, not True, so filters drop every row. Use F.col("x").isNull().
  • Comparisons with NULL give NULL, and filter keeps only True.
  • Aggregates (sum, avg, count(col)) ignore NULLs. count("*") counts rows.
  • Joins on = never match NULL keys (see How do you detect data skew? for skew effects).
  • groupBy puts all NULLs into one group.
  • Sorting: asc_nulls_first / asc_nulls_last / desc_nulls_* give explicit control.

Tools

Need API
Find nulls per column df.select([F.sum(F.col(c).isNull().cast("int")).alias(c) for c in df.columns])
Replace with constants df.fillna({"country": "UNKNOWN", "discount": 0})
Replace with another column F.coalesce("ship_date", "order_date")
Conditional F.when(F.col("x").isNull(), ...).otherwise(...)
Drop rows df.dropna(subset=["order_id"]), df.dropna(how="all"), df.dropna(thresh=3)
Null-safe equality df1.join(df2, df1.k.eqNullSafe(df2.k))
Empty strings → NULL F.when(F.trim("x") == "", None).otherwise(F.col("x")) or F.nullif (Spark 3.5+)
NaN (floats) F.isnan, df.na.fill handles NaN for numeric; nanvl

Notes

  • fillna with a dict only applies to columns whose type matches the value’s type. A string default won’t fill integer columns.
  • NaN isn’t NULL for floats. Handle both in numeric data from pandas or CSV sources.
  • Decide on null semantics per business column, because “unknown” and “not applicable” are different. Document them in contracts (see What are data contracts, and how do you enforce them?).

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

customers = spark.createDataFrame(
    [(1, "IN", None, " "), (2, None, 5.0, "a@x.com"), (3, "US", float("nan"), None)],
    "customer_id INT, country STRING, discount DOUBLE, email STRING")

null_counts = customers.select([F.sum(F.col(c).isNull().cast("int")).alias(c) for c in customers.columns])
null_counts.show()

clean = (customers
         .withColumn("email", F.when(F.trim("email") == "", None).otherwise(F.col("email")))
         .withColumn("discount", F.nanvl("discount", F.lit(None).cast("double")))
         .fillna({"country": "UNKNOWN", "discount": 0.0}))
clean.show()

Result: customer 1 has email NULL and discount 0.0. Customer 2 has country UNKNOWN. Customer 3 has its NaN discount turned into 0.0.

Common mistakes:

  • == None comparisons.
  • Ignoring NaN vs NULL.
  • Filling NULLs with 0 when 0 is a valid, different value.

10. Reading and writing with an explicit schema

Beginner · Common · I/O

When reading CSV or JSON, give Spark the schema yourself, with StructType or a DDL string such as "id INT, name STRING". This skips a slow pass that guesses types, guarantees the types you expect, and makes bad data visible instead of quietly turning columns into strings. Add a read mode to decide what happens to broken rows: PERMISSIVE keeps them in a corrupt-record column, DROPMALFORMED drops them and FAILFAST stops the job.

Why an explicit schema

  • Performance: inferSchema=true (CSV) or default JSON inference reads data an extra time, which is expensive on large inputs.
  • Correctness: inference can pick the wrong types (IDs with leading zeros become ints, mixed formats become strings) or vary between files and days.
  • Contracts: the schema documents what you expect (see What are data contracts, and how do you enforce them?).

Ways to define it

  • StructType([StructField("order_id", LongType(), False), ...]).
  • DDL string: "order_id BIGINT, amount DECIMAL(12,2), order_ts TIMESTAMP".
  • Reuse an existing table’s schema: spark.table("silver.orders").schema.

Parse modes (CSV/JSON)

Mode Malformed rows
PERMISSIVE (default) Keep row; fields NULL; raw text in columnNameOfCorruptRecord (must be in schema as STRING)
DROPMALFORMED Drop silently (risky: data loss)
FAILFAST Throw on first bad record

Useful CSV options: header, sep, quote, escape, multiLine, nullValue, emptyValue, dateFormat, timestampFormat, encoding, ignoreLeadingWhiteSpace.

Writing

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

schema = """
  order_id BIGINT,
  customer_id STRING,
  amount DECIMAL(12,2),
  order_ts TIMESTAMP,
  _corrupt_record STRING
"""

orders = (spark.read
          .schema(schema)
          .option("header", "true")
          .option("mode", "PERMISSIVE")
          .option("columnNameOfCorruptRecord", "_corrupt_record")
          .option("timestampFormat", "yyyy-MM-dd HH:mm:ss")
          .csv("s3://lake/landing/orders/2026-03-01/"))

orders = orders.cache()        # required before filtering only on the corrupt-record column
bad = orders.filter(F.col("_corrupt_record").isNotNull())
good = orders.filter(F.col("_corrupt_record").isNull()).drop("_corrupt_record")

bad.write.mode("append").json("s3://lake/quarantine/orders/2026-03-01/")
good.write.mode("append").format("delta").saveAsTable("bronze.orders")
orders.unpersist()

Spark disallows queries that reference only the internal corrupt-record column on raw CSV/JSON scans. Caching (or otherwise materialising) the parsed result first is the documented workaround.

Common mistakes:

  • inferSchema=true in production.
  • DROPMALFORMED without monitoring.
  • Storing money as DOUBLE.

11. Broadcast variables and accumulators

Beginner · Common · Shared Variables

A broadcast variable sends a read-only value, such as a lookup dictionary or a small model, to each executor once, instead of with every task. An accumulator is a counter or sum that tasks add to and only the driver reads, for metrics such as “bad records seen”. Be careful: if a task is retried, an update made inside a transformation can be counted twice. Only updates inside actions are counted exactly once.

Broadcast variables

  • bc = spark.sparkContext.broadcast(lookup_dict), then bc.value inside UDFs or RDD functions.
  • Sent once per executor and cached there, which avoids serialising a big object into every task closure.
  • Use them for small reference data in UDFs, ML model weights, or configuration maps.
  • Clean up with bc.unpersist() (executors) or bc.destroy() (everywhere).
  • For DataFrame joins, use broadcast joins (F.broadcast(df); see Broadcast joins: threshold, hints and limits), not broadcast variables. Joins let Catalyst optimise.

Accumulators

  • acc = spark.sparkContext.accumulator(0), then acc.add(1) in tasks and acc.value on the driver after an action.
  • Custom accumulators through AccumulatorParam (Python) or AccumulatorV2 (Scala).
  • Guarantees:
    • Updates in actions (foreach, foreachPartition) are applied once per task, even with retries.
    • Updates in transformations (map, inside UDFs) may be applied multiple times if tasks are retried, stages recomputed, or the DataFrame is evaluated by multiple actions. Treat them as approximate.
  • They show up in the Spark UI for named accumulators (Scala). In PySpark they’re mostly for driver-side reads.

Better alternatives for metrics

  • Compute counts with DataFrame aggregations (count of bad rows) as part of the pipeline, which is exact and lineage-safe.
  • Use the observable metrics API: df.observe("metrics", F.count(...).alias(...)) (Spark 3.3+ in PySpark) gives exact metrics per action or streaming batch.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F
from pyspark.sql.types import StringType

country_names = {"IN": "India", "US": "United States", "UK": "United Kingdom"}
bc_names = spark.sparkContext.broadcast(country_names)

@F.udf(StringType())
def country_name(code):
    return bc_names.value.get(code, "Unknown")

orders = spark.read.table("silver.orders")
named = orders.withColumn("country_name", country_name("country_code"))

# Exact metric via observe (preferred over accumulators inside transformations)
observed = named.observe("quality", F.sum(F.when(F.col("country_name") == "Unknown", 1).otherwise(0))
                                      .alias("unknown_countries"))
observed.write.mode("overwrite").saveAsTable("gold.orders_named")

For a tiny map like this, a broadcast join with a small DataFrame, or F.create_map with literals, avoids the UDF entirely and is faster. The UDF is shown to illustrate broadcast variables.

Common mistakes:

  • Trusting accumulator values updated in transformations.
  • Broadcasting huge objects.
  • Using broadcast variables where a broadcast join would let the optimizer work.

Learn more: PySpark Joins and Join Strategy

12. Output modes: append, update, complete

Beginner · Common · Structured Streaming

The output mode decides what each micro-batch writes to the sink. Append writes only new rows that will never change again. It is the default, and windowed aggregations need a watermark to use it. Update writes only the rows that changed since the last trigger. Complete rewrites the whole result table on every trigger, so it only suits small aggregated results. Not every mode works with every query or sink.

Mode Writes per batch Valid for Sink considerations
Append New final rows only Stateless queries; aggregations with watermark (emitted once finalised); stream-stream joins Works with Delta/file/Kafka; simplest
Update Rows that changed (new or updated aggregates) Aggregations (with/without watermark) Sink must handle upserts: use foreachBatch + MERGE for Delta
Complete Entire result table Aggregations only Full rewrite each batch: keep result small

Rules of thumb

  • Stateless ETL (parse, filter, enrich): append.
  • Windowed metrics with tolerable delay: append plus a watermark, which emits each window once (see Watermarks and late data in Structured Streaming).
  • Running totals or live counters that change continuously: update, plus foreachBatch MERGE into Delta.
  • Small dashboards (top 100 pages, per-country counts): complete into a small table.
  • Sorting a streaming DataFrame is only allowed after an aggregation in complete mode.

Common errors

  • “Append output mode not supported when there are streaming aggregations on streaming DataFrames without watermark.” Add a watermark, or use update or complete.
  • Using update mode directly with a Delta sink appends the changed rows (duplicates by key). Handle it with MERGE in foreachBatch.

Example

# Runtime: PySpark 3.5 — update mode with MERGE into Delta via foreachBatch
from pyspark.sql import functions as F
from delta.tables import DeltaTable

orders = spark.readStream.table("silver.orders_stream")
totals = orders.groupBy("customer_id").agg(F.sum("amount").alias("lifetime_spend"))

def upsert_totals(batch_df, batch_id):
    target = DeltaTable.forName(spark, "gold.customer_lifetime_spend")
    (target.alias("t")
        .merge(batch_df.alias("s"), "t.customer_id = s.customer_id")
        .whenMatchedUpdate(set={"lifetime_spend": "s.lifetime_spend"})
        .whenNotMatchedInsertAll()
        .execute())

(totals.writeStream
    .outputMode("update")
    .foreachBatch(upsert_totals)
    .option("checkpointLocation", "s3://lake/_checkpoints/customer_lifetime_spend/")
    .start())

This aggregation without a watermark keeps state for every customer forever. That is acceptable for a bounded key space (customers), but not for unbounded keys such as event IDs.

Common mistakes:

  • Complete mode on large results.
  • Update mode into an append-only sink, creating duplicates.
  • Not knowing append needs a watermark for aggregations.

13. Coding: deduplicate to keep the latest record per key

Beginner · Very Common · Coding Patterns

Use a window partitioned by the key and ordered by the newest timestamp first, with a unique tiebreaker. Add row_number() and keep the rows where it equals 1. Do not use dropDuplicates(["key"]) for this: it keeps a random row, not the latest. Another option is max_by (Spark 3.3 and later) inside a groupBy, which avoids sorting all the rows.

Approach 1: row_number (most common and clearest)

  • Window.partitionBy("customer_id").orderBy(F.desc("updated_at"), F.desc("ingested_at")).
  • Filter rn == 1. The tiebreaker ensures determinism when updated_at ties.
  • NULL handling: desc puts NULLs last by default, which is usually what you want.

Approach 2: groupBy + max_by / struct max

  • F.max_by("col", "updated_at") per column (Spark 3.3+).
  • Or F.max(F.struct("updated_at", "ingested_at", *other_cols)) and then expand the struct. The struct comparison orders by its fields left to right.
  • Often cheaper than a window, because there is no sort within groups (hash aggregation), but it is less readable.

Approach 3: dropDuplicates (only for exact duplicates or “any one row”)

  • df.dropDuplicates(["customer_id"]) keeps one arbitrary row per key. That is fine for exact-duplicate removal, not for “latest”.

Streaming variant: dropDuplicatesWithinWatermark for event-ID dedupe with bounded state (see Watermarks and late data in Structured Streaming).

Comparison

Method Deterministic latest? Cost Readability
row_number window Yes (with tiebreaker) Shuffle + sort per key High
max_by / struct max Yes Shuffle + hash aggregate Medium
dropDuplicates No Shuffle + hash aggregate High (but wrong for latest)

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F
from pyspark.sql.window import Window

updates = spark.createDataFrame([
    (1, "Asha",  "SMB", "2026-03-01 10:00:00", "2026-03-01 10:05:00"),
    (1, "Asha",  "MID", "2026-03-02 09:00:00", "2026-03-02 09:01:00"),
    (2, "Ben",   "ENT", "2026-03-01 11:00:00", "2026-03-01 11:00:30"),
    (2, "Ben",   "MID", "2026-03-01 11:00:00", "2026-03-01 11:02:00"),   # tie on updated_at
], "customer_id INT, name STRING, segment STRING, updated_at STRING, ingested_at STRING") \
  .withColumn("updated_at", F.to_timestamp("updated_at")) \
  .withColumn("ingested_at", F.to_timestamp("ingested_at"))

w = Window.partitionBy("customer_id").orderBy(F.desc("updated_at"), F.desc("ingested_at"))
latest = updates.withColumn("rn", F.row_number().over(w)).filter("rn = 1").drop("rn")
latest.show()

latest_alt = (updates.groupBy("customer_id")
              .agg(F.max(F.struct("updated_at", "ingested_at", "name", "segment")).alias("r"))
              .select("customer_id", "r.name", "r.segment", "r.updated_at"))

Result: customer 1 → MID (newer updated_at). Customer 2 → MID (same updated_at, but newer ingested_at wins the tie).

Common mistakes:

  • dropDuplicates for “latest”.
  • No tiebreaker.
  • orderBy on the whole DataFrame followed by dropDuplicates (order isn’t guaranteed to be preserved).

14. Coding: word count / frequency count

Beginner · Common · Coding Patterns

Read the text into a DataFrame with spark.read.text. Make it lower case, split it on anything that is not a letter, and explode it so each word is its own row. Remove empty strings, and stopwords if you like. Then groupBy("word").count() and sort by count, highest first. It is Spark’s classic hello world, and it shows narrow steps, one shuffle and partial aggregation before the shuffle.

Steps and what they show

  1. spark.read.text(path): one row per line, in a column named value. Input partitions are based on file splits (see How Spark decides the number of input partitions).
  2. lower + regexp_replace / split(value, r"[^a-z0-9']+"): narrow, per-row transformations.
  3. explode: one row per token, still narrow, but the row count grows.
  4. filter(word != ""): removes empty tokens created by leading punctuation.
  5. groupBy("word").count(): a wide transformation. Spark does partial_count per partition, a shuffle by word, then the final count (see groupByKey vs reduceByKey (RDD API)).
  6. orderBy(desc("count")): another shuffle (range partitioning) for a global sort. Use limit for top-K.

Variations interviewers ask for

  • Stopwords: a broadcast small DataFrame with a left anti join, or isin with a small list.
  • Top-K only: orderBy(...).limit(k) (Spark optimises this to a TakeOrderedAndProject, avoiding a full sort).
  • Per-document counts: groupBy("doc_id", "word").
  • Streaming word count: the same code on readStream, with complete or update output mode.
  • RDD version: flatMap → map((w, 1)) → reduceByKey (see RDD vs DataFrame vs Dataset).

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

lines = spark.createDataFrame(
    [("The quick brown fox.",), ("The lazy dog, the end!",), ("",)], "value STRING")

stopwords = spark.createDataFrame([("the",), ("a",), ("an",)], "word STRING")

word_counts = (lines
    .select(F.explode(F.split(F.lower("value"), r"[^a-z0-9']+")).alias("word"))
    .filter(F.col("word") != "")
    .join(F.broadcast(stopwords), "word", "left_anti")
    .groupBy("word").count()
    .orderBy(F.desc("count"), "word"))

word_counts.show()

Result (with stopwords removed): brown 1, dog 1, end 1, fox 1, lazy 1, quick 1. Without the stopword filter, “the” would have a count of 3.

Common mistakes:

  • Splitting only on spaces (punctuation stays attached).
  • Forgetting to filter empty strings.
  • Using collect() and Python Counter on the driver.

15. Spark Connect (client-server)

Beginner · Occasional · Architecture

Spark Connect (Spark 3.4 and later) separates your client from the Spark driver. A thin client, such as PySpark in your IDE, builds the query plan and sends it over the network (gRPC) to a Spark Connect server. The server runs the driver and does the work. Clients become small and keep working across Spark upgrades. One cluster can safely serve many users, and Spark is easier to use from other apps and languages.

Architecture

Client (Python/Scala/Go/Rust/...)          Spark Connect server (driver)
  DataFrame API → unresolved plan  --gRPC-->  analyse/optimise/execute
  results  <-- Arrow batches ----------------  executors process data

Benefits

  • Thin clients: no JVM needed locally for the Python client (pyspark-client package in Spark 4.0). Use it from laptops, notebooks, web apps or IDEs.
  • Stability: client and server upgrade more independently. Client crashes don’t kill the driver, and driver issues don’t crash the client process.
  • Multi-tenancy: many sessions on one server, with isolation.
  • Debugging: run code locally in an IDE against a remote cluster.

Limitations (vary by version)

  • No direct RDD API or SparkContext access from Connect clients.
  • Some APIs and features differ or arrived later (certain ML APIs, some streaming features). Parity improves with each release.
  • UDFs still execute on the server side (Python UDFs need matching Python environments on the server).

Where you see it

  • Databricks Connect (v13+) is built on Spark Connect, connecting local IDEs to Databricks clusters or serverless.
  • Databricks serverless and standard access mode compute use Spark Connect-style architecture, which is why RDD APIs are unavailable there (see Serverless compute for jobs and notebooks, RDD vs DataFrame vs Dataset).
  • OSS: start with sbin/start-connect-server.sh and connect using SparkSession.builder.remote("sc://host:15002").

Common mistakes:

  • Expecting RDD code to work over Connect.
  • Assuming results stream unlimited to the client (still return small results).
  • Confusing it with the Thrift/JDBC server.

Intermediate PySpark and Spark interview questions

16. What happens during a shuffle, and why is it expensive?

Intermediate · Very Common · Shuffle

A shuffle moves data between executors so that rows with the same key end up together. On the map side, tasks split their output by key, often sort it, and write it to local shuffle files. On the reduce side, each task fetches its piece from every map task over the network. It is expensive because of disk writes, network transfer, serialisation, sorting and memory pressure. Skew makes it worse, because one task can get far more data than the rest.

Mechanics (sort-based shuffle, the default)

  1. Map side (ShuffleMapStage): each task computes the target partition for each record (hash of the key modulo the number of partitions, or a range partitioner for orderBy). It buffers and sorts records by partition ID, spilling to disk if memory fills, and writes one data file plus an index file per task.
  2. Shuffle service: an external shuffle service, or Kubernetes and Databricks equivalents, can serve shuffle files even if the executor that wrote them is gone. This matters for dynamic allocation (see Dynamic allocation).
  3. Reduce side: each task fetches its blocks from all map outputs (spark.reducer.maxSizeInFlight controls the fetch parallelism), then merges and aggregates or joins them.

Why it costs so much

  • Disk: writes on the map side and reads on the reduce side, plus spill during sorts.
  • Network: all-to-all data transfer.
  • CPU: serialisation and deserialisation, compression, sorting.
  • Memory: buffers on both sides. Insufficient memory causes spill (see Executor memory model: unified, execution, storage, overhead).
  • Skew: a hot key sends a huge share of the data to one reducer (see How do you detect data skew?).
  • Fan-out: many map tasks × many reduce partitions means many small blocks and lots of overhead.

Metrics to read (Spark UI, Stages tab)

  • Shuffle Write Size and Records (map side), Shuffle Read Size and Records (reduce side).
  • Spill (Memory and Disk), Fetch Wait Time.
  • Task time distribution: the max far above the median means skew.

Reducing shuffle cost

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

events = spark.read.table("silver.events")

# Two separate shuffles on the same key...
a = events.groupBy("user_id").agg(F.count("*").alias("events"))
b = events.groupBy("user_id").agg(F.max("event_ts").alias("last_seen"))
two_shuffles = a.join(b, "user_id")

# ...vs one shuffle with both aggregations
one_shuffle = events.groupBy("user_id").agg(F.count("*").alias("events"),
                                            F.max("event_ts").alias("last_seen"))
one_shuffle.explain()   # single Exchange hashpartitioning(user_id)

Learn more: Spark Partitions, Shuffles and Data Skew

In depth: What causes a shuffle in Spark?

17. spark.sql.shuffle.partitions: default and tuning

Intermediate · Very Common · Configuration

spark.sql.shuffle.partitions sets how many partitions a shuffle produces in joins and aggregations. The default is 200. Too few gives huge partitions that spill to disk or run out of memory. Too many gives tiny partitions, with scheduling overhead and small output files. A common target is about 100–200 MB per partition. With AQE (on by default since Spark 3.2), Spark merges small partitions at run time, so you can start high and let AQE shrink them.

Why 200 is often wrong

  • 10 GB of shuffle data at 200 partitions is about 50 MB each, which is fine.
  • 2 TB at 200 partitions is about 10 GB each: spill, OOM and long straggling tasks.
  • 50 MB at 200 partitions is 250 KB each: scheduling overhead and 200 tiny output files.

Sizing approach

  1. Estimate the shuffle size from the Spark UI (Shuffle Write in the previous stage).
  2. Divide by the target partition size (about 128–256 MB). For example, 1 TB / 200 MB ≈ 5,000 partitions.
  3. Make it a multiple of total executor cores for even waves (with 400 cores, use 4,000 or 4,800).

With AQE (Spark 3.x)

  • spark.sql.adaptive.enabled = true (default since 3.2).
  • spark.sql.adaptive.coalescePartitions.enabled = true: merges small post-shuffle partitions toward spark.sql.adaptive.advisoryPartitionSizeInBytes (default 64 MB).
  • spark.sql.adaptive.coalescePartitions.initialPartitionNum: start with a high count and let AQE coalesce.
  • Databricks also offers auto-optimised shuffle (spark.sql.shuffle.partitions = auto).

Streaming caveat: for stateful Structured Streaming queries, the number of shuffle partitions is fixed in the checkpoint at first start. Changing the config later has no effect without a new checkpoint (see Preventing small files from streaming and frequent writes).

Not to confuse with

Example

# Runtime: PySpark 3.5
spark.conf.set("spark.sql.adaptive.enabled", "true")
spark.conf.set("spark.sql.adaptive.coalescePartitions.enabled", "true")
spark.conf.set("spark.sql.adaptive.advisoryPartitionSizeInBytes", str(128 * 1024 * 1024))
spark.conf.set("spark.sql.shuffle.partitions", "2000")   # upper bound; AQE coalesces down

result = (spark.read.table("silver.orders")
          .groupBy("customer_id")
          .agg({"amount": "sum"}))
result.write.mode("overwrite").saveAsTable("gold.customer_spend")

Common mistakes:

  • Never changing the default for very large or very small jobs.
  • Setting a huge count without AQE, creating thousands of tiny output files.
  • Changing it for a running stateful stream and expecting an effect.

Learn more: Spark Partitions, Shuffles and Data Skew

In depth: How does partition count affect Spark performance?

18. How Spark decides the number of input partitions

Intermediate · Common · Partitioning

For files, Spark packs files and pieces of files into input partitions of up to spark.sql.files.maxPartitionBytes, which is 128 MB by default. It also adds a cost of about 4 MB per file (openCostInBytes). It only splits files that can be split: Parquet can be split by row groups, but gzipped CSV cannot. So the count is roughly the total size divided by 128 MB, adjusted for the number and size of files.

For DataFrame file reads (Parquet, ORC, JSON, CSV)

  1. Compute maxSplitBytes = min(maxPartitionBytes, max(openCostInBytes, bytesPerCore)), where bytesPerCore = totalBytes / defaultParallelism (including per-file open costs).
  2. Split large splittable files into chunks of maxSplitBytes.
  3. Pack small files together into partitions up to maxSplitBytes, treating each file as at least openCostInBytes.

Consequences

Other sources

  • JDBC: one partition unless you set partitionColumn, lowerBound, upperBound and numPartitions, or provide predicates.
  • Kafka: one Spark partition per Kafka topic partition by default (minPartitions can split further).
  • Delta and Iceberg: follow file-based logic over the table’s data files after pruning.
  • RDD textFile: a minPartitions argument, and Hadoop input splits.

Tuning knobs

Setting Default Effect
spark.sql.files.maxPartitionBytes 128 MB Larger → fewer, bigger input tasks
spark.sql.files.openCostInBytes 4 MB Higher → fewer small files packed per partition
spark.sql.files.minPartitionNum (defaults to parallelism) Minimum target partitions

Example

# Runtime: PySpark 3.5
spark.conf.set("spark.sql.files.maxPartitionBytes", str(256 * 1024 * 1024))

df = spark.read.parquet("s3://lake/silver/events/event_date=2026-03-01/")
print("input partitions:", df.rdd.getNumPartitions())

# Parallel JDBC read: 16 partitions over an integer key range
orders = (spark.read.format("jdbc")
          .option("url", "jdbc:postgresql://orders-db.internal:5432/orders")
          .option("dbtable", "public.orders")
          .option("partitionColumn", "order_id")
          .option("lowerBound", "1")
          .option("upperBound", "50000000")
          .option("numPartitions", "16")
          .option("user", db_user).option("password", db_password)
          .load())

db_user and db_password should come from a secrets manager (see How do you manage secrets in pipelines?).

Common mistakes:

  • JDBC reads without partition options.
  • Expecting parallelism from one huge gzip file.
  • Confusing input partitions with shuffle partitions.

19. Join strategies: broadcast hash, shuffle hash, sort-merge

Intermediate · Very Common · Joins

Spark chooses a join strategy from the join type, the condition and the table sizes. A broadcast hash join copies the small table to every executor, so the big table is never shuffled. A sort-merge join shuffles and sorts both tables by key, then merges them; it is the default for large joins on equal keys. A shuffle hash join shuffles both sides and builds a hash table per partition. Joins without an equality condition fall back to slow nested-loop or cartesian joins.

Strategy Requirements Shuffle Best when Risk
Broadcast hash join (BHJ) Equi-join; one side small enough (≤ autoBroadcastJoinThreshold or hinted) No (large side) Fact × small dimension Broadcasting too-large tables → driver/executor OOM
Shuffle hash join (SHJ) Equi-join; one side much smaller per partition Both sides Medium-sized build side fits in memory per partition OOM if build partitions too large
Sort-merge join (SMJ) Equi-join; sortable keys Both sides (+ sort) Large × large Shuffle/sort cost; skew
Broadcast nested loop join (BNLJ) Any condition; one side broadcastable No Small non-equi joins Very slow on big data
Cartesian product No join condition / cross join Yes Rarely intended Explodes rows

Selection logic (simplified)

  1. Hints first: BROADCAST, MERGE, SHUFFLE_HASH, SHUFFLE_REPLICATE_NL.
  2. For equi-joins, broadcast if one side’s estimated size is under the threshold (default 10 MB) and the join type allows it. For a left outer join, the right side can be broadcast.
  3. Otherwise sort-merge join (spark.sql.join.preferSortMergeJoin = true), unless conditions favour shuffle hash.
  4. AQE re-plans at runtime: it can convert a sort-merge join to a broadcast join once actual sizes are known, switch to shuffle hash join with local maps, and split skewed partitions (see Adaptive Query Execution: what does it do?).

Reading the plan: look for BroadcastHashJoin, SortMergeJoin, ShuffledHashJoin and BroadcastNestedLoopJoin nodes, and their Exchange children (see Using explain() to read physical plans).

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

orders = spark.read.table("silver.orders")        # large
stores = spark.read.table("silver.stores")        # small
returns = spark.read.table("silver.returns")      # large

bhj = orders.join(F.broadcast(stores), "store_id")             # BroadcastHashJoin
smj = orders.join(returns, "order_id", "left")                 # SortMergeJoin (large x large)
shj = orders.hint("shuffle_hash").join(returns, "order_id")    # ShuffledHashJoin if feasible

bhj.explain()
smj.explain()

Common mistakes:

  • Broadcasting a multi-GB table.
  • Non-equi joins on large tables without adding an equality key.
  • Not checking the physical plan.

Learn more: PySpark Joins and Join Strategy

20. Broadcast joins: threshold, hints and limits

Intermediate · Very Common · Joins

Spark broadcasts a table automatically when its estimated size is under spark.sql.autoBroadcastJoinThreshold, which is 10 MB by default (-1 turns it off). You can force it with F.broadcast(df) or the /*+ BROADCAST(t) */ hint. With AQE, Spark also checks the real size at run time. Broadcasting avoids shuffling the big table, but the small table must fit in driver and executor memory, and one broadcast can never exceed 8 GB.

How a broadcast works

  1. The driver collects the small table (or executors build it, depending on the implementation), and builds a hash relation.
  2. It is sent to every executor (BitTorrent-like distribution) and kept in memory.
  3. Each task of the big table probes the hash table locally, with no shuffle.

Thresholds and settings

Setting Default Notes
spark.sql.autoBroadcastJoinThreshold 10 MB Static planning based on estimated size; -1 disables
spark.sql.adaptive.autoBroadcastJoinThreshold (defaults to the above) Used by AQE with actual post-shuffle sizes
spark.sql.broadcastTimeout 300 s Time allowed to build/broadcast
Max broadcast table size 8 GB (hard limit) Practical limits much lower: memory per executor

Estimates matter. Size estimates come from file sizes or statistics. After filters, Spark may overestimate (no statistics) or underestimate (compressed Parquet expands several-fold in memory). Run ANALYZE TABLE ... COMPUTE STATISTICS, or rely on AQE runtime decisions.

When to force a broadcast

  • A small dimension (even 50–500 MB in memory, if executors have room) joined with a huge fact.
  • A filtered side known to be small that the optimizer can’t estimate.

When not to

  • The “small” side is large or grows over time. Hints are static and silently become dangerous.
  • Driver memory is small: building broadcasts may pass through the driver.
  • Full outer joins, where broadcast hash join doesn’t apply.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", str(50 * 1024 * 1024))   # 50 MB

clicks = spark.read.table("silver.clicks")                       # 3 TB
campaigns = (spark.read.table("silver.campaigns")
             .filter(F.col("is_active")))                         # small after filter

joined = clicks.join(F.broadcast(campaigns), "campaign_id", "inner")
joined.explain()   # expect BroadcastHashJoin and BroadcastExchange

Common mistakes:

  • Permanent broadcast hints on growing tables.
  • Setting the threshold to several GB globally.
  • Forgetting that compressed size isn’t in-memory size.

Learn more: PySpark Joins and Join Strategy

In depth: When would you use a broadcast join in Spark?

21. How do you detect data skew?

Intermediate · Very Common · Performance

Skew means a few keys or partitions hold far more data than the rest. In the Spark UI, look for a stage where most tasks finish quickly but a few run much longer, with a much bigger shuffle read or spill. That is the sign. Confirm it by counting rows per join key or group key, and include NULLs and placeholder values such as “unknown”, because they are common causes.

Symptoms

  • Task time distribution: the median is seconds, and the max is tens of minutes.
  • Shuffle Read Size per task: a few tasks read gigabytes while the rest read megabytes.
  • Spill and OOM concentrated in the same few tasks.
  • The stage “hangs at 199/200 tasks”.
  • Executors sit idle while one task runs.

Confirming

  1. Find the operation in that stage (join, groupBy, window) and its key.
  2. Count per key: df.groupBy(key).count().orderBy(desc("count")).
  3. Check for NULLs or sentinel values (“unknown”, “-1”, empty strings), which often cause skew in joins and aggregations.
  4. Check distribution after filters. Skew may appear only for a particular date or segment.

Common causes

Cause Example
Natural popularity One mega-merchant, celebrity user, default product
NULL/placeholder keys Guest orders with NULL customer_id
Data quality issues Bots generating millions of events for one ID
Partitioning on low-cardinality column repartition("country") with one dominant country
Explosion explode on arrays with a few huge arrays

What to do next (see Fix skew with salting (with code), Adaptive Query Execution: what does it do?)

  • Let AQE skew join handle it (Spark 3.x, for sort-merge and shuffle hash joins).
  • Broadcast the smaller side if feasible.
  • Filter or separately process NULLs and hot keys.
  • Salting for aggregations and joins AQE doesn’t fix.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

orders = spark.read.table("silver.orders")

key_stats = (orders.groupBy("merchant_id").count()
             .agg(F.max("count").alias("max_rows"),
                  F.expr("percentile_approx(count, 0.5)").alias("median_rows"),
                  F.sum(F.when(F.col("merchant_id").isNull(), F.col("count"))).alias("null_key_rows")))
key_stats.show()

orders.groupBy("merchant_id").count().orderBy(F.desc("count")).show(10)

If max_rows is orders of magnitude larger than median_rows, or null_key_rows is large, the key is skewed.

Common mistakes:

  • Adding executors to fix skew.
  • Ignoring NULL keys.
  • Diagnosing without looking at task-level metrics.

Learn more: Spark Partitions, Shuffles and Data Skew

In depth: What is data skew and how can you mitigate it?

22. Adaptive Query Execution: what does it do?

Intermediate · Very Common · Optimisation

Adaptive Query Execution (AQE) is on by default since Spark 3.2. It improves the plan while the query runs, using the real sizes from shuffle stages that have already finished. It does three main things. It merges small shuffle partitions. It switches join strategy, for example to a broadcast join once one side turns out to be small. And it splits skewed partitions in joins. This removes a lot of manual tuning.

How it works: Spark executes the query stage by stage. At each shuffle or broadcast boundary (a “query stage”), it collects real output sizes and re-plans the remaining stages.

Main features

Feature Config (default) What it does
Coalesce shuffle partitions spark.sql.adaptive.coalescePartitions.enabled (true) Merges small adjacent partitions toward advisoryPartitionSizeInBytes (64 MB)
Switch join strategy spark.sql.adaptive.autoBroadcastJoinThreshold Converts SMJ to BHJ when a side is small at runtime; can use local shuffle readers to avoid extra shuffle
Skew join optimisation spark.sql.adaptive.skewJoin.enabled (true) Splits partitions larger than skewedPartitionFactor (5) × median and above skewedPartitionThresholdInBytes (256 MB) into sub-partitions, replicating the other side’s matching partition
Shuffled hash join preference spark.sql.adaptive.maxShuffledHashJoinLocalMapThreshold Picks SHJ when partitions are small enough
Empty relation propagation (enabled) Short-circuits joins with empty sides
Rebalance partitions REBALANCE hint Evens out partitions (useful before writes)

Limits

  • It needs a shuffle or exchange to gather statistics, so it can’t fix the first scan stage.
  • Skew handling applies to joins (sort-merge and shuffle hash), not to aggregations or window functions in general.
  • It doesn’t apply to streaming queries’ stateful operators in the same way (micro-batch planning is limited).
  • In the Spark UI, the plan shows AdaptiveSparkPlan isFinalPlan=true once execution finishes. Look at the final plan, not just the initial one.

Practical defaults: keep AQE on, set spark.sql.shuffle.partitions reasonably high (or use initialPartitionNum), and let AQE coalesce. Tune the advisory size for your workloads, typically 64–256 MB.

Example

# Runtime: PySpark 3.5 (AQE settings shown explicitly; most are defaults)
spark.conf.set("spark.sql.adaptive.enabled", "true")
spark.conf.set("spark.sql.adaptive.coalescePartitions.enabled", "true")
spark.conf.set("spark.sql.adaptive.advisoryPartitionSizeInBytes", str(128 * 1024 * 1024))
spark.conf.set("spark.sql.adaptive.skewJoin.enabled", "true")
spark.conf.set("spark.sql.adaptive.skewJoin.skewedPartitionFactor", "5")
spark.conf.set("spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes", str(256 * 1024 * 1024))

result = spark.table("silver.orders").join(spark.table("silver.payments"), "order_id")
result.explain()            # initial plan: AdaptiveSparkPlan isFinalPlan=false
result.write.mode("overwrite").saveAsTable("gold.order_payments")
# After run, the SQL tab shows the final plan with coalesced partitions / skew splits

Common mistakes:

  • Disabling AQE “for predictability” without evidence.
  • Expecting AQE to fix aggregation skew.
  • Reading only the initial plan.

Learn more: Adaptive Query Execution and Dynamic Partition Pruning

23. When caching hurts, and how to unpersist

Intermediate · Common · Caching

Caching hurts in four cases. When the data is used only once, so storing it is pure overhead. When the cache takes memory that running tasks need, causing more spill and garbage collection. When it is bigger than memory and keeps being evicted and recomputed. And when it holds old results while the source keeps changing. Cache only expensive results you reuse, and free them with unpersist() or spark.catalog.clearCache().

Costs of caching

  • Materialisation work: computing and storing all partitions, sometimes forced with count().
  • Memory pressure: cached blocks share the unified memory pool with execution (see Executor memory model: unified, execution, storage, overhead). Heavy caching can squeeze joins and aggregations, causing spill and GC pauses.
  • Eviction churn: data that doesn’t fit gets evicted (LRU) and recomputed. With MEMORY_ONLY you can end up paying for both caching and recomputation.
  • Plan interference: a cached DataFrame becomes an InMemoryRelation in plans. Filters may no longer push down to the source files, so caching a whole table and then filtering it can be slower than reading the filtered data directly.
  • Staleness: a cache holds a snapshot. If the underlying Delta table changes, the cached data doesn’t (Spark invalidates some caches on writes through the same session, but not on external changes).

Anti-patterns

  • df.cache() on every DataFrame “for speed”.
  • Caching right after reading a file that is used once.
  • Caching huge raw tables, then filtering.
  • Calling count() after every cache just to materialise it.
  • Never unpersisting in notebooks or long sessions, so memory slowly fills.

Better alternatives

  • Write an intermediate Delta or Parquet table if it’s reused across jobs, or if lineage is very long (also see checkpointing in Q287).
  • Rely on platform disk caches (Databricks disk cache) for repeated reads of the same files.
  • Restructure code so one pipeline writes multiple outputs in one pass where possible.

Releasing memory

  • df.unpersist() (asynchronous by default, blocking=True to wait).
  • spark.catalog.uncacheTable("t") and spark.catalog.clearCache().
  • Check the Spark UI Storage tab to make sure nothing unexpected is cached.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

raw = spark.read.table("bronze.events")

# Anti-pattern: cache huge raw table, then filter (loses file-level pruning, eats memory)
# raw.cache(); raw.count(); march = raw.filter(F.col("event_date") >= "2026-03-01")

# Better: filter first so pruning applies; cache only the reused, smaller intermediate
march = raw.filter(F.col("event_date") >= "2026-03-01").select("user_id", "event_type", "event_ts")
march.cache()
march.groupBy("event_type").count().write.mode("overwrite").saveAsTable("gold.march_event_counts")
march.groupBy("user_id").agg(F.max("event_ts").alias("last_seen")) \
     .write.mode("overwrite").saveAsTable("gold.march_last_seen")
march.unpersist()

Common mistakes:

  • Treating caching as free.
  • Caching before filtering.
  • Ignoring the Storage tab.

Learn more: Spark Caching, Checkpointing, Broadcast Variables and Accumulators

24. Python UDF vs pandas UDF vs built-in functions

Intermediate · Very Common · PySpark Performance

Built-in functions in pyspark.sql.functions run inside Spark’s engine and are fully optimised, so they are the fastest. A plain Python UDF processes one row at a time in a separate Python process, and every row is copied between the JVM and Python, so it is the slowest. A pandas UDF uses Apache Arrow to send whole column batches to Python and runs vectorised pandas or NumPy code. It is often 10 to 100 times faster than a plain UDF.

Option Execution Optimizer visibility Speed Use for
Built-in functions / SQL expressions JVM (codegen) / Photon Full (pushdown, pruning) Fastest Almost everything: string, date, JSON, arrays, math, regex
Python UDF (@udf) Row-by-row in Python workers Black box Slowest Truly custom logic with no built-in alternative, small data
Arrow-optimised Python UDF (Spark 3.5+, useArrow=True) Batches via Arrow, row logic Black box Faster than classic UDF Existing row UDFs, quick improvement
pandas UDF (@pandas_udf) Batches as pandas Series/DataFrames via Arrow Black box Fast Vectorised numeric/ML logic, library calls (scikit-learn, scipy)
mapInPandas / applyInPandas / mapInArrow Batches per partition/group Black box Fast Per-group models, complex per-partition processing
Scala/Java UDF JVM Mostly black box Fast Performance-critical custom logic

Why Python UDFs are slow

  • Data is serialised from JVM rows to Python objects (pickle) and back.
  • Row-at-a-time function calls in Python.
  • Catalyst can’t see inside, so no predicate pushdown or constant folding through the UDF.
  • NULL handling and type conversion overhead.

Guidelines

  1. Search for a built-in first: regexp_extract, from_json, transform (higher-order array functions), date_format, when/otherwise, aggregate.
  2. Use pandas UDFs when custom Python is necessary.
  3. Declare return types precisely.
  4. Apply UDFs after filtering, so fewer rows go through Python.
  5. Check the plan for BatchEvalPython (row UDF) or ArrowEvalPython (pandas UDF) nodes.

Example

# Runtime: PySpark 3.5 (pandas + pyarrow installed)
import pandas as pd
from pyspark.sql import functions as F
from pyspark.sql.types import DoubleType

orders = spark.read.table("silver.orders")

# 1) Built-in (preferred): tiered discount
with_builtin = orders.withColumn(
    "discount",
    F.when(F.col("amount") >= 500, F.col("amount") * 0.10)
     .when(F.col("amount") >= 100, F.col("amount") * 0.05)
     .otherwise(F.lit(0.0)))

# 2) Row-at-a-time Python UDF (slow)
@F.udf(returnType=DoubleType())
def discount_udf(amount):
    if amount is None:
        return None
    return amount * 0.10 if amount >= 500 else amount * 0.05 if amount >= 100 else 0.0

# 3) Vectorised pandas UDF (fast when Python is required)
@F.pandas_udf(DoubleType())
def discount_pudf(amount: pd.Series) -> pd.Series:
    rate = pd.Series(0.0, index=amount.index)
    rate[amount >= 100] = 0.05
    rate[amount >= 500] = 0.10
    return amount * rate

with_pudf = orders.withColumn("discount", discount_pudf(F.col("amount").cast("double")))

All three give the same results for non-NULL amounts. The pandas UDF returns NaN/NULL for NULL inputs automatically.

Common mistakes:

  • Writing UDFs for things built-ins already do.
  • Applying UDFs before filters.
  • Wrong return types, producing NULLs silently.

Learn more: PySpark UDFs, pandas UDFs and the Pandas API on Spark

In depth: When should you avoid Python UDFs in PySpark?

25. Dynamic allocation

Intermediate · Common · Resource Tuning

Dynamic allocation lets Spark add executors when tasks are waiting and remove them when they sit idle, between a minimum and a maximum you set. Removing an executor is only safe if its shuffle files stay available. That comes from an external shuffle service, or from shuffle tracking (common on Kubernetes), which keeps an executor alive while other tasks still need its shuffle data.

Key settings

Setting Purpose
spark.dynamicAllocation.enabled Turn on
minExecutors / maxExecutors / initialExecutors Bounds and start size
schedulerBacklogTimeout (1 s) / sustainedSchedulerBacklogTimeout How quickly to request more executors when tasks queue (requests grow exponentially)
executorIdleTimeout (60 s) Remove executors idle this long
cachedExecutorIdleTimeout (infinite) Idle timeout for executors holding cached data
shuffleTracking.enabled Keep executors with active shuffle data (no external service needed)
spark.shuffle.service.enabled Use external shuffle service (YARN/standalone)

Benefits

  • Shared clusters use resources efficiently, because jobs grab capacity when needed and release it afterwards.
  • Long-running applications (notebooks, Thrift servers) don’t hold idle executors.

Caveats

  • Executor start-up latency: scaling up takes time (container or VM allocation). Short, bursty stages may finish before new executors arrive.
  • Cached data pins executors (with the infinite cached idle timeout), or is lost when they’re removed.
  • Streaming: classic dynamic allocation isn’t well suited to Structured Streaming. Use fixed sizing, or platform features (Databricks enhanced autoscaling for pipelines).
  • Max executors must be capped on shared clusters, otherwise one job can starve others.

Relationship to cluster autoscaling: dynamic allocation scales executors within available resources. Cluster autoscaling (Databricks, EMR managed scaling, Kubernetes cluster autoscaler) scales nodes. They work together: pending executors trigger node scale-up. On Databricks, cluster autoscaling is the main mechanism (see Cluster sizing, autoscaling and spot instances).

Example

# Runtime: PySpark 3.5 on Kubernetes (settings applied at session creation)
from pyspark.sql import SparkSession

spark = (SparkSession.builder
         .appName("adhoc-analytics")
         .config("spark.dynamicAllocation.enabled", "true")
         .config("spark.dynamicAllocation.shuffleTracking.enabled", "true")
         .config("spark.dynamicAllocation.minExecutors", "2")
         .config("spark.dynamicAllocation.maxExecutors", "40")
         .config("spark.dynamicAllocation.executorIdleTimeout", "120s")
         .getOrCreate())

Common mistakes:

  • Enabling it without a shuffle service or shuffle tracking, which loses shuffle data.
  • No maxExecutors on shared clusters.
  • Expecting instant scale-up for short stages.

26. Reading the Spark UI

Intermediate · Common · Monitoring

The Spark UI has several tabs. Jobs shows one row per action. Stages shows each stage’s time, input, shuffle read and write, spill and how long the tasks took. Storage shows cached data. Environment shows the settings. Executors shows memory, garbage collection and failed tasks. The SQL tab shows the physical plan with numbers for each step. Use it to find the slowest stage and the reason: skew, spill, too much shuffle, too many tasks or garbage collection.

Tabs and what to look for

Tab Look for
Jobs Which action is slow; failed jobs; event timeline (executor add/remove)
Stages Duration; Input/Output; Shuffle Read/Write; Spill (Memory/Disk); Summary Metrics for tasks (min/25th/median/75th/max)
Stage detail → Tasks Individual slow tasks, their host, shuffle read size, GC time, errors
SQL / DataFrame Query DAG with operator metrics: rows output, time, data size, number of files read/pruned, broadcast sizes; AQE final plan
Storage Cached RDDs/DataFrames, fraction cached, memory vs disk
Executors Active/dead executors, memory used, GC time, task failures, shuffle totals, thread dumps
Environment Effective Spark configs (verify AQE, shuffle partitions, etc.)
Structured Streaming Input rate vs processing rate, batch duration, state size

Diagnostic patterns

  1. Skew: the max task time and shuffle read are far above the median (see How do you detect data skew?).
  2. Spill: non-zero spill disk or memory, so tasks are too large for memory (see Executor memory model: unified, execution, storage, overhead).
  3. Too many tiny tasks: thousands of tasks with median duration under 100 ms (see How Spark decides the number of input partitions).
  4. Too few tasks: fewer tasks than cores, so the cluster is underutilised.
  5. Scan heavy: large input, with “files pruned” small. Check partitioning, clustering and filters (see Predicate pushdown and partition pruning).
  6. GC: GC time above about 10% of task time (see How do you size executors (cores and memory)?).
  7. Failed or retried tasks: fetch failures (lost shuffle files, executor loss), OOM.
  8. Streaming lag: processing rate below input rate, with growing batch duration (see What is backpressure in streaming systems?).

History server: the Spark History Server, or the platform’s equivalent, keeps UIs for finished applications. Enable event logging (spark.eventLog.enabled) for post-mortems.

Example

# Runtime: PySpark 3.5 — label jobs so they're easy to find in the UI
sc = spark.sparkContext
sc.setJobGroup("daily_orders", "Build gold.daily_orders")
sc.setJobDescription("Aggregate paid orders by day and region")

(spark.read.table("silver.orders")
     .filter("status = 'paid'")
     .groupBy("order_date", "region").sum("amount")
     .write.mode("overwrite").saveAsTable("gold.daily_orders"))

print(sc.uiWebUrl)   # link to the live Spark UI (where accessible)

Common mistakes:

  • Only looking at total job duration.
  • Ignoring the SQL tab’s operator metrics.
  • Not checking effective configs in the Environment tab.

27. Using explain() to read physical plans

Intermediate · Common · Optimisation

df.explain(mode) prints the query plan without running the query. simple shows only the physical plan. extended also shows the logical plans. formatted gives a tidy outline with details for each step. Read the plan from the bottom up, starting at the scans. Check which filters were pushed down and which columns are read. Look for Exchange nodes (shuffles), the join types, broadcasts and Python UDF steps.

What to check, in order

  1. Scan nodes (FileScan parquet / Scan delta):
    • PushedFilters: [IsNotNull(order_date), GreaterThanOrEqual(order_date, 2026-03-01)] shows filter pushdown worked.
    • PartitionFilters: [...] shows partition pruning (see Predicate pushdown and partition pruning).
    • ReadSchema shows only the needed columns (column pruning).
  2. Exchange nodes:
    • Exchange hashpartitioning(customer_id, 200) is a shuffle by key.
    • BroadcastExchange builds a broadcast for a join.
    • Exchange rangepartitioning is a shuffle for a global sort.
    • Count them, because each is a shuffle (see What happens during a shuffle, and why is it expensive?).
  3. Joins: BroadcastHashJoin, SortMergeJoin, ShuffledHashJoin, BroadcastNestedLoopJoin (slow), CartesianProduct (danger) (see Join strategies: broadcast hash, shuffle hash, sort-merge).
  4. Aggregations: HashAggregate (partial) → Exchange → HashAggregate (final) is the normal two-phase aggregation. SortAggregate can appear for some types.
  5. Python: BatchEvalPython (row UDF), ArrowEvalPython (pandas UDF), FlatMapGroupsInPandas (see Python UDF vs pandas UDF vs built-in functions).
  6. Codegen: *(n) prefixes show whole-stage codegen groups (see Project Tungsten and whole-stage code generation).
  7. AQE: AdaptiveSparkPlan isFinalPlan=false before execution. After execution, the SQL tab shows the final plan (see Adaptive Query Execution: what does it do?).

SQL equivalent: EXPLAIN [EXTENDED | FORMATTED | COST | CODEGEN] <query>.

Habit worth building: run explain() before shipping any non-trivial transformation, and again after tuning changes. Comparing the two plans (number of Exchanges, join types, pushed filters) confirms the change did what you intended, before you spend cluster time measuring it.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

q = (spark.read.table("silver.orders")
     .filter(F.col("order_date") >= "2026-03-01")
     .join(F.broadcast(spark.read.table("silver.stores")), "store_id")
     .groupBy("region").agg(F.sum("amount").alias("revenue")))

q.explain(mode="formatted")

Abbreviated expected shape:

Common mistakes:

  • Not knowing what Exchange means.
  • Reading only the top of the plan.
  • Ignoring PushedFilters and ReadSchema.

28. Predicate pushdown and partition pruning

Intermediate · Very Common · Optimisation

Partition pruning skips whole folders, or table partitions, when your filter is on a partition column. Predicate pushdown sends filters down to the data source. Parquet, ORC, Delta and Iceberg readers then skip row groups or files using their min and max statistics, and JDBC sources filter inside the database. Both only work when the filter is on the plain column, not wrapped in a function. Together they cut the data read dramatically.

Partition pruning

  • With event_date=2026-03-01/ directories, WHERE event_date = '2026-03-01' reads one folder.
  • Delta and Iceberg prune using partition values in metadata. Iceberg’s hidden partitioning prunes on event_ts filters through transforms (see Delta Lake vs Apache Iceberg vs Apache Hudi).
  • Dynamic partition pruning prunes at runtime based on a join’s filtered dimension (see Dynamic partition pruning).

Predicate pushdown

  • File formats: filters become source filters (PushedFilters in the plan). Readers skip row groups (Parquet) or stripes (ORC) whose statistics don’t match (see Parquet internals: row groups, column chunks, pages and statistics). Delta uses per-file stats in the transaction log for data skipping before opening files.
  • JDBC: WHERE clauses are sent to the database. Aggregate and limit pushdown are available in newer versions for some sources.
  • Kafka: limited (offset ranges).

What breaks it

Pattern Problem Fix
F.to_date("event_ts") == "2026-03-01" when partitioned by event_date Filter not on partition column Filter event_date directly or use generated columns
F.year("order_date") == 2026 Function on column, not pushable as range order_date >= '2026-01-01' AND < '2027-01-01'
Filter after a Python UDF UDF blocks pushdown Filter before UDF
Casting the column (col.cast("string") == "42") Type mismatch Compare with correct type
Unsorted data Stats ranges overlap → little skipping Cluster/Z-order/sort (see Z-ORDER, Liquid Clustering vs partitioning vs Z-ORDER)
Stats not collected on that column (Delta first 32 columns) No skipping Reorder columns or configure stats columns

Verifying: look in explain() for PartitionFilters and PushedFilters (see Using explain() to read physical plans). In the SQL tab or query profile, compare files and bytes read with and without the filter.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

events = spark.read.table("silver.events")      # partitioned by event_date (DATE)

bad = events.filter(F.to_date("event_ts") == F.lit("2026-03-01"))       # scans all partitions
good = events.filter(F.col("event_date") == F.lit("2026-03-01").cast("date"))

bad.explain()    # PartitionFilters: []
good.explain()   # PartitionFilters: [isnotnull(event_date), (event_date = 2026-03-01)]

Common mistakes:

  • Assuming any filter prunes partitions.
  • Wrapping columns in functions.
  • Not knowing about Delta’s data skipping stats limit.

29. partitionBy vs bucketBy when writing

Intermediate · Common · Data Layout

write.partitionBy("col") creates one folder per value (col=value/), so queries that filter on that column can skip folders. Use it for columns with few values, such as dates. bucketBy(n, "col") spreads rows by hash into a fixed number of files, and only works with saveAsTable. Two tables bucketed the same way on the join key can then be joined or grouped without a shuffle. Bucketing is rarely used with Delta, where liquid clustering is preferred.

Aspect partitionBy bucketBy
Physical layout Directories per distinct value Fixed N files per (partition) by hash of key
Cardinality fit Low (dates, regions) High (customer_id, user_id)
Main benefit Partition pruning on filters Shuffle-free joins/aggregations on bucket key (both sides bucketed compatibly)
Requirements Any file-based write saveAsTable (Hive-compatible metastore); not supported for Delta tables
Risk Over-partitioning → small files (see How do you choose a partition key, and what is over-partitioning?) Wrong bucket count, mismatched bucketing across tables, files per bucket per task
Sorting n/a Optional sortBy within buckets

partitionBy tips

  • Partition by the column used in most filters, with a moderate number of values (hundreds to low thousands).
  • Repartition by the same columns before writing, to avoid many files per partition: df.repartition("event_date").write.partitionBy("event_date").
  • For Delta, use dynamic partition overwrite or replaceWhere to overwrite specific partitions safely (see Save modes and dynamic partition overwrite).

bucketBy tips

  • Both join sides must be bucketed on the join key with the same (or compatible) bucket counts to avoid shuffles. Spark 3 can join tables with bucket counts that are multiples of each other with configuration (spark.sql.bucketing.coalesceBucketsInJoin.enabled).
  • Writing with many tasks creates up to tasks × buckets files. Repartition to the bucket count first.
  • Bucketing is mostly used with Hive/Parquet tables in metastore-managed environments. For Delta and Iceberg, prefer liquid clustering (Delta; see Liquid Clustering vs partitioning vs Z-ORDER) or Iceberg bucket(N, col) partition transforms (see Delta Lake vs Apache Iceberg vs Apache Hudi).

Example

# Runtime: PySpark 3.5 (Hive-compatible metastore tables for bucketing)
from pyspark.sql import functions as F

events = spark.read.table("bronze.events")

# Directory partitioning by date, ~8 files per day
(events.repartition(8, "event_date")
       .write.mode("overwrite")
       .partitionBy("event_date")
       .format("parquet")
       .saveAsTable("silver.events_by_date"))

# Bucketing two large tables on customer_id for shuffle-free joins later
(spark.read.table("bronze.orders").repartition(64, "customer_id")
      .write.mode("overwrite").bucketBy(64, "customer_id").sortBy("customer_id")
      .format("parquet").saveAsTable("silver.orders_bucketed"))
(spark.read.table("bronze.customers").repartition(64, "customer_id")
      .write.mode("overwrite").bucketBy(64, "customer_id").sortBy("customer_id")
      .format("parquet").saveAsTable("silver.customers_bucketed"))

joined = spark.table("silver.orders_bucketed").join(spark.table("silver.customers_bucketed"), "customer_id")
joined.explain()   # expect SortMergeJoin without Exchange on either side

Common mistakes:

  • partitionBy on high-cardinality IDs.
  • Bucketing only one side.
  • Trying bucketBy with Delta tables.

30. Save modes and dynamic partition overwrite

Intermediate · Common · Writes

Save modes decide what happens when data already exists. append adds rows. overwrite replaces the data. errorifexists, the default, fails if the target exists. ignore quietly skips the write. With partitionOverwriteMode = dynamic, an overwrite replaces only the partitions that appear in your DataFrame, not the whole table. On Delta, replaceWhere overwrites exactly the rows that match a condition. These are the tools for reloading partitions safely.

Mode Behaviour Risk
append Add rows Duplicates on rerun (not idempotent)
overwrite (static, default) Replace the entire table/path Wipes other partitions if you meant to replace one
overwrite + dynamic partition overwrite Replace only partitions that appear in the incoming data Missing partitions in input are left untouched (intended)
errorifexists / error Fail if exists n/a
ignore Do nothing if exists Silent skips

Dynamic partition overwrite

  • spark.conf.set("spark.sql.sources.partitionOverwriteMode", "dynamic"), or the per-write option .option("partitionOverwriteMode", "dynamic").
  • Works for partitioned file tables. Delta also supports it (recent Delta and DBR versions).
  • This is idempotent per partition: rerunning a day replaces that day only (see What is idempotency, and why is it critical in pipelines?).

Delta replaceWhere

  • .option("replaceWhere", "event_date >= '2026-03-01' AND event_date < '2026-03-08'") overwrites exactly the rows matching the predicate, atomically. Delta validates that the written data satisfies the predicate (spark.databricks.delta.replaceWhere.constraintCheck.enabled).
  • It works on any columns (not just partitions) in newer Delta versions.
  • It is more explicit and safer than dynamic mode, because the replaced range is declared even if the input happens to be missing a day.

SQL equivalents

  • INSERT OVERWRITE TABLE t PARTITION (event_date = '2026-03-01') SELECT ... (static partition).
  • INSERT OVERWRITE t SELECT ... with dynamic mode enabled.
  • Delta: INSERT INTO t REPLACE WHERE event_date = '2026-03-01' SELECT ....

Example

# Runtime: PySpark 3.5 with Delta Lake
from pyspark.sql import functions as F

day = "2026-03-01"
daily = (spark.read.table("silver.orders")
         .filter(F.col("order_date") == day)
         .groupBy("order_date", "store_id").agg(F.sum("amount").alias("revenue")))

# Delta: atomically replace exactly that day's rows (safe to rerun)
(daily.write.format("delta")
      .mode("overwrite")
      .option("replaceWhere", f"order_date = '{day}'")
      .saveAsTable("gold.daily_store_revenue"))

# Parquet/partitioned tables: dynamic partition overwrite
(daily.write.mode("overwrite")
      .option("partitionOverwriteMode", "dynamic")
      .partitionBy("order_date")
      .parquet("s3://lake/gold/daily_store_revenue_parquet/"))

day is set by the orchestrator (for example from a run parameter) and validated as a date before use, not taken from user input.

Common mistakes:

  • Static overwrite on partitioned tables for single-partition reloads.
  • append in rerunnable jobs.
  • replaceWhere predicates that don’t match the written data.

31. Window functions in PySpark

Intermediate · Very Common · Window Functions

Define a window with Window.partitionBy(...).orderBy(...), and add a frame with rowsBetween or rangeBetween if you need one. Then use functions with .over(w): ranking (row_number, rank, dense_rank, ntile), looking back or ahead (lag, lead) and aggregates (sum, avg, count, max, first, last). All rows of one window partition go to one task, so a key with a huge number of rows causes skew and memory problems.

Building blocks

  • from pyspark.sql.window import Window
  • w = Window.partitionBy("customer_id").orderBy("order_ts")
  • Frames:
    • rowsBetween(Window.unboundedPreceding, Window.currentRow) for running totals (row-based).
    • rangeBetween(-6, 0) over a numeric order column for value-based ranges. Order by days-since-epoch for date ranges.
  • Default frame when orderBy is present: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW (peers with equal order values are included). Specify rowsBetween for exact row semantics (see Running totals and moving averages).

Common patterns

Task Code
Latest record per key row_number().over(w_desc) == 1 (see Coding: deduplicate to keep the latest record per key)
Previous value/delta F.lag("amount").over(w)
Running total F.sum("amount").over(w.rowsBetween(Window.unboundedPreceding, 0))
7-row moving average F.avg("x").over(w.rowsBetween(-6, 0))
Rank within group F.dense_rank().over(Window.partitionBy("cat").orderBy(F.desc("sales")))
Share of group total F.col("x") / F.sum("x").over(Window.partitionBy("grp"))
Sessionization lag + flag + cumulative sum (see LAG/LEAD: day-over-day change)

Performance

  • A window with partitionBy triggers a shuffle by the partition keys, plus a sort within partitions.
  • No partitionBy moves all data to one partition, a single task. Spark warns about it. Avoid it for big data.
  • Huge partitions (hot keys) can OOM or spill. Pre-aggregate, or narrow the partition key.
  • Several windows with the same partitionBy/orderBy share one shuffle and sort. Different specs add more.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F
from pyspark.sql.window import Window

orders = spark.read.table("silver.orders")

w = Window.partitionBy("customer_id").orderBy("order_ts")
w_run = w.rowsBetween(Window.unboundedPreceding, Window.currentRow)

enriched = (orders
    .withColumn("order_seq", F.row_number().over(w))
    .withColumn("prev_amount", F.lag("amount").over(w))
    .withColumn("days_since_prev",
                F.datediff("order_ts", F.lag("order_ts").over(w)))
    .withColumn("running_spend", F.sum("amount").over(w_run)))

Sample for one customer: orders of 100, 50 and 30 give order_seq 1, 2, 3; prev_amount NULL, 100, 50; and running_spend 100, 150, 180.

Common mistakes:

  • Windows without partitionBy on large data.
  • Relying on the default RANGE frame when there are ties.
  • Using rank when exactly one row per group is needed (use row_number).

32. explode, flattening nested JSON and arrays

Intermediate · Very Common · Semi-Structured Data

Turn JSON strings into columns with from_json(col, schema), or read JSON files with an explicit schema. Reach nested fields with dots, such as col("payload.customer.id"), or expand a struct with select("struct.*"). Turn arrays into rows with explode, which drops empty and NULL arrays. Use explode_outer to keep them, or posexplode to also get each item’s position. For an array of structs, explode first, then select the fields.

Toolkit

Function Purpose
from_json(col, schema) String → struct/array/map
to_json(col) Struct → JSON string
get_json_object(col, '$.a.b') / json_tuple Extract fields from JSON strings without full schema (slower, string results)
schema_of_json(sample) Infer a schema from a sample string
col("a.b"), select("a.*") Struct field access / flatten one level
explode / explode_outer / posexplode(_outer) Arrays/maps → rows
inline / inline_outer Array of structs → rows with struct columns flattened
transform, filter, aggregate, exists Higher-order functions on arrays without exploding
VARIANT type + parse_json (Spark 4.0+) Semi-structured column with efficient storage/access (see What’s new in Spark 4.0 for data engineers?)

Design considerations

  • Explode multiplies rows. Exploding two independent arrays in sequence creates a Cartesian product per row. Explode one, or use arrays_zip and then explode.
  • Empty arrays disappear with explode. Use explode_outer if the parent row must survive (for example orders with no items).
  • Schema drift: an explicit schema plus _rescued_data or a corrupt-record column (columnNameOfCorruptRecord) handles unexpected shapes (see Auto Loader schema inference, evolution and rescued data).
  • Prefer higher-order functions for per-element transforms when you don’t need rows, which avoids the explosion cost.
  • Model output: usually a parent table (one row per order) and a child table (one row per item), keyed by the parent ID (see Merge overlapping date ranges for the SQL view).

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F
from pyspark.sql.types import StructType, StructField, StringType, ArrayType, IntegerType, DoubleType

raw = spark.createDataFrame([
    ('{"order_id":"o1","customer":{"id":"c9","tier":"gold"},'
     '"items":[{"sku":"A","qty":2,"price":5.0},{"sku":"B","qty":1,"price":12.5}]}',),
    ('{"order_id":"o2","customer":{"id":"c3","tier":"basic"},"items":[]}',)
], "payload STRING")

schema = StructType([
    StructField("order_id", StringType()),
    StructField("customer", StructType([StructField("id", StringType()),
                                        StructField("tier", StringType())])),
    StructField("items", ArrayType(StructType([StructField("sku", StringType()),
                                               StructField("qty", IntegerType()),
                                               StructField("price", DoubleType())]))),
])

orders = raw.select(F.from_json("payload", schema).alias("o")).select("o.*")

order_items = (orders
    .select("order_id", F.col("customer.id").alias("customer_id"),
            F.explode_outer("items").alias("item"))
    .select("order_id", "customer_id", "item.sku", "item.qty", "item.price",
            (F.col("item.qty") * F.col("item.price")).alias("line_total")))
order_items.show()

Result: o1 produces 2 rows (A, line total 10.0; B, line total 12.5). o2 produces 1 row with NULL item fields, because of explode_outer.

Common mistakes:

  • Using explode and losing parents with empty arrays.
  • Exploding multiple arrays sequentially, creating an accidental Cartesian product.
  • get_json_object everywhere instead of parsing once with a schema.

33. groupByKey vs reduceByKey (RDD API)

Intermediate · Common · RDD API

reduceByKey(func) combines values for each key before the shuffle, and again after it. So only partial results cross the network. groupByKey() sends every single value for each key across the network and then groups them. That uses much more network and memory, and can crash on very popular keys. Prefer reduceByKey or aggregateByKey. In modern code, use DataFrame groupBy().agg(), which combines values early automatically.

Aspect groupByKey reduceByKey aggregateByKey / combineByKey
Map-side combine No Yes Yes
Shuffle volume All values One partial value per key per partition Partial accumulators
Output (key, Iterable[values]) (key, reduced value) (key, accumulator) of different type
Memory risk High (all values per key in memory on reducer) Low Low–moderate
Function requirement n/a Associative & commutative Seq + combine functions

Illustration

  • Word count with reduceByKey(lambda a, b: a + b): each partition sends (word, partial_count) once per distinct word.
  • With groupByKey().mapValues(sum): each partition sends every (word, 1) pair, which can be millions of tiny records.

When groupByKey is acceptable: when you genuinely need all values per key (for example ordering events per session in custom logic) and groups are bounded in size. Even then, DataFrame windows or applyInPandas are usually better.

DataFrame equivalent

  • df.groupBy("key").agg(F.sum("v")) already does partial_sum → shuffle → final sum (see the plan in Q276).
  • collect_list in a DataFrame is the “groupByKey” equivalent, with the same memory risks on hot keys (see Driver OOM vs executor OOM: causes and fixes).

Interview angle: this classic question tests whether you understand map-side combining and shuffle volume, which still applies to DataFrame aggregations and to distributed systems generally (MapReduce combiners).

Example

# Runtime: PySpark 3.5 (RDD API)
pairs = spark.sparkContext.parallelize(
    [("IN", 120.0), ("US", 80.0), ("IN", 30.0), ("UK", 15.0), ("US", 20.0)], numSlices=2)

revenue_reduce = pairs.reduceByKey(lambda a, b: a + b)              # map-side combine
revenue_group = pairs.groupByKey().mapValues(lambda vals: sum(vals))  # ships all values

# aggregateByKey: (sum, count) to compute average per key
zero = (0.0, 0)
avg_by_country = (pairs.aggregateByKey(zero,
                                       lambda acc, v: (acc[0] + v, acc[1] + 1),
                                       lambda a, b: (a[0] + b[0], a[1] + b[1]))
                       .mapValues(lambda s: s[0] / s[1]))
print(sorted(revenue_reduce.collect()), sorted(avg_by_country.collect()))

Result: IN 150.0, UK 15.0, US 100.0. Averages: IN 75.0, UK 15.0, US 50.0.

Common mistakes:

  • Using groupByKey for sums or counts.
  • Non-associative functions in reduceByKey.
  • Thinking DataFrame groupBy behaves like groupByKey (it doesn’t).

34. Structured Streaming programming model

Intermediate · Very Common · Structured Streaming

Structured Streaming treats a stream as a table that keeps growing. You write the same DataFrame or SQL query you would write for batch, and Spark runs it incrementally, by default in small micro-batches. Each trigger processes the new data, updates any state (aggregations, joins, deduplication), and writes the results to a sink. Checkpoints make it recover after failures, with exactly-once results for sinks that support it.

Core concepts

Execution modes

Mode Latency Guarantees Notes
Micro-batch (default) ~100 ms to minutes Exactly-once with replayable sources + idempotent sinks Most common
Continuous processing (experimental) ~1 ms At-least-once Limited operators
Real-time mode (newer, platform-specific; e.g. Databricks) Sub-second Varies Check availability

Event time and state

  • Group by event-time windows (window("event_ts", "10 minutes")).
  • Watermarks bound how late data can be and when state can be dropped (see Watermarks and late data in Structured Streaming). Without them, state grows forever for aggregations or deduplication on unbounded keys.

Operational aspects

  • Monitor with query.lastProgress / recentProgress (input rows per second, processing rate, batch duration, state rows), or a StreamingQueryListener.
  • Back-pressure and rate limits come through source options (maxOffsetsPerTrigger for Kafka, maxFilesPerTrigger for files) (see What is backpressure in streaming systems?).
  • Changing a query may break checkpoint compatibility (state schema, operators). Plan upgrades.

Example

# Runtime: PySpark 3.5 — Kafka -> parsed events -> 5-minute counts per page into Delta
from pyspark.sql import functions as F

events = (spark.readStream.format("kafka")
          .option("kafka.bootstrap.servers", "broker1:9092,broker2:9092")
          .option("subscribe", "page_views")
          .option("startingOffsets", "latest")
          .option("maxOffsetsPerTrigger", 500000)
          .load()
          .select(F.from_json(F.col("value").cast("string"),
                              "page_id STRING, user_id STRING, event_ts TIMESTAMP").alias("e"))
          .select("e.*"))

counts = (events.withWatermark("event_ts", "15 minutes")
          .groupBy(F.window("event_ts", "5 minutes"), "page_id")
          .count())

query = (counts.writeStream
         .outputMode("append")
         .format("delta")
         .option("checkpointLocation", "s3://lake/_checkpoints/page_view_counts/")
         .trigger(processingTime="1 minute")
         .toTable("gold.page_view_counts_5m"))

Common mistakes:

  • Stateful aggregations without watermarks.
  • No checkpoint location.
  • Expecting complete-mode output for huge results.

Learn more: Spark Structured Streaming: Triggers, Watermarks and Exactly-Once

35. Triggers: processingTime, availableNow, once, continuous

Intermediate · Common · Structured Streaming

Triggers decide when micro-batches run. By default, the next batch starts as soon as the last one finishes. processingTime="1 minute" runs at fixed intervals. availableNow=True processes all the data that is there when it starts, in several batches, then stops. once=True did the same in one batch and is now deprecated. continuous="1 second" is an experimental low-latency mode. Use availableNow for incremental jobs you run on a schedule.

Trigger Behaviour Typical use Notes
Default (unspecified) Next micro-batch starts immediately after previous completes Low-latency always-on pipelines Can burn compute when data is sparse
processingTime="N unit" Batch every N; if a batch takes longer, next starts immediately after Steady near-real-time with controlled cost Bigger intervals → fewer, larger commits (fewer small files)
availableNow=True (Spark 3.3+) Process all available data (respecting maxFilesPerTrigger/maxOffsetsPerTrigger) in multiple batches, then stop Scheduled incremental jobs (hourly/daily) using streaming semantics Replaces once
once=True Single batch of all available data, then stop Legacy Deprecated; ignores rate limits → risk of huge batch
continuous="1 second" Continuous processing with checkpoint interval Millisecond latency experiments Experimental, at-least-once, limited operators

Why availableNow is popular

  • You get streaming’s incremental bookkeeping (checkpointed offsets, exactly-once, state) with batch-like cost: the cluster starts, catches up, and shuts down.
  • Rate limits are respected, so a big backlog is processed in manageable batches.
  • It fits orchestration. Schedule it every 15 minutes in Airflow or Lakeflow Jobs.

Choosing intervals

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

source = (spark.readStream.format("delta")
          .option("maxFilesPerTrigger", 200)
          .table("bronze.orders"))

cleaned = source.filter(F.col("order_id").isNotNull())

# Scheduled incremental run: catch up, then stop
q = (cleaned.writeStream
     .option("checkpointLocation", "s3://lake/_checkpoints/silver_orders/")
     .trigger(availableNow=True)
     .toTable("silver.orders_clean"))
q.awaitTermination()

# Always-on alternative: every 30 seconds
# cleaned.writeStream.option("checkpointLocation", "...").trigger(processingTime="30 seconds").toTable("...")

Common mistakes:

  • Using once=True for large backlogs.
  • Default trigger on sparse data, which wastes compute.
  • Processing time intervals shorter than batch duration.

36. foreachBatch for upserts and multiple sinks

Intermediate · Common · Structured Streaming

foreachBatch(func) hands you each micro-batch as a normal DataFrame plus a batch ID, so you can use any batch code on it. Common uses are a MERGE into Delta, writing to several sinks, calling a database over JDBC, or custom deduplication. By default it is at-least-once: if a batch fails, it runs again with the same data and the same batch ID. So make the function safe to repeat, for example with MERGE on keys.

Why use it

  • Sinks without native streaming support (JDBC databases, some APIs).
  • Upserts and CDC application with MERGE (see MERGE INTO for upserts and its performance).
  • Multiple outputs from one stream (write to Delta and publish a summary). Cache the batch DataFrame if you write it more than once.
  • Batch-only operations unavailable on streaming DataFrames.

Idempotency patterns

Pattern How
MERGE on business key with dedupe Re-applying the same batch yields same final state
Delta idempotent writes .option("txnAppId", app_id).option("txnVersion", batch_id): Delta skips if already committed
Track batch_id in a control table Skip if batch already processed (for non-Delta sinks)
Overwrite partitions derived from batch replaceWhere for deterministic partitions

Dedupe within the batch: a micro-batch may contain several changes for the same key. Keep the latest per key (by sequence or event time) before MERGE. Otherwise MERGE fails with “multiple source rows matched”.

Performance

  • The function runs on the driver, but the DataFrame operations inside it run distributed. Don’t collect() inside.
  • If you write the batch to two sinks, batch_df.persist() first and unpersist() at the end.
  • Long MERGEs into big tables slow every batch. Cluster the target by the merge key and use deletion vectors (see Deletion vectors, MERGE INTO for upserts and its performance).

Example

# Runtime: PySpark 3.5 with Delta Lake — CDC upsert, idempotent and deduped
from pyspark.sql import functions as F
from pyspark.sql.window import Window
from delta.tables import DeltaTable

cdc = spark.readStream.table("bronze.customer_cdc")    # customer_id, op, change_seq, name, segment

def apply_cdc(batch_df, batch_id):
    latest = (batch_df
              .withColumn("rn", F.row_number().over(
                  Window.partitionBy("customer_id").orderBy(F.desc("change_seq"))))
              .filter("rn = 1").drop("rn"))
    (DeltaTable.forName(spark, "silver.customers").alias("t")
        .merge(latest.alias("s"), "t.customer_id = s.customer_id")
        .whenMatchedDelete(condition="s.op = 'D'")
        .whenMatchedUpdate(condition="s.change_seq > t.change_seq",
                           set={"name": "s.name", "segment": "s.segment", "change_seq": "s.change_seq"})
        .whenNotMatchedInsert(condition="s.op <> 'D'",
                              values={"customer_id": "s.customer_id", "name": "s.name",
                                      "segment": "s.segment", "change_seq": "s.change_seq"})
        .execute())

(cdc.writeStream
    .foreachBatch(apply_cdc)
    .option("checkpointLocation", "s3://lake/_checkpoints/silver_customers_cdc/")
    .trigger(availableNow=True)
    .start())

The change_seq > t.change_seq guard makes a replayed batch a no-op for rows already applied, which keeps the function idempotent.

Common mistakes:

  • Assuming foreachBatch is exactly-once.
  • No dedupe within the batch.
  • collect() inside the function.

Learn more: Delta Lake MERGE, Change Data Feed and Streaming

37. Coding: top-N per group and running totals

Intermediate · Very Common · Coding Patterns

For top-N per group, rank the rows with row_number (exactly N per group) or dense_rank (keeps ties) over partitionBy(group).orderBy(desc(metric)), then keep rank N or less. For a running total, use F.sum(metric).over(...) with orderBy(time) and rowsBetween(Window.unboundedPreceding, Window.currentRow). First total the data to the level you need, so the window runs over fewer rows.

Top-N per group

  • Pre-aggregate first (for example sales per product per category per month), then rank.
  • Choose a ranking function deliberately:
    • row_number: exactly N rows. Ties are broken arbitrarily unless you add a tiebreaker.
    • rank: ties share a rank, with gaps (1, 1, 3). Filtering ≤ N can return more than N.
    • dense_rank: ties share a rank, no gaps (1, 1, 2). Returns all items in the top N distinct values.

Running totals

  • A row frame from the start of the partition to the current row.
  • Specify rowsBetween. The default frame with orderBy is RANGE, which includes ties in the order column and can surprise you (see Running totals and moving averages).
  • Running totals reset per partition, so partition by the reset grain (customer, or customer and year).

Related variants

  • Running total within each month: partitionBy("customer_id", "month").
  • Rolling 7-day sums: rangeBetween over a numeric day index (days since epoch), or a 7-row frame if data is dense and daily.
  • Percent of total: F.col("x") / F.sum("x").over(Window.partitionBy("grp")).

Performance: one shuffle per distinct partition spec. Combine operations with the same window spec in one select.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F
from pyspark.sql.window import Window

sales = spark.read.table("silver.order_items")   # category, product_id, order_date, amount

# Top 3 products per category by revenue (ties included via dense_rank)
prod_rev = sales.groupBy("category", "product_id").agg(F.sum("amount").alias("revenue"))
w_rank = Window.partitionBy("category").orderBy(F.desc("revenue"))
top3 = (prod_rev.withColumn("rnk", F.dense_rank().over(w_rank))
                .filter(F.col("rnk") <= 3))

# Daily revenue running total per category
daily = sales.groupBy("category", "order_date").agg(F.sum("amount").alias("daily_revenue"))
w_run = (Window.partitionBy("category").orderBy("order_date")
               .rowsBetween(Window.unboundedPreceding, Window.currentRow))
running = daily.withColumn("running_revenue", F.sum("daily_revenue").over(w_run))

Sample: category “Toys” with daily revenue 100, 40 and 60 on consecutive days gives running revenue of 100, 140 and 200.

Common mistakes:

  • Ranking raw transactions instead of aggregates.
  • rank when exactly N rows are required.
  • Default RANGE frame with duplicate dates.

38. Spark vs pandas vs Polars: when to use each

Intermediate · Common · Tool Selection

Use pandas for small to medium data that easily fits in one machine’s memory, up to a few GB, and for quick analysis with its huge ecosystem. Use Polars when one machine is enough but you need more speed and less memory. It is multi-threaded and lazy, and can handle tens to hundreds of GB on a big machine. Use Spark when the data or the work is too big for one machine, or you need lakehouse integration, streaming and fault tolerance.

Dimension pandas Polars Spark (PySpark)
Execution Single-threaded mostly, eager Multi-threaded, lazy + eager, Rust engine Distributed, lazy, JVM/Photon
Data scale Fits in RAM (rule of thumb: data × 5–10 ≤ RAM) Larger-than-RAM possible via streaming engine on one machine TBs–PBs across cluster
Startup/overhead Minimal Minimal Cluster/session overhead (seconds–minutes)
API Mature, huge ecosystem Expression-based, consistent, fast DataFrame/SQL, rich connectors
Memory format NumPy (Arrow-backed options in 2.x) Apache Arrow Tungsten binary / Arrow for Python interchange
Fault tolerance None None Task retries, lineage
Lakehouse integration Via libraries (deltalake, pyiceberg) Via libraries (deltalake, pyiceberg) Native (Delta, Iceberg, UC/catalogs)
Streaming No Limited Structured Streaming
Best for EDA, small ETL, ML feature prep on samples Fast single-node ETL, local data apps, CI tests Big data ETL, streaming, shared platforms

Decision heuristics

  • Data under 1 GB and simple logic: pandas or Polars. Spark’s overhead dominates.
  • 1–100 GB on one strong machine: Polars (or DuckDB for SQL) is often faster and cheaper than a cluster.
  • Above roughly 100 GB, growing data, many concurrent jobs, or streaming: Spark.
  • Already on a Spark platform with governance (Unity Catalog), lineage and orchestration: Spark keeps operations consistent, even for medium data. Serverless reduces the overhead.
  • pandas API on Spark (pyspark.pandas) helps migrate pandas code to scale, but performance differs. Test it.

Interop

  • Spark → pandas: df.toPandas() (Arrow-accelerated with spark.sql.execution.arrow.pyspark.enabled). Small results only.
  • Polars ↔ Arrow ↔ Spark: pl.from_arrow, and Spark createDataFrame from pandas or Arrow.
  • pandas UDFs bring pandas logic into Spark at scale (see Python UDF vs pandas UDF vs built-in functions).

Example

# Runtime: Python 3.12 with polars 1.x, pandas 2.x; PySpark 3.5
import polars as pl
import pandas as pd

# Polars: lazy, multi-threaded single-node aggregation over Parquet files
top_regions_pl = (pl.scan_parquet("data/orders/*.parquet")
                    .filter(pl.col("order_date") >= pl.date(2026, 3, 1))
                    .group_by("region")
                    .agg(pl.col("amount").sum().alias("revenue"))
                    .sort("revenue", descending=True)
                    .collect())

# pandas: fine for a small extract
small = pd.read_parquet("data/orders/2026-03-01.parquet")
top_regions_pd = small.groupby("region", as_index=False)["amount"].sum().sort_values("amount", ascending=False)

# Spark: same logic at cluster scale
from pyspark.sql import functions as F
top_regions_spark = (spark.read.parquet("s3://lake/silver/orders/")
                     .filter(F.col("order_date") >= "2026-03-01")
                     .groupBy("region").agg(F.sum("amount").alias("revenue"))
                     .orderBy(F.desc("revenue")))

Advanced PySpark and Spark interview questions (experienced)

39. Fix skew with salting (with code)

Advanced · Very Common · Performance

Salting spreads one very large key over N partitions. On the skewed side, add a random salt number from 0 to N−1 to the key. For a join, copy each row on the other side N times, once per salt value, so every salted key still finds its match. For an aggregation, total by key and salt first, then total again by key. Before salting by hand, check whether AQE’s skew-join handling already fixes it.

Join salting

  1. On the large, skewed side: salt = floor(rand() * N).
  2. On the other side: cross join with range(N) to create N copies, one per salt.
  3. Join on (key, salt).
  4. Cost: the other side grows N times. Salt only hot keys to limit that (selective salting).

Aggregation salting (two-stage)

  1. groupBy(key, salt).agg(partial): work spreads across N tasks per hot key.
  2. groupBy(key).agg(combine partials).
  3. This works for decomposable aggregates (sum, count, min, max). For averages, sum and count separately. For distinct counts, use approximate sketches or two-step distinct logic.

Choosing N: roughly the hot key’s size divided by the target partition size. For example, a 20 GB hot key with 200 MB targets needs N ≈ 100.

Alternatives before salting

Example

# Runtime: PySpark 3.5 — selective salting for a skewed join
from pyspark.sql import functions as F

N = 32
orders = spark.read.table("silver.orders")            # skewed on merchant_id
merchants = spark.read.table("silver.merchants")      # too large to broadcast

hot_keys = [r.merchant_id for r in
            orders.groupBy("merchant_id").count().orderBy(F.desc("count")).limit(5).collect()]

orders_salted = orders.withColumn(
    "salt",
    F.when(F.col("merchant_id").isin(hot_keys), F.floor(F.rand(seed=7) * N)).otherwise(F.lit(0))
).withColumn("salt", F.col("salt").cast("int"))

salts = spark.range(N).withColumnRenamed("id", "salt").withColumn("salt", F.col("salt").cast("int"))
merchants_salted = (merchants.filter(F.col("merchant_id").isin(hot_keys)).crossJoin(salts)
                    .unionByName(merchants.filter(~F.col("merchant_id").isin(hot_keys))
                                 .withColumn("salt", F.lit(0))))

joined = orders_salted.join(merchants_salted, ["merchant_id", "salt"]).drop("salt")

# Two-stage salted aggregation
revenue = (orders_salted.groupBy("merchant_id", "salt").agg(F.sum("amount").alias("part_sum"))
           .groupBy("merchant_id").agg(F.sum("part_sum").alias("revenue")))

The hot-key list is collected to the driver, but it is tiny (5 values), so collect() is safe here. isin with a NULL in the list never matches, which is fine because NULL keys should be handled separately (see How do you detect data skew?).

Common mistakes:

  • Salting without replicating the other side, which loses matches.
  • Salting all keys, needlessly multiplying the other side.
  • Not checking AQE first.

Learn more: Spark Partitions, Shuffles and Data Skew

40. Catalyst optimizer phases

Advanced · Common · Optimisation Internals

Catalyst turns your DataFrame or SQL code into a fast plan in four steps. Analysis checks the table and column names and types against the catalog. Logical optimisation rewrites the plan with rules, such as pushing filters down, pre-computing constants and dropping unused columns. Physical planning picks how to run it, including the join strategy, using statistics. Code generation (Tungsten) then compiles the chosen plan into efficient Java code.

1. Analysis (unresolved → resolved logical plan)

  • Resolves table and column names, data types and functions through the catalog.
  • Errors like “cannot resolve column” appear here, at plan time, not at runtime.

2. Logical optimisation (rule-based)

  • Predicate pushdown: move filters closer to the source, through joins and projections, and into scans (see Predicate pushdown and partition pruning).
  • Column pruning: read only the needed columns.
  • Constant folding, null propagation and boolean simplification.
  • Combining filters and projections, removing redundant operations.
  • Join reordering (with CBO statistics; spark.sql.cbo.enabled, plus join reorder settings).
  • Subquery decorrelation (see Correlated subqueries and their performance).

3. Physical planning

  • Generates one or more physical plans: join strategy (broadcast, sort-merge, shuffle hash), aggregation strategy (hash vs sort), and exchange (shuffle) placement.
  • Uses size estimates and statistics (ANALYZE TABLE ... COMPUTE STATISTICS [FOR COLUMNS ...]) to choose.
  • AQE can re-plan later stages at runtime (see Adaptive Query Execution: what does it do?).

4. Code generation

  • Whole-stage code generation fuses chains of operators into single Java functions, avoiding virtual calls and intermediate objects (see Project Tungsten and whole-stage code generation). The operators show up with * and codegen IDs in explain() output.

What blocks Catalyst

  • Python UDFs are black boxes. No pushdown through them, and data is serialised to Python (see Python UDF vs pandas UDF vs built-in functions).
  • RDD operations (outside Catalyst).
  • Non-deterministic expressions limit some rewrites.
  • Missing statistics lead to poor join choices.

Extending it: Catalyst is rule-based and extensible. Libraries can add rules through SparkSessionExtensions, which Delta Lake uses for its commands.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

q = (spark.read.table("silver.orders")
     .join(spark.read.table("silver.customers"), "customer_id")
     .filter((F.col("country") == "IN") & (F.col("order_date") >= "2026-03-01"))
     .select("order_id", "amount", "segment"))

q.explain(mode="extended")   # parsed -> analyzed -> optimized logical -> physical plans

In the optimised logical plan, the filters on country and order_date are pushed below the join to each side, and only the needed columns are read from each table.

Common mistakes:

  • Not knowing what predicate pushdown or column pruning are.
  • Thinking Catalyst optimises Python UDF internals.
  • Never computing table statistics.

Learn more: Catalyst, Tungsten and Code Generation: How Spark SQL Optimises Queries

41. Project Tungsten and whole-stage code generation

Advanced · Common · Optimisation Internals

Project Tungsten is Spark’s work on using CPU and memory efficiently. It has three parts. Binary memory management stores rows in a compact format, often outside the Java heap, so there is less garbage collection. Cache-aware algorithms make better use of the CPU cache. Whole-stage code generation joins many steps of a stage into one generated Java function, which avoids slow per-row function calls and object creation.

1. Memory management and binary format

  • Rows are stored as UnsafeRow, a compact binary layout, instead of Java objects. That means less memory, less garbage collection, and fast comparisons and hashing on raw bytes.
  • Optional off-heap memory (spark.memory.offHeap.enabled) for some data structures.
  • Shuffle and sort operate on serialised binary data.

2. Cache-aware computation

  • Sorting and hashing structures are designed for CPU cache locality (for example sorting on key prefixes).

3. Whole-stage code generation (WSCG)

  • The classic “Volcano” model calls next() per row per operator, which costs a lot of virtual calls.
  • WSCG generates one tight loop for a pipeline (scan → filter → project → partial aggregate), compiled at runtime by Janino.
  • In explain() output, operators prefixed with *(n) share codegen stage n.
  • Falls back to interpreted execution when generated code would be too large (spark.sql.codegen.hugeMethodLimit) or for unsupported expressions.

Vectorised readers: Parquet and ORC vectorised readers decode columns in batches (spark.sql.parquet.enableVectorizedReader), feeding efficient columnar processing.

Beyond Tungsten: native engines (Photon on Databricks; Gluten/Velox and Comet in open source) push further with vectorised C++ or Rust execution (see What is Photon?).

Why it matters for engineers

  • Built-in functions benefit fully. Python UDFs break the generated pipeline and serialise data (see Python UDF vs pandas UDF vs built-in functions).
  • Very wide rows or extremely complex expressions can disable codegen. Splitting complex expressions can help.
  • Codegen issues sometimes appear as warnings in logs (“Generated method too long”), which hint at fallback to slower paths.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

df = (spark.range(0, 10_000_000)
      .withColumn("bucket", F.col("id") % 100)
      .filter(F.col("bucket") < 10)
      .groupBy("bucket").agg(F.sum("id").alias("total")))

df.explain(mode="formatted")
# Look for "WholeStageCodegen (1)" / "*(1)" groupings around Range, Filter, Project, HashAggregate

Common mistakes:

  • Not knowing what UnsafeRow or WSCG are at a high level.
  • Thinking Tungsten is a separate product to install.
  • Chaining hundreds of withColumn calls (plan bloat).

Learn more: Catalyst, Tungsten and Code Generation: How Spark SQL Optimises Queries

42. Executor memory model: unified, execution, storage, overhead

Advanced · Very Common · Memory

An executor’s Java heap (spark.executor.memory) has 300 MB reserved, then a shared region (60% of the rest by default). That region is split between execution memory, for shuffles, joins, sorts and aggregations, and storage memory, for cache and broadcasts. The rest is user memory. Outside the heap, spark.executor.memoryOverhead (the larger of 10% or 384 MB) covers off-heap needs, including PySpark’s Python processes. If the total goes over the container limit, the cluster kills the executor.

Layout (per executor)

Container memory  =  spark.executor.memory (heap)  +  memoryOverhead  (+ offHeap.size if enabled, + pyspark memory if set)

Heap:
 ├─ Reserved memory: 300 MB
 ├─ Unified memory: (heap - 300MB) * spark.memory.fraction (0.6)
 │    ├─ Storage (cache, broadcast)  ─┐  boundary moves:
 │    └─ Execution (shuffle, join,    ┘  execution can evict storage down to
 │        sort, aggregation buffers)     spark.memory.storageFraction (0.5) of unified
 └─ User memory: remaining 40% (your data structures, UDF objects, metadata)

Unified memory rules

  • Execution and storage borrow from each other.
  • Execution can evict cached blocks (down to the protected storageFraction). Storage can’t evict execution memory.
  • When execution memory runs out, operators spill to disk (sort and aggregation buffers), which is slower but safe.

Memory overhead

  • Off-heap allocations: JVM internals, thread stacks, NIO buffers, Netty for shuffle, and Python worker processes for UDFs and pandas UDFs. With spark.executor.pyspark.memory unset, Python memory comes from the overhead.
  • Container killed messages such as “Container killed by YARN for exceeding memory limits” or Kubernetes OOMKilled usually mean the overhead is too small (heavy Python UDFs, Arrow batches, many cores per executor). They are not necessarily heap problems.

Per-task memory: execution memory is shared across the concurrently running tasks on an executor, so each task gets roughly 1/N of it when N tasks are active. More cores per executor means less memory per task.

Symptoms and levers

Symptom Lever
Large spill (memory/disk) in stages More partitions (smaller tasks), more memory per core, fewer cores per executor
java.lang.OutOfMemoryError: Java heap space Reduce per-task data (skew, partitions), increase executor memory, avoid huge broadcasts
Container killed for exceeding memory limits Increase memoryOverhead, limit Python batch sizes (spark.sql.execution.arrow.maxRecordsPerBatch)
Long GC pauses Fewer cores per executor, smaller heaps, avoid caching huge deserialized objects

Common mistakes:

  • Raising executor.memory for container-killed errors that are overhead problems.
  • Not knowing spill is the safety valve.
  • Many cores per executor with little memory.

Learn more: Spark Memory, Executor Sizing and Cluster Tuning

43. Driver OOM vs executor OOM: causes and fixes

Advanced · Very Common · Memory

Driver out-of-memory usually comes from pulling data to the driver (collect, toPandas, building a big broadcast), from huge query plans, or from many jobs at once. Executor out-of-memory comes from too much data per task (skew or too few partitions), heavy operations (a huge explode, big windows, collect_list on a popular key), oversized broadcasts, or Python processes using more than the overhead. Fix how the data flows first, then change memory settings.

Driver OOM

Cause Fix
collect() / toPandas() / take(huge) Write results with df.write; aggregate before collecting; limit
Broadcasting large tables (driver may build broadcast) Lower/disable broadcast threshold; let AQE decide; don’t force hints on big tables
spark.driver.maxResultSize exceeded (default 1 GB) Avoid large results; raise cautiously only if truly needed
Giant plans (thousands of withColumn, loops unioning hundreds of DataFrames) Restructure: select with many expressions, build lists then one unionByName, checkpoint long lineage
Listing millions of files / huge metadata Compact files; table formats with metadata pruning
Many streaming queries or threads on one driver Split into separate jobs
Python driver code holding large objects (pandas) Keep data in Spark; process in executors

Executor OOM

Cause Fix
Skewed key → one giant partition AQE skew join, salting, filter NULL keys (see How do you detect data skew?, Fix skew with salting (with code))
Too few shuffle partitions Increase partitions; AQE coalesce (see spark.sql.shuffle.partitions: default and tuning)
collect_list/collect_set on hot keys, huge arrays Limit, pre-aggregate, or redesign
explode of large arrays Filter first, process in chunks
Wide window over huge partitions Narrower partition key, pre-aggregation
Broadcast too large for executors Disable broadcast for that join
Container killed (overhead) Raise memoryOverhead; reduce Arrow batch size (see Executor memory model: unified, execution, storage, overhead)
Too many cores per executor Fewer cores → more memory per task

Diagnosis

  • Read the stack trace and where it happened: driver logs vs executor logs.
  • Spark UI: failed stage, the tasks that failed, shuffle read size of those tasks (skew), and spill.
  • Executors tab: memory, GC time, failed task counts.
  • Reproduce on a sample with the same skew pattern, if possible.

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

events = spark.read.table("silver.events")

# Driver OOM risk: pulling 200M rows to pandas
# pdf = events.toPandas()

# Instead: aggregate in Spark, bring only the small result
daily = events.groupBy(F.to_date("event_ts").alias("d")).count()
pdf = daily.toPandas()                       # a few hundred rows

# Executor OOM risk: collecting all events per user into one array on a skewed key
# per_user = events.groupBy("user_id").agg(F.collect_list("event_type"))

# Instead: compute what you need without materialising giant arrays
per_user = events.groupBy("user_id", "event_type").count()

Learn more: Spark Memory, Executor Sizing and Cluster Tuning

44. How do you size executors (cores and memory)?

Advanced · Common · Resource Tuning

A common starting point is about 4–5 cores per executor, which gives good throughput without too much garbage collection. Give each core roughly 4–8 GB of heap. On every machine, leave room for memory overhead, the operating system and its services, usually 1 core and some memory. Then tune using the Spark UI: look at spill, garbage collection time and how long tasks take.

Worked example (self-managed YARN-style node)

  • Node: 16 cores, 128 GB RAM.
  • Reserve 1 core and about 8 GB for the OS and node manager, leaving 15 cores and about 120 GB.
  • 5 cores per executor gives 3 executors per node.
  • About 40 GB per executor container. Subtract overhead (max of 10% or 384 MB, about 4 GB), so spark.executor.memory ≈ 36 GB and memoryOverhead ≈ 4 GB.
  • On 10 nodes: 30 executors × 5 cores = 150 task slots. Leave one executor’s worth for the YARN Application Master if the driver runs in cluster mode.

Trade-offs

Few large executors (many cores) Many small executors (few cores)
Share broadcast variables and cache across more tasks Less GC pressure per JVM
Fewer JVMs → less overhead More JVMs → more overhead, more broadcast copies
Long GC pauses; HDFS throughput issues with >5 concurrent tasks Smaller heaps can’t handle big partitions
Single failure loses more work Better fault isolation

“Tiny” executors (1 core each) lose the benefits of sharing within the JVM. “Fat” executors (all cores of a node) suffer GC and I/O contention. The middle ground usually wins.

Managed platforms

  • Databricks: typically one executor per worker node, using all of its cores. You size by choosing node types (memory- vs compute- vs storage-optimised) and worker counts (see Cluster sizing, autoscaling and spot instances). Serverless sizes itself.
  • EMR: maximizeResourceAllocation or instance-fleet defaults. Still validate them.
  • Kubernetes: set pod CPU and memory requests and limits to match executor cores, memory and overhead.

Tuning from metrics

  • Spill or OOM: more memory per core (fewer cores per executor, or bigger nodes), or more partitions.
  • High GC time (>10% of task time): smaller heaps per executor or fewer cores, and less caching of deserialised objects.
  • Idle cores: too few partitions or tasks, or skew.
  • Short tasks (<100 ms): too many partitions, so reduce them.

Example

# spark-submit on YARN for the worked example above
spark-submit \
  --master yarn --deploy-mode cluster \
  --num-executors 29 \
  --executor-cores 5 \
  --executor-memory 36g \
  --conf spark.executor.memoryOverhead=4g \
  --driver-memory 8g \
  --conf spark.sql.shuffle.partitions=600 \
  orders_etl.py

45. Dynamic partition pruning

Advanced · Occasional · Optimisation

Dynamic partition pruning (Spark 3.0 and later) skips partitions of a big fact table while the query runs, using the filter from the other side of a join. A typical case: a fact table partitioned by date_key, joined to a date table filtered to “last week”. Spark first works out which keys pass the dimension filter, often reusing a broadcast, then uses them as a partition filter when it reads the fact table.

The problem

SELECT ... FROM fact_sales f JOIN dim_date d ON f.date_key = d.date_key
WHERE d.fiscal_week = '2026-W10'

The filter is on the dimension, not the fact’s partition column. Static planning can’t know which date_key values qualify, so without DPP it scans every fact partition.

How DPP works

  1. Spark sees that the fact is partitioned on the join key and that the other side has a selective filter.
  2. It builds a subquery (or reuses the broadcast of the filtered dimension) that collects the distinct join keys.
  3. It adds a dynamic pruning filter to the fact scan: date_key IN (keys from dim).
  4. Only matching partitions are read.

Conditions

  • spark.sql.optimizer.dynamicPartitionPruning.enabled = true (default).
  • The fact table is partitioned on the join key (directory partitions or table-format partitions).
  • An equi-join, with a selective filter on the other side.
  • Most effective when the dimension side is broadcast, because the broadcast result is reused (reuseBroadcastOnly default true).

In plans: look for dynamicpruningexpression(...) in the fact’s PartitionFilters, plus a SubqueryBroadcast node.

Related: Databricks and Delta also apply dynamic file pruning (DFP), which skips files (not just partitions) using join-key statistics. That helps clustered, unpartitioned tables too.

Common mistakes:

  • Not partitioning the fact on the join key and expecting DPP.
  • Applying functions to the join key, which blocks pruning.
  • Confusing DPP with static partition pruning.

46. Checkpointing vs caching (lineage truncation)

Advanced · Occasional · Fault Tolerance

Caching keeps computed data in memory or on local disk, but remembers the full plan. If a cached piece is lost, Spark rebuilds it from the original plan. checkpoint() writes the data to reliable storage and cuts the plan, so later work starts from the checkpoint files. localCheckpoint() does the same on executor storage: faster, but lost if an executor dies. Use checkpoints to cut very long or loop-based plans.

Aspect cache() / persist() checkpoint() (reliable) localCheckpoint()
Storage Executor memory/disk Reliable storage (HDFS/S3/ADLS/volumes) Executor storage
Lineage Kept Truncated Truncated
Survives executor loss Recompute from lineage Yes (re-read files) No (job fails if lost)
Eager? Lazy Eager by default (eager=True): runs a job Eager by default
Cost Memory Write to storage (extra job) Local write
Use Reuse within job Long/iterative lineage, very complex plans Speed-focused lineage cut in stable clusters

Why lineage length matters

  • Iterative algorithms (graph processing, iterative joins in loops, or a loop that unions or updates a DataFrame 100 times) build huge logical plans. Catalyst analysis and optimisation time grows, plans can hit stack overflows, and recomputation after failure can go all the way back to the start.
  • A checkpoint every N iterations keeps plans small and bounds recomputation.

Alternatives

  • Write an intermediate Delta or Parquet table and read it back. This is explicit, inspectable, reusable across jobs, and often clearer in production pipelines.
  • Restructure loops into set-based operations (one unionByName of a list, or a single join).

Streaming checkpoints are different. Structured Streaming checkpoints store offsets, commit logs and state for exactly-once recovery (see Checkpoints and exactly-once in Structured Streaming). The name is the same, but the purpose differs.

Example

# Runtime: PySpark 3.5 — iterative label propagation with periodic lineage truncation
from pyspark.sql import functions as F

spark.sparkContext.setCheckpointDir("s3://lake/_spark_checkpoints/label_propagation/")

edges = spark.read.table("silver.account_links")              # src, dst
labels = spark.read.table("silver.accounts").select(F.col("account_id").alias("node"),
                                                     F.col("account_id").alias("label"))
for i in range(20):
    propagated = (edges.join(labels, edges.src == labels.node)
                       .groupBy(F.col("dst").alias("node"))
                       .agg(F.min("label").alias("new_label")))
    labels = (labels.join(propagated, "node", "left")
                    .select("node", F.least("label", F.coalesce("new_label", "label")).alias("label")))
    if i % 5 == 4:
        labels = labels.checkpoint()      # truncate lineage every 5 iterations
labels.write.mode("overwrite").saveAsTable("gold.account_clusters")

Common mistakes:

  • Confusing streaming checkpoints with DataFrame checkpoints.
  • Not setting a checkpoint directory.
  • Using localCheckpoint on spot-heavy clusters.

47. Watermarks and late data in Structured Streaming

Advanced · Very Common · Structured Streaming

withWatermark("event_ts", "15 minutes") tells Spark to track the latest event time it has seen, minus 15 minutes. That is the watermark. Windows that end before the watermark are final: in append mode they are written out and their state is dropped. Events that arrive later than the watermark are ignored by stateful operations. Watermarks keep state from growing forever and set how late data is allowed to be.

Mechanics

  • Watermark = max(event_time seen) − delay, computed per micro-batch. It only moves forward.
  • For windowed aggregations:
    • Append mode: a window’s result is emitted once, after the watermark passes the window’s end. State for that window is then removed.
    • Update mode: results are emitted as they change, and state is still cleaned once the watermark passes.
  • Late events (event time below the watermark) are dropped from stateful operators. They are not guaranteed to be dropped immediately: Spark guarantees that data within the delay is kept, but data beyond it may or may not be processed.

Where watermarks are required or important

Operation Role
Windowed aggregation (append mode) Required to emit results
dropDuplicates / dropDuplicatesWithinWatermark (Spark 3.5+) Bounds dedup state
Stream-stream joins Needed (with time-range join conditions) to clean state (see Stream-stream joins)
transformWithState / arbitrary stateful ops Timers and state eviction

Multiple watermarks: with several inputs (joins, unions), the global watermark defaults to the minimum across inputs (spark.sql.streaming.multipleWatermarkPolicy = min), which is conservative. max is possible but drops more data.

Choosing the delay

  • Measure actual lateness, as a distribution of ingest_time − event_time. Choose a delay covering, say, p99.9, balanced against latency and state size.
  • Larger delays mean results are later in append mode, and state is larger.
  • Data arriving later than the watermark needs a separate correction path: batch reprocessing of late partitions, or a “late events” table (see How do you build data quality checks into a pipeline?).

Example

# Runtime: PySpark 3.5
from pyspark.sql import functions as F

events = spark.readStream.table("bronze.sensor_readings")    # device_id, reading, event_ts

per_10min = (events
    .withWatermark("event_ts", "20 minutes")
    .groupBy(F.window("event_ts", "10 minutes"), "device_id")
    .agg(F.avg("reading").alias("avg_reading"), F.count("*").alias("n")))

(per_10min.writeStream
    .outputMode("append")                         # emit each window once, after watermark passes
    .option("checkpointLocation", "s3://lake/_checkpoints/sensor_10min/")
    .trigger(processingTime="1 minute")
    .toTable("silver.sensor_10min"))

Walkthrough: the max event time seen is 10:45, so the watermark is 10:25. The 10:00–10:10 and 10:10–10:20 windows are final and emitted. An event stamped 10:05 arriving now is dropped. One stamped 10:28 still updates the 10:20–10:30 window.

Common mistakes:

  • No watermark on stateful queries.
  • Watermark on a processing-time column instead of event time.
  • Expecting late data to be updated in append mode.

Learn more: Spark Structured Streaming: Triggers, Watermarks and Exactly-Once

48. Checkpoints and exactly-once in Structured Streaming

Advanced · Common · Structured Streaming

A streaming checkpoint folder stores the offsets planned for each batch (a write-ahead log), a log of finished batches, the state store and metadata. On restart, Spark re-runs any batch that was planned but not finished, from the same offsets. With a source that can replay (Kafka, files, Delta) and a sink that is idempotent or transactional (Delta, file sink), you get exactly-once results end to end. Other sinks give at-least-once unless you make the writes safe to repeat.

Checkpoint contents

checkpointLocation/
 ├─ metadata          # query id
 ├─ offsets/          # batch N: source offsets planned (written BEFORE processing)
 ├─ commits/          # batch N: marked complete (written AFTER sink commit)
 ├─ state/            # state store files per operator/partition (HDFS-backed or RocksDB)
 └─ sources/          # source-specific metadata (e.g. file source seen files)

Recovery flow

  1. On restart, read the last offsets entry (batch N) and the last commit (batch N−1).
  2. If batch N wasn’t committed, re-run it with exactly the same offsets.
  3. The sink must handle that rerun without duplicates:
    • Delta sink: records (queryId, batchId) as a transaction. A replayed batch ID is skipped.
    • File sink: a _spark_metadata log of committed files. Readers that use it ignore uncommitted files.
    • Kafka sink: at-least-once (possible duplicates on retry), unless consumers dedupe or you use idempotent producer patterns at application level.
    • foreachBatch: your code must be idempotent. Use MERGE on keys, or Delta txnAppId/txnVersion options with the batchId (see foreachBatch for upserts and multiple sinks).

State store

  • The default HDFS-backed store keeps state in executor memory with checkpointed deltas.
  • The RocksDB state store (spark.sql.streaming.stateStore.providerClass) handles large state better (off-heap, on disk), and changelog checkpointing reduces checkpoint overhead.

Rules

  • One checkpoint per query. Never share one between queries.
  • Store it on durable storage (S3, ADLS, GCS, UC volumes), not local disk.
  • Query changes: changing stateful operators, grouping keys or output schema may be incompatible with the existing checkpoint. Spark 4.0 adds a state data source reader to inspect state. Plan a new checkpoint plus a backfill when you must.
  • Deleting a checkpoint means starting over: reprocessing from startingOffsets, which can produce duplicates downstream.

Common mistakes:

  • Assuming exactly-once with any sink.
  • Sharing or deleting checkpoints casually.
  • Changing stateful query logic without a migration plan.

Learn more: Spark Structured Streaming: Triggers, Watermarks and Exactly-Once

49. Stream-stream joins

Advanced · Occasional · Structured Streaming

Structured Streaming can join two streams, for example ad impressions with clicks. Spark keeps past rows from both sides in state, so a row that arrives later can still find its match. To stop that state growing forever, set a watermark on both streams and add a time limit to the join, such as “a click within 1 hour after the impression”. Outer joins also need watermarks, and they output unmatched rows only after the watermark passes.

Join types

Join Requirements Output timing
Inner Optional watermarks + time constraint (strongly recommended for bounded state) As soon as matches occur
Left/right outer Watermark on the opposite side + event-time range condition Matches immediately; non-matches (NULLs) after watermark guarantees no future match
Full outer, left semi Supported in Spark 3.1+ with same constraints Similar

State cleanup logic

  • With impression_ts and click_ts watermarked and the condition click_ts BETWEEN impression_ts AND impression_ts + INTERVAL 1 HOUR:
    • Spark knows an impression can’t match clicks older than its own time, or newer than one hour after it.
    • Once the click-side watermark passes impression_ts + 1 hour, that impression’s state is removed.
  • Without the range condition, state grows without bound even with watermarks.

Practical notes

  • Join keys must be equality conditions (impression_id), plus the time range.
  • Output mode is append only.
  • State size is roughly (input rate × retention window), so monitor stateOperators metrics in the progress output.
  • Stream-static joins (a stream joined to a Delta dimension table) are simpler and stateless. The static side is re-read each batch (Delta picks up the latest version). Use those for enrichment with dimensions.
  • For complex event correlation (sessions, multi-step funnels), transformWithState (Spark 4.0) or applyInPandasWithState gives custom state logic with timers.

Example

# Runtime: PySpark 3.5 — impressions joined to clicks within 1 hour
from pyspark.sql import functions as F

impressions = (spark.readStream.table("bronze.ad_impressions")
               .select("impression_id", "ad_id", F.col("event_ts").alias("impression_ts"))
               .withWatermark("impression_ts", "2 hours"))

clicks = (spark.readStream.table("bronze.ad_clicks")
          .select(F.col("impression_id").alias("click_impression_id"), F.col("event_ts").alias("click_ts"))
          .withWatermark("click_ts", "3 hours"))

attributed = impressions.join(
    clicks,
    F.expr("""
        click_impression_id = impression_id AND
        click_ts >= impression_ts AND
        click_ts <= impression_ts + interval 1 hour
    """),
    "leftOuter")

(attributed.writeStream
    .outputMode("append")
    .option("checkpointLocation", "s3://lake/_checkpoints/ad_attribution/")
    .toTable("silver.ad_attribution"))

Impressions with no click within an hour are emitted with NULL click columns. That happens after the click watermark passes impression_ts + 1 hour, roughly 4 hours after the impression given the 3-hour watermark.

Common mistakes:

  • No time-range condition.
  • Expecting outer-join NULL rows immediately.
  • Using a stream-stream join where a stream-static join would do.

50. What’s new in Spark 4.0 for data engineers?

Advanced · Occasional · Versions

Spark 4.0 (2025) turns ANSI SQL mode on by default, so bad casts and number overflows now raise errors instead of returning NULL. It adds the VARIANT type for semi-structured data, SQL scripting and pipe syntax, string collations, a Python Data Source API, and transformWithState for advanced stateful streaming. Spark Connect is improved and has a lightweight Python client, and Java 17 is the default. Test before upgrading, because the ANSI change can break existing jobs.

Key changes

Area Change Impact
ANSI mode default (spark.sql.ansi.enabled=true) Invalid casts, overflow, divide-by-zero raise errors instead of returning NULL/wrapping Existing pipelines may fail; use try_cast, try_divide, try_* functions
VARIANT type Efficient storage and querying of semi-structured data (parse_json, variant_get, : path syntax in some engines) Faster than JSON strings for nested data
SQL scripting Multi-statement scripts with variables, IF/WHILE/FOR, exception handlers Procedural SQL inside Spark
SQL pipe syntax FROM t |> WHERE ... |> AGGREGATE ... Readable step-by-step queries
Collations Case/accent-insensitive comparisons via COLLATE Fewer lower() workarounds
Python Data Source API Write custom batch/stream sources and sinks in Python Easier connectors
Python UDTFs (since 3.5) and improvements Table-valued Python functions Custom row-generating logic
Structured Streaming transformWithState Arbitrary stateful processing with timers, TTL, multiple state variables Replaces many flatMapGroupsWithState uses
State data source reader Read/inspect streaming state as a DataFrame Debugging stateful jobs
Spark Connect enhancements + pyspark-client Thin Python client package, closer API parity Remote clients, IDE development (see Spark Connect (client-server))
Java 17 default, Scala 2.13 Platform updates Rebuild JVM libraries
Observability/logging Structured JSON logging Easier log analysis

Upgrade checklist

  1. Run test suites with ANSI on. Fix casts (try_cast) and arithmetic edge cases.
  2. Check removed or deprecated APIs and configurations, and the library compatibility of connectors (Delta, Kafka, cloud connectors).
  3. Rebuild Scala and Java dependencies for 2.13 and Java 17.
  4. Diff outputs between versions on representative data (see Databricks Runtime versions and LTS).
  5. Review the release notes of your platform’s runtime that ships Spark 4.x (for example Databricks Runtime 17.x).

Example

-- Dialect: Spark SQL 4.0
SET spark.sql.ansi.enabled = true;

SELECT try_cast('12a' AS INT) AS safe_cast,          -- NULL instead of error
       try_divide(10, 0)       AS safe_div;          -- NULL instead of error

SELECT variant_get(parse_json('{"user":{"id":42,"tier":"gold"}}'), '$.user.tier', 'string') AS tier;

FROM silver.orders
|> WHERE order_date >= DATE'2026-03-01'
|> AGGREGATE SUM(amount) AS revenue GROUP BY region
|> ORDER BY revenue DESC;

By , Data Engineer and Founder of DataDank · Last reviewed Oct 2026 · Product names and defaults as of October 2026; code snippets state their dialect or runtime

Progress is saved in this browser only. No account needed.

Search
Filter by type