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): Expand the column (#58314)

Tom Owers9998cff71d0a
diff --git a/posthog/temporal/data_imports/sources/customer_io/source.py b/posthog/temporal/data_imports/sources/customer_io/source.py
index 74315b47cc56..7a54db793c05 100644
--- a/posthog/temporal/data_imports/sources/customer_io/source.py
+++ b/posthog/temporal/data_imports/sources/customer_io/source.py
@@ -1,6 +1,8 @@
from collections.abc import AsyncIterable, Iterable
from typing import TYPE_CHECKING, Any, Optional, cast
+import orjson
+import pyarrow as pa
from asgiref.sync import async_to_sync
from posthog.schema import (
@@ -14,6 +16,7 @@ from posthog.schema import (
)
from posthog.temporal.data_imports.pipelines.pipeline.typings import SourceInputs, SourceResponse
+from posthog.temporal.data_imports.pipelines.pipeline.utils import table_from_py_list
from posthog.temporal.data_imports.sources.common.base import (
ExternalWebhookInfo,
FieldType,
@@ -44,6 +47,32 @@ CIO_DOCS_WEBHOOKS_URL = "https://fly.customer.io/settings/webhooks/new/reporting
CIO_APP_API_KEY_URL = "https://fly.customer.io/settings/api_credentials?keyType=app"
+def _webhook_table_transformer(table: pa.Table) -> pa.Table:
+ if "data" not in table.column_names:
+ return table_from_py_list([])
+
+ data_col = table.column("data").to_pylist()
+ event_id_col = table.column("event_id").to_pylist() if "event_id" in table.column_names else [None] * table.num_rows
+ timestamp_col = (
+ table.column("timestamp").to_pylist() if "timestamp" in table.column_names else [None] * table.num_rows
+ )
+ metric_col = table.column("metric").to_pylist() if "metric" in table.column_names else [None] * table.num_rows
+
+ rows: list[dict[str, Any]] = []
+ for data, event_id, timestamp, metric in zip(data_col, event_id_col, timestamp_col, metric_col):
+ if data is None:
+ continue
+ # `data` typically arrives as a nested dict (pyarrow struct), but defensively
+ # handle the case where it's been serialized as a JSON string upstream.
+ row = orjson.loads(data) if isinstance(data, (str, bytes)) else dict(data)
+ row["event_id"] = event_id
+ row["timestamp"] = timestamp
+ row["metric"] = metric
+ rows.append(row)
+
+ return table_from_py_list(rows)
+
+
@SourceRegistry.register
class CustomerIOSource(
SimpleSource[CustomerIOSourceConfig],
@@ -231,7 +260,7 @@ class CustomerIOSource(
def items() -> Iterable[Any] | AsyncIterable[Any]:
if webhook_enabled:
- return webhook_source_manager.get_items()
+ return webhook_source_manager.get_items(table_transformer=_webhook_table_transformer)
return iter([])
return SourceResponse(
diff --git a/posthog/temporal/data_imports/sources/customer_io/tests/test_customer_io_source.py b/posthog/temporal/data_imports/sources/customer_io/tests/test_customer_io_source.py
index a83ac4f64e1d..cfb85fa1c052 100644
--- a/posthog/temporal/data_imports/sources/customer_io/tests/test_customer_io_source.py
+++ b/posthog/temporal/data_imports/sources/customer_io/tests/test_customer_io_source.py
@@ -3,9 +3,12 @@ from typing import Literal, cast
from unittest.mock import MagicMock, patch
+import pyarrow as pa
+
from posthog.schema import SourceFieldInputConfig, SourceFieldSelectConfig
from posthog.temporal.data_imports.pipelines.pipeline.typings import SourceResponse
+from posthog.temporal.data_imports.pipelines.pipeline.utils import table_from_py_list
from posthog.temporal.data_imports.sources.common.base import (
ExternalWebhookInfo,
WebhookCreationResult,
@@ -16,7 +19,7 @@ from posthog.temporal.data_imports.sources.customer_io.constants import (
CIO_WEBHOOK_SCHEMA_NAMES,
RESOURCE_TO_CIO_OBJECT_TYPE,
)
-from posthog.temporal.data_imports.sources.customer_io.source import CustomerIOSource
+from posthog.temporal.data_imports.sources.customer_io.source import CustomerIOSource, _webhook_table_transformer
from posthog.temporal.data_imports.sources.generated_configs import CustomerIOSourceConfig
@@ -294,3 +297,78 @@ class TestCustomerIOSourcePipelineDispatch:
schemas = source.get_schemas(_config(), team_id=1)
assert "messages" not in {s.name for s in schemas}
+
+
+class TestCustomerIOWebhookTableTransformer:
+ def test_lifts_data_fields_and_preserves_event_id_timestamp_and_metric(self):
+ table = table_from_py_list(
+ [
+ {
+ "event_id": "evt-1",
+ "timestamp": 1777655416,
+ "object_type": "email",
+ "metric": "sent",
+ "data": {
+ "recipient": "[email protected]",
+ "subject": "We don't want (all of) your money",
+ "journey_id": "32KE4CT86V53208000000000CF70",
+ },
+ }
+ ]
+ )
+
+ result = _webhook_table_transformer(table)
+ rows = result.to_pylist()
+
+ assert len(rows) == 1
+ assert rows[0]["event_id"] == "evt-1"
+ assert rows[0]["timestamp"] == 1777655416
+ assert rows[0]["metric"] == "sent"
+ assert rows[0]["recipient"] == "[email protected]"
+ assert rows[0]["subject"] == "We don't want (all of) your money"
+ assert rows[0]["journey_id"] == "32KE4CT86V53208000000000CF70"
+ # `object_type` is implicit in the schema name (e.g. `email_events`) and dropped.
+ assert "object_type" not in rows[0]
+
+ def test_handles_data_as_json_string(self):
+ # Defensive: if an upstream change serializes `data` as a JSON string instead
+ # of a nested struct, we still parse it correctly.
+ table = pa.table(
+ {
+ "event_id": ["evt-2"],
+ "timestamp": [1777655416],
+ "metric": ["opened"],
+ "data": ['{"recipient": "[email protected]"}'],
+ }
+ )
+
+ result = _webhook_table_transformer(table)
+ rows = result.to_pylist()
+
+ assert rows == [
+ {"event_id": "evt-2", "timestamp": 1777655416, "metric": "opened", "recipient": "[email protected]"}
+ ]
+
+ def test_skips_rows_with_null_data(self):
+ # A null `data` field would produce a row with no lifted columns — skip it
+ # rather than emit a sparse row that can't be partitioned reliably.
+ table = pa.table(
+ {
+ "event_id": ["evt-3", "evt-4"],
+ "timestamp": [1, 2],
+ "metric": ["sent", "clicked"],
+ "data": [None, {"recipient": "[email protected]"}],
+ }
+ )
+
+ result = _webhook_table_transformer(table)
+ rows = result.to_pylist()
+
+ assert rows == [{"event_id": "evt-4", "timestamp": 2, "metric": "clicked", "recipient": "[email protected]"}]
+
+ def test_returns_empty_when_data_column_missing(self):
+ table = pa.table({"event_id": ["evt-5"], "timestamp": [1]})
+
+ result = _webhook_table_transformer(table)
+
+ assert result.num_rows == 0