Skip to content

: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.

fix(data-imports): raise a clear error instead of an empty delta merge predicate (#82426)

Tom Owers6bef51b0b868
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_writer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_writer.py
index 6d292485b603..022d180c1072 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_writer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_writer.py
@@ -13,6 +13,7 @@ import pyarrow.compute as pc
from parameterized import parameterized
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arrow_utils import (
+ MissingPrimaryKeysException,
SchemaColumnTypeChangedException,
evolve_pyarrow_schema,
first_per_pk_table,
@@ -338,6 +339,26 @@ class TestLegacyDltTableReconciliation:
assert final.num_rows == 3
assert set(final.column("id").to_pylist()) == {1, 2, 3}
+ @pytest.mark.asyncio
+ async def test_incremental_merge_raises_when_primary_key_column_missing_from_batch(self, tmp_path: Path) -> None:
+ """A configured primary key that no longer matches any column in the batch (e.g. a stale
+ persisted key name after the source's schema changed) must fail clearly instead of building
+ an empty merge predicate — delta-rs rejects an empty predicate with an opaque
+ "sql parser error: Expected: an expression, found: EOF" DeltaError."""
+ delta_path = str(tmp_path / "table")
+ deltalake.write_deltalake(delta_path, pa.table({"id": [1, 2], "name": ["a", "b"]}))
+
+ helper = make_local_table_ref(delta_path)
+ batch = pa.table({"name": ["c"]})
+
+ with pytest.raises(MissingPrimaryKeysException):
+ await DeltaWriter(helper).write(
+ data=batch,
+ write_type="incremental",
+ should_overwrite_table=False,
+ primary_keys=["id"],
+ )
+
class TestAppendDecimalReconciliation:
"""Appending a decimal column that outgrew decimal128 must reconcile to the stored type.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/writer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/writer.py
index 49a29f25b687..fe96ae186840 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/writer.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/writer.py
@@ -14,6 +14,7 @@ from posthog.exceptions_capture import capture_exception
from posthog.sync import database_sync_to_async_pool
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arrow_utils import (
+ MISSING_PRIMARY_KEYS_ERROR,
MissingPrimaryKeysException,
align_incoming_decimals_to_delta,
first_per_pk_table,
@@ -356,6 +357,18 @@ class DeltaWriter:
if n in py_table_column_names:
normalized_primary_keys.append(n)
+ if not normalized_primary_keys:
+ # None of the configured primary key columns survived into this batch (e.g. a stale
+ # persisted key name that no longer matches the source's columns). Left unguarded, the
+ # unpartitioned path below joins an empty predicate_ops into "" and hands delta-rs an
+ # empty predicate, which its SQL parser rejects with an opaque "Expected: an expression,
+ # found: EOF" — and the partitioned path would merge on partition alone, matching rows
+ # that were never actually the same record. Fail clearly instead of either.
+ raise MissingPrimaryKeysException(
+ f"{MISSING_PRIMARY_KEYS_ERROR}: none of {list(primary_keys)!r} were found in the "
+ f"synced data (columns: {py_table_column_names!r})"
+ )
+
predicate_ops = _merge_predicate_ops(normalized_primary_keys)
# Phase 2 canary: try deltalite for the real merge. On success the delta-rs MERGE (and the