: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(alerts): disable sql alerts that clickhouse rejects as invalid (#117902)
posthog[bot]1b58470ff2b1
diff --git a/products/alerts/backend/evaluation/dispatcher.py b/products/alerts/backend/evaluation/dispatcher.pyindex de775caebd4f..06d8ee3edc08 100644--- a/products/alerts/backend/evaluation/dispatcher.py+++ b/products/alerts/backend/evaluation/dispatcher.py@@ -9,6 +9,7 @@ from posthog.schema import AlertCondition, InsightThreshold, IntervalType, NodeKfrom posthog.api.services.query import ExecutionModefrom posthog.dataclasses import frozen+from posthog.errors import CH_TRANSIENT_ERRORS, ExposedCHQueryErrorfrom posthog.hogql_queries.validation.validate_query import rule_violation_messagefrom posthog.schema_migrations.upgrade_manager import upgrade_insightfrom posthog.tasks.alerts.utils import WRAPPER_NODE_KINDS, AlertEvaluationResult@@ -84,6 +85,10 @@ def run_extractor(that is itself sound (see ``build_failure_exception`` in``posthog/hogql_queries/query_failure_handling.py``), and disabling on it would silence aworking alert over a load problem, so it keeps the ordinary failure path.++ A SQL insight that ClickHouse rejects with a user-safe error, such as a column type mismatch,+ gets the same verdict: the owner wrote the query, and it fails the same way until they fix it.+ Other kinds keep the ordinary path, because PostHog builds their SQL and the error can be a bug."""try:return extractor.extract(alert, insight, query, execution_mode)@@ -91,6 +96,10 @@ def run_extractor(if getattr(err, "served_from_query_failure_cache", False):raiseraise AlertExtractionError(rule_violation_message(err)) from err+ except ExposedCHQueryError as err:+ if isinstance(err, CH_TRANSIENT_ERRORS) or get_from_dict_or_attr(query, "kind") != NodeKind.HOG_QL_QUERY:+ raise+ raise AlertExtractionError(str(err)) from errdef check_detector_alert(diff --git a/products/alerts/backend/evaluation/test/test_dispatcher.py b/products/alerts/backend/evaluation/test/test_dispatcher.pyindex 498217517cb2..569a6f0c012b 100644--- a/products/alerts/backend/evaluation/test/test_dispatcher.py+++ b/products/alerts/backend/evaluation/test/test_dispatcher.py@@ -10,6 +10,7 @@ from rest_framework.exceptions import ValidationError as DRFValidationErrorfrom posthog.schema import NodeKindfrom posthog.api.services.query import ExecutionMode+from posthog.errors import CHQueryErrorNoCommonType, CHQueryErrorS3FileChangedDuringReadfrom posthog.hogql_queries.query_failure_handling import build_failure_exceptionfrom posthog.query_cache.failures import QueryFailureRecord@@ -34,8 +35,8 @@ class _Extractor:return self._outcome-def _run(outcome: Exception | ExtractionResult) -> ExtractionResult:- return run_extractor(_Extractor(outcome), MagicMock(), MagicMock(), {}, IF_STALE)+def _run(outcome: Exception | ExtractionResult, query: dict | None = None) -> ExtractionResult:+ return run_extractor(_Extractor(outcome), MagicMock(), MagicMock(), query or {}, IF_STALE)def _breaker_replay() -> Exception:@@ -69,6 +70,31 @@ def test_a_breaker_replay_keeps_the_ordinary_failure_path() -> None:_run(replay)+def test_a_sql_query_clickhouse_rejects_becomes_an_extraction_error() -> None:+ error = CHQueryErrorNoCommonType(+ "Code: 386. DB::Exception: There is no supertype for types DateTime64(6, 'UTC'), String. Stack trace: ...",+ code=386,+ code_name="no_common_type",+ )+ with pytest.raises(+ AlertExtractionError, match=r"^There is no supertype for types DateTime64\(6, 'UTC'\), String\.$"+ ):+ _run(error, {"kind": NodeKind.HOG_QL_QUERY, "query": "select 1"})+++ "error,query",+ [+ (CHQueryErrorNoCommonType("no supertype", code=386), {"kind": NodeKind.TRENDS_QUERY, "series": []}),+ (CHQueryErrorS3FileChangedDuringRead("file changed", code=36), {"kind": NodeKind.HOG_QL_QUERY}),+ ],+ ids=["posthog_built_query", "transient_error"],+)+def test_a_clickhouse_error_outside_the_owners_sql_keeps_the_ordinary_failure_path(error, query) -> None:+ with pytest.raises(type(error)):+ _run(error, query)++def test_a_successful_extraction_passes_through() -> None:result = ExtractionResult(series=[])assert _run(result) is result