: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(data-imports): merge webhook files with divergent column types (#86884)
Tom Owerse6a77cb151bf
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_webhook_s3.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_webhook_s3.pyindex d992b0422af3..028015a2ed02 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_webhook_s3.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_webhook_s3.py@@ -383,6 +383,42 @@ class TestWebhookSourceManager:# Only the successfully read file is removed; the vanished one was never touched.mock_s3._rm.assert_awaited_once_with("bucket/file.parquet")+ async def test_get_items_merges_files_with_incompatible_column_types(self):+ schema_id = "test-schema"+ manager = _make_manager(team_id=1, schema_id=schema_id)++ # Same field infers as int64 in one file and string in the other; concat_tables can't+ # promote across that, so get_items must fall back to a row-level rebuild instead of raising.+ file_a = _make_webhook_parquet_bytes([{"id": "1", "total_sales": 5}], team_id=1, schema_id=schema_id)+ file_b = _make_webhook_parquet_bytes([{"id": "2", "total_sales": "3"}], team_id=1, schema_id=schema_id)++ mock_s3 = AsyncMock()+ opens = []+ for parquet_bytes in (file_a, file_b):+ mock_file = AsyncMock()+ mock_file.read = AsyncMock(return_value=parquet_bytes)+ open_cm = AsyncMock()+ open_cm.__aenter__ = AsyncMock(return_value=mock_file)+ open_cm.__aexit__ = AsyncMock(return_value=False)+ opens.append(open_cm)+ mock_s3.open_async = AsyncMock(side_effect=opens)+ mock_s3._rm = AsyncMock()++ with (+ patch.object(+ manager,+ "_list_webhook_parquet_files",+ return_value=["s3://bucket/a.parquet", "s3://bucket/b.parquet"],+ ),+ _mock_s3_context(mock_s3),+ ):+ tables = [table async for table in manager.get_items()]++ assert len(tables) == 1+ assert tables[0].num_rows == 2+ assert pa.types.is_string(tables[0].schema.field("total_sales").type)+ assert sorted(id for id in tables[0].column("id").to_pylist() if id is not None) == ["1", "2"]+async def test_get_items_yields_nothing_when_no_files(self):manager = _make_manager()diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.pyindex cebaf8bd0ab4..44f1a653895b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/webhook_s3.py@@ -138,10 +138,19 @@ class WebhookSourceManager:await self._logger.adebug(f"Webhook source reading {len(files)} files")def finalize_batch(tables: list[pa.Table]) -> pa.Table:- # Dedupe across the whole concatenated batch, not per file: a yielded batch can span- # several S3 files, and the same id (e.g. a run's queued/completed events) can land in- # different files. A per-file pass would let both survive into one batch.- merged = pa.concat_tables(tables, promote_options="permissive")+ # Concatenate the whole batch, not per file: a yielded batch can span several S3 files,+ # and the same id (e.g. a run's queued/completed events) can land in different files, so+ # the downstream transformer must see all of them together to dedupe across the batch.+ try:+ merged = pa.concat_tables(tables, promote_options="permissive")+ except (pa.ArrowTypeError, pa.ArrowInvalid):+ # Each file's table is typed independently, so one payload field can infer as+ # different, non-promotable types across files (e.g. a number that arrives quoted in+ # one delivery and bare in another — string vs int64), which concat can't reconcile.+ # Rebuild through the shared row-to-table path, which resolves a column's mixed types+ # the same way a single multi-typed file already does.+ rows = [row for table in tables for row in table.to_pylist()]+ merged = table_from_py_list(rows)return table_transformer(merged) if table_transformer else mergedbatch_tables: list[pa.Table] = []