: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-warehouse): keep synced clickhouse sources on s3-backed tables (#74193)
Jovan Sakovicafc6d29fb6f9
diff --git a/products/warehouse_sources/backend/models/table.py b/products/warehouse_sources/backend/models/table.pyindex fb4184c25030..20d092c489c0 100644--- a/products/warehouse_sources/backend/models/table.py+++ b/products/warehouse_sources/backend/models/table.py@@ -750,8 +750,15 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletconnection_metadata=self.external_data_source.connection_metadata,)- # Engine-keyed (no is_direct_clickhouse) to satisfy the source-agnostic guard.- if self.external_data_source and self.external_data_source.direct_engine == "clickhouse":+ # Engine-keyed (no is_direct_clickhouse) to satisfy the source-agnostic guard. The+ # is_direct_query check is load-bearing: direct_engine ignores access_method, and a synced+ # source's tables must stay S3-backed — as a direct table, every ordinary query against+ # them fails (the printer's team_id guard doesn't skip DirectSQLTable).+ if (+ self.external_data_source+ and self.external_data_source.is_direct_query+ and self.external_data_source.direct_engine == "clickhouse"+ ):job_inputs = self.external_data_source.job_inputs or {}clickhouse_database = (self.options.get(DIRECT_CLICKHOUSE_DATABASE_OPTION)diff --git a/products/warehouse_sources/backend/tests/test_table.py b/products/warehouse_sources/backend/tests/test_table.pyindex 0da80a283aa6..77e5675cad66 100644--- a/products/warehouse_sources/backend/tests/test_table.py+++ b/products/warehouse_sources/backend/tests/test_table.py@@ -10,15 +10,55 @@ from django.test import SimpleTestCasefrom clickhouse_driver.errors import ServerExceptionfrom parameterized import parameterized+from posthog.hogql.database.direct_clickhouse_table import DirectClickHouseTablefrom posthog.hogql.database.models import DatabaseField, StringDatabaseField, UUIDDatabaseField+from posthog.hogql.database.s3_table import DataWarehouseTable as HogQLDataWarehouseTablefrom posthog.exceptions import ClickHouseAtCapacity+from products.warehouse_sources.backend.models.external_data_source import ExternalDataSourcefrom products.warehouse_sources.backend.models.table import (DataWarehouseTable,get_hogql_field_for_column,run_chdb_query,)+from products.warehouse_sources.backend.types import ExternalDataSourceType+++class TestHogQLDefinitionDirectDispatch(BaseTest):+ @parameterized.expand(+ [+ ("synced_clickhouse", ExternalDataSourceType.CLICKHOUSE, "warehouse", HogQLDataWarehouseTable),+ ("synced_clickhouse_cloud", ExternalDataSourceType.CLICKHOUSECLOUD, "warehouse", HogQLDataWarehouseTable),+ ("direct_clickhouse", ExternalDataSourceType.CLICKHOUSE, "direct", DirectClickHouseTable),+ ]+ )+ def test_clickhouse_table_class_respects_access_method(+ self,+ _name: str,+ source_type: str,+ access_method: str,+ expected_class: type,+ ) -> None:+ # A synced ClickHouse source's tables must stay S3-backed: a DirectSQLTable is+ # excluded from the printer's team_id-guard skip list, so resolving a synced table+ # as direct makes every ordinary query against it fail.+ source = ExternalDataSource(+ team=self.team,+ source_type=source_type,+ access_method=access_method,+ job_inputs={"database": "analytics"},+ )+ table = DataWarehouseTable(+ name="external_events",+ format="Parquet",+ team=self.team,+ url_pattern="s3://bucket/team_1/external_events",+ external_data_source=source,+ columns={"id": {"hogql": "StringDatabaseField", "clickhouse": "String", "valid": True}},+ )++ assert type(table.hogql_definition()) is expected_classclass TestDataWarehouseTableColumnOrder(BaseTest):