: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(exports): stop ai subscription planner emitting invalid HogQL (#71697)
Steven Shults070b8e296cba
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py b/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.pyindex fd52ab14dc23..35a3857656e5 100644--- a/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/prompts.py@@ -141,6 +141,13 @@ are common LLM mistakes that HogQL rejects:null-safe join keys with "Cannot determine join keys", so a JOIN will fail at execution time.(Person, session, and group/account data IS still available without a JOIN — see "Joined dataavailable" below.)+- `properties` (and `person.properties`) is a JSON string column, NOT a Map. Map functions+ (`mapKeys`, `mapValues`, `mapContains`) fail at execution. To enumerate an event's property KEYS+ use `JSONExtractKeys(properties)` — see the property-keys audit pattern below.+- Use `arrayFlatten(...)`; `flatten(...)` is not a HogQL function and fails validation.+- Never nest aggregate functions (e.g. `max(count())`, `sum(uniq(…))`). Compute each aggregate once+ and derive ratios from sibling aggregates in the same SELECT, guarding zero denominators+ (e.g. `countIf(cond) / nullIf(count(), 0) AS share`).- Window filter: write the placeholder token `{{date_range}}` verbatim where the window predicate goes.Never write `timestamp >= toDateTime('…')`, `now()`, `now() - INTERVAL …`, or `today()` for the window.- Time bucketing (for sub-windows WITHIN the range): `toStartOfHour(timestamp)`,@@ -242,6 +249,16 @@ Breakdown by a person property (USE the dotted path, NOT a JOIN):ORDER BY event_count DESCLIMIT 50+Property-keys audit (which property KEYS an event actually sends, e.g. "are properties being sent+that aren't in our spec?"). `properties` is a JSON string, so extract keys with+`JSONExtractKeys(properties)` and expand with `arrayJoin` — never `mapKeys(properties)`:+ SELECT arrayJoin(JSONExtractKeys(properties)) AS key, count() AS count+ FROM events+ WHERE event = '$pageview' AND {{date_range}}+ GROUP BY key+ ORDER BY count DESC+ LIMIT 50+Count distinct groups/accounts (the account itself, via the raw `$`-prefixed key, never bare`group_<index>`, which is only valid as `group_<index>.properties.<name>`):SELECT@@ -347,6 +364,14 @@ rewrite MUST follow the same HogQL syntax constraints used by the planner:UNNEST, or ARRAY JOIN on subqueries.- No JOINs of any kind, including self-joins on `event`. Use conditional aggregation instead(ClickHouse rejects HogQL's null-safe join keys).+- `properties` (and `person.properties`) is a JSON string column, NOT a Map, so Map functions+ (`mapKeys`, `mapValues`, `mapContains`) fail at execution. To enumerate property keys, replace+ `mapKeys(properties)` with `JSONExtractKeys(properties)` (expand rows with+ `arrayJoin(JSONExtractKeys(properties))`).+- `flatten(...)` is not a HogQL function; replace it with `arrayFlatten(...)`.+- Never nest aggregate functions (e.g. `max(count())`, `sum(uniq(…))`). Compute each aggregate once+ and derive ratios from sibling aggregates in the same SELECT, guarding zero denominators+ (e.g. `countIf(cond) / nullIf(count(), 0)`).- Schema note for "Field not found: group_<index>": 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>`;