: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(clickhouse): Raise non-retryable error on unknown table (#125348)
Tomás Farías Santana6e794142fcc6
diff --git a/posthog/temporal/common/clickhouse.py b/posthog/temporal/common/clickhouse.pyindex 3e0351e00607..a5d1498e93ee 100644--- a/posthog/temporal/common/clickhouse.py+++ b/posthog/temporal/common/clickhouse.py@@ -265,6 +265,14 @@ class ClickHouseCheckQueryStatusError(ClickHouseError):super().__init__(error_message, query_id=query_id)+class ClickHouseUnknownTableError(ClickHouseError):+ """Exception raised when a table does not exist.++ This should only happen in HogQL batch exports, when users try to select tables that+ batch exports cannot reach.+ """++def update_query_tags_with_temporal_info(query_tags: typing.Optional[QueryTags] = None):"""Updates query_tags with a temporal workflow's properties.@@ -441,6 +449,7 @@ class ClickHouseClient:"TOO_MANY_BYTES": ClickHouseTooManyBytesError,"TOO_MANY_SIMULTANEOUS_QUERIES": ClickHouseTooManySimultaneousQueriesError,"TIMEOUT_EXCEEDED": ClickHouseQueryTimeoutError,+ "UNKNOWN_TABLE": ClickHouseUnknownTableError,}for error_code, exc_class in ERROR_CODE_TO_EXCEPTION.items():if error_code in error_message:diff --git a/posthog/temporal/tests/test_clickhouse.py b/posthog/temporal/tests/test_clickhouse.pyindex b9a7a3414a43..f8cc6bd912e4 100644--- a/posthog/temporal/tests/test_clickhouse.py+++ b/posthog/temporal/tests/test_clickhouse.py@@ -23,6 +23,7 @@ from posthog.temporal.common.clickhouse import (ClickHouseQueryTimeoutError,ClickHouseTooManyBytesError,ClickHouseTooManySimultaneousQueriesError,+ ClickHouseUnknownTableError,add_log_comment_param,encode_clickhouse_data,)@@ -192,6 +193,10 @@ def _mock_internal_session_post(return_value):"Code: 279. DB::Exception: All replicas are stale: While executing Remote. (ALL_REPLICAS_ARE_STALE) (version x.x.x.x (official build))",ClickHouseAllReplicasAreStaleError,),+ (+ "Code: 60. DB::Exception: Table posthog.missing_table does not exist. (UNKNOWN_TABLE)",+ ClickHouseUnknownTableError,+ ),],ids=["MEMORY_LIMIT_EXCEEDED",@@ -199,6 +204,7 @@ def _mock_internal_session_post(return_value):"TOO_MANY_SIMULTANEOUS_QUERIES","TIMEOUT_EXCEEDED","ALL_REPLICAS_ARE_STALE",+ "UNKNOWN_TABLE",],)def test_clickhouse_error_code_maps_to_exception(clickhouse_client, error_text, expected_exception):diff --git a/products/batch_exports/backend/temporal/pipeline/internal_stage.py b/products/batch_exports/backend/temporal/pipeline/internal_stage.pyindex b1a2e0b966c7..b573473db738 100644--- a/products/batch_exports/backend/temporal/pipeline/internal_stage.py+++ b/products/batch_exports/backend/temporal/pipeline/internal_stage.py@@ -40,6 +40,7 @@ from posthog.temporal.common.clickhouse import (ClickHouseQueryStatus,ClickHouseQueryTimeoutError,ClickHouseTooManyBytesError,+ ClickHouseUnknownTableError,get_client,)from posthog.temporal.common.heartbeat import Heartbeater@@ -105,6 +106,10 @@ class HogQLQueryResourceLimitExceededError(Exception):"""+class HogQLTableNotSupportedError(Exception):+ """A user tried to select a table with HogQL that batch exports does not support."""++# Staging failures the user has to resolve; retrying them ourselves would achieve nothing.# The activity returns these as an `InternalStageResult.error` instead of raising, which fails the# run without failing the activity. This mirrors how the destination activities treat their own@@ -112,6 +117,7 @@ class HogQLQueryResourceLimitExceededError(Exception):NON_RETRYABLE_ERRORS: tuple[type[Exception], ...] = (DataIntervalInFutureError,HogQLQueryResourceLimitExceededError,+ HogQLTableNotSupportedError,InvalidFilterError,UnsupportedHogQLQueryError,)@@ -461,6 +467,19 @@ async def _stage_query_results(num_partitions=num_partitions,run_id=run_id,)+ except ClickHouseUnknownTableError as e:+ if isinstance(record_batch_model, HogQLQueryRecordBatchModel):+ # With HogQL, users may need to solve this, so we raise a more friendly+ # but unretryable version.+ table_names = ", ".join(record_batch_model.hogql_table_names)+ raise HogQLTableNotSupportedError(+ "Batch exports could not find a required table. "+ f"Tables referenced in your query: {table_names}. "+ "Contact support for help."+ ) from e+ # If not HogQL, this should never happen.+ # Raise retryable so we can figure out what's going on.+ raiseexcept ClickHouseError as e:_raise_on_hogql_resource_limit_error(e, model_name)raisediff --git a/products/batch_exports/backend/temporal/record_batch_model.py b/products/batch_exports/backend/temporal/record_batch_model.pyindex 68a81deee377..026bd38dac04 100644--- a/products/batch_exports/backend/temporal/record_batch_model.py+++ b/products/batch_exports/backend/temporal/record_batch_model.py@@ -9,6 +9,7 @@ from posthog.hogql.constants import HogQLQuerySettingsfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.errors import ExposedHogQLErrorfrom posthog.hogql.hogql import ast+from posthog.hogql.metadata import get_table_namesfrom posthog.hogql.parser import parse_expr, parse_selectfrom posthog.hogql.printer import prepare_ast_for_printing, print_prepared_astfrom posthog.hogql.visitor import clone_expr@@ -393,6 +394,7 @@ class HogQLQueryRecordBatchModel(RecordBatchModel):self.hogql_query = hogql_queryself.hogql_modifiers = load_hogql_modifiers(hogql_modifiers)self.parsed_hogql_query = parse_hogql_select_for_batch_export(hogql_query)+ self.hogql_table_names = tuple(sorted(get_table_names(self.parsed_hogql_query)))self.wait_for_data_interval_end = DATA_INTERVAL_END_PLACEHOLDER in find_interval_placeholders(self.parsed_hogql_query)diff --git a/products/batch_exports/backend/tests/temporal/test_record_batch_model.py b/products/batch_exports/backend/tests/temporal/test_record_batch_model.pyindex d06cd547c1c1..d0d0d85b400f 100644--- a/products/batch_exports/backend/tests/temporal/test_record_batch_model.py+++ b/products/batch_exports/backend/tests/temporal/test_record_batch_model.py@@ -426,10 +426,19 @@ class TestSessionsRecordBatchModelSelection:@pytest.mark.django_dbclass TestHogQLQueryRecordBatchModel:- async def test_as_query_with_parameters(self, ateam, auser, data_interval_start, data_interval_end):+ @pytest.mark.parametrize(+ "hogql_query",+ [+ "SELECT event AS event, distinct_id AS distinct_id FROM events",+ "WITH exported_events AS (SELECT event, distinct_id FROM events) "+ "SELECT event AS event, distinct_id AS distinct_id FROM exported_events",+ ],+ ids=["table", "cte"],+ )+ async def test_as_query_with_parameters(self, ateam, auser, data_interval_start, data_interval_end, hogql_query):model = HogQLQueryRecordBatchModel(team_id=ateam.id,- hogql_query="SELECT event AS event, distinct_id AS distinct_id FROM events",+ hogql_query=hogql_query,user_id=auser.pk,)printed_query, query_parameters = await model.as_query_with_parameters(data_interval_start, data_interval_end)@@ -441,6 +450,7 @@ class TestHogQLQueryRecordBatchModel:# without interval placeholders the query runs as-is, as of nowassert f"toDateTime64('{data_interval_end:%Y-%m-%d %H:%M:%S.%f}', 6, 'UTC')" not in printed_queryassert model.wait_for_data_interval_end is False+ assert model.hogql_table_names == ("events",)async def test_as_query_with_parameters_applies_data_interval(self, ateam, auser, data_interval_start, data_interval_end