Skip to content

: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.

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.py
index 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, NodeK
from posthog.api.services.query import ExecutionMode
from posthog.dataclasses import frozen
+from posthog.errors import CH_TRANSIENT_ERRORS, ExposedCHQueryError
from posthog.hogql_queries.validation.validate_query import rule_violation_message
from posthog.schema_migrations.upgrade_manager import upgrade_insight
from 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 a
working 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):
raise
raise 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 err
def check_detector_alert(
diff --git a/products/alerts/backend/evaluation/test/test_dispatcher.py b/products/alerts/backend/evaluation/test/test_dispatcher.py
index 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 DRFValidationError
from posthog.schema import NodeKind
from posthog.api.services.query import ExecutionMode
+from posthog.errors import CHQueryErrorNoCommonType, CHQueryErrorS3FileChangedDuringRead
from posthog.hogql_queries.query_failure_handling import build_failure_exception
from 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