: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-modeling): record parents from subqueries outside FROM (#100837)
Jovan Sakovic0572c1d68a3e
diff --git a/products/data_modeling/backend/models/modeling.py b/products/data_modeling/backend/models/modeling.pyindex d4b4dee81327..01ba8c4d11af 100644--- a/products/data_modeling/backend/models/modeling.py+++ b/products/data_modeling/backend/models/modeling.py@@ -19,6 +19,7 @@ from posthog.hogql.database.s3_table import DataWarehouseTable as HogQLDataWarehfrom posthog.hogql.errors import QueryErrorfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.resolver import Resolver, ResolverFactory+from posthog.hogql.visitor import TraversingVisitorfrom posthog.models.team import Teamfrom posthog.models.user import User@@ -478,12 +479,54 @@ def _select_queries_with_scope(return result+class _SubqueryCollector(TraversingVisitor):+ """Collect the subqueries a FROM-chain walk never reaches.++ Stops at every nested select rather than descending into it, so the caller can feed+ each one back through its own loop with the CTE scope that query can see. Skips a+ join's table and a CTE's body for the same reason: both already have an owner.+ """++ def __init__(self, root: ast.SelectQuery) -> None:+ self._root = root+ self.selects: list[ast.SelectQuery | ast.SelectSetQuery] = []++ def visit_select_query(self, node: ast.SelectQuery) -> None:+ if node is self._root:+ super().visit_select_query(node)+ return+ self.selects.append(node)++ def visit_select_set_query(self, node: ast.SelectSetQuery) -> None:+ self.selects.append(node)++ def visit_join_expr(self, node: ast.JoinExpr) -> None:+ # the FROM-chain walk already reaches node.table, but nothing else reaches an ON+ # constraint or a table function's arguments+ self.visit(node.constraint)+ for arg in node.table_args or []:+ self.visit(arg)+ self.visit(node.next_join)++ def visit_cte(self, node: ast.CTE) -> None:+ # a CTE body is expanded where it is referenced, so that an unreferenced one stays+ # out of the parent set+ pass+++def _subqueries_outside_from(query: ast.SelectQuery) -> list[ast.SelectQuery | ast.SelectSetQuery]:+ collector = _SubqueryCollector(query)+ collector.visit(query)+ return collector.selects++def get_parents_from_model_query(team: Team, model_name: str, model_query: str, database: Database | None = None) -> set[str]:"""Get parents from a given query.- The parents of a query are any names in the `FROM` clause of the query.+ The parents of a query are every name it reads: the `FROM` clause, and any subquery+ that sits elsewhere — a `WHERE`, a `HAVING`, the select list, a join's `ON`.Uses BoundedResolver to detect circular dependencies, cap nested viewdepth, and enforce a wall-clock deadline on view references. Resolver-levelmetrics (`DAG_RESOLUTION_*`) are emitted from the resolver itself, so this@@ -534,12 +577,32 @@ def get_parents_from_model_query(# lifetime of this function, so the id() identity is stable.expanded_ctes: set[int] = set()+ def record_or_queue(select: ast.SelectQuery | ast.SelectSetQuery, scope: CteScope) -> None:+ """Record a view by name, or queue an anonymous query to be walked.++ A saved-query view is itself the parent, so descending past it would report its+ sources instead of it. A SelectQuery view carries its name; a union body doesn't,+ so recover that one from the resolver's id map.+ """+ if isinstance(select, ast.SelectQuery):+ view_name = select.view_name+ else:+ view_name = resolver.union_view_name_by_id.get(id(select))++ if view_name is not None:+ parents.add(view_name)+ else:+ queries.extend(_select_queries_with_scope(select, scope))+while queries:query, scope = queries.pop()if query.ctes:scope = (query.ctes, *scope)+ for subquery in _subqueries_outside_from(query):+ record_or_queue(subquery, scope)+# a FROM clause is a next_join chain; PIVOT/UNPIVOT over a join nests another chain,# so keep a stack of chains still to walkpending_joins: list[ast.JoinExpr | None] = [query.select_from]@@ -563,17 +626,7 @@ def get_parents_from_model_query(join = join.next_joincontinueelif isinstance(table, ast.SelectQuery | ast.SelectSetQuery):- # a saved-query view is the parent, so record it rather than descending past- # it into its own sources. A SelectQuery view carries its name; a union body- # doesn't, so recover it from the resolver's id map- if isinstance(table, ast.SelectQuery):- view_name = table.view_name- else:- view_name = resolver.union_view_name_by_id.get(id(table))- if view_name is not None:- parents.add(view_name)- else:- queries.extend(_select_queries_with_scope(table, scope))+ record_or_queue(table, scope)join = join.next_joincontinueelif isinstance(table, ast.ValuesQuery):diff --git a/products/data_warehouse/backend/models/test/test_modeling.py b/products/data_warehouse/backend/models/test/test_modeling.pyindex 2df76b265afa..0fb03215b61a 100644--- a/products/data_warehouse/backend/models/test/test_modeling.py+++ b/products/data_warehouse/backend/models/test/test_modeling.py@@ -129,6 +129,34 @@ GET_PARENTS_TEST_CASES = [""",set(),),+ (+ "select event from events where person_id in (select id from persons)",+ {"events", "persons"},+ ),+ (+ "select event, (select count() from persons) as total from events",+ {"events", "persons"},+ ),+ (+ "select event, count() as c from events group by event having count() > (select count() from persons)",+ {"events", "persons"},+ ),+ (+ "select event from events order by (select count() from persons)",+ {"events", "persons"},+ ),+ (+ "with cte as (select id from persons) select event from events where person_id in (select id from cte)",+ {"events", "persons"},+ ),+ (+ "select event from events prewhere person_id in (select id from persons)",+ {"events", "persons"},+ ),+ (+ "select a.event from events a join events b on a.person_id in (select id from persons)",+ {"events", "persons"},+ ),]@@ -544,6 +572,21 @@ class TestBoundedResolver(BaseTest):assert get_parents_from_model_query(self.team, "caller", query) == expected_parents+ @parameterized.expand(+ [+ ("where", "select * from shared where event in (select event from mid)", {"shared", "mid"}),+ (+ "nested_in_from_subquery",+ "select * from (select * from shared where event in (select event from mid))",+ {"shared", "mid"},+ ),+ ],+ )+ def test_view_read_from_a_subquery_is_a_parent(self, _name: str, query: str, expected_parents: set[str]):+ self._make_diamond()++ assert get_parents_from_model_query(self.team, "caller", query) == expected_parents+def test_cycle_raises_typed_error_with_initial_view(self):DataWarehouseSavedQuery.objects.create(team=self.team,