: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(duckling): pass allow_missing to ducklake registration for schema evolution (#65091)
James Greenhilla46b0f7b9642
diff --git a/posthog/dags/events_backfill_to_duckling.py b/posthog/dags/events_backfill_to_duckling.pyindex c3440d2d7585..c1722444161c 100644--- a/posthog/dags/events_backfill_to_duckling.py+++ b/posthog/dags/events_backfill_to_duckling.py@@ -1473,8 +1473,15 @@ def register_files_with_duckling(context.log.info(f"Registering {len(files)} file(s) with DuckLake from {s3_glob}")for s3_path in files:+ # allow_missing tolerates columns the live ingestion path added to the+ # duckling table via schema evolution but the backfill export doesn't carry.+ # Safe because the export SELECT is a fixed column set — a missing critical+ # column (team_id/uuid/timestamp) would only arise from an export bug, not+ # normal operation, and would surface as NULL-filled rows in downstream reads.conn.execute(- psql.SQL("CALL ducklake_add_data_files({}, 'events', {}, schema => 'posthog')").format(+ psql.SQL(+ "CALL ducklake_add_data_files({}, 'events', {}, schema => 'posthog', allow_missing => true)"+ ).format(psql.Literal(alias),psql.Literal(s3_path),)@@ -1738,8 +1745,11 @@ def register_persons_files_with_duckling(context.log.info(f"Registering {len(files)} persons file(s) with DuckLake from {s3_glob}")for s3_path in files:+ # See the events registration site for the allow_missing rationale.conn.execute(- psql.SQL("CALL ducklake_add_data_files({}, 'persons', {}, schema => 'posthog')").format(+ psql.SQL(+ "CALL ducklake_add_data_files({}, 'persons', {}, schema => 'posthog', allow_missing => true)"+ ).format(psql.Literal(alias),psql.Literal(s3_path),)diff --git a/posthog/dags/test_events_backfill_to_duckling.py b/posthog/dags/test_events_backfill_to_duckling.pyindex 5ad9677d5c38..cd724c137cbe 100644--- a/posthog/dags/test_events_backfill_to_duckling.py+++ b/posthog/dags/test_events_backfill_to_duckling.py@@ -1171,6 +1171,10 @@ class TestRegisterFilesWithDuckling:assert conn.execute.call_count == 3calls = _registered_call_strings(conn)assert all("ducklake_add_data_files" in c for c in calls)+ # allow_missing => true must be on every CALL — it's what lets the backfill+ # tolerate columns the live ingestion path added to the duckling table via+ # schema evolution but the backfill export doesn't carry.+ assert all("allow_missing => true" in c for c in calls)assert all(f"'{path}'" in calls[i] for i, path in enumerate(files))@parameterized.expand(@@ -1371,3 +1375,33 @@ class TestFannedOutLayoutRoundTrip:# Exactly the re-run's rows — no duplication, orphaned run1 files not counted.assert self._count(conn) == 30++ def test_allow_missing_handles_schema_evolution(self, lake):+ """When the duckling table has a column the backfill export doesn't (schema+ evolution from the live ingestion path), allow_missing => true lets+ registration succeed with NULL fill. Without the flag, the same file is+ rejected — proving the flag is load-bearing, not decorative."""+ conn, root = lake++ # Simulate the live ingestion path adding a column the backfill doesn't export.+ conn.execute(f"ALTER TABLE {DUCKLAKE_ALIAS}.posthog.events ADD COLUMN captured_at TIMESTAMPTZ")++ # Write a fanned-out day WITHOUT captured_at — exactly what the backfill exports.+ file_glob = self._write_fanned_out_day(conn, root, "run1", n_files=1, rows_per_file=5)+ single_file = file_glob.replace("_*.parquet", "_0.parquet")++ # Negative control: without allow_missing, the same file is rejected with the+ # same class of error that broke the prod backfill.+ with pytest.raises(duckdb.Error, match="captured_at"):+ conn.execute(+ f"CALL ducklake_add_data_files('{DUCKLAKE_ALIAS}', 'events', '{single_file}', schema => 'posthog')"+ )++ # The fix: register_files_with_duckling passes allow_missing => true.+ self._delete_day(conn)+ assert self._register(conn, file_glob) == 1+ assert self._count(conn) == 5++ # The missing column is NULL-filled for every backfilled row.+ cap_vals = [r[0] for r in conn.execute(f"SELECT captured_at FROM {DUCKLAKE_ALIAS}.posthog.events").fetchall()]+ assert cap_vals == [None] * 5