: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): disable hive-partitioning inference on warehouse table introspection queries (#74875)
Tom Owersbedb3e01b213
diff --git a/products/warehouse_sources/backend/models/table.py b/products/warehouse_sources/backend/models/table.pyindex 3bab09051b3a..45e5612da9a8 100644--- a/products/warehouse_sources/backend/models/table.py+++ b/products/warehouse_sources/backend/models/table.py@@ -106,6 +106,15 @@ HIDDEN_COLUMNS: frozenset[str] = frozenset({"_dlt_id", "_dlt_load_id", "_ph_debu# subprocess lets us kill it and degrade to the ClickHouse-cluster fallback.CHDB_QUERY_TIMEOUT_SECONDS = 30.0+# ClickHouse's Hive-style partition inference guesses a type per partition-folder value it+# samples (e.g. our internal `_ph_partition_key`), independently of the physical column type.+# A table whose partition granularity changed over time (see repartition.py's tiering from+# week -> hour) mixes value shapes across folders — e.g. an hour-tier "2017-06-30T05" next to+# older week-tier folders — and CH can misclassify the column as Date, then fail to parse it.+# HogQLGlobalSettings.use_hive_partitioning disables this for the normal HogQL query path; the+# raw ClickHouse queries below bypass that path and must opt out the same way.+DISABLE_HIVE_PARTITIONING_SETTINGS: dict[str, int] = {"use_hive_partitioning": 0}+_CHDB_SUBPROCESS_SCRIPT = """import sys@@ -440,7 +449,7 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletquoted_placeholders = {k: escape_param_clickhouse(v) for k, v in placeholder_context.values.items()}# chdb doesn't support parameterized queries- chdb_query = f"DESCRIBE TABLE {s3_table_func}" % quoted_placeholders+ chdb_query = f"SET use_hive_partitioning = 0; DESCRIBE TABLE {s3_table_func}" % quoted_placeholders# TODO: upgrade chdb once https://github.com/chdb-io/chdb/issues/342 is actually resolved# See https://github.com/chdb-io/chdb/pull/374 for the fix@@ -471,7 +480,7 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletattempts = 5for i in range(attempts):try:- get_columns_settings: dict[str, int] = {}+ get_columns_settings: dict[str, int] = dict(DISABLE_HIVE_PARTITIONING_SETTINGS)if self._is_csv_format() and self.csv_allow_double_quotes is not None:get_columns_settings["format_csv_allow_double_quotes"] = (1 if self.csv_allow_double_quotes else 0@@ -530,6 +539,7 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletresult = sync_execute(f"SELECT max({escape_clickhouse_identifier(column)}) FROM {s3_table_func}",args=placeholder_context.values,+ settings=DISABLE_HIVE_PARTITIONING_SETTINGS,)return result[0][0]@@ -563,7 +573,7 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletquoted_placeholders = {k: escape_param_clickhouse(v) for k, v in placeholder_context.values.items()}# chdb doesn't support parameterized queries- chdb_query = f"SELECT count() FROM {s3_table_func}" % quoted_placeholders+ chdb_query = f"SET use_hive_partitioning = 0; SELECT count() FROM {s3_table_func}" % quoted_placeholderschdb_result = run_chdb_query(chdb_query)reader = csv.reader(StringIO(chdb_result))@@ -584,6 +594,7 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletresult = sync_execute(f"SELECT count() FROM {s3_table_func}",args=placeholder_context.values,+ settings=DISABLE_HIVE_PARTITIONING_SETTINGS,)except Exception as err:capture_exception(err)@@ -893,7 +904,7 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletsync_execute(f"SELECT 1 FROM {func} LIMIT 100",args=ctx.values,- settings={"format_csv_allow_double_quotes": 1 if setting else 0},+ settings={**DISABLE_HIVE_PARTITIONING_SETTINGS, "format_csv_allow_double_quotes": 1 if setting else 0},)except ClickHouseServerException as e:if e.code in self._CSV_PARSE_ERROR_CODES:diff --git a/products/warehouse_sources/backend/tests/test_table.py b/products/warehouse_sources/backend/tests/test_table.pyindex ccfac1b19ad9..270c83e53755 100644--- a/products/warehouse_sources/backend/tests/test_table.py+++ b/products/warehouse_sources/backend/tests/test_table.py@@ -89,6 +89,35 @@ class TestDataWarehouseTableColumnOrder(BaseTest):assert table.column_order == ["z", "a"]+class TestWarehouseQueryDisablesHivePartitioning(BaseTest):+ # ClickHouse infers a type for each Hive-style partition-folder value it samples (e.g. our+ # internal `_ph_partition_key`) independently of the column's declared type. A table whose+ # partition granularity changed over time mixes value shapes across folders (e.g. an+ # hour-tier "2017-06-30T05" alongside older week-tier folders), and CH can misclassify the+ # column as Date and then fail to parse it — HogQLGlobalSettings disables this inference for+ # the normal HogQL query path, so these raw `sync_execute` calls must opt out the same way.+ def _table(self) -> DataWarehouseTable:+ return DataWarehouseTable(name="t", format="Delta", team=self.team, url_pattern="s3://bucket/team_1/t")++ def test_get_count_disables_hive_partitioning(self) -> None:+ with patch(+ "products.warehouse_sources.backend.models.table.sync_execute", return_value=[(5,)]+ ) as mock_sync_execute:+ count = self._table().get_count()++ assert count == 5+ assert mock_sync_execute.call_args.kwargs["settings"]["use_hive_partitioning"] == 0++ def test_get_max_value_for_column_disables_hive_partitioning(self) -> None:+ with patch(+ "products.warehouse_sources.backend.models.table.sync_execute", return_value=[(42,)]+ ) as mock_sync_execute:+ value = self._table().get_max_value_for_column("created_at")++ assert value == 42+ assert mock_sync_execute.call_args.kwargs["settings"]["use_hive_partitioning"] == 0++class TestSafeExposeChError:# ClickHouseAtCapacity is a DRF APIException with no `.message`, so the capacity check# must run before the message-matching loop — reordering them would reintroduce an