: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(subscriptions): stop AI reports from using group columns on warehouse tables (#127205)
Vasco de Krijgerbffff8620274
diff --git a/products/subscriptions/backend/temporal/ai_subscription/prompts.py b/products/subscriptions/backend/temporal/ai_subscription/prompts.pyindex 045e35897336..76a8f53e8bab 100644--- a/products/subscriptions/backend/temporal/ai_subscription/prompts.py+++ b/products/subscriptions/backend/temporal/ai_subscription/prompts.py@@ -84,7 +84,16 @@ HOGQL_AI_SUBSCRIPTION_RULES = """Scheduled-report query-writing rules:reusable window tokens or preserve the failed query's existing tokens or literal bounds.- Use only tables, fields, events, and properties present in the supplied project context.- Keep results cheap and bounded. Prefer aggregation over raw rows, avoid wildcards on large tables,- and cap results with LIMIT 250."""+ and cap results with LIMIT 250.+- `group_<index>` and `$group_<index>` exist only on `events`. Data warehouse tables and views have no+ group columns, so never reference them there. To add an account's name or properties to a warehouse+ row, LEFT JOIN the `groups` table through an aliased subquery on that row's account key, for example+ `LEFT JOIN (SELECT key, toString(properties.name) AS account_name FROM groups WHERE index = 0) AS g+ ON g.key = toString(t.org_id)`. `groups` is always available, and this JOIN subquery does not count+ against the single-subquery limit. Alias every subquery used in a JOIN and prefix its columns with+ the alias.+- An error's "Available fields" list is a hint, not the full schema: it can end with "and N more". Prefer+ those columns, and keep any other column the project context confirms."""HOGQL_AI_SUBSCRIPTION_QUERY_WRITING_RULES = "\n\n".join((@@ -308,12 +317,13 @@ with an ASC tail to read both the most- and least-active events regardless of hoJoined data available WITHOUT writing a JOIN (the engine joins these automatically on `events`):- Person properties: `person.properties.<name>` (e.g. `person.properties.plan`). The property namesavailable for this project are listed in <project_context>.-- Group / account properties: `group_<index>.properties.<name>` (e.g. `group_0.properties.name`).+- Group / account properties on `events`: `group_<index>.properties.<name>` (e.g. `group_0.properties.name`).The index-to-type mapping for this project is listed in <project_context>.To COUNT or aggregate the groups/accounts themselves (not a property of theirs), use the rawgroup-key column `$group_<index>` (leading `$`). A bare `group_<index>` without a trailing`.properties.<name>` is NOT a scalar column and fails with "Field not found: group_<index>". See- the count-distinct-accounts pattern below.+ the count-distinct-accounts pattern below. These group forms work only on `events`; for a data+ warehouse table or view, join the `groups` table as described in the query-writing rules above.- Session attributes: `session.$session_duration` (seconds), `session.$pageview_count`,`session.$channel_type`, `session.$entry_pathname`, `session.$is_bounce`, `session.$end_timestamp`.Reference these as plain columns inside a single `FROM events` SELECT — never write `JOIN` for them.@@ -486,11 +496,12 @@ rewrite MUST follow the same HogQL syntax constraints used by the planner:FROM clauses, or scalar/IN comparisons. If the original used a CTE for cross-window comparison,rewrite it with conditional aggregation (`countIf(cond)`, `uniqIf(field, cond)`, `sumIf(...)`).- No LATERAL joins, recursive CTEs, UNNEST, or ARRAY JOIN on subqueries.-- Schema note for "Field not found: group_<index>": to count or aggregate a group/account, the raw+- Schema note for "Field not found: group_<index>" on `events`: to count or aggregate a group/account, the rawgroup-key column is `$group_<index>` with a leading `$` (e.g. `uniq($group_2)`,`uniqIf($group_2, cond)`). A bare `group_<index>` is only valid as `group_<index>.properties.<name>`;used as a scalar it does not resolve; replace it with `$group_<index>`. The same `$`-prefixed form- applies to person/session keys only via their documented paths, so do not add `$` elsewhere.+ applies to person/session keys only via their documented paths, so do not add `$` elsewhere. On a data+ warehouse table or view neither form exists: join the `groups` table as described in the rules above.- Time window: PRESERVE the original query's window tokens or literal `toDateTime('…')` boundsverbatim — those are the report's fixed analysis window. `{{date_range}}` and `{{compare_date_range}}`are whole time filters (`timestamp >= … AND timestamp < …`); `{{window_start}}`, `{{window_end}}` and