: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(hogql): resolve person.distinct_id filter under person-on-events (#57937)
Thomas Obermüller21604a0abe0d
diff --git a/posthog/hogql/property.py b/posthog/hogql/property.pyindex 48a262ccd51b..e2c94459ba27 100644--- a/posthog/hogql/property.py+++ b/posthog/hogql/property.py@@ -797,9 +797,17 @@ def property_to_expr(value = property.valueif property.type == "person" and property.key == "distinct_id":- # distinct_id is not stored in person.properties — it lives in the- # person_distinct_id2 table and is exposed via the `pdi` lazy join on persons.- chain = ["person", "pdi"] if scope != "person" else ["pdi"]+ # distinct_id is not stored in person.properties.+ # - In event scope, events.distinct_id is a real column — no join needed.+ # - In person scope, persons.pdi.distinct_id resolves via the lazy join on persons.+ # Routing through `events.person.pdi` would break under person-on-events mode,+ # where `events.person` is rebound to the `poe` virtual table which has no `pdi` field.+ if scope == "event":+ chain = []+ elif scope == "person":+ chain = ["pdi"]+ else:+ chain = ["person", "pdi"]elif property.type == "person" and scope != "person":chain = ["person", "properties"]elif property.type == "event" and scope == "replay_entity":diff --git a/posthog/hogql/test/test_property.py b/posthog/hogql/test/test_property.pyindex a79154c1f6ab..95cf90db0ade 100644--- a/posthog/hogql/test/test_property.py+++ b/posthog/hogql/test/test_property.py@@ -11,13 +11,18 @@ from posthog.schema import (EmptyPropertyFilter,FlagPropertyFilter,HogQLPropertyFilter,+ HogQLQueryModifiers,+ PersonsOnEventsMode,PropertyOperator,RetentionEntity,)from posthog.hogql import ast+from posthog.hogql.context import HogQLContextfrom posthog.hogql.errors import QueryError+from posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.parser import parse_expr+from posthog.hogql.printer.utils import prepare_and_print_astfrom posthog.hogql.property import (entity_to_expr,has_aggregation,@@ -939,12 +944,13 @@ class TestProperty(BaseTest):@parameterized.expand([- ("event_scope", "event", "person.pdi.distinct_id = 'abc'"),+ ("event_scope", "event", "distinct_id = 'abc'"),("person_scope", "person", "pdi.distinct_id = 'abc'"),])def test_person_distinct_id_property(self, _name, scope, expected_expr):- # distinct_id is not stored in person.properties — it's exposed via the pdi lazy join.+ # distinct_id is not stored in person.properties — it's the events.distinct_id column+ # in event scope, and reachable via the pdi lazy join in person scope.self.assertEqual(self._property_to_expr({"type": "person", "key": "distinct_id", "value": "abc", "operator": "exact"},@@ -953,6 +959,39 @@ class TestProperty(BaseTest):self._parse_expr(expected_expr),)+ @parameterized.expand(+ [+ ("disabled", PersonsOnEventsMode.DISABLED),+ ("no_override_props_on_events", PersonsOnEventsMode.PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS),+ ("override_props_on_events", PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS),+ ("override_props_joined", PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_JOINED),+ ]+ )+ def test_person_distinct_id_property_resolves_under_all_poe_modes(self, _name, mode):+ # Regression test for "Field not found: pdi" — previously, the special case for+ # {type: person, key: distinct_id} routed through events.person.pdi, which broke under+ # person-on-events modes that rebind events.person to the `poe` virtual table.+ expr = property_to_expr(+ {"type": "person", "key": "distinct_id", "value": "abc", "operator": "is_not"},+ team=self.team,+ scope="event",+ )+ query_ast = ast.SelectQuery(+ select=[ast.Call(name="count", args=[])],+ select_from=ast.JoinExpr(table=ast.Field(chain=["events"])),+ where=expr,+ )+ context = HogQLContext(+ team_id=self.team.pk,+ enable_select_queries=True,+ modifiers=create_default_modifiers_for_team(self.team, HogQLQueryModifiers(personsOnEventsMode=mode)),+ )+ sql, _ = prepare_and_print_ast(query_ast, context=context, dialect="clickhouse")+ # Used to raise QueryError("Field not found: pdi") under PoE modes that rebind+ # events.person to the `poe` virtual table.+ assert "distinct_id" in sql+ assert "pdi" not in sql+def test_entity_to_expr_actions_type_with_id(self):action_mock = MagicMock()with patch("posthog.models.Action.objects.get", return_value=action_mock):