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(hogql): alias joined subqueries (#99180)

Paul D'Ambra1e5b23570ae5
diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.py
index 1aff447abb1b..5b476ce65d0c 100644
--- a/posthog/hogql/printer/test/test_printer.py
+++ b/posthog/hogql/printer/test/test_printer.py
@@ -2250,6 +2250,20 @@ class TestPrinter(BaseTest):
),
)
+ @parameterized.expand(
+ [
+ ("select_query", "SELECT 1 FROM events CROSS JOIN (SELECT 1)"),
+ ("select_set_query", "SELECT 1 FROM events CROSS JOIN (SELECT 1 UNION ALL SELECT 2)"),
+ ]
+ )
+ def test_select_cross_join_subquery_uses_generated_alias(self, _name: str, query: str) -> None:
+ self.assertIn("AS __join_1", self._select(query))
+
+ def test_select_cross_join_subquery_avoids_later_alias(self) -> None:
+ printed = self._select("SELECT 1 FROM events CROSS JOIN (SELECT 1) CROSS JOIN events AS __join_1")
+
+ self.assertIn("AS __join_2", printed)
+
def test_left_join_team_id_in_on_clause(self):
# LEFT JOINs should have team_id in ON clause, not WHERE, to preserve LEFT JOIN semantics
context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)
diff --git a/posthog/hogql/resolver.py b/posthog/hogql/resolver.py
index 7f703f132f30..e56561a25fcb 100644
--- a/posthog/hogql/resolver.py
+++ b/posthog/hogql/resolver.py
@@ -1561,8 +1561,11 @@ class Resolver(CloningVisitor):
if node.constraint and node.constraint.constraint_type == "USING":
# visit USING constraint before adding the table to avoid ambiguous names
node.constraint = self.visit_join_constraint(node.constraint)
- if node.alias is None and self._join_chain_has_using(node):
- node.alias = self._synthesize_using_join_alias(scope)
+ if node.alias is None:
+ if self._join_chain_has_using(node):
+ node.alias = self._synthesize_using_join_alias(scope)
+ 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))
@@ -1779,6 +1782,20 @@ class Resolver(CloningVisitor):
self._synthetic_using_join_aliases.add(alias)
return alias
+ def _synthesize_join_alias(self, scope: ast.SelectQueryType, node: ast.JoinExpr) -> str:
+ """Alias a joined sub-select because ClickHouse requires a name for it."""
+ reserved_aliases = set(scope.tables)
+ next_join = node.next_join
+ while next_join is not None:
+ if next_join.alias is not None:
+ reserved_aliases.add(next_join.alias)
+ next_join = next_join.next_join
+
+ index = 1
+ while f"__join_{index}" in reserved_aliases:
+ index += 1
+ return f"__join_{index}"
+
def _using_constraint_column_names(self, constraint: ast.JoinConstraint) -> list[str]:
exprs = constraint.expr.exprs if isinstance(constraint.expr, ast.Tuple) else [constraint.expr]
column_names: list[str] = []