: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(hogql): degrade autocomplete gracefully on malformed source query (#71542)
posthog[bot]5cc44079fdf7
diff --git a/posthog/hogql/autocomplete.py b/posthog/hogql/autocomplete.pyindex 80690d5f562f..69539c475a39 100644--- a/posthog/hogql/autocomplete.py+++ b/posthog/hogql/autocomplete.py@@ -463,7 +463,12 @@ def get_hogql_autocomplete():source_query = parse_select("select 1")else:- source_query = get_query_runner(query=query.sourceQuery, team=team).to_query()+ try:+ source_query = get_query_runner(query=query.sourceQuery, team=team).to_query()+ except Exception:+ # A malformed source query (e.g. an unquoted reserved keyword used as an+ # identifier) must not break autocomplete — degrade gracefully instead.+ source_query = parse_select("select 1")else:source_query = parse_select("select 1")diff --git a/posthog/hogql/test/test_autocomplete.py b/posthog/hogql/test/test_autocomplete.pyindex 06c11c0f8911..ec5dbde913df 100644--- a/posthog/hogql/test/test_autocomplete.py+++ b/posthog/hogql/test/test_autocomplete.py@@ -2,6 +2,8 @@ from typing import Optionalfrom posthog.test.base import APIBaseTest, ClickhouseTestMixin+from parameterized import parameterized+from posthog.schema import (AutocompleteCompletionItemKind,HogLanguage,@@ -572,12 +574,19 @@ class TestAutocomplete(ClickhouseTestMixin, APIBaseTest):assert "some_view" not in [x.label for x in results.suggestions]assert "DELETED" not in [x.label for x in results.suggestions]- def test_autocomplete_empty_source_query(self):+ @parameterized.expand(+ [+ ("empty", ""),+ # An unquoted reserved keyword used as an alias makes the source query unparseable.+ ("reserved_keyword_alias", "select 1 as team_id"),+ ]+ )+ def test_autocomplete_degrades_gracefully_for_bad_source_query(self, _name: str, source_query: str):autocomplete = HogQLAutocomplete(kind="HogQLAutocomplete",query="SELECT * FROM e",language=HogLanguage.HOG_QL,- sourceQuery=HogQLQuery(query=""), # Empty source query+ sourceQuery=HogQLQuery(query=source_query),startPosition=15,endPosition=15,)