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(cohorts): inline positional GROUP BY/ORDER BY in static cohort queries (#73867)

posthog[bot]04b87347e862
diff --git a/products/cohorts/backend/models/test/test_util.py b/products/cohorts/backend/models/test/test_util.py
index 8e394358349c..8bcf2c21add0 100644
--- a/products/cohorts/backend/models/test/test_util.py
+++ b/products/cohorts/backend/models/test/test_util.py
@@ -777,6 +777,75 @@ class TestCohortUtils(BaseTest):
self.assertIn("Could not find a person_id, actor_id, id, or distinct_id column", str(cm.exception))
+ @parameterized.expand(
+ [
+ # Unbounded query: positional GROUP BY must be inlined (bounded ORDER BY is exercised below).
+ (
+ "unbounded_group_by",
+ "SELECT count(), person_id FROM events GROUP BY 2",
+ "group by 2",
+ "group by",
+ ),
+ # Bounded query keeps its ORDER BY, so a positional ORDER BY must be inlined too.
+ (
+ "bounded_order_by",
+ "SELECT person_id, count() AS cnt FROM events GROUP BY 1 ORDER BY 2 DESC LIMIT 100",
+ "order by 2",
+ "order by count()",
+ ),
+ # LIMIT BY is also positional and marks the query bounded, so its ordinal must be inlined too.
+ (
+ "bounded_limit_by",
+ "SELECT person_id, event FROM events ORDER BY person_id LIMIT 1 BY 2",
+ "by 2",
+ "by events.event",
+ ),
+ ]
+ )
+ def test_print_cohort_hogql_query_resolves_positional_references(
+ self, _name, query, dangling_ordinal, resolved_clause
+ ):
+ # A positional GROUP BY/ORDER BY ordinal (e.g. `GROUP BY 2`) references the Nth SELECT
+ # expression. Collapsing the SELECT to the single actor column leaves any ordinal past the
+ # first pointing out of bounds, so ClickHouse rejects the query and the cohort never fills.
+ cohort = Cohort.objects.create(
+ team=self.team,
+ name="Test Positional Cohort",
+ query={"kind": "HogQLQuery", "query": query},
+ )
+
+ context = HogQLContext(team_id=self.team.id, enable_select_queries=True)
+
+ sql = print_cohort_hogql_query(cohort, context, team=self.team).lower()
+
+ self.assertIn("as actor_id", sql)
+ # The ordinal must be inlined to the real expression, not left dangling past the collapsed SELECT.
+ self.assertNotIn(dangling_ordinal, sql)
+ # Dropping the clause outright would also remove the ordinal, but it would change who lands in
+ # the cohort, so the clause itself has to survive.
+ self.assertIn(resolved_clause, sql)
+
+ def test_print_cohort_hogql_query_leaves_positional_references_on_wildcard_select(self):
+ # `*` is a single node in the SELECT list until the resolver fans it out into one node per
+ # column, and ClickHouse numbers positional arguments against that expanded list. Guessing
+ # which expression ordinal 2 means here would quietly build a different cohort, so the
+ # ordinals stay untouched and the query keeps failing the way it does without any inlining.
+ cohort = Cohort.objects.create(
+ team=self.team,
+ name="Test Wildcard Positional Cohort",
+ query={
+ "kind": "HogQLQuery",
+ "query": "SELECT *, count() AS cnt FROM events GROUP BY 1 ORDER BY 2 DESC LIMIT 100",
+ },
+ )
+
+ context = HogQLContext(team_id=self.team.id, enable_select_queries=True)
+
+ sql = print_cohort_hogql_query(cohort, context, team=self.team).lower()
+
+ self.assertIn("group by 1", sql)
+ self.assertIn("order by 2", sql)
+
class TestGetNestedCohortIds(BaseTest):
def test_no_cohort_references(self):
diff --git a/products/cohorts/backend/models/util.py b/products/cohorts/backend/models/util.py
index 7ba45a0939c3..fa643ce683df 100644
--- a/products/cohorts/backend/models/util.py
+++ b/products/cohorts/backend/models/util.py
@@ -24,6 +24,7 @@ from posthog.hogql.modifiers import create_default_modifiers_for_team
from posthog.hogql.parser import parse_select
from posthog.hogql.printer import prepare_and_print_ast
from posthog.hogql.resolver_utils import extract_select_queries
+from posthog.hogql.visitor import clone_expr
from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.client.connection import ClickHouseUser, Workload
@@ -353,6 +354,64 @@ def _sanitize_query_for_cohort(query_dict: dict) -> dict:
return query_dict
+def _select_list_positions_are_known(select_list: list[ast.Expr]) -> bool:
+ """Whether ordinal N reliably maps to `select_list[N - 1]`.
+
+ `*`, `table.*` and `COLUMNS(...)` are each a single node here and only fan out into one node per
+ real column later, in the resolver. ClickHouse numbers positional arguments against that expanded
+ list, so mapping an ordinal onto the unexpanded list would point at the wrong expression and
+ silently change who ends up in the cohort. Leave those queries alone so they keep failing loudly
+ instead.
+ """
+ for expr in select_list:
+ if isinstance(expr, ast.Alias):
+ expr = expr.expr
+ if isinstance(expr, ast.ColumnsExpr):
+ return False
+ if isinstance(expr, ast.Field) and expr.chain and str(expr.chain[-1]) == "*":
+ return False
+ return True
+
+
+def _inline_positional_references(select_query: ast.SelectQuery) -> None:
+ """Replace positional GROUP BY/ORDER BY/LIMIT BY ordinals with the SELECT expression they point at.
+
+ In ClickHouse a bare integer in GROUP BY/ORDER BY/LIMIT BY (`GROUP BY 2`) is a 1-based reference
+ into the SELECT list. `print_cohort_hogql_query` collapses that list to a single actor column, so
+ any ordinal other than 1 would dangle afterwards. Resolving each ordinal against the original
+ SELECT list up front keeps the grouping/ordering semantics intact once the list shrinks.
+
+ Ordinals nested inside GROUP BY GROUPING SETS are not resolved, because those entries are tuples
+ of expressions rather than bare ordinals. Such a query keeps failing the way it does today.
+ """
+ if not _select_list_positions_are_known(select_query.select):
+ return
+
+ select_list = select_query.select
+
+ def resolve(expr: ast.Expr) -> ast.Expr:
+ if (
+ isinstance(expr, ast.Constant)
+ and isinstance(expr.value, int)
+ and not isinstance(expr.value, bool)
+ and 1 <= expr.value <= len(select_list)
+ ):
+ target = select_list[expr.value - 1]
+ return clone_expr(target.expr if isinstance(target, ast.Alias) else target)
+ return expr
+
+ if select_query.group_by:
+ select_query.group_by = [resolve(expr) for expr in select_query.group_by]
+
+ if select_query.order_by:
+ for order_expr in select_query.order_by:
+ order_expr.expr = resolve(order_expr.expr)
+
+ # Only the LIMIT BY expressions are positional; `n` is the per-group row count, not an ordinal.
+ if select_query.limit_by:
+ select_query.limit_by.exprs = [resolve(expr) for expr in select_query.limit_by.exprs]
+
+
def print_cohort_hogql_query(cohort: Cohort, hogql_context: HogQLContext, *, team: Team) -> str:
from posthog.hogql_queries.query_runner import get_query_runner
@@ -368,6 +427,13 @@ def print_cohort_hogql_query(cohort: Cohort, hogql_context: HogQLContext, *, tea
uses_actor_id = False
for select_query in extract_select_queries(query):
+ # Positional GROUP BY/ORDER BY ordinals (e.g. `GROUP BY 2`) reference the Nth SELECT
+ # expression. We're about to collapse the SELECT list down to a single actor column,
+ # which leaves those ordinals pointing past the end of the new one-column list, so
+ # ClickHouse rejects the query with an out-of-bounds positional argument error. Inline
+ # the referenced expression now so the reference survives the collapse.
+ _inline_positional_references(select_query)
+
# Ordering is meaningless for an unbounded cohort, and an ORDER BY that references a
# computed select alias (e.g. `ai_active_days`) would dangle once we collapse the
# SELECT to just the actor column below, breaking HogQL resolution. Drop it — but only