Skip to content

: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.

fix(endpoints): return can't-materialize for nested alias conflicts (#115924)

posthog[bot]0cf30cc4ddd3
diff --git a/products/endpoints/backend/materialization_transforms.py b/products/endpoints/backend/materialization_transforms.py
index cc1eff5e6525..62e9498e8d38 100644
--- a/products/endpoints/backend/materialization_transforms.py
+++ b/products/endpoints/backend/materialization_transforms.py
@@ -574,17 +574,30 @@ def _select_alias_conflict(select: ast.SelectQuery, vars_in_context: list[Materi
return None
-def _context_select(
- ast_node: ast.SelectQuery | ast.SelectSetQuery, cte_name: Optional[str]
-) -> Optional[ast.SelectQuery]:
- """The SELECT a variable's columns get added to — the CTE body for CTE-bound variables,
- else the top-level query — mirroring the contexts the transformer visits."""
- if cte_name is None:
- return ast_node if isinstance(ast_node, ast.SelectQuery) else None
- if isinstance(ast_node, ast.SelectQuery) and ast_node.ctes and cte_name in ast_node.ctes:
- body = ast_node.ctes[cte_name].expr
- return body if isinstance(body, ast.SelectQuery) else None
- return None
+class _AliasConflictFinder(TraversingVisitor):
+ def __init__(self, contexts: dict[Optional[str], list[MaterializableVariable]]) -> None:
+ super().__init__()
+ self._contexts = contexts
+ self._current_cte_name: Optional[str] = None
+ self.conflict: Optional[str] = None
+
+ def visit_select_query(self, node: ast.SelectQuery) -> None:
+ for cte_name, cte in (node.ctes or {}).items():
+ prev_cte = self._current_cte_name
+ self._current_cte_name = cte_name
+ self.visit(cte.expr)
+ self._current_cte_name = prev_cte
+
+ original_ctes = node.ctes
+ node.ctes = None
+ try:
+ super().visit_select_query(node)
+ finally:
+ node.ctes = original_ctes
+
+ vars_in_context = self._contexts.get(self._current_cte_name)
+ if self.conflict is None and vars_in_context:
+ self.conflict = _select_alias_conflict(node, vars_in_context)
def _find_alias_conflict(
@@ -601,11 +614,9 @@ def _find_alias_conflict(
contexts: dict[Optional[str], list[MaterializableVariable]] = {}
for var in result_vars:
contexts.setdefault(var.cte_name, []).append(var)
- for cte_name, vars_in_context in contexts.items():
- select = _context_select(ast_node, cte_name)
- if select is not None and (conflict := _select_alias_conflict(select, vars_in_context)) is not None:
- return _alias_conflict_reason(conflict)
- return None
+ finder = _AliasConflictFinder(contexts)
+ finder.visit(ast_node)
+ return _alias_conflict_reason(finder.conflict) if finder.conflict is not None else None
def _has_joins(node: ast.SelectQuery) -> bool:
diff --git a/products/endpoints/backend/tests/test_variable_materialization.py b/products/endpoints/backend/tests/test_variable_materialization.py
index aaca2434d1cd..35aa54f4199b 100644
--- a/products/endpoints/backend/tests/test_variable_materialization.py
+++ b/products/endpoints/backend/tests/test_variable_materialization.py
@@ -1070,6 +1070,40 @@ class TestQueryTransformation(APIBaseTest):
assert "conflicts with an existing SELECT alias" in reason
assert var_infos == []
+ @parameterized.expand(
+ [
+ (
+ "join_subquery",
+ "SELECT e.event, count() AS c FROM events e "
+ "JOIN (SELECT person_id, properties.segment AS cohort FROM events) s ON e.person_id = s.person_id "
+ "WHERE e.properties.cohort = {variables.cohort} "
+ "GROUP BY e.event",
+ ),
+ (
+ "union_leg",
+ "SELECT event, count() AS c FROM ("
+ "SELECT event, properties, properties.segment AS cohort FROM events "
+ "UNION ALL SELECT event, properties, properties.segment AS cohort FROM events"
+ ") WHERE properties.cohort = {variables.cohort} "
+ "GROUP BY event",
+ ),
+ ]
+ )
+ def test_alias_collision_in_nested_select_rejected_preflight(self, _name, query_str):
+ query = {
+ "kind": "HogQLQuery",
+ "query": query_str,
+ "variables": {
+ "var-1": {"variableId": "var-1", "code_name": "cohort", "value": ""},
+ },
+ }
+
+ can_materialize, reason, var_infos = analyze_variables_for_materialization(query)
+
+ assert can_materialize is False
+ assert "conflicts with an existing SELECT alias" in reason
+ assert var_infos == []
+
def test_transform_alias_collision_raises_not_supported(self):
# Backstop: if the transform is reached directly (bypassing pre-flight) on a colliding
# query, it raises MaterializationNotSupportedError (a 400), not a bare ValueError (a 500).