: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): preserve trino functions in saved-view diagnostics (#101357)
Eric Duongd4d46e06d393
diff --git a/docs/internal/hogql-trino-compiler.md b/docs/internal/hogql-trino-compiler.mdindex bb563805f649..0f39ec8cf065 100644--- a/docs/internal/hogql-trino-compiler.md+++ b/docs/internal/hogql-trino-compiler.md@@ -490,7 +490,9 @@ ClickHouse and the other dialects retain their existing function registry, validTrino's weighted median takes a value and weight, and its `If` form also takes a condition.Trino's `quantiles` and `quantilesIf` take percentile parameters separately from value arguments.Trino's `ifNotFinite` takes the value and its replacement.-Manifest compilation with `include_hogql=True` uses Trino resolution and preserves function-call syntax in the diagnostic HogQL and column names.+Both manifest and Django-backed compilation with `include_hogql=True` use Trino resolution and preserve function-call syntax in diagnostic HogQL.+The saved-view translation workflow uses Django-backed compilation so it can resolve saved-query dependencies while rendering these diagnostics.+Manifest compilation also preserves function-call syntax in inferred column names.The Trino compiler validates function support and arguments; diagnostic rendering does not consult the ClickHouse registry.Enabling these diagnostics preserves the generated Trino SQL and bound values.Saved-query table metadata is captured eagerly only for Trino's detached compilation; other dialects retain lazy lookup and explicit missing-database errors.diff --git a/products/managed_warehouse/backend/tests/test_trino_compiler.py b/products/managed_warehouse/backend/tests/test_trino_compiler.pyindex c41012e644f2..e4f5f91bb635 100644--- a/products/managed_warehouse/backend/tests/test_trino_compiler.py+++ b/products/managed_warehouse/backend/tests/test_trino_compiler.py@@ -5,7 +5,7 @@ from unittest import mockfrom rest_framework.response import Response-from posthog.schema import HogQLQuery, HogQLQueryModifiers+from posthog.schema import HogQLQueryfrom posthog.hogql.transforms.trino.manifest import (TrinoCatalogManifest,@@ -130,20 +130,15 @@ class TestCompileHogQLToTrinoSQL:assert compiled.values == {"hogql_val_0": "signup"}assert compiled.hogql == "SELECT event FROM events WHERE equals(event, 'signup') LIMIT 50000"- @pytest.mark.parametrize(- ("include_hogql", "expected_hogql", "expected_print_calls"),- [(False, None, 1), (True, "SELECT event FROM events", 2)],- )- def test_populates_core_table_locators_from_control_plane_state(- self, include_hogql: bool, expected_hogql: str | None, expected_print_calls: int- ) -> None:- team = _team()- membership = _membership(team_id=team.pk, organization_id=str(team.organization_id))- database = mock.MagicMock()- locators = {"events": ("org_catalog", "posthog", "events_production")}- modifiers = HogQLQueryModifiers()+ @pytest.mark.django_db+ @pytest.mark.parametrize("include_hogql", [False, True])+ def test_populates_core_table_locators_from_control_plane_state(self, include_hogql: bool) -> None:+ organization = Organization.objects.create(name="trino-core-locators")+ team = Team.objects.create(organization=organization)+ membership = _membership(team_id=team.pk, organization_id=str(organization.pk))with (+ mock.patch("products.managed_warehouse.backend.cp_teams.list_org_teams", return_value=[]),mock.patch("products.managed_warehouse.backend.trino_compiler.get_ready_trino_catalog_name",return_value="org_catalog",@@ -152,19 +147,6 @@ class TestCompileHogQLToTrinoSQL:"products.managed_warehouse.backend.trino_compiler.get_org_team_membership",return_value=membership,),- mock.patch("posthog.hogql.database.database.Database.create_for", return_value=database) as create_database,- mock.patch(- "posthog.hogql.modifiers.create_default_modifiers_for_team",- return_value=modifiers,- ),- mock.patch(- "products.managed_warehouse.backend.trino_compiler.build_trino_table_locators",- return_value=locators,- ) as build_locators,- mock.patch(- "posthog.hogql.printer.utils.prepare_and_print_ast",- side_effect=[("SELECT event FROM target", None), ("SELECT event FROM events", None)],- ) as prepare_and_print,):compiled = compile_hogql_to_trino_sql(team.pk,@@ -174,25 +156,82 @@ class TestCompileHogQLToTrinoSQL:expansion_mode=TrinoExpansionMode.DJANGO,)- assert compiled.sql == "SELECT event FROM target"+ assert compiled.sql == (+ 'SELECT "org_catalog"."posthog"."events_production"."event" '+ 'FROM "org_catalog"."posthog"."events_production" LIMIT 1'+ )assert compiled.values == {}- assert compiled.hogql == expected_hogql- build_locators.assert_called_once_with(- database,- team.pk,- catalog_name="org_catalog",- table_names=membership.table_names,+ assert compiled.hogql == ("SELECT event FROM events LIMIT 1" if include_hogql else None)++ @pytest.mark.django_db+ @pytest.mark.parametrize(+ "expression",+ [+ "date(translation_source.timestamp)",+ "ifnotfinite(1.0, 2.0)",+ "medianexactweighted(3, 2)",+ "cardinality([translation_source.event])",+ "json_value('{\"a\": 1}', '$.a')",+ ],+ )+ def test_django_diagnostics_preserve_trino_compilation_with_saved_query_dependencies(self, expression: str) -> None:+ organization = Organization.objects.create(name="trino-diagnostics")+ team = Team.objects.create(organization=organization)+ membership = _membership(team_id=team.pk, organization_id=str(organization.pk))+ DataWarehouseSavedQuery.objects.create(+ team=team,+ name="translation_source",+ query={"kind": "HogQLQuery", "query": "SELECT event, timestamp FROM events"},+ columns={+ "event": {"clickhouse": "String", "hogql": "StringDatabaseField"},+ "timestamp": {"clickhouse": "DateTime", "hogql": "DateTimeDatabaseField"},+ },)- create_database.assert_called_once()- trino_context = prepare_and_print.call_args_list[0].args[1]- assert trino_context.trino_table_locators == locators- assert trino_context.modifiers is modifiers- assert prepare_and_print.call_args_list[0].kwargs["dialect"] == "trino"- assert prepare_and_print.call_count == expected_print_calls- if include_hogql:- hogql_context = prepare_and_print.call_args_list[1].args[1]- assert hogql_context.database is database- assert prepare_and_print.call_args_list[1].kwargs["dialect"] == "hogql"+ query = HogQLQuery(+ query=f"SELECT {expression} AS result FROM translation_source WHERE translation_source.event = {{event}}",+ values={"event": "signup"},+ )++ with (+ mock.patch("products.managed_warehouse.backend.cp_teams.list_org_teams", return_value=[]),+ mock.patch(+ "products.managed_warehouse.backend.trino_compiler.get_ready_trino_catalog_name",+ return_value="org_catalog",+ ),+ mock.patch(+ "products.managed_warehouse.backend.trino_compiler.get_org_team_membership",+ return_value=membership,+ ),+ ):+ without_diagnostics = compile_hogql_to_trino_sql(+ team.pk,+ query,+ team=team,+ expansion_mode=TrinoExpansionMode.DJANGO,+ bypass_warehouse_access_control=True,+ )+ compiled = compile_hogql_to_trino_sql(+ team.pk,+ query,+ team=team,+ expansion_mode=TrinoExpansionMode.DJANGO,+ bypass_warehouse_access_control=True,+ include_hogql=True,+ )+ assert compiled.hogql is not None+ round_trip = compile_hogql_to_trino_sql(+ team.pk,+ HogQLQuery(query=compiled.hogql),+ team=team,+ expansion_mode=TrinoExpansionMode.DJANGO,+ bypass_warehouse_access_control=True,+ )++ assert (compiled.sql, compiled.values) == (without_diagnostics.sql, without_diagnostics.values)+ assert (round_trip.sql, round_trip.values) == (compiled.sql, compiled.values)+ assert '"org_catalog"."posthog"."events_production"' in compiled.sql+ assert "signup" in compiled.values.values()+ assert "translation_source" in compiled.hogqldef test_fails_closed_without_a_ready_catalog(self) -> None:team = _team()diff --git a/products/managed_warehouse/backend/trino_compiler.py b/products/managed_warehouse/backend/trino_compiler.pyindex c8cf2a287d9f..d2499a05d77e 100644--- a/products/managed_warehouse/backend/trino_compiler.py+++ b/products/managed_warehouse/backend/trino_compiler.py@@ -194,7 +194,11 @@ def compile_hogql_to_trino_sql(from posthog.hogql.modifiers import create_default_modifiers_for_team # noqa: PLC0415from posthog.hogql.parser import parse_select, sanitize_client_parser_mode # noqa: PLC0415from posthog.hogql.placeholders import find_placeholders, replace_placeholders # noqa: PLC0415+ from posthog.hogql.printer.trino_hogql import (+ TrinoHogQLPrinter, # noqa: PLC0415 -- keeps compiler imports off Django startup+ )from posthog.hogql.printer.utils import prepare_and_print_ast # noqa: PLC0415+ from posthog.hogql.resolver import resolve_types # noqa: PLC0415 -- keeps compiler imports off Django startupfrom posthog.hogql.variables import replace_variables # noqa: PLC0415from posthog.hogql.visitor import clone_expr # noqa: PLC0415@@ -276,7 +280,10 @@ def compile_hogql_to_trino_sql(modifiers=query_modifiers,bypass_warehouse_access_control=bypass_warehouse_access_control,database=database,+ trino_table_locators=trino_table_locators,+ restricted_properties=trino_context.restricted_properties,)- hogql_pretty, _ = prepare_and_print_ast(hogql_ast, hogql_context, dialect="hogql")+ resolved_hogql = resolve_types(hogql_ast, hogql_context, dialect="trino")+ hogql_pretty = TrinoHogQLPrinter(context=hogql_context).visit(resolved_hogql)return TrinoCompiledQuery(sql=trino_sql, values=dict(trino_context.values), hogql=hogql_pretty)