:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(warehouse-sources): fix false cdc delete-enrichment violations (#106736)
Daniel RC9f3c98786af7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/batcher.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/batcher.pyindex 337bb3bcfcfb..a3a1b1ca51c5 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/batcher.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/batcher.py@@ -232,12 +232,16 @@ def enrich_delete_rows(pk_arrays = [table.column(col).to_pylist() for col in present_pks]- # Build lookup: pk_tuple -> data from last non-DELETE row in this batch- batch_lookup: dict[tuple, dict[str, object]] = {}+ # Each DELETE row's index -> data from the last non-DELETE row before it with the same PK. A row+ # after the delete is a re-insert of the key, not the state the delete removed.+ latest_by_key: dict[tuple, dict[str, object]] = {}+ batch_lookup: dict[int, dict[str, object]] = {}for i, op in enumerate(ops):+ key = tuple(arr[i] for arr in pk_arrays)if op != "D":- key = tuple(arr[i] for arr in pk_arrays)- batch_lookup[key] = {col: table.column(col)[i].as_py() for col in table_data_cols}+ latest_by_key[key] = {col: table.column(col)[i].as_py() for col in table_data_cols}+ elif key in latest_by_key:+ batch_lookup[i] = latest_by_key[key]# Build lookup from existing DeltaLake rows (cross-batch fallback)existing_lookup: dict[tuple, dict[str, object]] = {}@@ -261,7 +265,7 @@ def enrich_delete_rows(for i in delete_indices:key = tuple(arr[i] for arr in pk_arrays)- source = batch_lookup.get(key) or existing_lookup.get(key)+ source = batch_lookup.get(i) or existing_lookup.get(key)if source:for col in all_data_cols:# Only fill if the DELETE row's column is currently nulldiff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/load_resolution.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/load_resolution.pyindex 1add8c110040..c67f6f719c9f 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/load_resolution.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/load_resolution.py@@ -159,7 +159,20 @@ def verify_delete_enrichment(# Narrow before materializing: a wide table would otherwise pull every data column of the whole# batch into Python to inspect a capped handful of rows.ops = table.column(CDC_OP_COLUMN).to_pylist()- delete_indices = [i for i, op in enumerate(ops) if op == "D"][:MAX_VERIFIED_DELETE_ROWS]+ pk_arrays = [table.column(c).to_pylist() for c in present_pks]+ # A delete that follows another change to its key in this batch takes its values from that+ # change, not from the target, so a column that change set to NULL stays NULL correctly.+ # Keys are built per row as the scan reaches it, and the scan stops at the cap.+ changed_in_batch: set[tuple] = set()+ delete_indices: list[int] = []+ for i, op in enumerate(ops):+ key = tuple(a[i] for a in pk_arrays)+ if op != "D":+ changed_in_batch.add(key)+ elif key not in changed_in_batch:+ delete_indices.append(i)+ if len(delete_indices) == MAX_VERIFIED_DELETE_ROWS:+ breakif not delete_indices:return emptydeletes = table.take(pa.array(delete_indices, type=pa.int64()))diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_batcher.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_batcher.pyindex 3db4312f420d..832e66069f33 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_batcher.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_batcher.py@@ -473,11 +473,12 @@ class TestEnrichDeleteRows:assert result.column(CDC_OP_COLUMN)[1].as_py() == "D"assert result.column(DELETED_COLUMN)[1].as_py() is True- def test_delete_after_update_uses_update_data(self):+ def test_delete_uses_the_last_change_before_it(self):events = [_make_event(op="I", columns={"id": 1, "name": "Alice"}),_make_event(op="U", columns={"id": 1, "name": "Bob"}),_make_event(op="D", columns={"id": 1}),+ _make_event(op="I", columns={"id": 1, "name": "Carol"}),]table = self._make_raw_table(events)result = enrich_delete_rows(table, ["id"])diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_load_resolution.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_load_resolution.pyindex 94c2331528f1..97c4ab68d4a8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_load_resolution.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_load_resolution.py@@ -131,17 +131,21 @@ class TestVerifyDeleteEnrichment:assert report.delete_rows_checked == 1assert report.columns == ()- def test_unenriched_delete_is_reported(self):- table = _batch([1], [None], ["D"])+ @parameterized.expand([("alone", [1], [None], ["D"]), ("before_a_reinsert", [1, 1], [None, "carol"], ["D", "I"])])+ def test_unenriched_delete_is_reported(self, _name, ids, names, ops):+ table = _batch(ids, names, ops)report = verify_delete_enrichment(table, ["id"], _existing([1], ["alice"]))assert not report.okassert report.rows_with_nulled_columns == 1assert report.columns == ("name",)- def test_ignores_non_delete_rows(self):+ @parameterized.expand(+ [("update", [1], [None], ["U"]), ("delete_after_that_update", [1, 1], [None, None], ["U", "D"])]+ )+ def test_ignores_a_null_the_source_set(self, _name, ids, names, ops):# A NULL on an UPDATE is the source's own value, not lost data.- table = _batch([1], [None], ["U"])+ table = _batch(ids, names, ops)report = verify_delete_enrichment(table, ["id"], _existing([1], ["alice"]))assert report.ok