: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
feat(flags): enable the persons id pushdown for the flag-calls list (#110487)
Phil Haack64d035671f84
diff --git a/posthog/hogql_queries/events_query_runner.py b/posthog/hogql_queries/events_query_runner.pyindex 12555e875204..13645a41880c 100644--- a/posthog/hogql_queries/events_query_runner.py+++ b/posthog/hogql_queries/events_query_runner.py@@ -17,6 +17,7 @@ from posthog.schema import (EventPropertyFilter,EventsQuery,EventsQueryResponse,+ HogQLQueryModifiers,PropertyGroupFilter,PropertyGroupFilterValue,PropertyOperator,@@ -430,6 +431,14 @@ class EventsQueryRunner(AnalyticsQueryRunner[EventsQueryResponse]):right=ast.Tuple(exprs=[ast.Constant(value=key) for key in flag_keys]),)+ def _query_modifiers(self, table: EventsListTable) -> HogQLQueryModifiers:+ # flag_evaluations rows carry no person properties, so a person filter joins persons.+ # The pushdown limits that join to the persons that the page's flag calls reach.+ # An explicit personIdPushdown value on the team or the query still wins.+ if table is FLAG_EVALUATIONS_LIST_TABLE and self.modifiers.personIdPushdown is None:+ return self.modifiers.model_copy(update={"personIdPushdown": True})+ return self.modifiers+def _query_context(self, table: EventsListTable) -> HogQLContext:context = self.build_hogql_context()assert context.database is not None@@ -695,12 +704,13 @@ class EventsQueryRunner(AnalyticsQueryRunner[EventsQueryResponse]):# Only this path reads flag_evaluations. Callers that run to_query() in their own context keep reading# events. Their select or database may need columns that flag_evaluations lacks.table = self._list_table()+ modifiers = self._query_modifiers(table)query_result = self.paginator.execute_hogql_query(query=self._build_query(table),team=self.team,query_type="EventsQuery",timings=self.timings,- modifiers=self.modifiers,+ modifiers=modifiers,limit_context=self.limit_context,user=self.user,context=self._query_context(table),@@ -723,7 +733,7 @@ class EventsQueryRunner(AnalyticsQueryRunner[EventsQueryResponse]):types=[t for _, t in query_result.types] if query_result.types else [],timings=self.timings.to_list(),hogql=query_result.hogql,- modifiers=self.modifiers,+ modifiers=modifiers,nextCursor=self._next_cursor(),**self.paginator.response_params(),)diff --git a/posthog/hogql_queries/test/test_events_query_runner.py b/posthog/hogql_queries/test/test_events_query_runner.pyindex b790699de7c0..b36b7733a119 100644--- a/posthog/hogql_queries/test/test_events_query_runner.py+++ b/posthog/hogql_queries/test/test_events_query_runner.py@@ -26,6 +26,7 @@ from posthog.schema import (EventsQuery,EventsQueryActionStep,GroupPropertyFilter,+ HogQLQueryModifiers,PersonPropertyFilter,PropertyOperator,)@@ -1289,12 +1290,23 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):"person_property_filter",[PersonPropertyFilter(key="email", value=FLAG_EVALUATIONS_EMAIL, operator=PropertyOperator.EXACT)],False,+ None,+ ),+ ("test_account_filter", [], True, None),+ (+ "person_id_pushdown_turned_off",+ [PersonPropertyFilter(key="email", value=FLAG_EVALUATIONS_EMAIL, operator=PropertyOperator.EXACT)],+ False,+ False,),- ("test_account_filter", [], True),])def test_flag_evaluations_only_organization_reads_flag_calls_from_flag_evaluations(- self, _name: str, person_filters: list[PersonPropertyFilter], filter_test_accounts: bool+ self,+ _name: str,+ person_filters: list[PersonPropertyFilter],+ filter_test_accounts: bool,+ person_id_pushdown: bool | None,):self._set_flag_evaluations_mode(FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY)self.team.test_account_filters = [@@ -1353,6 +1365,7 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):],filterTestAccounts=filter_test_accounts,after="-30d",+ modifiers=HogQLQueryModifiers(personIdPushdown=person_id_pushdown),)with self.capture_select_queries() as queries:response = EventsQueryRunner(query=query, team=self.team).run()@@ -1362,6 +1375,10 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):assert len(re.findall(r"\bAS\s+flag_evaluations__person\s+ON\b", page_queries[0])) == 1assert isinstance(response, CachedEventsQueryResponse)assert f"in(flag_key, tuple('{FLAG_EVALUATIONS_FLAG_KEY}'))" in response.hogql+ expect_pushdown = person_id_pushdown is not False+ assert ("SELECT DISTINCT" in " ".join(page_queries[0].split())) is expect_pushdown+ assert response.modifiers is not None+ assert response.modifiers.personIdPushdown is expect_pushdownassert "properties.$feature_flag," not in response.hogqlassert len(response.results) == 1star, *columns = response.results[0]