: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
chore(hogql): document the flag_evaluations person_id join limitation (#104868)
Phil Haack096dcd384ee1
diff --git a/ee/hogai/chat_agent/sql/prompts.py b/ee/hogai/chat_agent/sql/prompts.pyindex 2d19be7d5b36..db87ef21eee6 100644--- a/ee/hogai/chat_agent/sql/prompts.py+++ b/ee/hogai/chat_agent/sql/prompts.py@@ -64,7 +64,8 @@ Standardized events/properties such as pageview or screen start with `$`. Custom`virtual_table` and `lazy_table` fields are connections to linked tables, e.g. the virtual table field `person` allows accessing person properties like so: `person.properties.foo`.<person_id_join_limitation>-CRITICAL: There is a known issue with queries where JOIN constraints reference events.person_id fields.+CRITICAL: There is a known issue with queries where JOIN constraints reference the person_id field of+events or flag_evaluations.TECHNICAL CAUSE:The person_id fields are ExpressionFields that expand to expressions referencing override tables@@ -88,6 +89,13 @@ PROBLEMATIC PATTERNS:But e_all__override is defined later in the SQL, causing the error.+3. Joining flag_evaluations to any table on flag_evaluations.person_id:+ ❌ FROM posthog.flag_evaluations fe JOIN events e ON fe.person_id = e.person_id++ flag_evaluations.person_id is the same kind of ExpressionField, so this fails with+ "QueryError: Field not found: flag_evaluation_person_id", naming a column the query never used.+ Note this table is only reachable as `posthog.flag_evaluations`; the bare name does not resolve.+REQUIRED WORKAROUNDS:1. For accessing person data, use the person virtual table from events:✅ SELECT e.person.id, e.person.properties.email, e.event@@ -107,7 +115,16 @@ REQUIRED WORKAROUNDS:FROM events eWHERE e.person_id IN (SELECT DISTINCT person_id FROM events WHERE ...)-NEVER use events.person_id directly in JOIN ON constraints - always use one of the workarounds above.+4. For flag_evaluations, join on distinct_id, or filter with WHERE IN:+ ✅ FROM posthog.flag_evaluations fe JOIN events e ON fe.distinct_id = e.distinct_id+ ✅ SELECT p.id FROM persons p+ WHERE p.id IN (SELECT DISTINCT person_id FROM posthog.flag_evaluations WHERE flag_key = 'my-flag')+ A CROSS JOIN with the equality moved into WHERE also works.+ The distinct_id join matches raw ids only, so it pairs a logged-out evaluation with the logged-out+ events alone. Use the WHERE IN form when the question is about people rather than devices.++NEVER use the person_id of events or flag_evaluations directly in JOIN ON constraints - always use one+of the workarounds above.</person_id_join_limitation>"""# This is the authoritative code-owned query-writing guidance. Other HogQL consumers may adddiff --git a/posthog/hogql/ai.py b/posthog/hogql/ai.pyindex cfbad545b93b..e3b881d42e59 100644--- a/posthog/hogql/ai.py+++ b/posthog/hogql/ai.py@@ -67,7 +67,8 @@ Standardized events/properties such as pageview or screen start with `$`. Custom`virtual_table` and `lazy_table` fields are connections to linked tables, e.g. the virtual table field `person` allows accessing person properties like so: `person.properties.foo`.<person_id_join_limitation>-CRITICAL: There is a known issue with queries where JOIN constraints reference events.person_id fields.+CRITICAL: There is a known issue with queries where JOIN constraints reference the person_id field of+events or flag_evaluations.TECHNICAL CAUSE:The person_id fields are ExpressionFields that expand to expressions referencing override tables@@ -91,6 +92,13 @@ PROBLEMATIC PATTERNS:But e_all__override is defined later in the SQL, causing the error.+3. Joining flag_evaluations to any table on flag_evaluations.person_id:+ ❌ FROM posthog.flag_evaluations fe JOIN events e ON fe.person_id = e.person_id++ flag_evaluations.person_id is the same kind of ExpressionField, so this fails with+ "QueryError: Field not found: flag_evaluation_person_id", naming a column the query never used.+ Note this table is only reachable as `posthog.flag_evaluations`; the bare name does not resolve.+REQUIRED WORKAROUNDS:1. For accessing person data, use the person virtual table from events:✅ SELECT e.person.id, e.person.properties.email, e.event@@ -110,7 +118,16 @@ REQUIRED WORKAROUNDS:FROM events eWHERE e.person_id IN (SELECT DISTINCT person_id FROM events WHERE ...)-NEVER use events.person_id directly in JOIN ON constraints - always use one of the workarounds above.+4. For flag_evaluations, join on distinct_id, or filter with WHERE IN:+ ✅ FROM posthog.flag_evaluations fe JOIN events e ON fe.distinct_id = e.distinct_id+ ✅ SELECT p.id FROM persons p+ WHERE p.id IN (SELECT DISTINCT person_id FROM posthog.flag_evaluations WHERE flag_key = 'my-flag')+ A CROSS JOIN with the equality moved into WHERE also works.+ The distinct_id join matches raw ids only, so it pairs a logged-out evaluation with the logged-out+ events alone. Use the WHERE IN form when the question is about people rather than devices.++NEVER use the person_id of events or flag_evaluations directly in JOIN ON constraints - always use one+of the workarounds above.</person_id_join_limitation>""".strip()