: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(data-warehouse): return 400 for malformed saved query SQL (#67328)
posthog[bot]4930fbbdf011
diff --git a/products/data_warehouse/backend/presentation/views/saved_query.py b/products/data_warehouse/backend/presentation/views/saved_query.pyindex 46e2bceadcd6..69baa7cad28f 100644--- a/products/data_warehouse/backend/presentation/views/saved_query.py+++ b/products/data_warehouse/backend/presentation/views/saved_query.py@@ -614,10 +614,13 @@ class DataWarehouseSavedQuerySerializer(user = self.context["request"].usercontext = HogQLContext(team_id=team_id, user=user, enable_select_queries=True)- select_ast = parse_select(query["query"])+ try:+ select_ast = parse_select(query["query"])- find_placeholders = FindPlaceholders()- find_placeholders.visit(select_ast)+ find_placeholders = FindPlaceholders()+ find_placeholders.visit(select_ast)+ except ExposedHogQLError as err:+ raise exceptions.ValidationError(detail=f"Invalid query: {err}")if len(find_placeholders.placeholder_fields) > 0:placeholder = find_placeholders.placeholder_fields.pop()placeholder_string = ".".join(str(field) for field in placeholder if field is not None)diff --git a/products/data_warehouse/backend/tests/api/test_saved_query.py b/products/data_warehouse/backend/tests/api/test_saved_query.pyindex 6e0dd38ce63e..f347e690821a 100644--- a/products/data_warehouse/backend/tests/api/test_saved_query.py+++ b/products/data_warehouse/backend/tests/api/test_saved_query.py@@ -374,6 +374,21 @@ class TestSavedQuery(APIBaseTest):response_json = response.json()assert "Filters and placeholder expressions are not allowed in views" in response_json["detail"]+ def test_create_with_malformed_query_returns_validation_error(self):+ response = self.client.post(+ f"/api/environments/{self.team.id}/warehouse_saved_queries/",+ {+ "name": "test_malformed",+ "query": {+ "kind": "HogQLQuery",+ "query": "select * from events *",+ },+ },+ )+ assert response.status_code == 400, response.content+ response_json = response.json()+ assert "Invalid query" in response_json["detail"]+def test_delete(self):query_name = "test_query"saved_query = DataWarehouseSavedQuery.objects.create(team=self.team, name=query_name)