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(warehouse-suggestions): stop reporting customer view sql errors (#130190)

Thiago Salvatore2985fb97115c
diff --git a/products/warehouse_suggestions/backend/logic/view_costs.py b/products/warehouse_suggestions/backend/logic/view_costs.py
index 888a52b4c84e..123a295b8a3e 100644
--- a/products/warehouse_suggestions/backend/logic/view_costs.py
+++ b/products/warehouse_suggestions/backend/logic/view_costs.py
@@ -4,6 +4,8 @@ from uuid import UUID
from django.core.cache import cache
+from clickhouse_driver.errors import ServerException
+
from posthog.hogql import ast
from posthog.hogql.constants import HogQLGlobalSettings
from posthog.hogql.query import execute_hogql_query
@@ -11,7 +13,12 @@ from posthog.hogql.query import execute_hogql_query
from posthog.clickhouse.query_tagging import Product, tag_queries
from posthog.clickhouse.workload import Workload
from posthog.dataclasses import frozen
-from posthog.errors import ClickHouseQueryTimeOut
+from posthog.errors import (
+ ClickHouseQueryTimeOut,
+ QueryErrorCategory,
+ classify_query_error,
+ look_up_clickhouse_error_code_meta,
+)
from posthog.exceptions_capture import capture_exception
from posthog.models.team import Team
@@ -24,6 +31,7 @@ from .rules import MeasurementRules
MEASUREMENT_QUERY_NAME = "warehouse_suggestions_view_cost"
UNFINISHED_KEY_PREFIX = "warehouse_suggestions:unfinished_view_cost"
FAILED_MEASUREMENT = 0
+VIEW_SQL_REJECTED_BY_CLICKHOUSE = "NOT_IMPLEMENTED"
class MeasurementOutcome(StrEnum):
@@ -75,7 +83,8 @@ def measure_view_cost(team_id: int, saved_query_id: UUID, rules: MeasurementRule
_remember_unfinished(team_id, saved_query_id, rules, seconds=rules.max_seconds, fingerprint=fingerprint)
return MeasurementOutcome.CAPPED
except Exception as error:
- capture_exception(error)
+ if not _is_rejected_view_sql(error):
+ capture_exception(error)
_remember_unfinished(team_id, saved_query_id, rules, seconds=FAILED_MEASUREMENT, fingerprint=fingerprint)
return MeasurementOutcome.FAILED
return MeasurementOutcome.MEASURED
@@ -90,6 +99,15 @@ def full_read_query(view_name: str, column_names: list[str]) -> ast.SelectQuery:
)
+def _is_rejected_view_sql(error: Exception) -> bool:
+ if classify_query_error(error) == QueryErrorCategory.USER_ERROR:
+ return True
+ return (
+ isinstance(error, ServerException)
+ and look_up_clickhouse_error_code_meta(error).name == VIEW_SQL_REJECTED_BY_CLICKHOUSE
+ )
+
+
def _remember_unfinished(
team_id: int, saved_query_id: UUID, rules: MeasurementRules, *, seconds: int, fingerprint: str
) -> None:
diff --git a/products/warehouse_suggestions/backend/tests/test_view_costs.py b/products/warehouse_suggestions/backend/tests/test_view_costs.py
index 7aba395ba2bc..da1db47d8671 100644
--- a/products/warehouse_suggestions/backend/tests/test_view_costs.py
+++ b/products/warehouse_suggestions/backend/tests/test_view_costs.py
@@ -3,9 +3,12 @@ from unittest.mock import patch
from django.core.cache import cache
+from clickhouse_driver.errors import ServerException
from parameterized import parameterized
-from posthog.errors import ClickHouseQueryTimeOut
+from posthog.hogql.errors import QueryError
+
+from posthog.errors import ClickHouseQueryTimeOut, wrap_clickhouse_query_error
from products.data_modeling.backend.facade.models import DataWarehouseSavedQuery
from products.warehouse_suggestions.backend.logic.rules import RULES
@@ -43,9 +46,40 @@ class TestMeasureViewCost(ClickhouseTestMixin, BaseTest):
@parameterized.expand(
[
- ("timeout", ClickHouseQueryTimeOut(), True, MeasurementOutcome.CAPPED, RULES.measurement.max_seconds),
- ("other_error", ValueError("broken view"), True, MeasurementOutcome.FAILED, FAILED_MEASUREMENT),
- ("no_stored_columns", None, False, MeasurementOutcome.FAILED, FAILED_MEASUREMENT),
+ (
+ "timeout",
+ ClickHouseQueryTimeOut(),
+ True,
+ MeasurementOutcome.CAPPED,
+ RULES.measurement.max_seconds,
+ False,
+ ),
+ ("other_error", ValueError("broken view"), True, MeasurementOutcome.FAILED, FAILED_MEASUREMENT, True),
+ (
+ "clickhouse_rejects_view_sql",
+ wrap_clickhouse_query_error(ServerException("Cannot coalesce decimals", code=48)),
+ True,
+ MeasurementOutcome.FAILED,
+ FAILED_MEASUREMENT,
+ False,
+ ),
+ (
+ "clickhouse_storage_failure",
+ wrap_clickhouse_query_error(ServerException("S3 error", code=499)),
+ True,
+ MeasurementOutcome.FAILED,
+ FAILED_MEASUREMENT,
+ True,
+ ),
+ (
+ "hogql_rejects_view_sql",
+ QueryError("Unknown table"),
+ True,
+ MeasurementOutcome.FAILED,
+ FAILED_MEASUREMENT,
+ False,
+ ),
+ ("no_stored_columns", None, False, MeasurementOutcome.FAILED, FAILED_MEASUREMENT, False),
]
)
def test_a_measurement_that_does_not_finish_is_remembered(
@@ -55,12 +89,20 @@ class TestMeasureViewCost(ClickhouseTestMixin, BaseTest):
has_columns: bool,
expected_outcome: MeasurementOutcome,
expected_cap: int,
+ expected_captured: bool,
) -> None:
if not has_columns:
DataWarehouseSavedQuery.objects.filter(id=self.saved_query.id).update(columns={})
- with patch(f"{VIEW_COSTS_MODULE}.execute_hogql_query", side_effect=error):
+ with (
+ patch(f"{VIEW_COSTS_MODULE}.execute_hogql_query", side_effect=error),
+ patch(f"{VIEW_COSTS_MODULE}.capture_exception") as capture_exception,
+ ):
outcome = measure_view_cost(self.team.pk, self.saved_query.id, RULES.measurement)
unfinished = unfinished_measurement(self.team.pk, self.saved_query.id)
- assert (outcome, unfinished.seconds if unfinished else None) == (expected_outcome, expected_cap)
+ assert (outcome, unfinished.seconds if unfinished else None, capture_exception.called) == (
+ expected_outcome,
+ expected_cap,
+ expected_captured,
+ )