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-warehouse): survive pyarrow string_view in v3 cdc delete enrichment (#71003)

estefania718f5e999bc2
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
index 0b2756ee932f..fddb5010f0b1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
@@ -68,6 +68,29 @@ def _get_write_type(sync_type: SyncTypeLiteral) -> Literal["incremental", "full_
return "full_refresh"
+def _read_existing_rows_by_first_pk(
+ existing_delta_table: deltalake.DeltaTable,
+ first_pk: str,
+ first_components: list[Any],
+) -> pa.Table:
+ """Read the existing rows whose `first_pk` value is in `first_components`.
+
+ Prefers Delta filter pushdown (prunes files, cheap). pyarrow >= 21 materializes
+ string columns as `string_view`, whose `equal`/`greater_equal` compute kernels are
+ unimplemented, so pushing an `in` predicate down onto a string primary key raises
+ `ArrowNotImplementedError`. When that happens, read the table and filter in PyArrow
+ after casting the key to `string`, which has working kernels.
+ """
+ try:
+ return existing_delta_table.to_pyarrow_table(filters=[(first_pk, "in", first_components)])
+ except pa.lib.ArrowNotImplementedError:
+ logger.warning("cdc_delete_enrichment_pushdown_fallback", primary_key=first_pk)
+ existing = existing_delta_table.to_pyarrow_table()
+ key = existing.column(first_pk).cast(pa.string())
+ wanted = pa.array(first_components, pa.string())
+ return existing.filter(pc.is_in(key, value_set=wanted))
+
+
def _apply_partitioning(
export_signal: ExportSignalMessage,
pa_table: pa.Table,
@@ -512,8 +535,8 @@ def process_message(
# For composite PKs that IN is a superset — narrow in PyArrow below.
first_pk = present_pks[0]
first_components = list({t[0] for t in delete_key_set})
- existing_rows = existing_delta_table.to_pyarrow_table(
- filters=[(first_pk, "in", first_components)]
+ existing_rows = _read_existing_rows_by_first_pk(
+ existing_delta_table, first_pk, first_components
)
# For composite PKs the IN filter is a superset — narrow to exact matches.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_processor.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_processor.py
index 50cffefb9023..0c2c4899cf2e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_processor.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/test_processor.py
@@ -1,9 +1,11 @@
+import tempfile
from typing import Any
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
import pyarrow as pa
+from deltalake import DeltaTable, write_deltalake
from parameterized import parameterized
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.consts import PARTITION_KEY
@@ -12,6 +14,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
_get_write_type,
_mark_job_completed,
_promote_staged_cursor,
+ _read_existing_rows_by_first_pk,
process_message,
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.test_mocks import mock_delta_table
@@ -338,3 +341,40 @@ class TestMarkJobCompleted:
mock_finish_row_tracking.assert_not_awaited()
# The pipeline lock must be released either way, or the next sync is blocked.
mock_release.assert_called_once_with(team_id=1, schema_id="schema-1", token="wf-run-1")
+
+
+# Regression guard for #70476: pyarrow 21+ string_view broke delta pushdown on string PKs.
+class TestReadExistingRowsByFirstPk:
+ @staticmethod
+ def _string_view_table(path: str) -> DeltaTable:
+ sv = pa.string_view()
+ write_deltalake(
+ path,
+ pa.table({"id": pa.array(["a", "b", "c"], sv), "val": pa.array([1, 2, 3], pa.int64())}),
+ mode="overwrite",
+ )
+ # Second file so file pruning is exercised.
+ write_deltalake(
+ path,
+ pa.table({"id": pa.array(["d", "e", "f"], sv), "val": pa.array([4, 5, 6], pa.int64())}),
+ mode="append",
+ )
+ return DeltaTable(path)
+
+ @parameterized.expand(
+ [
+ ("subset_across_files", ["a", "e"], ["a", "e"]),
+ ("single", ["c"], ["c"]),
+ ("superset_with_miss", ["b", "zzz"], ["b"]),
+ ("no_match", ["nope"], []),
+ ]
+ )
+ def test_returns_matching_rows_on_string_view_pk(
+ self, _name: str, components: list[str], expected_ids: list[str]
+ ) -> None:
+ with tempfile.TemporaryDirectory() as path:
+ delta_table = self._string_view_table(path)
+
+ result = _read_existing_rows_by_first_pk(delta_table, "id", components)
+
+ assert set(result.column("id").to_pylist()) == set(expected_ids)