Skip to content

: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.

fix(flags): read an unqualified event beside a join as the events scan's (#125267)

Phil Haack1f5f5e7c8c6d
diff --git a/posthog/hogql/database/schema/test/test_flag_evaluations.py b/posthog/hogql/database/schema/test/test_flag_evaluations.py
index 61aefb207137..c4ce2a379528 100644
--- a/posthog/hogql/database/schema/test/test_flag_evaluations.py
+++ b/posthog/hogql/database/schema/test/test_flag_evaluations.py
@@ -41,7 +41,7 @@ from posthog.hogql.context import HogQLContext
from posthog.hogql.database.database import Database
from posthog.hogql.database.schema.flag_evaluations import add_events_list_fields_to_flag_evaluations
from posthog.hogql.database.schema.util.where_clause_extractor import SESSION_BUFFER_DAYS
-from posthog.hogql.errors import QueryError
+from posthog.hogql.errors import QueryError, ResolutionError
from posthog.hogql.parser import parse_select
from posthog.hogql.printer.utils import prepare_and_print_ast
from posthog.hogql.property import property_to_expr
@@ -84,14 +84,16 @@ FLAG_CALL_CONJUNCTS = (
FLAG_CALLS_ON_EVENTS = f"SELECT count() FROM events WHERE {FLAG_CALL_CONJUNCTS}"
PAGEVIEW_CONJUNCTS = "event = '$pageview' AND timestamp >= now() - INTERVAL 7 DAY"
PROBE_DISTINCT_IDS = "(SELECT 'probe-distinct-id' AS id) AS d"
-UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN = (
- f"SELECT count() FROM events AS e JOIN {PROBE_DISTINCT_IDS} ON e.distinct_id = d.id WHERE {FLAG_CALL_CONJUNCTS}"
+FLAG_CALLS_AFTER_A_JOIN = (
+ f"SELECT count() FROM {PROBE_DISTINCT_IDS} JOIN events AS e ON e.distinct_id = d.id "
+ "WHERE e.event = '$feature_flag_called' AND e.properties.$feature_flag = 'probe-flag' "
+ "AND e.timestamp >= now() - INTERVAL 7 DAY"
)
def _create_flag_calls_expression(team: Team) -> str:
DataWarehouseExpression.objects.for_team(team.pk).create(
- team=team, table_name="events", field_name="flag_calls", expression=f"({UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN})"
+ team=team, table_name="events", field_name="flag_calls", expression=f"({FLAG_CALLS_AFTER_A_JOIN})"
)
# The transform runs only when the query text names $feature_flag_called. The expression body is not part of that
# text. Without this filter, the transform skips the query. The test would then pass even if record_added stopped
@@ -779,6 +781,12 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
"AND uuid NOT IN (SELECT posthog.events.uuid FROM posthog.events)",
[(1,)],
),
+ (
+ "unqualified_reads_beside_a_joined_subquery",
+ "SELECT count() FROM events JOIN (SELECT 'probe-distinct-id' AS id) AS d ON events.distinct_id = d.id "
+ f"WHERE {FLAG_CALL_CONJUNCTS}",
+ [(1,)],
+ ),
(
"cte",
f"WITH calls AS (SELECT uuid FROM events WHERE {FLAG_CALL_CONJUNCTS}) SELECT count() FROM calls",
@@ -825,37 +833,28 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
@parameterized.expand(
[
- ("unqualified_filters_next_to_a_join", UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN, [(1,)]),
(
"query_built_without_source_positions",
- clear_locations(parse_select(UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN)),
+ clear_locations(parse_select(FLAG_CALLS_AFTER_A_JOIN)),
[(1,)],
),
(
"union_of_one_select_object_twice",
- ast.SelectSetQuery.create_from_queries(
- [parse_select(UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN)] * 2, "UNION ALL"
- ),
+ ast.SelectSetQuery.create_from_queries([parse_select(FLAG_CALLS_AFTER_A_JOIN)] * 2, "UNION ALL"),
[(1,), (1,)],
),
(
"union_of_one_parsed_template_twice",
ast.SelectSetQuery.create_from_queries(
[
- parse_select(UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN),
- parse_select(UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN),
+ parse_select(FLAG_CALLS_AFTER_A_JOIN),
+ parse_select(FLAG_CALLS_AFTER_A_JOIN),
],
"UNION ALL",
),
[(1,), (1,)],
),
- (
- "events_after_another_table",
- f"SELECT count() FROM {PROBE_DISTINCT_IDS} JOIN events AS e ON e.distinct_id = d.id "
- "WHERE e.event = '$feature_flag_called' AND e.properties.$feature_flag = 'probe-flag' "
- "AND e.timestamp >= now() - INTERVAL 7 DAY",
- [(1,)],
- ),
+ ("events_after_another_table", FLAG_CALLS_AFTER_A_JOIN, [(1,)]),
(
"self_join_with_both_sides_pinned",
"SELECT count() FROM events AS a JOIN events AS b ON a.uuid = b.uuid "
@@ -871,17 +870,17 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
),
(
"scan_in_a_subquery",
- f"SELECT * FROM ({UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN})",
+ f"SELECT * FROM ({FLAG_CALLS_AFTER_A_JOIN})",
[(1,)],
),
(
"scan_in_a_type_cast",
- f"SELECT ({UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN})::String",
+ f"SELECT ({FLAG_CALLS_AFTER_A_JOIN})::String",
[("1",)],
),
(
"scan_in_a_cte_body",
- f"WITH calls AS ({UNQUALIFIED_FLAG_CALLS_NEXT_TO_A_JOIN}) SELECT * FROM calls",
+ f"WITH calls AS ({FLAG_CALLS_AFTER_A_JOIN}) SELECT * FROM calls",
[(1,)],
),
(
@@ -978,8 +977,7 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
),
(
"posthog_events_next_to_a_join",
- f"SELECT count() FROM posthog.events AS e JOIN {PROBE_DISTINCT_IDS} ON e.distinct_id = d.id "
- f"WHERE {FLAG_CALL_CONJUNCTS}",
+ FLAG_CALLS_AFTER_A_JOIN.replace("JOIN events AS e", "JOIN posthog.events AS e"),
"it reads events in a shape that can't be moved",
),
]
@@ -1072,6 +1070,14 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
== self._query_events(query, rewrite=False, modifiers=modifiers).clickhouse
)
+ def test_query_reading_event_that_another_source_has_raises_instead_of_reading_flag_evaluations(self):
+ query = (
+ "SELECT count() FROM events JOIN (SELECT distinct_id, event FROM events) AS calls "
+ f"ON events.distinct_id = calls.distinct_id WHERE {FLAG_CALL_CONJUNCTS}"
+ )
+ with self.assertRaisesRegex(ResolutionError, "Ambiguous query"):
+ self._query_events(query)
+
@parameterized.expand(
[
("no_lower_bound", "SELECT distinct_id FROM events WHERE event = '$feature_flag_called'"),
diff --git a/posthog/hogql/transforms/flag_calls_from_flag_evaluations.py b/posthog/hogql/transforms/flag_calls_from_flag_evaluations.py
index 7542b3fef234..6dd60d1986be 100644
--- a/posthog/hogql/transforms/flag_calls_from_flag_evaluations.py
+++ b/posthog/hogql/transforms/flag_calls_from_flag_evaluations.py
@@ -127,7 +127,9 @@ def can_read_flag_evaluations(
It holds when all of these hold:
- 1. A top-level conjunct pins the scan's `event` to `$feature_flag_called`.
+ 1. A top-level conjunct pins the scan's `event` to `$feature_flag_called`. A name without a table prefix reads
+ the scan unless a select alias shadows it or, in a chain of two or more parts, its head names another
+ source in FROM.
2. A top-level conjunct bounds the scan's `timestamp` from below at or after the coverage start. A time with no
zone reads in `timezone`, the team's zone, wherever the printer reads it that way.
3. The scan is not sampled, because `flag_evaluations` has no sampling key. Insight runners attach `SAMPLE 1`
@@ -352,12 +354,14 @@ class EventsScan:
self,
join: ast.JoinExpr,
alias: str,
- has_other_sources: bool,
+ other_tables: frozenset[str],
flag_key_compares_as_string: Callable[[], bool],
+ qualified_only: bool = False,
):
self.join = join
self.alias = alias
- self.has_other_sources = has_other_sources
+ self.other_tables = other_tables
+ self.qualified_only = qualified_only
self.flag_key_compares_as_string = flag_key_compares_as_string
self.shadowed: frozenset[str] = frozenset()
self.qualifier_shadows: frozenset[str] = frozenset()
@@ -378,8 +382,9 @@ class EventsScan:
scan = cls(
join=join,
alias=join.alias or "__".join(parts),
- # A field without a table prefix can bind to a joined table or an ARRAY JOIN alias instead.
- has_other_sources=join.next_join is not None or bool(node.array_join_list),
+ # The resolver reads the head of a chain of two or more parts as one of these tables first. Some warehouse
+ # tables match a name in any case.
+ other_tables=frozenset(name.lower() for name in ctes.union(_join_names(join.next_join))),
flag_key_compares_as_string=flag_key_compares_as_string,
)
collector = _AliasCollector(node)
@@ -405,7 +410,14 @@ class EventsScan:
unqualified = self._without_dotted_qualifier(expr.chain)
if unqualified is not None:
return unqualified
- if self.has_other_sources or expr.chain[0] in self.shadowed:
+ if self.qualified_only:
+ return None
+ head = str(expr.chain[0])
+ if len(expr.chain) > 1 and head.lower() in self.other_tables:
+ return None
+ # The resolver rejects a name that two sources in FROM have as ambiguous. Events has every scan column, so a
+ # name that no select alias shadows reads the scan in any query that resolves.
+ if head in self.shadowed:
return None
return expr.chain
@@ -971,7 +983,8 @@ class _EventsScanSwapper(ScanVisitor):
join=clone.join,
alias=clone.join.alias or "events",
# The clone is unresolved, so only a name qualified with this scan's alias can count as a read of it.
- has_other_sources=True,
+ other_tables=frozenset(),
+ qualified_only=True,
flag_key_compares_as_string=self.flag_key_compares_as_string,
)
refusal = events_scan.shape_refusal(clone.select)
diff --git a/posthog/hogql/transforms/test/test_flag_calls_from_flag_evaluations.py b/posthog/hogql/transforms/test/test_flag_calls_from_flag_evaluations.py
index 26410787b74e..abf7589fabe0 100644
--- a/posthog/hogql/transforms/test/test_flag_calls_from_flag_evaluations.py
+++ b/posthog/hogql/transforms/test/test_flag_calls_from_flag_evaluations.py
@@ -26,6 +26,7 @@ IN_COVERAGE = COVERAGE_START + timedelta(days=30)
FLAG_CALLED = "event = '$feature_flag_called'"
FLAG_KEY = "properties.$feature_flag = 'my-flag'"
BOUND = "timestamp >= now() - INTERVAL 7 DAY"
+UNQUALIFIED = f"{FLAG_CALLED} AND {FLAG_KEY} AND {BOUND}"
OR_OF_AND_BRANCHES = (
"((properties.$feature_flag = 'a' AND distinct_id = 'x') OR (properties.$feature_flag = 'b' AND distinct_id = 'y'))"
)
@@ -81,6 +82,10 @@ def _self_joined(where: str) -> str:
return f"SELECT count() FROM events AS e JOIN events AS f ON e.uuid = f.uuid WHERE {where}"
+def _beside(join: str) -> str:
+ return f"SELECT count() FROM events AS e {join} WHERE {UNQUALIFIED}"
+
+
@time_machine.travel(NOW, tick=False)
class TestCanReadFlagEvaluations(SimpleTestCase):
def _can_read(
@@ -165,6 +170,52 @@ class TestCanReadFlagEvaluations(SimpleTestCase):
"AND e.timestamp >= now() - INTERVAL 7 DAY"
),
),
+ (
+ "unqualified_event_with_a_join",
+ _joined(
+ f"{FLAG_CALLED} AND e.properties.$feature_flag = 'my-flag' AND e.timestamp >= now() - INTERVAL 7 DAY"
+ ),
+ ),
+ (
+ "unqualified_bound_with_a_join",
+ _joined(f"e.event = '$feature_flag_called' AND e.properties.$feature_flag = 'my-flag' AND {BOUND}"),
+ ),
+ (
+ "unqualified_event_beside_a_table_aliased_event",
+ "SELECT count() FROM events AS e JOIN persons AS event ON e.person_id = event.id "
+ "WHERE event = '$feature_flag_called' AND e.properties.$feature_flag = 'my-flag' "
+ "AND e.timestamp >= now() - INTERVAL 7 DAY",
+ ),
+ (
+ "unqualified_reads_beside_a_joined_subquery",
+ _beside(
+ "INNER JOIN (SELECT person_id AS cohort_person_id, 1 AS value FROM raw_cohort_people) AS cohort_join "
+ "ON e.person_id = cohort_join.cohort_person_id"
+ ),
+ ),
+ (
+ "unqualified_reads_beside_a_joined_union",
+ _beside(
+ "LEFT JOIN (SELECT person_id AS cohort_person_id, 1 AS matched FROM static_cohort_people "
+ "UNION ALL SELECT person_id AS cohort_person_id, 1 AS matched FROM raw_cohort_people) AS __in_cohort "
+ "ON __in_cohort.cohort_person_id = e.person_id"
+ ),
+ ),
+ (
+ "unqualified_reads_beside_a_joined_cte",
+ "WITH actors AS (SELECT person_id AS actor_id, min(timestamp) AS t_0 FROM events GROUP BY actor_id) "
+ + _beside("INNER JOIN actors ON e.person_id = actors.actor_id"),
+ ),
+ (
+ "unqualified_reads_beside_an_array_join",
+ f"SELECT n FROM events ARRAY JOIN [1, 2] AS n WHERE {UNQUALIFIED}",
+ ),
+ (
+ "alias_that_reads_the_scan_beside_a_subquery_with_an_event_column",
+ "SELECT e.event AS event, count() FROM events AS e "
+ "JOIN (SELECT distinct_id, event FROM events) AS calls ON e.distinct_id = calls.distinct_id "
+ f"WHERE {UNQUALIFIED} GROUP BY event",
+ ),
("flag_key_array_access", _where(FLAG_CALLED, "properties['$feature_flag'] = 'my-flag'", BOUND)),
(
"flag_key_array_access_by_the_alias",
@@ -283,11 +334,23 @@ class TestCanReadFlagEvaluations(SimpleTestCase):
_where(FLAG_KEY, BOUND, f"uuid IN (SELECT uuid FROM events WHERE {FLAG_CALLED})"),
),
(
- "unqualified_event_with_a_join",
- _joined(
- f"{FLAG_CALLED} AND e.properties.$feature_flag = 'my-flag' AND e.timestamp >= now() - INTERVAL 7 DAY"
+ "flag_key_under_a_cte_named_properties",
+ "WITH properties AS (SELECT 'my-flag' AS `$feature_flag`) " + _where(FLAG_CALLED, FLAG_KEY, BOUND),
+ ),
+ (
+ "flag_key_beside_a_table_named_properties",
+ _beside("JOIN properties ON e.distinct_id = properties.distinct_id"),
+ ),
+ (
+ "flag_key_beside_a_subquery_named_properties",
+ _beside(
+ "JOIN (SELECT person_id AS id FROM raw_cohort_people) AS properties ON e.person_id = properties.id"
),
),
+ (
+ "array_join_alias_that_shadows_event",
+ f"SELECT count() FROM events ARRAY JOIN ['$feature_flag_called'] AS event WHERE {UNQUALIFIED}",
+ ),
(
"pin_on_a_joined_alias",
_self_joined(
@@ -305,10 +368,6 @@ class TestCanReadFlagEvaluations(SimpleTestCase):
("not_between", _where(FLAG_CALLED, FLAG_KEY, "timestamp NOT BETWEEN now() - INTERVAL 7 DAY AND now()")),
("bound_inside_an_or", _where(FLAG_CALLED, FLAG_KEY, f"({BOUND} OR distinct_id = 'd')")),
("bound_on_another_column", _where(FLAG_CALLED, FLAG_KEY, "created_at >= now() - INTERVAL 7 DAY")),
- (
- "unqualified_bound_with_a_join",
- _joined(f"e.event = '$feature_flag_called' AND e.properties.$feature_flag = 'my-flag' AND {BOUND}"),
- ),
(
"bound_on_a_joined_alias",
_self_joined(
@@ -374,10 +433,6 @@ class TestCanReadFlagEvaluations(SimpleTestCase):
_where(FLAG_CALLED, "person.properties.$feature_flag = 'my-flag'", BOUND),
),
("pin_on_another_property", _where(FLAG_CALLED, "properties.$feature_flag_response = 'my-flag'", BOUND)),
- (
- "unqualified_flag_key_with_a_join",
- _joined(f"e.event = '$feature_flag_called' AND {FLAG_KEY} AND e.timestamp >= now() - INTERVAL 7 DAY"),
- ),
(
"flag_key_on_a_joined_alias",
_self_joined(
diff --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.py
index 025ccc363081..2949cb013b81 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
@@ -6732,6 +6732,44 @@ class TestTrendsQueryRunner(ClickhouseTestMixin, APIBaseTest):
if breakdown is not None:
assert response.results[0]["breakdown_value"] == "true"
+ def test_flag_call_series_broken_down_by_two_cohorts_reads_flag_evaluations_once_events_gets_no_flag_calls(self):
+ OrganizationFeatureFlagsConfig.objects.update_or_create(
+ organization=self.organization,
+ defaults={"flag_evaluations_mode": FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY},
+ )
+ cohorts = []
+ for name, calls in (("p1", 2), ("p2", 1)):
+ person = _create_person(team=self.team, distinct_ids=[name], properties={"name": name})
+ _create_flag_evaluations(self.team.pk, "probe-flag", count=calls, distinct_id=name, person_id=person.uuid)
+ cohort = Cohort.objects.create(
+ team=self.team,
+ groups=[{"properties": [{"key": "name", "value": name, "type": "person"}]}],
+ name=f"cohort {name}",
+ )
+ cohort.calculate_people_ch(pending_version=0)
+ cohorts.append(cohort)
+
+ with patch(
+ "posthog.hogql.database.database.feature_enabled_or_false",
+ side_effect=lambda key, *args, **kwargs: key == "hogql-flag-calls-from-flag-evaluations",
+ ):
+ # Two cohorts make the runner join `__in_cohort`, and the series pin compares `event` without a table name.
+ response = self._run_trends_query(
+ "-7d",
+ None,
+ IntervalType.DAY,
+ [_flag_call_event_series(self.team, ["probe-flag"])],
+ breakdown=BreakdownFilter(breakdown_type=BreakdownType.COHORT, breakdown=[c.pk for c in cohorts]),
+ hogql_modifiers=HogQLQueryModifiers(
+ personsOnEventsMode=PersonsOnEventsMode.PERSON_ID_OVERRIDE_PROPERTIES_ON_EVENTS
+ ),
+ )
+
+ assert {result["breakdown_value"]: result["count"] for result in response.results} == {
+ cohorts[0].pk: 2,
+ cohorts[1].pk: 1,
+ }
+
def test_multiple_breakdowns_work_with_formula(self):
self._create_test_events()
flush_persons_and_events()