: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(flags): decide the flag calls swap by organization mode alone (#126076)
Phil Haack08818279b0ef
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex 51802cbfe1e6..1e25c830ec67 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -458,7 +458,7 @@ _DATABASE_ROOT_NODE_BLOBS_LOCK = threading.Lock()# LRU, and fresh entries survive the sweep. Reads are lock-free (dict.get is atomic under the# GIL); mutations and the sweep hold the lock, because the sweep iterates the dict and concurrent# inserts would raise RuntimeError mid-iteration.-_CACHEABLE_TEAM_FLAGS = frozenset({"managed-viewsets", "hogql-flag-calls-from-flag-evaluations"})+_CACHEABLE_TEAM_FLAGS = frozenset({"managed-viewsets"})_TEAM_FLAG_CACHE: dict[tuple[str, str], tuple[float, bool]] = {} # key -> (evaluated_at, value)_TEAM_FLAG_CACHE_LOCK = threading.Lock()_TEAM_FLAG_CACHE_MAX_ENTRIES = 50_000@@ -480,16 +480,6 @@ def _evaluate_warehouse_access_control_flag(team: Team) -> bool:)-def _evaluate_flag_calls_rewrite_flag(team: Team) -> bool:- return feature_enabled_or_false(- "hogql-flag-calls-from-flag-evaluations",- str(team.uuid),- groups={"organization": str(team.organization_id)},- group_properties={"organization": {"id": str(team.organization_id)}},- send_feature_flag_events=False,- )--def _cached_team_flag(flag_key: str, team: Team, evaluate: Callable[[], bool]) -> bool:# Not cache_for/CachedFunction: its TTL is fixed at decoration time, and this TTL is a live# instance setting so ops can retune or disable the cache without a redeploy.@@ -2070,11 +2060,9 @@ class Database(BaseModel):# A direct-connection catalog has no "posthog" node to hold the table.include_flag_evaluations_table = not is_direct_query and is_flag_evaluations_table_enabled(team)# This checks the read mode, not is_flag_evaluations_table_enabled. That one is true under DEBUG for an- # organization still on EVENTS. The flag goes first because it reads no row.+ # organization still on EVENTS.read_mode: int = FlagEvaluationsMode.EVENTS- if include_flag_evaluations_table and _cached_team_flag(- "hogql-flag-calls-from-flag-evaluations", team, lambda: _evaluate_flag_calls_rewrite_flag(team)- ):+ if include_flag_evaluations_table:read_mode = get_flag_evaluations_read_mode(team.organization_id)read_flag_calls_from_flag_evaluations = read_mode != FlagEvaluationsMode.EVENTSflag_calls_only_in_flag_evaluations = read_mode == FlagEvaluationsMode.FLAG_EVALUATIONS_ONLYdiff --git a/posthog/hogql/database/schema/test/test_flag_evaluations.py b/posthog/hogql/database/schema/test/test_flag_evaluations.pyindex c4ce2a379528..8a385131fb6f 100644--- a/posthog/hogql/database/schema/test/test_flag_evaluations.py+++ b/posthog/hogql/database/schema/test/test_flag_evaluations.py@@ -56,6 +56,7 @@ from posthog.constants import AvailableFeaturefrom posthog.hogql_queries.events_query_runner import SELECT_STAR_FROM_EVENTS_FIELDSfrom posthog.models import PropertyDefinition, Teamfrom posthog.models.group.util import create_group+from posthog.models.instance_setting import override_instance_configfrom posthog.models.property import TableColumnfrom posthog.models.utils import uuid7from posthog.test.persons import create_group_type_mapping, create_person@@ -268,13 +269,12 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):modifiers: HogQLQueryModifiers | None = None,mode: FlagEvaluationsMode = FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY,) -> HogQLQueryResponse:+ # The force-events switch does not move FLAG_EVALUATIONS_ONLY. The events-side baseline therefore runs on mode 1.OrganizationFeatureFlagsConfig.objects.update_or_create(- organization=self.organization, defaults={"flag_evaluations_mode": mode}+ organization=self.organization,+ defaults={"flag_evaluations_mode": mode if rewrite else FlagEvaluationsMode.READ_FLAG_EVALUATIONS},)- with patch(- "posthog.hogql.database.database.feature_enabled_or_false",- side_effect=lambda key, *args, **kwargs: rewrite and key == "hogql-flag-calls-from-flag-evaluations",- ):+ with override_instance_config("FLAG_EVALUATIONS_READS_FORCE_EVENTS", not rewrite):return execute_hogql_query(query, team=self.team, filters=filters, modifiers=modifiers, pretty=False)def _override(self, person_id: uuid.UUID, version: int, is_deleted: bool = False) -> None:@@ -727,7 +727,7 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):assert f"toIntervalDay({SESSION_BUFFER_DAYS})" in clickhouse- def test_flag_call_query_on_events_reads_no_flag_evaluations_with_the_flag_off(self):+ def test_flag_call_query_on_events_reads_no_flag_evaluations_when_reads_are_forced_to_events(self):assert self._query_events(FLAG_CALLS_ON_EVENTS, rewrite=False).results == [(0,)]@parameterized.expand(@@ -993,7 +993,7 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):mode = FlagEvaluationsMode.READ_FLAG_EVALUATIONSassert (self._query_events(query, modifiers=modifiers, mode=mode).clickhouse- == self._query_events(query, rewrite=False, modifiers=modifiers, mode=mode).clickhouse+ == self._query_events(query, rewrite=False, modifiers=modifiers).clickhouse)with self.assertRaisesRegex(QueryError, error):self._query_events(query, modifiers=modifiers)@@ -1514,7 +1514,7 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):"AND properties.$feature_flag = 'TRUE' AND timestamp >= now() - INTERVAL 7 DAY")- from_events = self._query_events(query, rewrite=False, mode=mode).results+ from_events = self._query_events(query, rewrite=False).resultsswapped = self._query_events(query, mode=mode)@@ -1549,7 +1549,7 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):event_uuid=str(row_uuid),)flush_persons_and_events()- from_events = self._query_events(query, rewrite=False, mode=FlagEvaluationsMode.READ_FLAG_EVALUATIONS)+ from_events = self._query_events(query, rewrite=False)rewritten = self._query_events(query, mode=FlagEvaluationsMode.READ_FLAG_EVALUATIONS)assert from_events.results == [(expected_count,)]@@ -1596,7 +1596,7 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):modifiers = HogQLQueryModifiers(personsOnEventsMode=PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS)mode = FlagEvaluationsMode.READ_FLAG_EVALUATIONS- from_events = self._query_events(query, rewrite=False, modifiers=modifiers, mode=mode)+ from_events = self._query_events(query, rewrite=False, modifiers=modifiers)rewritten = self._query_events(query, modifiers=modifiers, mode=mode)assert rewritten.results == from_events.resultsdiff --git a/posthog/hogql/database/test/test_database.py b/posthog/hogql/database/test/test_database.pyindex b0f9a5ce6e1c..4889861f227d 100644--- a/posthog/hogql/database/test/test_database.py+++ b/posthog/hogql/database/test/test_database.py@@ -483,49 +483,42 @@ class TestDatabase(BaseTest, QueryMatchingTest):@parameterized.expand([- ("events_mode", FlagEvaluationsMode.EVENTS, True, False),+ ("events_mode", FlagEvaluationsMode.EVENTS, False),# DEBUG puts the table in the catalog on EVENTS, but events still holds every flag call.- param("events_mode_under_debug", FlagEvaluationsMode.EVENTS, True, False, debug=True),+ param("events_mode_under_debug", FlagEvaluationsMode.EVENTS, False, debug=True),param("events_mode_with_the_table_forced_in",FlagEvaluationsMode.EVENTS,- True,False,include_flag_evaluations_table=True,),- ("read_mode", FlagEvaluationsMode.READ_FLAG_EVALUATIONS, True, True),- ("read_mode_with_the_flag_off", FlagEvaluationsMode.READ_FLAG_EVALUATIONS, False, False),+ ("read_mode", FlagEvaluationsMode.READ_FLAG_EVALUATIONS, True),param("read_mode_with_reads_forced_to_events",FlagEvaluationsMode.READ_FLAG_EVALUATIONS,- True,False,reads_forced_to_events=True,),- ("only_mode", FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY, True, True),- ("only_mode_with_the_flag_off", FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY, False, False),+ ("only_mode", FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY, True),# The switch skips FLAG_EVALUATIONS_ONLY, whose flag calls are no longer in events.param("only_mode_with_reads_forced_to_events",FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY,True,- True,reads_forced_to_events=True,),param("only_mode_without_the_table",FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY,- True,False,include_flag_evaluations_table=False,),])- def test_read_flag_calls_from_flag_evaluations_follows_the_read_mode_and_the_flag(+ def test_read_flag_calls_from_flag_evaluations_follows_the_read_mode(self,_name: str,mode: FlagEvaluationsMode,- flag_enabled: bool,expected: bool,reads_forced_to_events: bool = False,debug: bool = False,@@ -538,12 +531,6 @@ class TestDatabase(BaseTest, QueryMatchingTest):with (override_settings(DEBUG=debug),override_instance_config("FLAG_EVALUATIONS_READS_FORCE_EVENTS", reads_forced_to_events),- patch(- "posthog.hogql.database.database.feature_enabled_or_false",- side_effect=lambda key, *args, **kwargs: (- flag_enabled and key == "hogql-flag-calls-from-flag-evaluations"- ),- ),):database = Database.create_for(team=self.team, include_flag_evaluations_table=include_flag_evaluations_tablediff --git a/posthog/hogql/test/test_metadata.py b/posthog/hogql/test/test_metadata.pyindex b64b35bce570..02cfec4ae349 100644--- a/posthog/hogql/test/test_metadata.py+++ b/posthog/hogql/test/test_metadata.py@@ -437,7 +437,7 @@ class TestMetadata(ClickhouseTestMixin, APIBaseTest):_move_notices("command:editor.action.deleteLines"),),("malformed_announcement_url", _FLAG_CALLED_QUERY, True, 1, _move_notices("https://[broken")),- ("read_from_flag_evaluations_while_the_rewrite_is_off", _FLAG_CALLS_FROM_FLAG_EVALUATIONS_QUERY, True, 1),+ ("read_from_flag_evaluations_in_events_mode", _FLAG_CALLS_FROM_FLAG_EVALUATIONS_QUERY, True, 1),param("read_from_flag_evaluations",_FLAG_CALLS_FROM_FLAG_EVALUATIONS_QUERY,@@ -445,16 +445,15 @@ class TestMetadata(ClickhouseTestMixin, APIBaseTest):0,_move_notices("https://example.com/announcement"),"https://example.com/announcement",- rewrite_enabled=True,+ mode=FlagEvaluationsMode.READ_FLAG_EVALUATIONS,expected_notices=1,),- param("refused_by_the_rewrite", _FLAG_CALLED_QUERY, True, 1, rewrite_enabled=True),+ param("refused_by_the_swap", _FLAG_CALLED_QUERY, True, 1, mode=FlagEvaluationsMode.READ_FLAG_EVALUATIONS),param("read_from_flag_evaluations_without_coverage_once_events_gets_no_flag_calls",_FLAG_CALLED_QUERY,True,0,- rewrite_enabled=True,expected_notices=1,mode=FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY,),@@ -464,7 +463,7 @@ class TestMetadata(ClickhouseTestMixin, APIBaseTest):"AND properties.$feature_flag = 'my-flag' AND timestamp >= now() - INTERVAL 7 DAY",True,1,- rewrite_enabled=True,+ mode=FlagEvaluationsMode.READ_FLAG_EVALUATIONS,),param("falls_back_when_a_name_binds_to_a_flag_evaluations_column",@@ -472,7 +471,7 @@ class TestMetadata(ClickhouseTestMixin, APIBaseTest):"AND properties.$feature_flag = 'my-flag' AND timestamp >= now() - INTERVAL 7 DAY",True,1,- rewrite_enabled=True,+ mode=FlagEvaluationsMode.READ_FLAG_EVALUATIONS,),])@@ -486,12 +485,12 @@ class TestMetadata(ClickhouseTestMixin, APIBaseTest):expected: int,move_notices: FeatureFlagResult | None = _MOVE_NOTICES_WITHOUT_URL,expected_url: str | None = None,- rewrite_enabled: bool = False,expected_notices: int = 0,- mode: FlagEvaluationsMode = FlagEvaluationsMode.READ_FLAG_EVALUATIONS,+ mode: FlagEvaluationsMode = FlagEvaluationsMode.EVENTS,) -> None:OrganizationFeatureFlagsConfig.objects.update_or_create(- organization=self.organization, defaults={"flag_evaluations_mode": mode}+ organization=self.organization,+ defaults={"flag_evaluations_mode": mode},)DataWarehouseSavedQuery.objects.create(team=self.team,@@ -513,12 +512,6 @@ class TestMetadata(ClickhouseTestMixin, APIBaseTest):return_value=flag_evaluations_enabled,),patch("posthoganalytics.get_feature_flag_result", return_value=move_notices),- patch(- "posthog.hogql.database.database.feature_enabled_or_false",- side_effect=lambda key, *args, **kwargs: (- rewrite_enabled and key == "hogql-flag-calls-from-flag-evaluations"- ),- ),):metadata = self._select(query)diff --git a/posthog/hogql_queries/test/test_events_query_runner.py b/posthog/hogql_queries/test/test_events_query_runner.pyindex 668d79b60cde..30d01adca9d7 100644--- a/posthog/hogql_queries/test/test_events_query_runner.py+++ b/posthog/hogql_queries/test/test_events_query_runner.py@@ -1478,7 +1478,7 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):"flag_evaluations_only_with_action_steps",FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY,{"event": "$feature_flag_called", "actionSteps": [EventsQueryActionStep(event="$feature_flag_called")]},- EVENTS_FLAG_KEY,+ FLAG_EVALUATIONS_FLAG_KEY,),])diff --git a/posthog/hogql_queries/test/test_hogql_cohort_query.py b/posthog/hogql_queries/test/test_hogql_cohort_query.pyindex fb1c9ea7cff3..abfff0f5db07 100644--- a/posthog/hogql_queries/test/test_hogql_cohort_query.py+++ b/posthog/hogql_queries/test/test_hogql_cohort_query.py@@ -622,7 +622,7 @@ class TestHogQLCohortQuery(ClickhouseTestMixin, APIBaseTest):)return {str(row[0]) for row in rows}- def test_performed_flag_call_criterion_reads_flag_evaluations_when_the_rewrite_is_on(self) -> None:+ def test_performed_flag_call_criterion_reads_flag_evaluations_in_flag_evaluations_only_mode(self) -> None:OrganizationFeatureFlagsConfig.objects.update_or_create(organization=self.organization,defaults={"flag_evaluations_mode": FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY},@@ -665,11 +665,7 @@ class TestHogQLCohortQuery(ClickhouseTestMixin, APIBaseTest):},)- with patch(- "posthog.hogql.database.database.feature_enabled_or_false",- side_effect=lambda key, *args, **kwargs: key == "hogql-flag-calls-from-flag-evaluations",- ):- cohort.calculate_people_ch(pending_version=0)+ cohort.calculate_people_ch(pending_version=0)self.assertEqual(self._cohort_member_ids(cohort, 0), {str(caller.uuid)})diff --git a/posthog/models/test/test_data_deletion_request.py b/posthog/models/test/test_data_deletion_request.pyindex 11a01be655cd..9541685ef21a 100644--- a/posthog/models/test/test_data_deletion_request.py+++ b/posthog/models/test/test_data_deletion_request.py@@ -236,11 +236,7 @@ def test_compile_hogql_predicate_keeps_a_flag_call_subquery_on_events(team, flagpredicate = f"distinct_id IN ({flag_calls})"request = DataDeletionRequest(**_base_kwargs(team_id=team.id, events=["$pageview"], hogql_predicate=predicate))- with patch(- "posthog.hogql.database.database.feature_enabled_or_false",- side_effect=lambda key, *args, **kwargs: key == "hogql-flag-calls-from-flag-evaluations",- ):- sql, _ = compile_hogql_predicate(request)+ sql, _ = compile_hogql_predicate(request)assert "flag_evaluations" not in sqldiff --git a/products/product_analytics/backend/hogql_queries/trends/test/test_trends_query_runner.py b/products/product_analytics/backend/hogql_queries/trends/test/test_trends_query_runner.pyindex f262cd9be4c8..fa56c842793c 100644--- a/products/product_analytics/backend/hogql_queries/trends/test/test_trends_query_runner.py+++ b/products/product_analytics/backend/hogql_queries/trends/test/test_trends_query_runner.py@@ -6667,7 +6667,7 @@ class TestTrendsQueryRunner(ClickhouseTestMixin, APIBaseTest):),])- def test_flag_call_series_reads_flag_evaluations_when_the_rewrite_is_on(+ def test_flag_call_series_reads_flag_evaluations_when_the_org_reads_flag_evaluations(self,_name: str,series: Callable[[Team, list[str]], EventsNode | ActionsNode],@@ -6693,24 +6693,20 @@ class TestTrendsQueryRunner(ClickhouseTestMixin, APIBaseTest):if math is not None:node = node.model_copy(update={"math": math})- with patch(- "posthog.hogql.database.database.feature_enabled_or_false",- side_effect=lambda key, *args, **kwargs: key == "hogql-flag-calls-from-flag-evaluations",- ):- # Mode 2 raises when the swapped query reads people differently, so the persons-on-events mode is fixed.- # Mode 1 reads events on the native-JSON events table, so the events table is fixed too.- response = self._run_trends_query(- "-7d",- None,- IntervalType.DAY,- [node],- breakdown=breakdown,- filter_test_accounts=filter_test_accounts,- hogql_modifiers=HogQLQueryModifiers(- personsOnEventsMode=PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS,- useNewEventsSchema=False,- ),- )+ # Mode 2 raises when the swapped query reads people differently, so the persons-on-events mode is fixed.+ # Mode 1 reads events on the native-JSON events table, so the events table is fixed too.+ response = self._run_trends_query(+ "-7d",+ None,+ IntervalType.DAY,+ [node],+ breakdown=breakdown,+ filter_test_accounts=filter_test_accounts,+ hogql_modifiers=HogQLQueryModifiers(+ personsOnEventsMode=PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS,+ useNewEventsSchema=False,+ ),+ )assert response.results[0]["count"] == expected_countif breakdown is not None: