: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): bound saved view expansion during compilation (#125950)
Marius Andrad225de6522a8
diff --git a/docs/published/handbook/engineering/databases/hogql-python.md b/docs/published/handbook/engineering/databases/hogql-python.mdindex 0a586eafaf2b..dae538973a2d 100644--- a/docs/published/handbook/engineering/databases/hogql-python.md+++ b/docs/published/handbook/engineering/databases/hogql-python.md@@ -53,6 +53,12 @@ This accounting is not a limit on Python process memory.The `range()` builtin checks its result size against the remaining VM allowance before allocating the list.Queries that exceed these limits fail during expansion; reduce the number or size of the placeholder expressions to stay within them.+Saved views allow at most 100 nested views and 1,000 expansions per compilation, including repeated references to the same view.+Their cumulative expanded SQL source is limited to 4 MiB (UTF-8), charged before each parse; this is not a Python memory limit.+Circular dependencies fail with a query error. Soft saves skip column inference but still validate view dependencies.+Query preparation shares a 60-second cooperative deadline across parsing, resolution, transformations, and printing.+AST visitors check the deadline while walking; native parser calls are checked when they return, rather than interrupted mid-call.+## Pattern matching during query preparationExpressions inside HogQL placeholders execute in the Python HogVM.diff --git a/posthog/hogql/compilation.py b/posthog/hogql/compilation.pynew file mode 100644index 000000000000..546bf3291d08--- /dev/null+++ b/posthog/hogql/compilation.py@@ -0,0 +1,71 @@+import time+from collections.abc import Callable+from contextvars import ContextVar+from functools import wraps+from typing import ParamSpec, TypeVar++from posthog.hogql.errors import QueryError++MAX_VIEW_DEPTH = 100+MAX_VIEW_EXPANSIONS = 1000+MAX_EXPANDED_VIEW_SQL_BYTES = 4 * 1024 * 1024+COMPILATION_TIMEOUT_SECONDS = 60.0+++class CompilationBudget:+ def __init__(self) -> None:+ self.deadline = time.monotonic() + COMPILATION_TIMEOUT_SECONDS+ self.view_expansions = 0+ self.expanded_view_sql_bytes = 0++ def check_time(self) -> None:+ if time.monotonic() > self.deadline:+ raise QueryError("HogQL compilation exceeded its time limit. Simplify the query and try again.")++ def expand_view(self, query: str) -> None:+ self.check_time()+ if self.view_expansions >= MAX_VIEW_EXPANSIONS:+ raise QueryError(+ f"Query exceeds the limit of {MAX_VIEW_EXPANSIONS} saved view expansions. "+ "Reduce the number of saved view references."+ )+ query_bytes = len(query.encode("utf-8"))+ if self.expanded_view_sql_bytes + query_bytes > MAX_EXPANDED_VIEW_SQL_BYTES:+ raise QueryError(+ "Query exceeds the saved view SQL expansion size limit. "+ "Reduce the size or number of referenced saved views."+ )+ self.view_expansions += 1+ self.expanded_view_sql_bytes += query_bytes+++# Nested compilers and transforms must share the outer request's budget, including+# visitors that do not carry a HogQLContext. ContextVar keeps concurrent requests isolated.+compilation_budget: ContextVar[CompilationBudget | None] = ContextVar("hogql_compilation_budget", default=None)++P = ParamSpec("P")+R = TypeVar("R")+++def bounded_compilation(function: Callable[P, R]) -> Callable[P, R]:+ @wraps(function)+ def wrapped(*args: P.args, **kwargs: P.kwargs) -> R:+ budget = compilation_budget.get()+ token = None+ if budget is None:+ budget = CompilationBudget()+ token = compilation_budget.set(budget)+ try:+ budget.check_time()+ result = function(*args, **kwargs)+ budget.check_time()+ return result+ except RecursionError as error:+ raise QueryError(+ "HogQL query exceeds the supported nesting depth. Reduce nested subqueries or saved views."+ ) from error+ finally:+ if token is not None:+ compilation_budget.reset(token)++ return wrappeddiff --git a/posthog/hogql/printer/utils.py b/posthog/hogql/printer/utils.pyindex 1fbabcce1517..a56f22104729 100644--- a/posthog/hogql/printer/utils.py+++ b/posthog/hogql/printer/utils.py@@ -11,6 +11,7 @@ if TYPE_CHECKING:from posthog.hogql import astfrom posthog.hogql.base import _T_AST+from posthog.hogql.compilation import bounded_compilationfrom posthog.hogql.constants import SQL_TARGET_DIALECTS, HogQLDialect, HogQLGlobalSettingsfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.database import Database@@ -94,6 +95,7 @@ def to_printed_hogql()[0]+@bounded_compilationdef prepare_and_print_ast(node: _T_AST,context: HogQLContext,@@ -182,6 +184,7 @@ def prepare_and_print_ast(context.type_observability = previous_type_observability+@bounded_compilationdef prepare_ast_for_printing(node: _T_AST, # node is mutatedcontext: HogQLContext,@@ -495,6 +498,7 @@ def prepare_ast_for_printing(return node+@bounded_compilationdef print_prepared_ast(node: _T_AST,context: HogQLContext,diff --git a/posthog/hogql/query.py b/posthog/hogql/query.pyindex 62e5cd1a0be7..cfc062a67e6b 100644--- a/posthog/hogql/query.py+++ b/posthog/hogql/query.py@@ -19,6 +19,7 @@ from posthog.schema import ()from posthog.hogql import ast, query_stats+from posthog.hogql.compilation import bounded_compilationfrom posthog.hogql.constants import (HogQLDialect,HogQLGlobalSettings,@@ -877,6 +878,7 @@ class HogQLQueryExecutor:self._evaluate_prompt_jev()return self.select_query, self._prompt_jev_tables+ @bounded_compilationdef _prepare_execution(self, *, embedded_select: bool = False) -> _PreparedExecution:self.context.clear_reads()self._parse_query()diff --git a/posthog/hogql/resolver.py b/posthog/hogql/resolver.pyindex 67e48f91be4b..8647fbc7e201 100644--- a/posthog/hogql/resolver.py+++ b/posthog/hogql/resolver.py@@ -9,6 +9,7 @@ import re2from posthog.hogql import astfrom posthog.hogql.ast import ConstantType, FieldTraverserTypefrom posthog.hogql.base import _T_AST+from posthog.hogql.compilation import MAX_VIEW_DEPTH, CompilationBudget, bounded_compilation, compilation_budgetfrom posthog.hogql.constants import SQL_TARGET_DIALECTS, HogQLDialectfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.database import Database@@ -282,6 +283,7 @@ def _mark_view_body(query: ast.SelectQuery | ast.SelectSetQuery, view_name: str)_mark_view_body(branch, view_name)+@bounded_compilationdef resolve_types(node: _T_AST,context: HogQLContext,@@ -442,6 +444,9 @@ class Resolver(CloningVisitor):self.scopes: list[ast.SelectQueryType] = scopes or []self.ctes: dict[str, ast.CTE] = {}self.current_view_depth: int = 0+ self._compilation_budget = compilation_budget.get() or CompilationBudget()+ self._resolving_saved_views: set[str] = set()+ self._pending_saved_view: str | None = Noneself.context = contextself.dialect = dialectself.database = context.database@@ -463,6 +468,7 @@ class Resolver(CloningVisitor):return self._scope_table_column_aliases.setdefault(id(scope), {})def visit(self, node: ast.AST | None):+ self._compilation_budget.check_time()if isinstance(node, ast.Expr) and node.type is not None:raise ResolutionError(f"Type already resolved for {type(node).__name__} ({type(node.type).__name__}). Can't run again."@@ -1483,16 +1489,23 @@ class Resolver(CloningVisitor):database_table = lower_trino_table(database_table, self.context)if isinstance(database_table, SavedQuery):- self.current_view_depth += 1-- node.table = parse_select(str(database_table.query))+ if database_table.name in self._resolving_saved_views:+ raise QueryError(f"Circular dependency detected in view '{database_table.name}'")+ if self.current_view_depth >= MAX_VIEW_DEPTH:+ raise QueryError(+ f"Query exceeds the limit of {MAX_VIEW_DEPTH} nested saved views. Reduce the view nesting depth."+ )+ view_query = str(database_table.query)+ self._compilation_budget.expand_view(view_query)+ node.table = parse_select(view_query)_mark_view_body(node.table, database_table.name)-node.alias = table_alias or database_table.name- node = self.visit(node)-- self.current_view_depth -= 1- return node+ previous_pending = self._pending_saved_view+ self._pending_saved_view = database_table.name+ try:+ return self.visit(node)+ finally:+ self._pending_saved_view = previous_pendingif isinstance(database_table, LazyTable):if isinstance(database_table, PersonsTable):@@ -1606,6 +1619,8 @@ class Resolver(CloningVisitor):return nodeelif isinstance(node.table, ast.SelectQuery) or isinstance(node.table, ast.SelectSetQuery):+ view_name = self._pending_saved_view+ self._pending_saved_view = Nonenode = cast(ast.JoinExpr, clone_expr(node))if node.constraint and node.constraint.constraint_type == "USING":# visit USING constraint before adding the table to avoid ambiguous names@@ -1616,7 +1631,16 @@ class Resolver(CloningVisitor):elif node.join_type is not None:node.alias = self._synthesize_join_alias(scope, node)- node.table = cast("ast.SelectQuery | ast.SelectSetQuery", super().visit(node.table))+ # Only the body is on the dependency path; next_join contains sibling views.+ if view_name is not None:+ self._resolving_saved_views.add(view_name)+ self.current_view_depth += 1+ try:+ node.table = cast("ast.SelectQuery | ast.SelectSetQuery", super().visit(node.table))+ finally:+ if view_name is not None:+ self.current_view_depth -= 1+ self._resolving_saved_views.remove(view_name)# Remap column names if column_aliases is provided (e.g. AS v(id, name))if node.column_aliases and node.table.type:diff --git a/posthog/hogql/test/test_query.py b/posthog/hogql/test/test_query.pyindex 1bd42a6cbe8a..04becf0dec36 100644--- a/posthog/hogql/test/test_query.py+++ b/posthog/hogql/test/test_query.py@@ -35,9 +35,11 @@ from posthog.schema import ()from posthog.hogql import ast+from posthog.hogql.compilation import COMPILATION_TIMEOUT_SECONDSfrom posthog.hogql.constants import HogQLQuerySettingsfrom posthog.hogql.direct_connection import INVALID_CONNECTION_ID_ERROR, get_direct_connection_sourcefrom posthog.hogql.errors import ExposedHogQLError, QueryError+from posthog.hogql.parser import parse_selectfrom posthog.hogql.printer import prepare_ast_for_printing as unmocked_prepare_ast_for_printingfrom posthog.hogql.property import property_to_exprfrom posthog.hogql.query import HogQLQueryExecutor, execute_hogql_query@@ -423,6 +425,22 @@ class TestQuery(ClickhouseTestMixin, APIBaseTest):required_level="viewer",)+ def test_preparation_deadline_includes_initial_parse(self) -> None:+ parsed = parse_select("SELECT 1")+ with (+ patch("posthog.hogql.compilation.time.monotonic", return_value=0.0) as clock,+ patch("posthog.hogql.query.sync_execute") as execute,+ ):++ def slow_parse(*args: object, **kwargs: object) -> ast.SelectQuery | ast.SelectSetQuery:+ clock.return_value = COMPILATION_TIMEOUT_SECONDS + 1+ return parsed++ with patch("posthog.hogql.query.parse_select", side_effect=slow_parse):+ with self.assertRaisesRegex(QueryError, "compilation exceeded its time limit"):+ execute_hogql_query("SELECT 1", team=self.team)+ execute.assert_not_called()+@patch("posthog.hogql.query.sync_execute")def test_execute_hogql_query_rejects_non_direct_connection_before_clickhouse(self, mock_sync_execute):selected_source = ExternalDataSource.objects.create(diff --git a/posthog/hogql/test/test_resolver.py b/posthog/hogql/test/test_resolver.pyindex 7a312d168aa1..f63464724532 100644--- a/posthog/hogql/test/test_resolver.py+++ b/posthog/hogql/test/test_resolver.py@@ -7,7 +7,7 @@ import time_machinefrom posthog.test.base import BaseTestfrom unittest.mock import patch-from django.test import override_settings+from django.test import SimpleTestCase, override_settingsfrom parameterized import parameterized@@ -15,6 +15,7 @@ from posthog.schema import HogQLQueryModifiersimport posthog.hogql.resolver_utils as resolver_utilsfrom posthog.hogql import ast+from posthog.hogql.compilation import COMPILATION_TIMEOUT_SECONDSfrom posthog.hogql.constants import MAX_SELECT_RETURNED_ROWSfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.database import Database@@ -22,6 +23,8 @@ from posthog.hogql.database.models import (DateTimeDatabaseField,ExpressionField,FieldTraverser,+ IntegerDatabaseField,+ SavedQuery,StringDatabaseField,StringJSONDatabaseField,Table,@@ -34,12 +37,125 @@ from posthog.hogql.errors import QueryErrorfrom posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.printer import prepare_and_print_ast, print_prepared_ast-from posthog.hogql.resolver import ResolutionError, resolve_types+from posthog.hogql.resolver import ResolutionError, Resolver, resolve_typesfrom posthog.hogql.resolver_utils import extract_base_table_types, lookup_field_by_namefrom posthog.hogql.test.utils import pretty_dataclassesfrom posthog.hogql.visitor import clone_expr+class TestSavedViewBounds(SimpleTestCase):+ def _context(self, views: dict[str, str]) -> HogQLContext:+ database = Database()+ database.tables.merge_with(+ TableNode(+ children={+ name: TableNode(+ name=name,+ table=SavedQuery(id=name, name=name, query=query, fields={"x": IntegerDatabaseField(name="x")}),+ )+ for name, query in views.items()+ }+ )+ )+ return HogQLContext(database=database, enable_select_queries=True)++ @parameterized.expand(+ [+ ("direct", {"first": "select * from first"}),+ ("indirect", {"first": "select * from second", "second": "select * from first"}),+ ("union", {"first": "select 1 union all select * from first"}),+ ]+ )+ def test_rejects_cycles(self, _name: str, views: dict[str, str]) -> None:+ with self.assertRaisesRegex(QueryError, "Circular dependency"):+ resolve_types(parse_select("select * from first"), self._context(views), "hogql")++ @parameterized.expand(+ [+ ("sibling", "select a.x from leaf a join leaf b on a.x = b.x", "select 1 as x"),+ ("union", "select a.x from leaf a join leaf b on a.x = b.x", "select 1 as x union all select 2 as x"),+ ("shadow", "select * from leaf", "with leaf as (select 1 as x) select * from leaf"),+ ]+ )+ def test_repeated_views_are_not_cycles_or_nested_depth(self, _name: str, query: str, body: str) -> None:+ with patch("posthog.hogql.resolver.MAX_VIEW_DEPTH", 1):+ resolved = resolve_types(parse_select(query), self._context({"leaf": body}), "hogql")+ self.assertIsNotNone(resolved.type)++ @parameterized.expand([("default", False), ("direct_resolver", True)])+ def test_repeated_expansions_share_a_limit(self, _name: str, direct: bool) -> None:+ views = {"leaf": "select 1 as x"}+ previous = "leaf"+ for name in ("middle", "top"):+ views[name] = f"select a.x from {previous} a join {previous} b on a.x = b.x"+ previous = name+ context = self._context(views)+ with patch("posthog.hogql.compilation.MAX_VIEW_EXPANSIONS", 6):+ with self.assertRaisesRegex(QueryError, "6 saved view expansions"):+ if direct:+ Resolver(context, "hogql").visit(parse_select("select * from top"))+ else:+ resolve_types(parse_select("select * from top"), context, "hogql")+ with patch("posthog.hogql.compilation.MAX_VIEW_EXPANSIONS", 7):+ self.assertIsNotNone(resolve_types(parse_select("select * from top"), context, "hogql").type)++ def test_nested_depth_is_bounded(self) -> None:+ context = self._context({"first": "select * from second", "second": "select 1"})+ with patch("posthog.hogql.resolver.MAX_VIEW_DEPTH", 1):+ with self.assertRaisesRegex(QueryError, "1 nested saved views"):+ resolve_types(parse_select("select * from first"), context, "hogql")++ def test_repeated_views_share_sql_size_budget(self) -> None:+ body = "select '✓' as x"+ context = self._context({"leaf": body})+ with patch("posthog.hogql.compilation.MAX_EXPANDED_VIEW_SQL_BYTES", len(body.encode("utf-8"))):+ self.assertIsNotNone(resolve_types(parse_select("select * from leaf"), context, "hogql").type)+ with self.assertRaisesRegex(QueryError, "SQL expansion size limit"):+ resolve_types(parse_select("select a.x from leaf a cross join leaf b"), context, "hogql")++ def test_preparation_and_nested_resolvers_share_expansion_budget(self) -> None:+ context = self._context({"leaf": "select 1 as x"})++ def resolve_another_view(node: ast.SelectQuery | ast.SelectSetQuery) -> ast.SelectQuery | ast.SelectSetQuery:+ resolve_types(parse_select("select * from leaf"), context, "hogql")+ return node++ with patch("posthog.hogql.compilation.MAX_VIEW_EXPANSIONS", 1):+ with self.assertRaisesRegex(QueryError, "1 saved view expansions"):+ prepare_and_print_ast(+ parse_select("select * from leaf"),+ context,+ "hogql",+ prepared_ast_transform=resolve_another_view,+ )++ def test_printing_uses_preparation_deadline_and_next_query_gets_fresh_budget(self) -> None:+ context = self._context({})+ with patch("posthog.hogql.compilation.time.monotonic", return_value=0.0) as clock:++ def advance_clock(node: ast.SelectQuery | ast.SelectSetQuery) -> ast.SelectQuery | ast.SelectSetQuery:+ clock.return_value = COMPILATION_TIMEOUT_SECONDS + 1+ return node++ with self.assertRaisesRegex(QueryError, "compilation exceeded its time limit"):+ prepare_and_print_ast(parse_select("select 1"), context, "hogql", prepared_ast_transform=advance_clock)+ sql, _ = prepare_and_print_ast(parse_select("select 1"), context, "hogql")+ self.assertIn("SELECT 1", sql)++ def test_deadline_is_checked_during_view_expansion(self) -> None:+ context = self._context({"leaf": "select 1 as x"})+ with patch("posthog.hogql.compilation.time.monotonic", return_value=0.0) as clock:++ def parse_slow_view(query: str) -> ast.SelectQuery | ast.SelectSetQuery:+ parsed = parse_select(query)+ clock.return_value = COMPILATION_TIMEOUT_SECONDS + 1+ return parsed++ with patch("posthog.hogql.resolver.parse_select", side_effect=parse_slow_view):+ with self.assertRaisesRegex(QueryError, "compilation exceeded its time limit"):+ resolve_types(parse_select("select * from leaf"), context, "hogql")++class TestResolver(BaseTest):maxDiff = Nonesnapshot: Anydiff --git a/posthog/hogql/visitor.py b/posthog/hogql/visitor.pyindex 3a03ea2d7655..e8e54736f2b5 100644--- a/posthog/hogql/visitor.py+++ b/posthog/hogql/visitor.py@@ -4,6 +4,7 @@ from typing import Any, Generic, Optional, TypeVarfrom posthog.hogql import astfrom posthog.hogql.ast import SelectSetNodefrom posthog.hogql.base import AST, Expr+from posthog.hogql.compilation import compilation_budgetfrom posthog.hogql.errors import BaseHogQLErrorfrom posthog.hogql.utils import is_simple_value@@ -27,6 +28,9 @@ def clear_locations(expr: T_AST) -> T_AST:class Visitor(Generic[T]):def visit(self, node: AST | None) -> T:+ budget = compilation_budget.get()+ if budget is not None:+ budget.check_time()if node is None:return node # type: ignorediff --git a/products/data_modeling/backend/models/modeling.py b/products/data_modeling/backend/models/modeling.pyindex 196a0d004fa2..661b255efcdf 100644--- a/products/data_modeling/backend/models/modeling.py+++ b/products/data_modeling/backend/models/modeling.py@@ -135,6 +135,7 @@ class BoundedResolver(Resolver):When `enforce_bounds=False`, depth and deadline violations emit metrics butdo not raise. Cycle detection still raises unconditionally — without it theresolver would loop forever and the deadline check would never run.+ The core Resolver's hard compilation limits still apply in observe-only mode.The deadline is checked on every `visit()` call, not only at view boundaries,so expensive non-join work between views is also interruptible. Metricsdiff --git a/products/data_warehouse/backend/models/test/test_modeling.py b/products/data_warehouse/backend/models/test/test_modeling.pyindex e59fbaf5c2bd..042254c3e3bf 100644--- a/products/data_warehouse/backend/models/test/test_modeling.py+++ b/products/data_warehouse/backend/models/test/test_modeling.py@@ -550,9 +550,6 @@ class TestResolutionMetrics(BaseTest):class TestResolverFactoryInjection(BaseTest):- """Confirms prepare_ast_for_printing honors resolver_factory — proves the workflow path can- swap in BoundedResolver instead of the unbounded base Resolver."""-def test_prepare_ast_for_printing_uses_injected_bounded_resolver(self):from posthog.hogql.printer import prepare_ast_for_printing@@ -583,8 +580,7 @@ class TestResolverFactoryInjection(BaseTest):with pytest.raises(ResolutionDepthExceededError):prepare_ast_for_printing(query_node, context=context, dialect="clickhouse", resolver_factory=factory)- def test_prepare_ast_for_printing_default_resolver_is_unbounded(self):- """Sanity check: without the factory, no depth bound is applied — proves the kwarg is the opt-in."""+ def test_prepare_ast_for_printing_default_resolver_accepts_short_view_chain(self):from posthog.hogql.printer import prepare_ast_for_printingDataWarehouseSavedQuery.objects.create(@@ -601,7 +597,6 @@ class TestResolverFactoryInjection(BaseTest):query_node = parse_select("select * from v1")context = HogQLContext(team_id=self.team.pk, team=self.team, enable_select_queries=True)- # Should not raise — the default base Resolver has no depth bound.prepare_ast_for_printing(query_node, context=context, dialect="clickhouse")def test_shared_deadline_anchor_spans_multiple_resolvers(self):diff --git a/products/data_warehouse/backend/presentation/views/saved_query/editing.py b/products/data_warehouse/backend/presentation/views/saved_query/editing.pyindex d82bd54bde78..3a96dc3e711e 100644--- a/products/data_warehouse/backend/presentation/views/saved_query/editing.py+++ b/products/data_warehouse/backend/presentation/views/saved_query/editing.py@@ -347,6 +347,15 @@ class DataWarehouseSavedQuerySerializer(capture_exception(e)logger.exception("Failed to report view action", analytics_event=event)+ def _validate_view_dependencies(self, team: Team, name: str, query: str) -> None:+ try:+ get_parents_from_model_query(team, name, query, database=self.context["database"])+ except (RecursionError, ResolutionCycleError):+ raise serializers.ValidationError("Model contains a cycle")+ except Exception as error:+ _capture_unless_user_error(error)+ raise _view_types_validation_error(error)+def create(self, validated_data):validated_data["team_id"] = self.context["team_id"]validated_data["created_by"] = self.context["request"].user@@ -358,6 +367,8 @@ class DataWarehouseSavedQuerySerializer(validated_data.pop("edited_history_id", None)view = DataWarehouseSavedQuery(**validated_data)+ self._validate_view_dependencies(self.context["get_team"](), view.name, validated_data["query"]["query"])+if not soft_update:try:# The columns will be inferred from the query@@ -471,6 +482,12 @@ class DataWarehouseSavedQuerySerializer(inferred_columns: dict[str, dict[str, Any]] | None = Noneinferred_external_tables: list[str] | None = None+ if "query" in validated_data or "name" in validated_data:+ self._validate_view_dependencies(+ instance.team,+ validated_data.get("name", instance.name),+ validated_data.get("query", instance.query)["query"],+ )if "query" in validated_data and not soft_update:# Inference runs the query on ClickHouse, so it happens before the row lock, on a probe# carrying the pending query and name rather than the stored ones, as create() does.@@ -480,12 +497,6 @@ class DataWarehouseSavedQuerySerializer(query=validated_data["query"],)try:- get_parents_from_model_query(- instance.team,- probe.name,- validated_data["query"]["query"],- database=self.context["database"],- )client_types = self.context["request"].data.get("types", [])if len(client_types) == 0:inferred_columns = probe.get_columns(user=self.context["request"].user)diff --git a/products/data_warehouse/backend/tests/api/test_saved_query.py b/products/data_warehouse/backend/tests/api/test_saved_query.pyindex c53b6881663d..e2d8b394a9fa 100644--- a/products/data_warehouse/backend/tests/api/test_saved_query.py+++ b/products/data_warehouse/backend/tests/api/test_saved_query.py@@ -55,6 +55,35 @@ from products.warehouse_sources.backend.facade.types import DataWarehouseManagedclass TestSavedQuery(APIBaseTest):+ def test_query_rejects_excessive_view_expansion_before_execution(self) -> None:+ previous = ""+ for name in ("leaf", "middle", "top"):+ query = f"select a.x from {previous} a join {previous} b on a.x = b.x" if previous else "select 1 as x"+ view = DataWarehouseSavedQuery(team=self.team, name=name, query={"kind": "HogQLQuery", "query": query})+ view.set_columns({"x": {"hogql": "integer", "clickhouse": "Int64", "valid": True}})+ view.save()+ previous = name++ with (+ patch("posthog.hogql.compilation.MAX_VIEW_EXPANSIONS", 6),+ patch("posthog.hogql.query.sync_execute") as execute,+ ):+ response = self.client.post(+ f"/api/environments/{self.team.id}/query/",+ {"query": {"kind": "HogQLQuery", "query": "select * from top"}, "refresh": "force_blocking"},+ )+ self.assertEqual(response.status_code, 400, response.content)+ self.assertIn("saved view expansions", response.json()["detail"])+ execute.assert_not_called()++ def test_soft_create_still_skips_column_inference(self) -> None:+ with patch.object(DataWarehouseSavedQuery, "get_columns", side_effect=AssertionError("inference ran")):+ response = self.client.post(+ f"/api/environments/{self.team.id}/warehouse_saved_queries/",+ {"name": "constant_view", "query": {"kind": "HogQLQuery", "query": "select 1"}, "soft_update": True},+ )+ self.assertEqual(response.status_code, 201, response.content)+def test_create_with_folder(self):folder = DataWarehouseSavedQueryFolder.objects.create(team=self.team, name="Marketing", created_by=self.user)@@ -1857,8 +1886,15 @@ class TestSavedQuery(APIBaseTest):# Verify get_columns was calledmock_get_columns.assert_called_once()- @parameterized.expand([("direct", False), ("through_another_view", True)])- def test_update_rejects_query_cycle(self, _name: str, use_intermediate_view: bool) -> None:+ @parameterized.expand(+ [+ ("direct", False, False),+ ("through_another_view", True, False),+ ("soft_direct", False, True),+ ("soft_through_another_view", True, True),+ ]+ )+ def test_update_rejects_query_cycle(self, _name: str, use_intermediate_view: bool, soft_update: bool) -> None:original_query = "select event as event from events LIMIT 100"response = self.client.post(f"/api/environments/{self.team.id}/warehouse_saved_queries/",@@ -1887,6 +1923,7 @@ class TestSavedQuery(APIBaseTest):{"query": {"kind": "HogQLQuery", "query": f"select * from {referenced_view}"},"edited_history_id": saved_query["latest_history_id"],+ "soft_update": soft_update,},)