70. Apply a CDC Batch (MERGE Semantics)
Difficulty: Hard · Topics: Lakehouse, Window Functions, Joins
target is the current table (id, name, tier). changes is a batch of change events with id, op ('I' insert, 'U' update, 'D' delete), name, tier and an increasing sequence number seq. An id can change several times in one batch.
Apply the batch the way MERGE INTO would: for each id only its latest change counts; a delete removes the row, an insert or update writes the new values (an update for an unknown id is treated as an insert). Return the resulting table: id, name, tier.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
target
| id | name | tier |
|---|---|---|
| 1 | Asha | free |
| 2 | Ben | pro |
| 3 | Chitra | free |
changes
| id | op | name | tier | seq |
|---|---|---|---|---|
| 2 | U | Ben | enterprise | 1 |
| 3 | D | null | null | 2 |
| 4 | I | Dev | free | 3 |
| 4 | U | Dev | pro | 4 |
Expected output
| id | name | tier |
|---|---|---|
| 1 | Asha | free |
| 2 | Ben | enterprise |
| 4 | Dev | pro |
Hints
Hint 1
First reducechanges to the latest event per id: row_number over a window by id ordered by seq descending.Hint 2
Rows oftarget with no change at all are kept as they are: a left_anti join finds them.Hint 3
Union the untouched rows with the latest changes whoseop is not 'D'.Learn the concepts
- MERGE INTO and upserts · 7 min read. Matched, not-matched and not-matched-by-source clauses, step by step, and the duplicate-source error.
- Change Data Feed · 5 min read. Read the row-level inserts, updates and deletes made to a Delta table between two versions.
- Copy-on-write vs merge-on-read · 6 min read. Two ways to apply updates and deletes, and which one fits a given workload.
PySpark functions you'll practise
- Row Number
- Window spec
- join
- left_anti
- filter
- select
- withColumn
- orderBy
- unionByName
Related problems
- What Changed Between Two Versions · Hard · Lakehouse, Joins, Strings
- Net Changes from a Change Feed · Hard · Lakehouse, Aggregations, Conditional Logic
- Rebuild a Table from Its Transaction Log · Medium · Lakehouse, Joins, Aggregations
- Files VACUUM Can Delete · Medium · Lakehouse, Joins, Aggregations
- Iceberg Hidden Partition Layout · Medium · Lakehouse, Aggregations, Dates
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice