Databricks engineering with SQL, PySpark, and Delta Lake
Choosing compute, expressing transformations, and moving notebook experiments into repeatable Databricks jobs.
Separate the platform from its components
combines managed compute, orchestration, SQL serving, and governance. Spark is the distributed execution engine used by many workloads, is its Python API, and Delta Lake supplies transactional table storage. A serves SQL workloads; job compute executes scheduled engineering tasks. Select compute according to workload, isolation, startup latency, and cost.
Use notebooks for exploration and explanations, while keeping reusable transformation logic in versioned modules or SQL files. A production job should declare its entry point, dependencies, parameters, identity, and output tables so another engineer can reproduce a run without hidden notebook state.
SQL correctness starts with grain
Define what one output row represents before writing a join or aggregation. Joining orders to multiple customer-history rows can multiply revenue even though the query completes successfully. Check key uniqueness, effective-date predicates, and unmatched rows explicitly.
SQL preserve row-level context while ranking or aggregating within a partition. For deduplication, use ROW_NUMBER() with PARTITION BY order_id and descending order by source_version, then ingest_sequence. The ordering needs a documented, deterministic tie-breaker. Filter to rank one before merging into a target when the contract requires one source row per key.
SQL null comparisons use three-valued logic: use IS NULL to test for nulls. A WHERE predicate on the nullable side of a LEFT JOIN can remove unmatched rows and change the result's meaning. Include null keys and missing dimension rows in fixtures.
Keep PySpark work distributed
DataFrame operations build a lazy execution plan. Prefer built-in expressions that Spark can optimize over when equivalent functionality exists. Avoid collecting large datasets to the driver or converting them to pandas as an intermediate transformation step.
Use df.explain(mode="formatted") and the Spark UI to inspect scans, exchanges, join strategies, and task imbalance. SQL and the DataFrame API often produce comparable plans for equivalent expressions; compare the actual plans and measurements rather than assuming one language is faster.
Make Delta writes safe to rerun
Choose append, scoped overwrite, or from the data's semantics. MERGE requires a well-defined business key and source-version rule. Deduplicate source records and handle deletes explicitly; an upsert that ignores delete events leaves obsolete records behind.
Use Databricks Jobs to declare task dependencies, retries, timeouts, and concurrency limits. Record the code revision, runtime, parameters, source range, and output table versions for each run. Keep checkpoints and environment-specific destinations explicit, and test retry behavior before relying on automatic recovery.
MERGE does not automatically make every pipeline idempotent. Non-deterministic source selection, incrementing target values on every match, and duplicate source keys can still produce incorrect retries.
Worked example
Choose the latest order before summing
Incoming order versions contain one update. The output grain is one row per order, using the greatest source_version.
| order_id | source_version | amount |
|---|---|---|
| 42 | 1 | 10 |
| 42 | 2 | 15 |
| 43 | 1 | 8 |
What to notice: ROW_NUMBER() by order_id, ordered by source_version descending, keeps (42, 2, 15) and (43, 1, 8). The total is 23; summing all incoming versions incorrectly gives 33.
Practice / self-test
1. What distinguishes Databricks, Spark, PySpark, and Delta Lake?
Reveal answer
Databricks is the managed platform; Spark executes distributed computations; PySpark is Spark's Python API; Delta Lake provides transactional table storage.
2. A join doubles revenue without raising an error. What do you inspect first?
Reveal answer
Check input grain and join-key cardinality, especially duplicate dimension keys or multiple effective-date matches. Compare counts and totals before and after the join.
3. Why can collect() cause a production failure?
Reveal answer
It transfers results to the driver, whose memory and network capacity are limited. Keep large transformations distributed and collect only intentionally bounded results.
4. Why can a MERGE still be unsafe on retry?
Reveal answer
Its matching and update rules may apply a change again, or its source may change across runs. Use deterministic inputs, source-version conditions, and explicit deduplication.