: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): classify warehouse table read failures before reporting them (#104469)
Tom Owersb02ae54a2513
diff --git a/products/warehouse_sources/backend/models/table.py b/products/warehouse_sources/backend/models/table.pyindex 74aff5043c1b..998c83365366 100644--- a/products/warehouse_sources/backend/models/table.py+++ b/products/warehouse_sources/backend/models/table.py@@ -100,8 +100,65 @@ ExtractErrors = {"deserialize thrift": CORRUPTED_PARQUET_METADATA_MESSAGE,"Rows have different amount of values": "The provided file has rows with different amount of values","The operation is not valid for the object's storage class": "Some files in the bucket are archived (e.g. Glacier or S3 Intelligent-Tiering archive). Restore them to Standard storage or narrow the URL pattern to exclude archived files.",+ # TOO_MANY_REDIRECTS, raised when the URL pattern points at something that answers with a+ # redirect (a website endpoint, a share link) instead of the object itself.+ "Too many redirects": "The files URL redirected too many times, so PostHog couldn't read it. Check that the URL points straight at your bucket rather than at a share or website link, then try again.",+ # UNKNOWN_IDENTIFIER. A column the table still expects is gone from the files, most often the+ # incremental field of a source whose upstream schema changed.+ "Unknown expression or function identifier": "A column PostHog expected isn't in your files any more. Refresh the table schema, then check the fields the sync is set to read.",}++def raw_error_message(err: Exception) -> str:+ """The engine's own message text, whichever engine raised it.++ ClickHouse errors carry it on `.message`, which `wrap_clickhouse_query_error` may rewrite, so+ the raw attribute is read before any wrapping. chdb runs out of process and `run_chdb_query`+ re-raises its stderr as a RuntimeError, so there the message is just `str(err)`.+ """+ if isinstance(err, ClickHouseServerException) and err.message is not None:+ return err.message+ return str(err)+++def classify_warehouse_read_error(err: Exception) -> str | None:+ """The user-facing message for a read failure that the customer's files, bucket or+ configuration caused, or None when nothing recognizes the error.++ Both engines read the same objects and report the same conditions, so one lookup over+ `ExtractErrors` serves the chdb path and the ClickHouse path. A None is what sends an error to+ tracking, so a needle belongs here only when the customer can act on the result.+ """+ message = raw_error_message(err)+ for needle, user_facing_message in ExtractErrors.items():+ if needle in message:+ return user_facing_message+ return None+++def is_expected_warehouse_read_error(err: Exception) -> bool:+ """Whether a read failure is already understood, so reporting it adds nothing.++ Two kinds qualify. The first is a cause the customer owns, which+ `classify_warehouse_read_error` turns into an actionable message that the caller surfaces+ instead. The second is a known-transient object-store blip, which the caller retries and which+ `TransientObjectStoreError` already keeps out of tracking at the Temporal boundary. An+ unrecognized error is neither, and must still reach error tracking so a real defect stays+ visible.+ """+ if classify_warehouse_read_error(err) is not None:+ return True++ # Deferred: pipelines.core.delta.errors pulls in posthog.temporal.common.errors ->+ # temporalio, which must stay off django.setup(), where this model loads in every process.+ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.errors import ( # noqa: PLC0415+ TRANSIENT_OBJECT_STORE_ERRORS,+ )++ message = raw_error_message(err)+ return any(needle in message for needle in TRANSIENT_OBJECT_STORE_ERRORS)++type DataWarehouseTableColumn = str | dict[str, Any]type DataWarehouseTableColumns = dict[str, DataWarehouseTableColumn]@@ -131,6 +188,11 @@ REJECTED_COLUMN_NAME_CHARACTERS: frozenset[str] = frozenset("`\\\r\n\0")# subprocess lets us kill it and degrade to the ClickHouse-cluster fallback.CHDB_QUERY_TIMEOUT_SECONDS = 30.0+# The table size at which build_function_call switches from s3() to s3Cluster(), restated here+# because it is not exported. A table at or above it is read across the cluster for a reason, so+# the single embedded chdb process has no chance of reading it inside the budget above.+S3_CLUSTER_TABLE_SIZE_MIB = 1024+# 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@@ -538,6 +600,10 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Delet# A source added a column mid-stream, so the parquet parts under one table disagree on# column count. ClickHouse reads the same files fine; only chdb refuses the mixed set."reading from files with different schema is not possible",+ # CHDB_QUERY_TIMEOUT_SECONDS is a deliberate budget, not a promise the read fits in it, so+ # a dataset that outgrows the budget reports the size of the customer's table rather than+ # a fault. get_count keeps the largest tables off chdb for the same reason.+ "chdb query timed out after",)# chdb 4 links delta-kernel only in its Linux wheels, so macOS dev boxes have no deltaLake().# On Linux the same error means the engine lost Delta support and has to reach error tracking.@@ -551,6 +617,19 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletreturn Truereturn any(substring in message for substring in self._SUPPRESSED_CHDB_ERROR_SUBSTRINGS)+ def _capture_unexpected_chdb_error(self, err: Exception) -> None:+ """Report a chdb failure only when the ClickHouse fallback does not already answer it.++ Both chdb call sites fall back to the cluster, so a failure the fallback absorbs costs the+ user nothing, and a failure the customer owns is surfaced by the fallback with an+ actionable message instead. Reporting either one buries the failures that need a person.+ An unrecognized failure still reports, which is what keeps a chdb-side regression visible.+ """+ if self._is_suppressed_chdb_error(err) or is_expected_warehouse_read_error(err):+ structlog.get_logger(__name__).debug("chdb read failed, reading through ClickHouse", exc_info=err)+ return+ capture_exception(err)+def set_columns(self, columns: dict[str, Any]) -> None:"""Assign ``columns`` and record its order together.@@ -596,7 +675,6 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletcontext=placeholder_context,table_size_mib=0, # Use the non-cluster s3 table function for chdb)- logger = structlog.get_logger(__name__)try:# chdb hangs in CI during testsif TEST:@@ -609,10 +687,7 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletreader = csv.reader(StringIO(chdb_result))result = [tuple(row) for row in reader]except Exception as chdb_error:- if self._is_suppressed_chdb_error(chdb_error):- logger.debug(chdb_error)- else:- capture_exception(chdb_error)+ self._capture_unexpected_chdb_error(chdb_error)tag_queries(team_id=self.team.pk,@@ -636,7 +711,8 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletbreakexcept Exception as err:if i >= attempts - 1:- capture_exception(err)+ if not is_expected_warehouse_read_error(err):+ capture_exception(err)if safe_expose_ch_error:self._safe_expose_ch_error(err)else:@@ -698,14 +774,29 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Delet# has no non-empty/readable files for the configured format (e.g. before the# first successful sync). The caller handles a None return by resetting and# triggering a refresh.- if err.code != 636:+ if err.code != 636 and not is_expected_warehouse_read_error(err):capture_exception(err)return Noneexcept Exception as err:- capture_exception(err)+ if not is_expected_warehouse_read_error(err):+ capture_exception(err)return None- def get_count(self, safe_expose_ch_error=True) -> int:+ def _count_fits_the_chdb_budget(self) -> bool:+ """Whether counting this table is worth one attempt inside run_chdb_query's budget.++ A count reads the whole dataset, and a table at or above the s3Cluster threshold cannot be+ read that way by one embedded process inside a fixed budget. Trying anyway spends the whole+ budget on every count of a large table before the cluster does the work regardless, so a+ table whose recorded size is already over the threshold goes straight to ClickHouse.+ A table with no recorded size counts as small, because that is what a table holds before+ its first size calculation.+ """+ return self.size_in_s3_mib is None or self.size_in_s3_mib < S3_CLUSTER_TABLE_SIZE_MIB++ def _count_with_chdb(self) -> int | None:+ """The row count read by the embedded engine, or None when it cannot answer and ClickHouse+ has to."""placeholder_context = HogQLContext(team_id=self.team.pk)s3_table_func = build_function_call(url=self.url_pattern,@@ -717,41 +808,62 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Delettable_size_mib=0, # Use the non-cluster s3 table function for chdb)try:- # chdb hangs in CI during tests- if TEST:- raise Exception()-quoted_placeholders = {k: escape_param_clickhouse(v) for k, v in placeholder_context.values.items()}# chdb doesn't support parameterized querieschdb_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))- result = [tuple(row) for row in reader]+ rows = [tuple(row) for row in reader]except Exception as chdb_error:- capture_exception(chdb_error)-- try:- tag_queries(- team_id=self.team.pk,- table_id=self.id,- warehouse_query=True,- name="get_count",- product=Product.WAREHOUSE,- feature=Feature.QUERY,- )+ self._capture_unexpected_chdb_error(chdb_error)+ return None- result = sync_execute(- f"SELECT count() FROM {s3_table_func}",- args=placeholder_context.values,- settings=DISABLE_HIVE_PARTITIONING_SETTINGS,- )- except Exception as err:+ # An empty read carries no count, so ClickHouse answers instead of an IndexError on the+ # row that is not there.+ return int(rows[0][0]) if rows else None++ def get_count(self, safe_expose_ch_error=True) -> int:+ # chdb hangs in CI during tests+ if not TEST and self._count_fits_the_chdb_budget():+ chdb_count = self._count_with_chdb()+ if chdb_count is not None:+ return chdb_count++ placeholder_context = HogQLContext(team_id=self.team.pk)+ s3_table_func = build_function_call(+ url=self.url_pattern,+ queryable_folder=self.queryable_folder,+ format=self.format,+ access_key=self.credential.access_key if self.credential else None,+ access_secret=self.credential.access_secret if self.credential else None,+ context=placeholder_context,+ # The real size, so a table too large for one node is counted across the cluster+ # instead of on the node chdb already could not finish on.+ table_size_mib=self.size_in_s3_mib,+ )+ try:+ tag_queries(+ team_id=self.team.pk,+ table_id=self.id,+ warehouse_query=True,+ name="get_count",+ product=Product.WAREHOUSE,+ feature=Feature.QUERY,+ )++ result = sync_execute(+ f"SELECT count() FROM {s3_table_func}",+ args=placeholder_context.values,+ settings=DISABLE_HIVE_PARTITIONING_SETTINGS,+ )+ except Exception as err:+ if not is_expected_warehouse_read_error(err):capture_exception(err)- if safe_expose_ch_error:- self._safe_expose_ch_error(err)- else:- raise+ if safe_expose_ch_error:+ self._safe_expose_ch_error(err)+ else:+ raisereturn int(result[0][0])@@ -1170,10 +1282,10 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Delet)def _safe_expose_ch_error(self, err):- # Match ExtractErrors against the raw ClickHouse message: wrap_clickhouse_query_error may- # rewrite the message for some codes (e.g. STD_EXCEPTION), which would hide the substrings- # we key on here.- raw_message = err.message if isinstance(err, ClickHouseServerException) else str(err)+ # Classify against the raw ClickHouse message: wrap_clickhouse_query_error may rewrite the+ # message for some codes (e.g. STD_EXCEPTION), which would hide the substrings we key on.+ raw_message = raw_error_message(err)+ user_facing_message = classify_warehouse_read_error(err)err = wrap_clickhouse_query_error(err)# Only ClickHouse ServerException-derived errors carry a `.message`. Everything else —@@ -1206,9 +1318,8 @@ class DataWarehouseTable(CreatedMetaFields, UpdatedMetaFields, UUIDTModel, Deletif any(needle in raw_message for needle in TRANSIENT_OBJECT_STORE_ERRORS):raise TransientObjectStoreError(raw_message)- for key, value in ExtractErrors.items():- if key in raw_message:- raise Exception(value)+ if user_facing_message is not None:+ raise Exception(user_facing_message)raise Exception("Could not read the files from your storage bucket. Check that the files URL pattern, file format, "diff --git a/products/warehouse_sources/backend/tests/test_table.py b/products/warehouse_sources/backend/tests/test_table.pyindex df8f7f65374e..0f112bc2d689 100644--- a/products/warehouse_sources/backend/tests/test_table.py+++ b/products/warehouse_sources/backend/tests/test_table.py@@ -18,11 +18,13 @@ from posthog.hogql.escape_sql import escape_param_clickhousefrom posthog.clickhouse.client import sync_executefrom posthog.exceptions import ClickHouseAtCapacity+from posthog.models import Teamfrom products.warehouse_sources.backend.models.credential import DataWarehouseCredentialfrom products.warehouse_sources.backend.models.external_data_source import ExternalDataSourcefrom products.warehouse_sources.backend.models.table import (DataWarehouseTable,+ classify_warehouse_read_error,get_hogql_field_for_column,run_chdb_query,)@@ -202,6 +204,143 @@ class TestSafeExposeChError:DataWarehouseTable()._safe_expose_ch_error(delta_kernel_error)+class TestWarehouseReadErrorClassification:+ # Engine wording, written from the shape of each failure rather than copied from a customer's+ # table: a bucket that refuses the read, a parquet footer the reader can't parse, a URL that+ # answers with a redirect, and a column that left the files. The needles are what the engines+ # emit, so a rephrasing on either side has to fail here instead of silently dropping the error+ # back into error tracking as an unactionable ClickHouse string.+ @pytest.mark.parametrize(+ "engine_error,expected_fragment",+ [+ (+ RuntimeError(+ "Code: 1001. DB::Exception: parquet::ParquetException: Couldn't deserialize thrift: "+ "TProtocolException: Exceeded size limit. (STD_EXCEPTION)"+ ),+ "corrupted or oversized metadata",+ ),+ (+ ServerException(+ "DB::Exception: Received DeltaLake kernel error ObjectStoreError: The operation lacked "+ "the necessary privileges to complete for path team_0_source_0/table_0/_delta_log",+ code=742,+ ),+ "Access was denied when reading the provided file",+ ),+ (+ ServerException(+ "DB::Exception: Too many redirects: The table structure cannot be extracted from a "+ "Parquet format file.",+ code=230,+ ),+ "redirected too many times",+ ),+ (+ ServerException(+ "DB::Exception: Unknown expression or function identifier `updated_at` in scope "+ "SELECT max(updated_at) FROM s3('https://example.com/exports/**.parquet')",+ code=47,+ ),+ "isn't in your files any more",+ ),+ (+ ServerException("DB::Exception: A failure shape nobody has classified yet.", code=999),+ None,+ ),+ ],+ )+ def test_recurring_engine_errors_map_to_an_actionable_message(+ self, engine_error: Exception, expected_fragment: str | None+ ) -> None:+ classified = classify_warehouse_read_error(engine_error)++ if expected_fragment is None:+ assert classified is None+ else:+ assert classified is not None and expected_fragment in classified+++class TestChdbFailureReporting:+ # Both chdb call sites fall back to ClickHouse, so a chdb failure that the fallback answers+ # must not reach error tracking. Each of these used to mint one issue per read, which is what+ # buried the failures that need a person.+ @pytest.mark.parametrize(+ "chdb_error,reported",+ [+ (RuntimeError("chdb query timed out after 30.0s"), False),+ (+ RuntimeError(+ "Code: 48. DB::Exception: Reading from files with different schema is not possible. "+ "The table has 7 columns, which is different from 8 columns in the file. (NOT_IMPLEMENTED)"+ ),+ False,+ ),+ (+ RuntimeError(+ "Code: 742. DB::Exception: Received DeltaLake kernel error ObjectStoreError: The "+ "operation lacked the necessary privileges to complete. (DELTA_KERNEL_ERROR)"+ ),+ False,+ ),+ (+ RuntimeError(+ "Code: 1001. DB::Exception: parquet::ParquetException: Couldn't deserialize thrift: "+ "TProtocolException: Exceeded size limit. (STD_EXCEPTION)"+ ),+ False,+ ),+ (RuntimeError("Code: 999. DB::Exception: A failure shape nobody has classified yet."), True),+ ],+ )+ def test_only_unrecognized_chdb_failures_reach_error_tracking(self, chdb_error: Exception, reported: bool) -> None:+ with patch("products.warehouse_sources.backend.models.table.capture_exception") as mock_capture:+ DataWarehouseTable()._capture_unexpected_chdb_error(chdb_error)++ assert mock_capture.called is reported+++class TestGetCountEngineChoice:+ def _table(self, size_in_s3_mib: float | None) -> DataWarehouseTable:+ return DataWarehouseTable(+ name="t",+ format=DataWarehouseTable.TableFormat.DeltaS3Wrapper,+ team=Team(id=1),+ url_pattern="s3://bucket/team_0/t/",+ size_in_s3_mib=size_in_s3_mib,+ )++ @pytest.mark.parametrize(+ "size_in_s3_mib,uses_chdb",+ [+ (None, True),+ (10.0, True),+ (4096.0, False),+ ],+ )+ def test_chdb_is_skipped_for_a_table_it_cannot_count_in_its_budget(+ self, size_in_s3_mib: float | None, uses_chdb: bool+ ) -> None:+ # A count reads the whole dataset, so a table already big enough for s3Cluster never+ # finishes inside run_chdb_query's budget. Attempting it anyway added the full budget to+ # every count of a large table before ClickHouse did the work regardless.+ with (+ patch("products.warehouse_sources.backend.models.table.TEST", False),+ patch(+ "products.warehouse_sources.backend.models.table.run_chdb_query", return_value="7\n"+ ) as mock_run_chdb,+ patch(+ "products.warehouse_sources.backend.models.table.sync_execute", return_value=[(7,)]+ ) as mock_sync_execute,+ ):+ count = self._table(size_in_s3_mib).get_count()++ assert count == 7+ assert mock_run_chdb.called is uses_chdb+ if not uses_chdb:+ assert "s3Cluster(" in mock_sync_execute.call_args.args[0]++class TestRunChdbQuery:def test_hung_query_is_killed_and_raises_instead_of_blocking(self) -> None:# Real subprocess: chdb import alone exceeds the timeout, so this exercises the