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(clickhouse): exclude ALIAS/EPHEMERAL columns from the sync query (#78762)

Tom Owers4cea7e44f9e4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
index 680454dea8af..a02ab458985d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/clickhouse.py
@@ -881,11 +881,16 @@ def _is_materialized_view_engine(engine: str | None) -> bool:
def _get_table(client: ClickHouseClient, database: str, table_name: str) -> Table[ClickHouseColumn]:
"""Read columns + table type for a single table from system tables."""
+ # Skip ALIAS and EPHEMERAL columns, matching `get_schemas`'s discovery query — see its
+ # comment for why: our `SELECT *` expands to an explicit column list, and an included
+ # ALIAS whose defining expression no longer resolves fails the whole sync query with
+ # UNKNOWN_IDENTIFIER (code 47), while EPHEMERAL columns aren't selectable at all.
cols_result = client.query(
"""
SELECT name, type
FROM system.columns
WHERE database = %(database)s AND table = %(table)s
+ AND default_kind NOT IN ('ALIAS', 'EPHEMERAL')
ORDER BY position ASC
""",
parameters={"database": database, "table": table_name},
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
index 1839638dbcd1..6c2809186c8d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
@@ -1070,6 +1070,28 @@ class TestGetSchemas:
assert set(schemas.keys()) == {"events"}
+class TestGetTable:
+ """Tests `_get_table`, used by the sync path to build the SELECT column list."""
+
+ def _make_mock_client(self, cols_rows, engine: str | None = "MergeTree"):
+ client = MagicMock()
+ cols_result = MagicMock()
+ cols_result.result_rows = cols_rows
+ engine_result = MagicMock()
+ engine_result.result_rows = [(engine,)] if engine is not None else []
+ client.query.side_effect = [cols_result, engine_result]
+ return client
+
+ def test_excludes_alias_and_ephemeral_columns_from_query(self):
+ from products.warehouse_sources.backend.temporal.data_imports.sources.clickhouse import clickhouse as ch_module
+
+ client = self._make_mock_client([("id", "UInt64")])
+ ch_module._get_table(client, "default", "events")
+
+ cols_query = client.query.call_args_list[0].args[0]
+ assert "default_kind NOT IN ('ALIAS', 'EPHEMERAL')" in cols_query
+
+
class TestSourceClassValidateCredentials:
"""High-level checks on validate_credentials error mapping."""