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): keep columns the arrow schema does not carry (#104459)

Tom Owers6e5255232e99
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_hogql_schema.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_hogql_schema.py
index a0b0c3103027..3e5aa7f87e6b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_hogql_schema.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_hogql_schema.py
@@ -176,22 +176,36 @@ class TestMergeColumnsRaceCondition:
assert result["user_id"] == {"clickhouse": "Int64", "hogql": "IntegerDatabaseField"}
- def test_merge_columns_skips_column_when_hogql_type_missing_from_schema(self):
- existing_columns = {
- "user_id": {"clickhouse": "String", "hogql": "StringDatabaseField"},
- }
- db_columns = {"user_id": "String", "new_col": "Int64"}
+ @parameterized.expand(
+ [
+ ("text_column", "Nullable(String)", {}, "StringDatabaseField"),
+ ("timestamp_column", "Nullable(DateTime64(6))", {}, "DateTimeDatabaseField"),
+ ("integer_column", "Int64", {}, "IntegerDatabaseField"),
+ ("boolean_bookkeeping_column", "Bool", {}, "BooleanDatabaseField"),
+ ("clickhouse_type_absent_from_the_mapping", "Ring", {}, "UnknownDatabaseField"),
+ (
+ "prior_json_typing_wins_over_the_fallback",
+ "Nullable(String)",
+ {"extra_col": {"clickhouse": "String", "hogql": "StringJSONDatabaseField"}},
+ "StringJSONDatabaseField",
+ ),
+ ]
+ )
+ def test_merge_columns_types_column_from_clickhouse_when_absent_from_schema(
+ self, _name, clickhouse_type, existing_columns, expected_hogql
+ ):
+ db_columns = {"user_id": "String", "extra_col": clickhouse_type}
table_schema = {"user_id": "StringDatabaseField"}
result = merge_columns(db_columns, table_schema, existing_columns)
- assert "user_id" in result
- assert "new_col" not in result
+ assert result["user_id"]["hogql"] == "StringDatabaseField"
+ assert result["extra_col"] == {"clickhouse": clickhouse_type, "hogql": expected_hogql}
def test_merge_columns_keeps_binary_column_registered_by_hogql_schema(self):
# A binary Arrow column (e.g. a BigQuery BYTES field) is read back by ClickHouse's
- # deltaLake() reader as String, so HogQLSchema must register a type for it or this
- # column is dropped from `columns` on every sync.
+ # deltaLake() reader as String, so HogQLSchema must map it to the same HogQL type
+ # the ClickHouse reader implies, or the two disagree on the column's type.
table = pa.table({"key": pa.array([b"abc"], type=pa.binary())})
schema = HogQLSchema()
schema.add_pyarrow_table(table)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py
index edfa02b829dd..4b9b8152788f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_sync.py
@@ -19,7 +19,6 @@ import dlt.extract.incremental.transform
from clickhouse_driver.errors import ServerException
from structlog.types import FilteringBoundLogger
-from posthog.exceptions_capture import capture_exception
from posthog.sync import database_sync_to_async_pool
from posthog.temporal.common.logger import get_logger
from posthog.temporal.common.utils import retry_on_db_connection_drop
@@ -31,6 +30,7 @@ from products.warehouse_sources.backend.models.external_data_schema import (
update_sync_type_config_keys,
)
from products.warehouse_sources.backend.models.table import DataWarehouseTable
+from products.warehouse_sources.backend.models.util import hogql_type_name_for_clickhouse_type
from products.warehouse_sources.backend.temporal.data_imports.naming_convention import NamingConvention
from products.warehouse_sources.backend.temporal.data_imports.pipelines.common.db_retry import (
retry_on_operational_error,
@@ -58,17 +58,21 @@ def merge_columns(
) -> dict[str, Any]:
"""Build column metadata, preserving StringJSONDatabaseField from prior runs.
+ db_columns comes from ClickHouse introspection of the published files, so it is the
+ authority on which columns exist and what each one holds. table_schema_dict only refines
+ that typing: ClickHouse reports a plain string column and a JSON string column both as
+ String, and the Arrow schema of the written data is the only place that distinction
+ survives. A column the Arrow schema never carried is therefore still a real column, so it
+ takes its type from ClickHouse. Do not skip such a column, because skipping it removes it
+ from the table metadata, and so from HogQL, on every sync.
+
Columns present in existing_columns but absent from db_columns are preserved
to avoid losing schema information when get_columns() returns incomplete
results during a sync (e.g., transient S3/ClickHouse introspection failures).
"""
columns: dict[str, Any] = {}
for column_name, db_column_type in db_columns.items():
- hogql_type = table_schema_dict.get(column_name)
-
- if hogql_type is None:
- capture_exception(Exception(f"HogQL type not found for column: {column_name}"))
- continue
+ hogql_type = table_schema_dict.get(column_name) or hogql_type_name_for_clickhouse_type(db_column_type)
existing_column = existing_columns.get(column_name)
existing_hogql_type = existing_column.get("hogql") if isinstance(existing_column, dict) else None