: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(managed-warehouse): preserve limits in trino translation (#104041)
Eric Duongdcffba4285c4
diff --git a/docs/internal/hogql-trino-compiler.md b/docs/internal/hogql-trino-compiler.mdindex 50d976cbcf61..12719ce7e5ed 100644--- a/docs/internal/hogql-trino-compiler.md+++ b/docs/internal/hogql-trino-compiler.md@@ -144,6 +144,11 @@ queries = [compiler.compile(query) for query in batch]Create a new compiler when the batch needs fresh control-plane placement or team-table mappings. The one-shot `compile_hogql_to_trino_sql(...)` API prepares and compiles in one call. Django expansion stays one-shot because its schema and semantic expansion depend on query-specific team and user state.+Both managed compilation modes preserve the source query's limits and offsets in the generated SQL and diagnostic HogQL.+They do not add an implicit row limit or cap an explicit limit at the interactive query maximum.+For example, an unbounded query stays unbounded, and `LIMIT 75000` stays `LIMIT 75000`.+After deploying this behavior, rerun saved-view translation for queries compiled with the implicit cap; previously stored SQL is not rewritten automatically.+Pass `expansion_mode=TrinoExpansionMode.DJANGO` when a query requires actions, cohorts, saved queries, filters, variables, access-controlled warehouse discovery, or other Django-backed semantic expansion. This compatibility mode builds the full database and maps:- `events` and `persons` to the project's provisioned tables in the `posthog` schema;diff --git a/products/managed_warehouse/backend/tests/test_trino_compiler.py b/products/managed_warehouse/backend/tests/test_trino_compiler.pyindex 69cb2f23e152..df661dda849b 100644--- a/products/managed_warehouse/backend/tests/test_trino_compiler.py+++ b/products/managed_warehouse/backend/tests/test_trino_compiler.py@@ -200,7 +200,18 @@ class TestReadyTrinoCatalogName:class TestCompileHogQLToTrinoSQL:- def test_preserves_sql_bind_values_and_diagnostics_across_the_transpiler_boundary(self) -> None:+ @pytest.mark.parametrize(+ "limit_clause, trino_limit_clause",+ [+ ("", ""),+ (" LIMIT 1", " LIMIT 1"),+ (" LIMIT 75000", " LIMIT 75000"),+ (" LIMIT 75000 OFFSET 3", " OFFSET 3 ROWS LIMIT 75000"),+ ],+ )+ def test_preserves_sql_bind_values_and_diagnostics_across_the_transpiler_boundary(+ self, limit_clause: str, trino_limit_clause: str+ ) -> None:team = _team()membership = _membership(team_id=team.pk, organization_id=str(team.organization_id))@@ -221,7 +232,9 @@ class TestCompileHogQLToTrinoSQL:):compiled = compile_hogql_to_trino_sql(team.pk,- HogQLQuery(query="SELECT event FROM events WHERE event = {event}", values={"event": "signup"}),+ HogQLQuery(+ query="SELECT event FROM events WHERE event = {event}" + limit_clause, values={"event": "signup"}+ ),team=team,include_hogql=True,)@@ -233,14 +246,25 @@ class TestCompileHogQLToTrinoSQL:assert compiled.sql == ('SELECT "org_catalog"."posthog"."events_production"."event" ''FROM "org_catalog"."posthog"."events_production" '- 'WHERE ("org_catalog"."posthog"."events_production"."event" = %(hogql_val_0)s) LIMIT 50000'+ 'WHERE ("org_catalog"."posthog"."events_production"."event" = %(hogql_val_0)s)' + trino_limit_clause)assert compiled.values == {"hogql_val_0": "signup"}- assert compiled.hogql == "SELECT event FROM events WHERE equals(event, 'signup') LIMIT 50000"+ assert compiled.hogql == "SELECT event FROM events WHERE equals(event, 'signup')" + limit_clause@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:+ @pytest.mark.parametrize(+ "limit_clause, trino_limit_clause",+ [+ ("", ""),+ (" LIMIT 1", " LIMIT 1"),+ (" LIMIT 75000", " LIMIT 75000"),+ (" LIMIT 75000 OFFSET 3", " OFFSET 3 ROWS LIMIT 75000"),+ ],+ )+ def test_populates_core_table_locators_from_control_plane_state(+ self, include_hogql: bool, limit_clause: str, trino_limit_clause: str+ ) -> 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))@@ -258,7 +282,7 @@ class TestCompileHogQLToTrinoSQL:):compiled = compile_hogql_to_trino_sql(team.pk,- HogQLQuery(query="SELECT event FROM events LIMIT 1"),+ HogQLQuery(query="SELECT event FROM events" + limit_clause),team=team,include_hogql=include_hogql,expansion_mode=TrinoExpansionMode.DJANGO,@@ -266,10 +290,10 @@ class TestCompileHogQLToTrinoSQL:assert compiled.sql == ('SELECT "org_catalog"."posthog"."events_production"."event" '- 'FROM "org_catalog"."posthog"."events_production" LIMIT 1'+ 'FROM "org_catalog"."posthog"."events_production"' + trino_limit_clause)assert compiled.values == {}- assert compiled.hogql == ("SELECT event FROM events LIMIT 1" if include_hogql else None)+ assert compiled.hogql == ("SELECT event FROM events" + limit_clause if include_hogql else None)@pytest.mark.django_db@pytest.mark.parametrize(diff --git a/products/managed_warehouse/backend/trino_compiler.py b/products/managed_warehouse/backend/trino_compiler.pyindex e5fac11dc372..c75c0449df2d 100644--- a/products/managed_warehouse/backend/trino_compiler.py+++ b/products/managed_warehouse/backend/trino_compiler.py@@ -58,6 +58,7 @@ class PreparedTrinoCompiler:filters=query.filters,variables=query.variables,modifiers=query.modifiers,+ limit_top_select=False,include_hogql=include_hogql,)except TrinoLoweringError as error:@@ -299,6 +300,7 @@ def compile_hogql_to_trino_sql(team=team,user=user,enable_select_queries=True,+ limit_top_select=False,modifiers=query_modifiers,# Match the access-controlled database above instead of widening it during resolution.bypass_warehouse_access_control=bypass_warehouse_access_control,@@ -315,6 +317,7 @@ def compile_hogql_to_trino_sql(team=team,user=user,enable_select_queries=True,+ limit_top_select=False,modifiers=query_modifiers,bypass_warehouse_access_control=bypass_warehouse_access_control,database=database,