: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): return 400 for hogql errors in sql alert simulate (#119171)
posthog[bot]38ec31cc173b
diff --git a/products/alerts/backend/evaluation/detector.py b/products/alerts/backend/evaluation/detector.pyindex de3e71d7a83f..685edf93f5c2 100644--- a/products/alerts/backend/evaluation/detector.py+++ b/products/alerts/backend/evaluation/detector.py@@ -504,6 +504,7 @@ def simulate_detector_on_insight(# module's extractor classes — importing the registry at module load would cycle.from products.alerts.backend.evaluation.dispatcher import ( # noqa: PLC0415 — breaks dispatcher↔detector import cycleDETECTOR_EXTRACTORS,+ owner_sql_errors,)extractor = DETECTOR_EXTRACTORS.get(kind)@@ -520,7 +521,8 @@ def simulate_detector_on_insight(config=config,evaluation_delay_intervals=evaluation_delay_intervals,)- result, interval_value = extractor.simulate(insight, query, ctx)+ with owner_sql_errors(query):+ result, interval_value = extractor.simulate(insight, query, ctx)interval_metadata = ({"evaluation_delay_intervals": result.evaluated_interval.delay,diff --git a/products/alerts/backend/evaluation/dispatcher.py b/products/alerts/backend/evaluation/dispatcher.pyindex 99397c94b770..57d0bf1a7ff7 100644--- a/products/alerts/backend/evaluation/dispatcher.py+++ b/products/alerts/backend/evaluation/dispatcher.py@@ -7,6 +7,8 @@ from rest_framework.exceptions import ValidationError as DRFValidationErrorfrom posthog.schema import AlertCondition, InsightThreshold, IntervalType, NodeKind+from posthog.hogql.errors import ExposedHogQLError+from posthog.api.services.query import ExecutionModefrom posthog.dataclasses import frozenfrom posthog.errors import CH_TRANSIENT_ERRORS, ExposedCHQueryError@@ -66,6 +68,23 @@ def _resolve_execution_mode(alert: AlertConfiguration, kind: NodeKind, query: obreturn execution_mode_for_alert(interval, high_frequency=alert.is_high_frequency_interval)+@contextmanager+def owner_sql_errors(query: object) -> Iterator[None]:+ """Raise a user-safe failure of a SQL insight's own query as an ``AlertExtractionError``.++ The owner wrote the query, such as a HogQL call with the wrong arguments or a column type+ mismatch, 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. A transient ClickHouse error also+ keeps the ordinary path, because the query itself can be sound.+ """+ try:+ yield+ except (ExposedHogQLError, 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 run_extractor(extractor: Extractor,alert: AlertConfiguration,@@ -86,20 +105,16 @@ def run_extractor(``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.+ A SQL insight that HogQL or ClickHouse rejects with a user-safe error gets the same verdict+ (see ``owner_sql_errors``)."""try:- return extractor.extract(alert, insight, query, execution_mode)+ with owner_sql_errors(query):+ return extractor.extract(alert, insight, query, execution_mode)except DRFValidationError as err: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 80740b239515..de4d9c53fc6f 100644--- a/products/alerts/backend/evaluation/test/test_dispatcher.py+++ b/products/alerts/backend/evaluation/test/test_dispatcher.py@@ -9,6 +9,8 @@ from rest_framework.exceptions import ValidationError as DRFValidationErrorfrom posthog.schema import NodeKind+from posthog.hogql.errors import QueryError+from posthog.api.services.query import ExecutionModefrom posthog.caching.insight_result import InsightResultfrom posthog.errors import CHQueryErrorNoCommonType, CHQueryErrorS3FileChangedDuringRead@@ -76,15 +78,26 @@ 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\.$"- ):+ "error,expected_message",+ [+ (+ CHQueryErrorNoCommonType(+ "Code: 386. DB::Exception: There is no supertype for types DateTime64(6, 'UTC'), String. Stack trace: ...",+ code=386,+ code_name="no_common_type",+ ),+ r"^There is no supertype for types DateTime64\(6, 'UTC'\), String\.$",+ ),+ (+ QueryError("Function 'toStartOfHour' expects 1 argument, found 2"),+ r"^Function 'toStartOfHour' expects 1 argument, found 2$",+ ),+ ],+ ids=["clickhouse", "hogql"],+)+def test_a_sql_query_the_engine_rejects_becomes_an_extraction_error(error, expected_message) -> None:+ with pytest.raises(AlertExtractionError, match=expected_message):_run(error, {"kind": NodeKind.HOG_QL_QUERY, "query": "select 1"})@@ -93,10 +106,11 @@ def test_a_sql_query_clickhouse_rejects_becomes_an_extraction_error() -> None:[(CHQueryErrorNoCommonType("no supertype", code=386), {"kind": NodeKind.TRENDS_QUERY, "series": []}),(CHQueryErrorS3FileChangedDuringRead("file changed", code=36), {"kind": NodeKind.HOG_QL_QUERY}),+ (QueryError("Unknown function"), {"kind": NodeKind.TRENDS_QUERY, "series": []}),],- ids=["posthog_built_query", "transient_error"],+ ids=["posthog_built_query", "transient_error", "posthog_built_hogql"],)-def test_a_clickhouse_error_outside_the_owners_sql_keeps_the_ordinary_failure_path(error, query) -> None:+def test_a_query_error_outside_the_owners_sql_keeps_the_ordinary_failure_path(error, query) -> None:with pytest.raises(type(error)):_run(error, query)diff --git a/products/alerts/backend/tests/api/test_alert.py b/products/alerts/backend/tests/api/test_alert.pyindex 4909b9bb2bae..72a0ba04087f 100644--- a/products/alerts/backend/tests/api/test_alert.py+++ b/products/alerts/backend/tests/api/test_alert.py@@ -15,8 +15,11 @@ from rest_framework import statusfrom posthog.schema import AlertCalculationInterval, AlertConditionType, AlertState, InsightThresholdType+from posthog.hogql.errors import QueryError+from posthog.api.tagged_item import set_tags_on_objectfrom posthog.constants import AvailableFeature+from posthog.errors import CHQueryErrorNoCommonTypefrom posthog.models import Userfrom posthog.models.integration import Integrationfrom posthog.models.personal_api_key import PersonalAPIKey@@ -1822,6 +1825,28 @@ class TestAlertSimulate(TrendsInsightAPITest):assert "needs at least" in response.json()["detail"]assert not AlertCheck.objects.filter(alert_configuration__insight=insight).exists()+ @parameterized.expand(+ [+ ("hogql", QueryError("Function 'toStartOfHour' expects 1 argument, found 2"), "toStartOfHour"),+ ("clickhouse", CHQueryErrorNoCommonType("There is no supertype", code=386), "no supertype"),+ ]+ )+ @mock.patch("products.alerts.backend.evaluation.hogql.calculate_for_query_based_insight")+ def test_simulate_sql_query_error_returns_validation_error(self, _name, error, message, mock_calculate) -> None:+ insight = Insight.objects.create(team=self.team, query={"kind": "HogQLQuery", "query": "SELECT 1 AS value"})+ mock_calculate.side_effect = error+ response = self.client.post(+ f"/api/projects/{self.team.id}/alerts/simulate",+ {+ "insight": insight.id,+ "detector_config": {"type": "zscore", "threshold": 0.9, "window": 30},+ "config": {"type": "HogQLAlertConfig", "evaluation": "last_row", "column": "value"},+ },+ format="json",+ )+ assert response.status_code == status.HTTP_400_BAD_REQUEST, response.content+ assert message in response.json()["detail"]+@mock.patch("products.alerts.backend.presentation.views.alert.simulate_detector_on_insight")def test_simulate_uses_default_detector_config(self, mock_simulate) -> None:mock_simulate.return_value = {