Delta Lake MERGE: Keep corrections right after a restart
E7 initially reports five rejects. A correction changes the value to three. The source then resends an older revision. An unconditional upsert can overwrite the correction with stale information.
Define identity, order and message meaning
The source supplies a stable event_id and a strictly increasing seq per ID. Each revision contains the complete state. Arrival time does not determine business order. Our target stores the latest state. If auditors also need every revision, keep a separate raw history.
| Delivery | seq | Rejected | Expected target |
|---|---|---|---|
| Initial event | 10 | 5 | Version 10, value 5 |
| Correction | 12 | 3 | Version 12, value 3 |
| Late message | 11 | 4 | Still version 12, value 3 |
| Replay | 12 | 3 | Still version 12, value 3 |
- seq 105 rejected partsInitial message
- seq 114 rejected partsOlder than version 12
- seq 123 rejected partsCurrent state
Supply exactly one source row per key to MERGE
Check for missing keys, missing sequences and conflicting payloads for the same (event_id, seq), including conflicts with known revisions. Collapse exact repeats. Quarantine conflicting versions. Only then rank by descending sequence within each ID to select its latest full state. Arbitrary tie-breaking would hide ambiguity rather than resolve it.
The resulting source is named batch_latest below. The target silver_current must likewise contain at most one row per ID. Multiple source matches can make an update ambiguous. If the source sends patches containing only changed fields, selecting its last row can lose earlier fields. Such patches must be folded in business order to reconstruct the complete state.
MERGE INTO silver_current AS t
USING batch_latest AS s
ON t.event_id = s.event_id
WHEN MATCHED AND s.seq > t.seq THEN
UPDATE SET t.seq = s.seq, t.rejected = s.rejected,
t.is_deleted = s.is_deleted
WHEN NOT MATCHED THEN
INSERT (event_id, seq, rejected, is_deleted)
VALUES (s.event_id, s.seq, s.rejected, s.is_deleted);Delete, replay and verify
A deletion has a sequence too. This example keeps an is_deleted marker plus the last accepted revision. Active reports filter out marked rows. If version 13 deletes E7 and version 11 then arrives, the stored 13 protects the target. Physically removing the row immediately could make the old revision appear to be an entirely new key.
Idempotence here means applying the same cleaned batch again yields the same business state. Test it deliberately. Apply a batch twice, send an older batch and then a genuinely newer revision. Compare keys, sequence, payload and deletion flags for all affected IDs. A simple target row count would miss an overwritten value. Coordinate concurrent writers and handle transaction conflicts by retrying against the same well-defined input.
Select the latest permissible revision within a batch
After conflict checks, validated_versions contains at most one unambiguous state per ID and sequence. The SQL below produces the merge source. Ordering within each ID matters. Selecting a single greatest sequence for the entire batch would lose changes to other events.
CREATE OR REPLACE TEMP VIEW batch_latest AS
SELECT event_id, seq, rejected, is_deleted
FROM (
SELECT *, ROW_NUMBER() OVER (
PARTITION BY event_id ORDER BY seq DESC
) AS revision_rank
FROM validated_versions
) ranked
WHERE revision_rank = 1;If E7 already has version 12 in the target and a new batch contains only 10 and 11, the ranking yields 11. Only the condition in MERGE prevents the regression. The two checks protect different boundaries: ranking resolves order within the delivery, and the target comparison protects the already published state.
Try it yourself
Version 13 deletes E7. A retry delivers version 12 and then version 14 explicitly reactivates E7 with two rejects. What should users see after each step?
Show the solution
After 13 the target retains a deletion marker and the active report hides E7. Version 12 is older and changes nothing. If the source permits reactivation and sets is_deleted to false, version 14 makes E7 visible with a value of two.