Architecture & Foundations

A MERGE cannot delete what the source stopped sending

Boitumelo Dikoko •

A MERGE can insert and update. On its own, it cannot delete what the source stopped sending.

That gap left over 46,000 ghost rows in a single table on a platform our team worked on. Nothing failed. Every run was green.

What happened

The feed vendor never sends corrections as corrections. When something changes, it re-sends the whole set that record belongs to. Anything it leaves out of the re-send is gone on its side.

Not on ours. The incremental model ran a MERGE on row id: insert the new rows, update the changed ones. Rows the vendor quietly dropped matched nothing in the batch, so the MERGE left them alone. Ids got re-issued, records moved to a different parent, counters were renumbered, and the old rows stayed.

Some records ended up with two conflicting values side by side: the old one and its replacement. A report reading that table could show either.

The MERGE did exactly what it was told. The design asked the wrong question of the source.

The question to ask first

Before you choose an incremental strategy, ask how the source sends corrections.

  • It sends row-level changes with a stable id (inserts, updates, explicit deletes): MERGE on that id is the right tool.
  • It re-sends a whole set (a match, an order, a day) and drops what no longer exists: replace by that scope.
  • It changes records for a few days, then settles: sync a fixed window, including deletes.

Replace by scope

For whole-set sources, make the key the scope, not the row. Each batch replaces every scope it contains, in one atomic write:

-- Databricks Runtime 17.2 and above, or a SQL warehouse
INSERT INTO TABLE silver.events
REPLACE USING (match_id)
SELECT * FROM batch_events;

REPLACE USING atomically deletes every target row whose match_id appears in the batch, then inserts the batch. On runtimes 16.3 to 17.1 it only works when match_id is the table’s partition column. A row the source dropped from a re-sent set disappears with it. On older runtimes, the same effect is a DELETE of the batch’s scopes and an INSERT. Run both inside one job step, and check what readers see between the two writes.

One caution: a scope that arrives empty replaces nothing, so it keeps its old rows. If the source can send “this set is now empty”, handle that case explicitly.

What about WHEN NOT MATCHED BY SOURCE?

Delta’s MERGE has a WHEN NOT MATCHED BY SOURCE ... DELETE clause (Databricks Runtime 12.2 and above), and it does delete target rows that are missing from the source. Without a condition, though, it deletes every target row that is not in this batch, which on an incremental load is most of the table.

Its condition can only reference target columns. That makes it a good fit for the third case, a fixed window:

MERGE INTO silver.orders t
USING (SELECT * FROM bronze.orders WHERE order_date >= current_date() - INTERVAL '5' DAY) s
ON t.order_id = s.order_id
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *
WHEN NOT MATCHED BY SOURCE AND t.order_date >= current_date() - INTERVAL '5' DAY THEN DELETE;

It’s a poor fit for “whatever scopes this batch happens to contain”, which is exactly the whole-set case. There, replace by scope.

Check your own tables

  1. For each incremental table, write down how its source sends corrections. If nobody knows, that’s the finding.
  2. For any whole-set source loaded by a row-level MERGE, count the rows whose scope was re-sent after they were last written. Those are your ghosts.
  3. Re-run one recent batch in dev and compare row counts before and after. A re-run that changes the count is telling you something.

If you’d like a second opinion on one of your incremental models, book a 20-minute call and I’ll tell you how I’d test it.