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.

feat(flags): count flag exposure and errors in the watched conditions (#127693)

Gustavo H. Strassburger2a802738b1f0
diff --git a/posthog/models/property/relative_date.py b/posthog/models/property/relative_date.py
index 0310fe041a0a..c5dbc3582589 100644
--- a/posthog/models/property/relative_date.py
+++ b/posthog/models/property/relative_date.py
@@ -32,10 +32,13 @@ def determine_parsed_date_for_property_matching(value: ValueT) -> datetime.datet
# Note: Any changes to this function need to be reflected in the rust version
# rust/feature-flags/src/properties/relative_date.rs
-def relative_date_parse_for_feature_flag_matching(value: str) -> Optional[datetime.datetime]:
+def relative_date_parse_for_feature_flag_matching(
+ value: str, now: Optional[datetime.datetime] = None
+) -> Optional[datetime.datetime]:
+ """The date that `value`, such as "-7d", stands for, counted back from `now` or from the current time."""
regex = r"^-?(?P<number>[0-9]+)(?P<interval>[a-z])$"
match = re.search(regex, value)
- parsed_dt = datetime.datetime.now(tz=ZoneInfo("UTC"))
+ parsed_dt = now or datetime.datetime.now(tz=ZoneInfo("UTC"))
if match:
number = int(match.group("number"))
diff --git a/products/feature_flags/backend/flag_windows/errors.py b/products/feature_flags/backend/flag_windows/errors.py
index 3f563f491bbd..5f9c4d94674e 100644
--- a/products/feature_flags/backend/flag_windows/errors.py
+++ b/products/feature_flags/backend/flag_windows/errors.py
@@ -9,11 +9,12 @@ Flag rollback suggestions attribute errors to an edit from this split and from t
- Levels 4 and 5 follow all errors over time. An error of an issue first seen after the edit is stronger evidence than
an older error that became more frequent.
-The query reads only `$exception` events, so it costs far less than the exposure query. `flag_windows/query.py` holds
-the counting rules.
+The query reads the `$exception` events, and the flag calls of the watched flags for the counts in the watched conditions.
+`flag_windows/query.py` holds the counting rules.
"""
from collections.abc import Mapping, Sequence
+from dataclasses import field
from datetime import datetime, timedelta
from posthog.hogql import ast
@@ -22,7 +23,21 @@ from posthog.hogql.parser import parse_expr, parse_select
from posthog.dataclasses import frozen
from posthog.models import Team
-from products.feature_flags.backend.flag_windows.query import FlagRead, WatchedEdit, counts_by_bucket
+from products.feature_flags.backend.flag_windows.query import (
+ ConditionMatch,
+ EditCounts,
+ FlagRead,
+ WatchedEdit,
+ counts_by_bucket,
+ in_condition_columns,
+ read_in_condition,
+)
+
+
+@frozen
+class ConditionErrors:
+ exposed_with_errors: int
+ unexposed_with_errors: int
@frozen
@@ -38,6 +53,9 @@ class ErrorCounts:
unexposed_with_errors: int
# The people (or groups) whose error event carries each variant that the flag serves. Empty for a boolean flag.
by_variant: Mapping[str, int]
+ # The exposed and unexposed counts, only for the people (or groups) in the watched conditions. Empty without
+ # watched conditions.
+ in_condition: Mapping[ConditionMatch, ConditionErrors] = field(default_factory=dict)
@frozen
@@ -56,26 +74,27 @@ def errors_by_bucket(
counts = counts_by_bucket(
team,
edits,
- _conditions,
+ _counts,
start,
end,
bucket,
query_type="flag_errors_by_bucket",
event="$exception",
- join=_issue_first_seen_join(),
+ joins=[_issue_first_seen_join()],
)
- return {
- edit.id: [
- ErrorBucket(
- start=row.start,
- in_bucket=_errors(edit, row.in_bucket),
- since_edit=_errors(edit, row.since_edit),
- )
- for row in counts[edit.id]
- if _errors(edit, row.in_bucket).with_errors
- ]
- for edit in edits
- }
+ return {edit.id: _buckets(edit, counts[edit.id]) for edit in edits}
+
+
+def _buckets(edit: WatchedEdit, counts: EditCounts) -> list[ErrorBucket]:
+ return [
+ ErrorBucket(
+ start=row.start,
+ in_bucket=_errors(edit, counts.matches, row.in_bucket),
+ since_edit=_errors(edit, counts.matches, row.since_edit),
+ )
+ for row in counts.buckets
+ if _errors(edit, counts.matches, row.in_bucket).with_errors
+ ]
def _issue_first_seen_join() -> ast.JoinExpr:
@@ -98,7 +117,11 @@ def _issue_first_seen_join() -> ast.JoinExpr:
)
-def _conditions(edit: WatchedEdit, read: FlagRead) -> list[ast.Expr]:
+def _counts(edit: WatchedEdit, read: FlagRead) -> list[ast.Expr]:
+ unexposed = parse_expr(
+ "{with_flag} AND NOT {exposed}",
+ placeholders={"with_flag": read.with_flag, "exposed": read.exposed},
+ )
return [
ast.Constant(value=True),
# A fingerprint that isn't synced to ClickHouse yet has no issue, and counts as new. An event without a
@@ -111,20 +134,23 @@ def _conditions(edit: WatchedEdit, read: FlagRead) -> list[ast.Expr]:
placeholders={"edited_at": ast.Constant(value=edit.edited_at)},
),
read.exposed,
- parse_expr(
- "{with_flag} AND NOT {exposed}",
- placeholders={"with_flag": read.with_flag, "exposed": read.exposed},
- ),
+ unexposed,
*[read.has_variant(variant) for variant in edit.variants],
+ *in_condition_columns(read, [read.exposed, unexposed]),
]
-def _errors(edit: WatchedEdit, values: tuple[int, ...]) -> ErrorCounts:
- with_errors, with_new_errors, exposed_with_errors, unexposed_with_errors, *by_variant = values
+def _errors(edit: WatchedEdit, matches: Sequence[ConditionMatch], values: tuple[int, ...]) -> ErrorCounts:
+ with_errors, with_new_errors, exposed_with_errors, unexposed_with_errors, *rest = values
+ by_variant, in_condition_values = rest[: len(edit.variants)], rest[len(edit.variants) :]
return ErrorCounts(
with_errors=with_errors,
with_new_errors=with_new_errors,
exposed_with_errors=exposed_with_errors,
unexposed_with_errors=unexposed_with_errors,
by_variant=dict(zip(edit.variants, by_variant, strict=True)),
+ in_condition={
+ match: ConditionErrors(exposed_with_errors=counts[0], unexposed_with_errors=counts[1])
+ for match, counts in read_in_condition(matches, in_condition_values).items()
+ },
)
diff --git a/products/feature_flags/backend/flag_windows/exposure.py b/products/feature_flags/backend/flag_windows/exposure.py
index 7a11808b01e8..8e7bb076fba7 100644
--- a/products/feature_flags/backend/flag_windows/exposure.py
+++ b/products/feature_flags/backend/flag_windows/exposure.py
@@ -10,6 +10,7 @@ rules.
"""
from collections.abc import Mapping, Sequence
+from dataclasses import field
from datetime import datetime, timedelta
from posthog.hogql import ast
@@ -17,7 +18,21 @@ from posthog.hogql import ast
from posthog.dataclasses import frozen
from posthog.models import Team
-from products.feature_flags.backend.flag_windows.query import FlagRead, WatchedEdit, counts_by_bucket
+from products.feature_flags.backend.flag_windows.query import (
+ ConditionMatch,
+ EditCounts,
+ FlagRead,
+ WatchedEdit,
+ counts_by_bucket,
+ in_condition_columns,
+ read_in_condition,
+)
+
+
+@frozen
+class ConditionExposure:
+ with_flag: int
+ exposed: int
@frozen
@@ -28,6 +43,8 @@ class ExposureCounts:
exposed: int
# The people (or groups) with each variant that the flag serves. Empty for a boolean flag.
by_variant: Mapping[str, int]
+ # The same counts, only for the people (or groups) in the watched conditions. Empty without watched conditions.
+ in_condition: Mapping[ConditionMatch, ConditionExposure] = field(default_factory=dict)
@frozen
@@ -43,27 +60,40 @@ def exposure_by_bucket(
) -> dict[str, list[ExposureBucket]]:
"""The exposure of each edit in each bucket of `[start, end)`, by edit id, oldest bucket first. A bucket in which
nobody carried the flag after the edit has no entry. A person whose value changed counts under both values."""
- counts = counts_by_bucket(team, edits, _conditions, start, end, bucket, query_type="flag_exposure_by_bucket")
- return {
- edit.id: [
- ExposureBucket(
- start=row.start,
- in_bucket=_exposure(edit, row.in_bucket),
- since_edit=_exposure(edit, row.since_edit),
- )
- for row in counts[edit.id]
- if _exposure(edit, row.in_bucket).with_flag
- ]
- for edit in edits
- }
-
-
-def _conditions(edit: WatchedEdit, read: FlagRead) -> list[ast.Expr]:
- return [read.with_flag, read.exposed, *[read.has_variant(variant) for variant in edit.variants]]
-
-
-def _exposure(edit: WatchedEdit, values: tuple[int, ...]) -> ExposureCounts:
- with_flag, exposed, *by_variant = values
+ counts = counts_by_bucket(team, edits, _counts, start, end, bucket, query_type="flag_exposure_by_bucket")
+ return {edit.id: _buckets(edit, counts[edit.id]) for edit in edits}
+
+
+def _buckets(edit: WatchedEdit, counts: EditCounts) -> list[ExposureBucket]:
+ return [
+ ExposureBucket(
+ start=row.start,
+ in_bucket=_exposure(edit, counts.matches, row.in_bucket),
+ since_edit=_exposure(edit, counts.matches, row.since_edit),
+ )
+ for row in counts.buckets
+ if _exposure(edit, counts.matches, row.in_bucket).with_flag
+ ]
+
+
+def _counts(edit: WatchedEdit, read: FlagRead) -> list[ast.Expr]:
+ return [
+ read.with_flag,
+ read.exposed,
+ *[read.has_variant(variant) for variant in edit.variants],
+ *in_condition_columns(read, [read.with_flag, read.exposed]),
+ ]
+
+
+def _exposure(edit: WatchedEdit, matches: Sequence[ConditionMatch], values: tuple[int, ...]) -> ExposureCounts:
+ with_flag, exposed, *rest = values
+ by_variant, in_condition_values = rest[: len(edit.variants)], rest[len(edit.variants) :]
return ExposureCounts(
- with_flag=with_flag, exposed=exposed, by_variant=dict(zip(edit.variants, by_variant, strict=True))
+ with_flag=with_flag,
+ exposed=exposed,
+ by_variant=dict(zip(edit.variants, by_variant, strict=True)),
+ in_condition={
+ match: ConditionExposure(with_flag=counts[0], exposed=counts[1])
+ for match, counts in read_in_condition(matches, in_condition_values).items()
+ },
)
diff --git a/products/feature_flags/backend/flag_windows/query.py b/products/feature_flags/backend/flag_windows/query.py
index f0fa37174a78..5fdd03c4fd04 100644
--- a/products/feature_flags/backend/flag_windows/query.py
+++ b/products/feature_flags/backend/flag_windows/query.py
@@ -17,35 +17,89 @@ people per bucket and since the edit.
on a large flag a count of a subset can come out a little above the count of the whole, for example `exposed` above
`with_flag`. `uniqExact` would keep every person id of every count of every bucket in memory, in a query that reads
every event of the team.
+- An edit moves the people of some conditions only, and the people that match no condition get false before and after
+ it. The counts in the watched conditions leave the others out, in two ways that the backtest compares:
+ - By `$feature_flag_reason`, which the SDKs put on `$feature_flag_called` from the `/flags` response. The reason names
+ the condition that matched, but not the condition whose rollout left the person out, so on a flag with many
+ conditions the people outside the rollout of any condition count. A server SDK that evaluates the flag locally
+ sends no condition, so its people don't count. The SDKs send `$feature_flag_called` once per value of the flag, so a
+ person whose value the edit kept sends none after it and doesn't count either. One scan of `$feature_flag_called`
+ per kind of actor finds the reasons of every edit, and joins them to the events by the actor.
+ - By the properties of each event against the property filters of the conditions. Person properties come from the
+ event, so an event without person processing does not match a person filter. A filter on another flag always
+ matches, because the query can't evaluate a flag. A person who matches an earlier condition at 100% rollout gets
+ the flag from it, so they don't count. A relative date such as "-7d" counts back from the range end, so a rerun
+ of the same range gives the same counts. A condition that the query can't express, such as one on a deleted
+ static cohort, leaves its edit without counts by properties. A value that ClickHouse rejects when the query runs,
+ such as text compared with a number, leaves every edit without them.
One query per team reads every watched edit: a query per edit would read the same events again.
"""
import math
-from collections.abc import Callable, Sequence
+from collections.abc import Callable, Iterable, Mapping, Sequence
+from dataclasses import field, replace
from datetime import datetime, timedelta
+from enum import StrEnum
+from typing import Any
+
+import structlog
+from rest_framework.exceptions import ValidationError
from posthog.schema import HogQLQueryModifiers, PersonsOnEventsMode
from posthog.hogql import ast
from posthog.hogql.constants import MAX_SELECT_RETURNED_ROWS, HogQLGlobalSettings
from posthog.hogql.parser import parse_expr
+from posthog.hogql.property import property_to_expr
from posthog.hogql.query import execute_hogql_query
+from posthog.hogql.visitor import clone_expr
from posthog.clickhouse.client.connection import Workload
from posthog.clickhouse.query_tagging import Feature, Product, tags_context
from posthog.dataclasses import frozen
+from posthog.errors import InternalCHQueryError
from posthog.models import Team
+from products.feature_flags.backend.facade.enums import FlagEvaluationsMode
+from products.feature_flags.backend.facade.flags import get_flag_evaluations_read_mode
+from products.feature_flags.backend.person_sampling import SPILL_GROUP_BY_AFTER_BYTES
+from products.feature_flags.backend.user_blast_radius import (
+ replace_proxy_properties,
+ unevaluable_filters_as_validation_errors,
+)
+
+logger = structlog.get_logger(__name__)
+
# The health check query settings (posthog/temporal/health_checks/query.py). Nobody waits for the result, so the scan of
# every event of a team leaves CPU to the queries people wait for.
QUERY_TIMEOUT_SECONDS = 30
QUERY_MAX_THREADS = 2
+# ClickHouse rejects these when a property filter compares a value it can't cast or compile: CANNOT_PARSE_TEXT,
+# CANNOT_PARSE_DATETIME, TYPE_MISMATCH, CANNOT_PARSE_NUMBER and CANNOT_COMPILE_REGEXP.
+PROPERTY_VALUE_ERROR_CODES = frozenset({6, 41, 53, 72, 427})
+
IN_BUCKET = "_finalizeAggregation({state})"
SINCE_EDIT = "uniqMerge({state}) OVER (ORDER BY bucket ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)"
+class ConditionMatch(StrEnum):
+ REASON = "reason"
+ PROPERTIES = "properties"
+
+
+@frozen
+class WatchedCondition:
+ # The position of the condition in the flag after the edit. `$feature_flag_reason` counts from 1.
+ position: int
+ # The property filters of the condition, as `filters.groups` stores them.
+ properties: tuple[Mapping[str, Any], ...]
+ # The property filters of each earlier condition with a 100% rollout. A person who matches one of them gets the
+ # flag from that condition, so the edit can't move them.
+ earlier_at_full_rollout: tuple[tuple[Mapping[str, Any], ...], ...] = ()
+
+
@frozen
class WatchedEdit:
"""A flag edit that raised exposure, as the window queries read it."""
@@ -61,6 +115,8 @@ class WatchedEdit:
variants: tuple[str, ...]
# The edit enabled the flag, so no event carried the flag before it.
enables: bool
+ # The conditions whose people the edit can move. Without them, the counts in the watched conditions are left out.
+ conditions: tuple[WatchedCondition, ...] = ()
@frozen
@@ -77,6 +133,8 @@ class FlagRead:
exposed: ast.Expr
# The variant key on the event. Compare it with a served variant.
variant_key: ast.Expr
+ # The person (or group) is in a watched condition, by each way of matching. Empty without watched conditions.
+ in_condition: Mapping[ConditionMatch, ast.Expr] = field(default_factory=dict)
def has_variant(self, variant: str) -> ast.Expr:
return parse_expr(
@@ -94,13 +152,30 @@ class BucketCounts:
since_edit: tuple[int, ...]
-def read_flag(edit: WatchedEdit) -> FlagRead:
- if edit.group_type_index is None:
- actor = ast.Field(chain=["person_id"])
- has_actor: ast.Expr = ast.Constant(value=True)
- else:
- actor = ast.Field(chain=[f"$group_{edit.group_type_index}"])
- has_actor = parse_expr("notEmpty({actor})", placeholders={"actor": actor})
+@frozen
+class EditCounts:
+ # The ways of matching that the edit has counts in the watched conditions for, in the order of their columns.
+ matches: tuple[ConditionMatch, ...]
+ buckets: tuple[BucketCounts, ...]
+
+
+@frozen
+class _Actor:
+ # The person, or the group that a group flag aggregates by.
+ expr: ast.Expr
+ # The event has the actor. A group flag needs the group on the event.
+ present: ast.Expr
+
+
+def _actor(group_type_index: int | None) -> _Actor:
+ if group_type_index is None:
+ return _Actor(expr=ast.Field(chain=["person_id"]), present=ast.Constant(value=True))
+ actor = ast.Field(chain=[f"$group_{group_type_index}"])
+ return _Actor(expr=actor, present=parse_expr("notEmpty({actor})", placeholders={"actor": actor}))
+
+
+def read_flag(edit: WatchedEdit, in_condition: Mapping[ConditionMatch, ast.Expr]) -> FlagRead:
+ actor = _actor(edit.group_type_index)
# A flag key can hold any character, so it goes in as an AST node, never as HogQL text.
property_name = f"$feature/{edit.flag_key}"
flag_value = ast.Call(name="toString", args=[ast.Field(chain=["properties", property_name])])
@@ -137,23 +212,372 @@ def read_flag(edit: WatchedEdit) -> FlagRead:
)
else:
exposed = parse_expr("{flag_value} = 'true'", placeholders={"flag_value": flag_value})
- return FlagRead(actor=actor, has_actor=has_actor, with_flag=with_flag, exposed=exposed, variant_key=variant_key)
+ return FlagRead(
+ actor=actor.expr,
+ has_actor=actor.present,
+ with_flag=with_flag,
+ exposed=exposed,
+ variant_key=variant_key,
+ in_condition=in_condition,
+ )
+
+
+def _in_condition(
+ team: Team, edit: WatchedEdit, by_reason: ast.Expr | None, end: datetime, with_properties: bool
+) -> dict[ConditionMatch, ast.Expr]:
+ """`by_reason` is the column of `_reason_joins` for the edit, or None without watched conditions."""
+ if by_reason is None:
+ return {}
+ in_condition = {ConditionMatch.REASON: parse_expr("{by_reason} = 1", placeholders={"by_reason": by_reason})}
+ if not with_properties:
+ return in_condition
+ try:
+ # The context turns every error of a filter that the query can't express into a ValidationError: a cohort that
+ # was deleted after the edit or no longer exists, a cohort id that isn't a number, a filter type HogQL can't
+ # convert. `property_to_expr` raises an AssertionError for a date filter whose value isn't a string.
+ with unevaluable_filters_as_validation_errors():
+ in_condition[ConditionMatch.PROPERTIES] = ast.Or(
+ exprs=[_matches_condition(team, condition, end) for condition in edit.conditions]
+ )
+ except (ValidationError, AssertionError) as error:
+ # The edit gets no counts by properties, so that the query still counts the other edits of the team. The
+ # message can quote a filter value, such as an email pattern, so only its type goes to the logs.
+ logger.warning(
+ "flag_window_condition_without_expression",
+ team_id=team.pk,
+ flag_key=edit.flag_key,
+ error_type=type(error).__name__,
+ )
+ return in_condition
+
+
+def _matches_condition(team: Team, condition: WatchedCondition, end: datetime) -> ast.Expr:
+ def matches(properties: Sequence[Mapping[str, Any]]) -> ast.Expr:
+ return property_to_expr(
+ replace_proxy_properties(team, {"properties": list(properties)}, now=end), team, scope="event"
+ )
+
+ # The query reads a filter on another flag as always true, so leaving out the people of an earlier condition with
+ # one would leave out everyone.
+ earlier = [
+ properties
+ for properties in condition.earlier_at_full_rollout
+ if not any(prop.get("type") == "flag" for prop in properties)
+ ]
+ if not earlier:
+ return matches(condition.properties)
+ return ast.And(
+ exprs=[
+ matches(condition.properties),
+ ast.Not(expr=ast.Or(exprs=[matches(properties) for properties in earlier])),
+ ]
+ )
+
+
+@frozen
+class _FlagCalls:
+ """Where the reason joins read the flag calls of a team."""
+
+ table: tuple[str, ...]
+ # Limits the rows to flag calls.
+ only_calls: ast.Expr
+ flag_key: ast.Expr
+ # The stored person id. The events read it too, because the query doesn't apply person merges.
+ person_id: ast.Expr
+
+ @classmethod
+ def of(cls, team: Team) -> "_FlagCalls":
+ # Ingestion writes no flag calls to events for some teams of an organization on FLAG_EVALUATIONS_ONLY. The
+ # flag_evaluations swap can't move this scan, because there `person_id` applies person merges. So the scan
+ # reads `flag_evaluations` by name. In the other modes events holds every flag call, and flag_evaluations
+ # only those of the teams that ingestion writes there.
+ if get_flag_evaluations_read_mode(team.organization_id) != FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY:
+ return cls(
+ table=("events",),
+ only_calls=parse_expr("event = '$feature_flag_called'"),
+ flag_key=parse_expr("properties.$feature_flag"),
+ person_id=ast.Field(chain=["person_id"]),
+ )
+ return cls(
+ table=("posthog", "flag_evaluations"),
+ only_calls=ast.Constant(value=True),
+ flag_key=ast.Field(chain=["flag_key"]),
+ person_id=ast.Field(chain=["flag_evaluation_person_id"]),
+ )
+
+
+def _reason_joins(
+ team: Team, edits: Sequence[WatchedEdit], end: datetime
+) -> tuple[list[ast.JoinExpr], dict[str, ast.Expr]]:
+ """The joins that tell, for each edit with watched conditions, whether the actor of an event sent a flag call
+ with a watched reason after the edit, and the column of each edit. One join per kind of actor, so the query reads
+ the flag calls once for all the edits of that kind."""
+ edits_by_actor: dict[int | None, list[tuple[int, WatchedEdit]]] = {}
+ for index, edit in enumerate(edits):
+ if edit.conditions:
+ edits_by_actor.setdefault(edit.group_type_index, []).append((index, edit))
+ if not edits_by_actor:
+ return [], {}
+ calls = _FlagCalls.of(team)
+ joins: list[ast.JoinExpr] = []
+ columns: dict[str, ast.Expr] = {}
+ for group_type_index, watched in edits_by_actor.items():
+ alias = "flag_reasons" if group_type_index is None else f"flag_reasons_{group_type_index}"
+ actor = _actor(group_type_index)
+ reasons_by_edit = {edit.id: _watched_reasons(edit) for _, edit in watched}
+ subquery = ast.SelectQuery(
+ select=[
+ ast.Alias(alias="actor", expr=calls.person_id if group_type_index is None else actor.expr),
+ *[
+ ast.Alias(
+ alias=f"e{index}",
+ expr=_called_with_watched_reason(edit, calls, reasons_by_edit[edit.id]),
+ )
+ for index, edit in watched
+ ],
+ ],
+ select_from=ast.JoinExpr(table=ast.Field(chain=list(calls.table))),
+ # The reasons of every watched edit keep only the calls that some column can count, and the HAVING keeps
+ # only the actors that one does. That holds the join's table to the actors in a watched condition.
+ where=parse_expr(
+ """
+ {only_calls}
+ AND timestamp >= {since}
+ AND timestamp < {end}
+ AND {flag_key} IN {flag_keys}
+ AND properties.$feature_flag_reason IN {reasons}
+ AND {has_actor}
+ """,
+ placeholders={
+ "only_calls": calls.only_calls,
+ "since": ast.Constant(value=min(edit.edited_at for _, edit in watched)),
+ "end": ast.Constant(value=end),
+ "flag_key": calls.flag_key,
+ "flag_keys": _constants(edit.flag_key for _, edit in watched),
+ "reasons": _constants(reason for reasons in reasons_by_edit.values() for reason in reasons),
+ "has_actor": actor.present,
+ },
+ ),
+ group_by=[ast.Field(chain=["actor"])],
+ having=ast.Or(
+ exprs=[
+ parse_expr("{column} = 1", placeholders={"column": ast.Field(chain=[f"e{index}"])})
+ for index, _ in watched
+ ]
+ ),
+ )
+ joins.append(
+ ast.JoinExpr(
+ join_type="LEFT JOIN",
+ table=subquery,
+ alias=alias,
+ constraint=ast.JoinConstraint(
+ expr=parse_expr(
+ "{reasons_actor} = {actor}",
+ placeholders={"reasons_actor": ast.Field(chain=[alias, "actor"]), "actor": actor.expr},
+ ),
+ constraint_type="ON",
+ ),
+ )
+ )
+ for index, edit in watched:
+ columns[edit.id] = ast.Field(chain=[alias, f"e{index}"])
+ return joins, columns
+
+
+def _watched_reasons(edit: WatchedEdit) -> list[str]:
+ # The descriptions of `FeatureFlagMatchReason` in rust/feature-flags/src/api/types.rs. "Out of rollout bound" names
+ # no condition, so it can come from a watched condition or from any other.
+ reasons = [f"Matched condition set {condition.position + 1}" for condition in edit.conditions]
+ reasons.append("Out of rollout bound")
+ return reasons
+
+
+def _called_with_watched_reason(edit: WatchedEdit, calls: _FlagCalls, reasons: Sequence[str]) -> ast.Expr:
+ return parse_expr(
+ """
+ max(
+ {flag_key} = {edit_flag_key}
+ AND timestamp >= {edited_at}
+ AND properties.$feature_flag_reason IN {reasons}
+ )
+ """,
+ placeholders={
+ "flag_key": calls.flag_key,
+ "edit_flag_key": ast.Constant(value=edit.flag_key),
+ "edited_at": ast.Constant(value=edit.edited_at),
+ "reasons": _constants(reasons),
+ },
+ )
+
+
+def _constants(values: Iterable[str]) -> ast.Tuple:
+ return ast.Tuple(exprs=[ast.Constant(value=value) for value in sorted(set(values))])
+
+
+def in_condition_columns(read: FlagRead, counts: Sequence[ast.Expr]) -> list[ast.Expr]:
+ """Each of `counts`, only for the people in the watched conditions, for each way of matching that `read` has."""
+ return [ast.And(exprs=[count, in_condition]) for in_condition in read.in_condition.values() for count in counts]
+
+
+def read_in_condition(
+ matches: Sequence[ConditionMatch], values: Sequence[int]
+) -> dict[ConditionMatch, tuple[int, ...]]:
+ """The values of the columns from `in_condition_columns`, split evenly between the ways of matching."""
+ if not matches:
+ return {}
+ per_match = len(values) // len(matches)
+ return {
+ match: tuple(values[position * per_match : (position + 1) * per_match])
+ for position, match in enumerate(matches)
+ }
+
+
+def _chain(joins: Sequence[ast.JoinExpr]) -> ast.JoinExpr | None:
+ head: ast.JoinExpr | None = None
+ for join in reversed(joins):
+ head = replace(join, next_join=head)
+ return head
+
+
+def _states(index: int, edit: WatchedEdit, read: FlagRead, counts: Sequence[ast.Expr]) -> list[ast.Alias]:
+ return [
+ ast.Alias(
+ alias=f"e{index}_{position}",
+ expr=parse_expr(
+ "uniqState(if({has_actor} AND timestamp >= {edited_at} AND {count}, {actor}, NULL))",
+ placeholders={
+ "actor": read.actor,
+ "has_actor": read.has_actor,
+ "edited_at": ast.Constant(value=edit.edited_at),
+ "count": count,
+ },
+ ),
+ )
+ for position, count in enumerate(counts)
+ ]
+
+
+@frozen
+class _BucketQuery:
+ team: Team
+ edits: Sequence[WatchedEdit]
+ counts: Callable[[WatchedEdit, FlagRead], Sequence[ast.Expr]]
+ start: datetime
+ end: datetime
+ seconds: int
+ buckets: int
+ query_type: str
+ event: str | None
+ joins: tuple[ast.JoinExpr, ...]
+
+ def run(self, with_properties: bool) -> dict[str, EditCounts]:
+ # Each run builds its own tree, because resolving a query can change the nodes in it.
+ reason_joins, reason_columns = _reason_joins(self.team, self.edits, self.end)
+ joins = [*(clone_expr(join) for join in self.joins), *reason_joins]
+ reads = [
+ read_flag(edit, _in_condition(self.team, edit, reason_columns.get(edit.id), self.end, with_properties))
+ for edit in self.edits
+ ]
+ states_by_edit = [
+ _states(index, edit, read, self.counts(edit, read))
+ for index, (edit, read) in enumerate(zip(self.edits, reads))
+ ]
+ states = [state for edit_states in states_by_edit for state in edit_states]
+ # Buckets start at `start`, not at a multiple of the bucket since the epoch, so a range that starts at an edit
+ # time does not begin with a partial bucket.
+ bucket_start = parse_expr(
+ "toStartOfInterval(timestamp, toIntervalSecond({seconds}), {start})",
+ placeholders={"seconds": ast.Constant(value=self.seconds), "start": ast.Constant(value=self.start)},
+ )
+ where = parse_expr(
+ "timestamp >= {start} AND timestamp < {end}",
+ placeholders={"start": ast.Constant(value=self.start), "end": ast.Constant(value=self.end)},
+ )
+ if self.event is not None:
+ where = ast.And(
+ exprs=[where, parse_expr("event = {event}", placeholders={"event": ast.Constant(value=self.event)})]
+ )
+ per_bucket = ast.SelectQuery(
+ select=[ast.Alias(alias="bucket", expr=bucket_start), *states],
+ select_from=ast.JoinExpr(table=ast.Field(chain=["events"]), next_join=_chain(joins)),
+ where=where,
+ group_by=[ast.Field(chain=["bucket"])],
+ )
+ query = ast.SelectQuery(
+ select=[
+ ast.Field(chain=["bucket"]),
+ *[
+ parse_expr(template, placeholders={"state": ast.Field(chain=[state.alias])})
+ for state in states
+ for template in (IN_BUCKET, SINCE_EDIT)
+ ],
+ ],
+ select_from=ast.JoinExpr(table=per_bucket),
+ order_by=[ast.OrderExpr(expr=ast.Field(chain=["bucket"]), order="ASC")],
+ # One row per bucket. HogQL adds LIMIT 100 to a query without one.
+ limit=ast.Constant(value=self.buckets),
+ )
+ with tags_context(
+ product=Product.FEATURE_FLAGS,
+ feature=Feature.HEALTH_CHECK,
+ team_id=self.team.pk,
+ org_id=self.team.organization_id,
+ ):
+ response = execute_hogql_query(
+ query,
+ team=self.team,
+ query_type=self.query_type,
+ workload=Workload.OFFLINE,
+ # The join that applies person merges costs seconds on large teams and moves the counts by a tiny
+ # fraction, because a merge moves few people.
+ modifiers=HogQLQueryModifiers(
+ personsOnEventsMode=PersonsOnEventsMode.PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS
+ ),
+ # A query that breaks on timeout returns partial counts, which would read as lower counts.
+ settings=HogQLGlobalSettings(
+ max_execution_time=QUERY_TIMEOUT_SECONDS,
+ max_threads=QUERY_MAX_THREADS,
+ timeout_overflow_mode="throw",
+ # The reason joins group the flag calls by actor. On a large team the aggregation goes to disk
+ # instead of failing the counts of every edit.
+ max_bytes_before_external_group_by=SPILL_GROUP_BY_AFTER_BYTES,
+ ),
+ )
+
+ rows_by_edit: dict[str, list[BucketCounts]] = {edit.id: [] for edit in self.edits}
+ for row in response.results or []:
+ values = iter(row[1:])
+ for edit, edit_states in zip(self.edits, states_by_edit):
+ # Each state gives the bucket's own value, then the value since the edit.
+ pairs = [(int(next(values)), int(next(values))) for _ in edit_states]
+ rows_by_edit[edit.id].append(
+ BucketCounts(
+ start=row[0],
+ in_bucket=tuple(pair[0] for pair in pairs),
+ since_edit=tuple(pair[1] for pair in pairs),
+ )
+ )
+ return {
+ edit.id: EditCounts(matches=tuple(read.in_condition), buckets=tuple(rows_by_edit[edit.id]))
+ for edit, read in zip(self.edits, reads)
+ }
def counts_by_bucket(
team: Team,
edits: Sequence[WatchedEdit],
- conditions: Callable[[WatchedEdit, FlagRead], Sequence[ast.Expr]],
+ counts: Callable[[WatchedEdit, FlagRead], Sequence[ast.Expr]],
start: datetime,
end: datetime,
bucket: timedelta,
query_type: str,
event: str | None = None,
- join: ast.JoinExpr | None = None,
-) -> dict[str, list[BucketCounts]]:
- """For each edit, the people (or groups) that meet each of its conditions after the edit, in each bucket of
+ joins: Sequence[ast.JoinExpr] = (),
+) -> dict[str, EditCounts]:
+ """For each edit, the people (or groups) that each of its `counts` holds for after the edit, in each bucket of
`[start, end)` that has events, oldest bucket first. `query_type` names the query in the query log. `event` limits
- the scan to one event name. `join` joins a table to the events, so that the conditions of every edit can read it."""
+ the scan to one event name. `joins` join tables to the events, so that the counts of every edit can read them."""
seconds = bucket.total_seconds()
if seconds < 1 or seconds != int(seconds):
raise ValueError(f"A bucket must be a whole number of seconds, got {bucket}")
@@ -168,91 +592,24 @@ def counts_by_bucket(
if not edits:
return {}
- states_by_edit = [_states(index, edit, conditions(edit, read_flag(edit))) for index, edit in enumerate(edits)]
- states = [state for edit_states in states_by_edit for state in edit_states]
- # Buckets start at `start`, not at a multiple of the bucket since the epoch, so a range that starts at an edit
- # time does not begin with a partial bucket.
- bucket_start = parse_expr(
- "toStartOfInterval(timestamp, toIntervalSecond({seconds}), {start})",
- placeholders={"seconds": ast.Constant(value=int(seconds)), "start": ast.Constant(value=start)},
- )
- where = parse_expr(
- "timestamp >= {start} AND timestamp < {end}",
- placeholders={"start": ast.Constant(value=start), "end": ast.Constant(value=end)},
- )
- if event is not None:
- where = ast.And(exprs=[where, parse_expr("event = {event}", placeholders={"event": ast.Constant(value=event)})])
- per_bucket = ast.SelectQuery(
- select=[ast.Alias(alias="bucket", expr=bucket_start), *states],
- select_from=ast.JoinExpr(table=ast.Field(chain=["events"]), next_join=join),
- where=where,
- group_by=[ast.Field(chain=["bucket"])],
- )
- query = ast.SelectQuery(
- select=[
- ast.Field(chain=["bucket"]),
- *[
- parse_expr(template, placeholders={"state": ast.Field(chain=[state.alias])})
- for state in states
- for template in (IN_BUCKET, SINCE_EDIT)
- ],
- ],
- select_from=ast.JoinExpr(table=per_bucket),
- order_by=[ast.OrderExpr(expr=ast.Field(chain=["bucket"]), order="ASC")],
- # One row per bucket. HogQL adds LIMIT 100 to a query without one.
- limit=ast.Constant(value=buckets),
+ query = _BucketQuery(
+ team=team,
+ edits=edits,
+ counts=counts,
+ start=start,
+ end=end,
+ seconds=int(seconds),
+ buckets=buckets,
+ query_type=query_type,
+ event=event,
+ joins=tuple(joins),
)
- with tags_context(
- product=Product.FEATURE_FLAGS, feature=Feature.HEALTH_CHECK, team_id=team.pk, org_id=team.organization_id
- ):
- response = execute_hogql_query(
- query,
- team=team,
- query_type=query_type,
- workload=Workload.OFFLINE,
- # The join that applies person merges costs seconds on large teams and moves the counts by a tiny
- # fraction, because a merge moves few people.
- modifiers=HogQLQueryModifiers(
- personsOnEventsMode=PersonsOnEventsMode.PERSON_ID_NO_OVERRIDE_PROPERTIES_ON_EVENTS
- ),
- # A query that breaks on timeout returns partial counts, which would read as lower counts.
- settings=HogQLGlobalSettings(
- max_execution_time=QUERY_TIMEOUT_SECONDS,
- max_threads=QUERY_MAX_THREADS,
- timeout_overflow_mode="throw",
- ),
- )
-
- result: dict[str, list[BucketCounts]] = {edit.id: [] for edit in edits}
- for row in response.results or []:
- values = iter(row[1:])
- for edit, edit_states in zip(edits, states_by_edit):
- # Each state gives the bucket's own value, then the value since the edit.
- pairs = [(int(next(values)), int(next(values))) for _ in edit_states]
- result[edit.id].append(
- BucketCounts(
- start=row[0],
- in_bucket=tuple(pair[0] for pair in pairs),
- since_edit=tuple(pair[1] for pair in pairs),
- )
- )
- return result
-
-
-def _states(index: int, edit: WatchedEdit, conditions: Sequence[ast.Expr]) -> list[ast.Alias]:
- read = read_flag(edit)
- return [
- ast.Alias(
- alias=f"e{index}_{position}",
- expr=parse_expr(
- "uniqState(if({has_actor} AND timestamp >= {edited_at} AND {condition}, {actor}, NULL))",
- placeholders={
- "actor": read.actor,
- "has_actor": read.has_actor,
- "edited_at": ast.Constant(value=edit.edited_at),
- "condition": condition,
- },
- ),
- )
- for position, condition in enumerate(conditions)
- ]
+ try:
+ return query.run(with_properties=True)
+ except InternalCHQueryError as error:
+ if error.code not in PROPERTY_VALUE_ERROR_CODES:
+ raise
+ # ClickHouse doesn't say which filter it rejected, so every edit loses its counts by properties. Its message
+ # quotes the value it couldn't parse, so only the code goes to the logs.
+ logger.warning("flag_window_properties_rejected", team_id=team.pk, code=error.code)
+ return query.run(with_properties=False)
diff --git a/products/feature_flags/backend/person_sampling.py b/products/feature_flags/backend/person_sampling.py
index f6e4f15cb113..40c17dda888d 100644
--- a/products/feature_flags/backend/person_sampling.py
+++ b/products/feature_flags/backend/person_sampling.py
@@ -14,6 +14,9 @@ from posthog.models.property import PropertyGroup
from posthog.models.team.team import Team
from posthog.schema_enums import PersonsArgMaxVersion
+# Above this, a GROUP BY goes to disk instead of failing the query on the memory limit.
+SPILL_GROUP_BY_AFTER_BYTES = 4 * 1024**3
+
# 1-in-64 sample of the person keyspace. Sampling keys on person id (the dedup key), so every
# version row of a sampled person lands in the sample and the argMax dedup stays exact within it.
SAMPLE_MODULUS = 64
@@ -48,7 +51,7 @@ def bounded_memory_settings() -> HogQLGlobalSettings:
# Left unset it inherits the cluster profile, which may accept partial results.
timeout_overflow_mode="throw",
optimize_aggregation_in_order=True,
- max_bytes_before_external_group_by=4 * 1024**3,
+ max_bytes_before_external_group_by=SPILL_GROUP_BY_AFTER_BYTES,
)
@@ -60,7 +63,7 @@ def count_settings(sample_modulus: Optional[int]) -> HogQLGlobalSettings:
if sample_modulus is not None:
return HogQLGlobalSettings(
timeout_overflow_mode="throw",
- max_bytes_before_external_group_by=4 * 1024**3,
+ max_bytes_before_external_group_by=SPILL_GROUP_BY_AFTER_BYTES,
)
return bounded_memory_settings()
diff --git a/products/feature_flags/backend/test/test_blast_radius_v2.py b/products/feature_flags/backend/test/test_blast_radius_v2.py
index 7a5c9dbf0e03..77085d517beb 100644
--- a/products/feature_flags/backend/test/test_blast_radius_v2.py
+++ b/products/feature_flags/backend/test/test_blast_radius_v2.py
@@ -47,6 +47,18 @@ class TestBlastRadiusQueryV2(ClickhouseTestMixin, APIBaseTest):
assert (result.affected, result.total) == (affected, total)
assert (result.affected, result.total) == (v1_result.affected, v1_result.total)
+ def test_a_boolean_filter_value_matches_people_stored_with_a_json_boolean(self):
+ for i, subscribed in enumerate([True, True, False, "True"], start=1):
+ _create_person(team=self.team, distinct_ids=[f"user-{i}"], properties={"subscribed": subscribed})
+ flush_persons_and_events()
+ filters = {"properties": [{"key": "subscribed", "type": "person", "value": [True], "operator": "exact"}]}
+
+ v1_result = get_user_blast_radius(self.team, filters)
+ result = get_person_blast_radius_v2(self.team, filters)
+
+ # The flag evaluator also serves the string "True", but HogQL compares strings case-sensitively.
+ assert (result.affected, result.total, v1_result.affected) == (2, 4, 2)
+
def test_flag_dependency_is_weighted_on_both_paths(self):
self._create_persons([True, True, True, False])
dependency_flag = FeatureFlag.objects.create(
diff --git a/products/feature_flags/backend/test/test_flag_windows_errors.py b/products/feature_flags/backend/test/test_flag_windows_errors.py
index af0dd2ba9819..d4972fdb9cfb 100644
--- a/products/feature_flags/backend/test/test_flag_windows_errors.py
+++ b/products/feature_flags/backend/test/test_flag_windows_errors.py
@@ -1,3 +1,4 @@
+import dataclasses
from datetime import UTC, datetime, timedelta
from typing import Any
@@ -12,8 +13,8 @@ from products.error_tracking.backend.facade.testing import (
create_issue_fingerprint,
sync_issue_to_clickhouse,
)
-from products.feature_flags.backend.flag_windows.errors import ErrorCounts, errors_by_bucket
-from products.feature_flags.backend.flag_windows.query import WatchedEdit
+from products.feature_flags.backend.flag_windows.errors import ConditionErrors, ErrorCounts, errors_by_bucket
+from products.feature_flags.backend.flag_windows.query import ConditionMatch, WatchedCondition, WatchedEdit
EDITED_AT = datetime(2026, 10, 1, 12, 0, tzinfo=UTC)
WINDOW = timedelta(minutes=30)
@@ -38,9 +39,14 @@ Event = tuple[str, str, int, dict[str, Any]]
class TestErrorsByBucket(ClickhouseTestMixin, BaseTest):
- def _send(self, events: list[Event]) -> None:
+ def _send(self, events: list[Event], plans: dict[str, str] | None = None) -> None:
+ plans = plans or {}
for distinct_id in dict.fromkeys(distinct_id for distinct_id, _, _, _ in events):
- create_person(team=self.team, distinct_ids=[distinct_id])
+ create_person(
+ team=self.team,
+ distinct_ids=[distinct_id],
+ properties={"plan": plans[distinct_id]} if distinct_id in plans else {},
+ )
for distinct_id, event, minutes, properties in events:
_create_event(
team=self.team,
@@ -152,3 +158,33 @@ class TestErrorsByBucket(ClickhouseTestMixin, BaseTest):
{edit_id: buckets[0].since_edit.with_new_errors for edit_id, buckets in errors.items()},
{"boolean": 1, "earlier": 3},
)
+
+ def test_errors_in_the_watched_conditions_split_by_reason_and_by_properties(self) -> None:
+ def called(flag_key: str, reason: str, value: bool) -> dict[str, Any]:
+ return {"$feature_flag": flag_key, "$feature_flag_reason": reason, f"$feature/{flag_key}": value}
+
+ self._send(
+ [
+ ("person-1", "$feature_flag_called", 1, called("new-checkout", "Matched condition set 2", True)),
+ ("person-1", "$exception", 2, {"$feature/new-checkout": True}),
+ ("person-2", "$feature_flag_called", 3, called("new-checkout", "Out of rollout bound", False)),
+ ("person-2", "$exception", 4, {"$feature/new-checkout": False}),
+ ("person-3", "$feature_flag_called", 5, called("other-flag", "Matched condition set 2", True)),
+ ("person-3", "$exception", 6, {"$feature/new-checkout": True}),
+ ("person-4", "$exception", 7, {"$feature/new-checkout": True}),
+ ],
+ plans={"person-1": "pro", "person-2": "pro", "person-3": "free", "person-4": "pro"},
+ )
+ pro_plan = WatchedCondition(
+ position=1, properties=({"key": "plan", "type": "person", "value": ["pro"], "operator": "exact"},)
+ )
+
+ errors = self._since_edit(dataclasses.replace(BOOLEAN, conditions=(pro_plan,)))
+
+ self.assertEqual(
+ errors.in_condition,
+ {
+ ConditionMatch.REASON: ConditionErrors(exposed_with_errors=1, unexposed_with_errors=1),
+ ConditionMatch.PROPERTIES: ConditionErrors(exposed_with_errors=2, unexposed_with_errors=1),
+ },
+ )
diff --git a/products/feature_flags/backend/test/test_flag_windows_exposure.py b/products/feature_flags/backend/test/test_flag_windows_exposure.py
index 162717afe2cb..fc1ad4d344b2 100644
--- a/products/feature_flags/backend/test/test_flag_windows_exposure.py
+++ b/products/feature_flags/backend/test/test_flag_windows_exposure.py
@@ -1,8 +1,15 @@
import dataclasses
+from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from typing import Any
-from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
+from posthog.test.base import (
+ BaseTest,
+ ClickhouseTestMixin,
+ _create_event,
+ _create_flag_evaluations,
+ flush_persons_and_events,
+)
from django.test import SimpleTestCase
@@ -11,10 +18,20 @@ from parameterized import parameterized
from posthog.hogql.constants import MAX_SELECT_RETURNED_ROWS
from posthog.models import Team
-from posthog.test.persons import create_person
+from posthog.models.group.util import raw_create_group_ch
+from posthog.test.persons import create_group_type_mapping, create_person
-from products.feature_flags.backend.flag_windows.exposure import ExposureBucket, ExposureCounts, exposure_by_bucket
-from products.feature_flags.backend.flag_windows.query import WatchedEdit
+from products.cohorts.backend.models.cohort import Cohort
+from products.event_definitions.backend.models.property_definition import PropertyDefinition
+from products.feature_flags.backend.facade.enums import FlagEvaluationsMode
+from products.feature_flags.backend.flag_windows.exposure import (
+ ConditionExposure,
+ ExposureBucket,
+ ExposureCounts,
+ exposure_by_bucket,
+)
+from products.feature_flags.backend.flag_windows.query import ConditionMatch, WatchedCondition, WatchedEdit
+from products.feature_flags.backend.models.organization_feature_flags_config import OrganizationFeatureFlagsConfig
START = datetime(2026, 10, 1, 12, 0, tzinfo=UTC)
BUCKET = timedelta(minutes=10)
@@ -44,18 +61,33 @@ ENABLED = WatchedEdit(
id="enabled", flag_key="new-checkout", edited_at=START, group_type_index=None, variants=(), enables=True
)
+PRO_PLAN = WatchedCondition(
+ position=1, properties=({"key": "plan", "type": "person", "value": ["pro"], "operator": "exact"},)
+)
+EVERYONE = WatchedCondition(position=0, properties=())
+
# (distinct id, minutes after START, event properties)
Event = tuple[str, int, dict[str, Any]]
+def _text_compared_with_a_number(team: Team) -> dict[str, Any]:
+ PropertyDefinition.objects.create(
+ team=team, name="age", property_type="Numeric", type=PropertyDefinition.Type.PERSON
+ )
+ return {"key": "age", "type": "person", "value": "abc", "operator": "gt"}
+
+
class TestExposureByBucket(ClickhouseTestMixin, BaseTest):
- def _send(self, events: list[Event]) -> None:
+ def _send(
+ self, events: list[Event], event: str = "$pageview", people: dict[str, dict[str, Any]] | None = None
+ ) -> None:
+ people = people or {}
for distinct_id in dict.fromkeys(distinct_id for distinct_id, _, _ in events):
- create_person(team=self.team, distinct_ids=[distinct_id])
+ create_person(team=self.team, distinct_ids=[distinct_id], properties=people.get(distinct_id, {}))
for distinct_id, minutes, properties in events:
_create_event(
team=self.team,
- event="$pageview",
+ event=event,
distinct_id=distinct_id,
timestamp=START + timedelta(minutes=minutes),
properties=properties,
@@ -211,6 +243,300 @@ class TestExposureByBucket(ClickhouseTestMixin, BaseTest):
},
)
+ def test_counts_in_the_watched_conditions_by_reason_and_by_properties(self) -> None:
+ def called(reason: str, value: bool, flag_key: str = "new-checkout") -> dict[str, Any]:
+ return {"$feature_flag": flag_key, "$feature_flag_reason": reason, f"$feature/{flag_key}": value}
+
+ self._send(
+ [
+ ("person-1", 1, called("Matched condition set 2", True)),
+ ("person-2", 2, called("Out of rollout bound", False)),
+ ("person-3", 3, called("Matched condition set 1", True)),
+ ("person-4", 4, called("Evaluated locally", True)),
+ ("person-5", 5, called("Matched condition set 1", True)),
+ ("person-6", 6, called("Matched condition set 1", True, flag_key="other-flag")),
+ (
+ "person-7",
+ 7,
+ {**called("Matched condition set 2", True, flag_key="other-flag"), "$feature/new-checkout": True},
+ ),
+ ("person-8", 2, called("Matched condition set 1", True, flag_key="other-flag")),
+ ("person-9", 8, called("Matched condition set 3", True)),
+ ("person-10", 9, called("Matched condition set 1", True)),
+ ],
+ event="$feature_flag_called",
+ people={
+ "person-1": {"plan": "pro"},
+ "person-2": {"plan": "pro"},
+ "person-3": {"plan": "free"},
+ "person-4": {"plan": "pro"},
+ "person-5": {"plan": "free"},
+ "person-7": {"plan": "free"},
+ "person-9": {"plan": "enterprise"},
+ "person-10": {"plan": "pro", "beta": "yes"},
+ },
+ )
+ _create_event(
+ team=self.team,
+ event="$feature_flag_called",
+ distinct_id="person-5",
+ timestamp=START - timedelta(minutes=5),
+ properties=called("Matched condition set 2", True),
+ )
+ _create_event(
+ team=self.team,
+ event="$pageview",
+ distinct_id="person-8",
+ timestamp=START + timedelta(minutes=8),
+ properties={"$feature/other-flag": True},
+ )
+ flush_persons_and_events()
+ beta = ({"key": "beta", "type": "person", "value": ["yes"], "operator": "exact"},)
+ enterprise = WatchedCondition(
+ position=2, properties=({"key": "plan", "type": "person", "value": ["enterprise"], "operator": "exact"},)
+ )
+ on_another_flag = ({"key": "123", "type": "flag", "value": True, "operator": "flag_evaluates_to"},)
+ edit = dataclasses.replace(
+ BOOLEAN,
+ conditions=(dataclasses.replace(PRO_PLAN, earlier_at_full_rollout=(beta, on_another_flag)), enterprise),
+ )
+ other = dataclasses.replace(
+ BOOLEAN,
+ id="other",
+ flag_key="other-flag",
+ edited_at=START + timedelta(minutes=3),
+ conditions=(EVERYONE,),
+ )
+
+ exposure = exposure_by_bucket(self.team, [edit, other], START - timedelta(minutes=10), START + BUCKET, BUCKET)
+
+ self.assertEqual(
+ {edit_id: buckets[-1].since_edit.in_condition for edit_id, buckets in exposure.items()},
+ {
+ "boolean": {
+ ConditionMatch.REASON: ConditionExposure(with_flag=3, exposed=2),
+ ConditionMatch.PROPERTIES: ConditionExposure(with_flag=4, exposed=3),
+ },
+ "other": {
+ ConditionMatch.REASON: ConditionExposure(with_flag=1, exposed=1),
+ ConditionMatch.PROPERTIES: ConditionExposure(with_flag=3, exposed=3),
+ },
+ },
+ )
+
+ def test_a_relative_date_in_a_condition_counts_back_from_the_range_end(self) -> None:
+ self.team.timezone = "Asia/Tokyo"
+ self.team.save()
+ self._send(
+ [
+ ("person-1", 1, {"$feature/new-checkout": True}),
+ ("person-2", 2, {"$feature/new-checkout": True}),
+ ("person-3", 3, {"$feature/new-checkout": True}),
+ ],
+ people={
+ "person-1": {"signed_up_at": (START - timedelta(days=2)).isoformat()},
+ "person-2": {"signed_up_at": (START - timedelta(days=9)).isoformat()},
+ # Before the cutoff in UTC, but after it if the cutoff were read in the team's timezone.
+ "person-3": {"signed_up_at": (START + BUCKET - timedelta(days=7, hours=3)).isoformat()},
+ },
+ )
+ recent_sign_up = WatchedCondition(
+ position=0,
+ properties=({"key": "signed_up_at", "type": "person", "value": "-7d", "operator": "is_date_after"},),
+ )
+ edit = dataclasses.replace(BOOLEAN, conditions=(recent_sign_up,))
+
+ exposure = exposure_by_bucket(self.team, [edit], START, START + BUCKET, BUCKET)
+
+ self.assertEqual(
+ exposure[edit.id][0].in_bucket.in_condition[ConditionMatch.PROPERTIES],
+ ConditionExposure(with_flag=1, exposed=1),
+ )
+
+ @parameterized.expand(
+ [
+ (
+ "deleted_static_cohort",
+ lambda team: {
+ "key": "id",
+ "type": "cohort",
+ "value": Cohort.objects.create(team=team, name="Beta", is_static=True, deleted=True).pk,
+ },
+ True,
+ ),
+ ("cohort_id_not_a_number", lambda team: {"key": "id", "type": "cohort", "value": "beta"}, True),
+ (
+ "date_filter_without_a_string",
+ lambda team: {"key": "signed_up_at", "type": "person", "value": 7, "operator": "is_date_after"},
+ True,
+ ),
+ ("invalid_regex", lambda team: {"key": "email", "type": "person", "value": "(", "operator": "regex"}, True),
+ # ClickHouse rejects these values only when the query runs, so no edit keeps its counts by properties.
+ ("text_compared_with_a_number", _text_compared_with_a_number, False),
+ (
+ "date_that_clickhouse_cant_parse",
+ lambda team: {"key": "signed_up_at", "type": "person", "value": "abc", "operator": "is_date_after"},
+ False,
+ ),
+ ]
+ )
+ def test_a_condition_the_query_cant_evaluate_leaves_out_only_counts_by_properties(
+ self,
+ _name: str,
+ broken_filter: Callable[[Team], dict[str, Any]],
+ healthy_keeps_properties: bool,
+ ) -> None:
+ self._send(
+ [
+ (
+ "person-1",
+ 1,
+ {
+ "$feature_flag": "new-checkout",
+ "$feature_flag_reason": "Matched condition set 1",
+ "$feature/new-checkout": True,
+ },
+ )
+ ],
+ event="$feature_flag_called",
+ people={"person-1": {"age": 30, "signed_up_at": "2026-09-01"}},
+ )
+ broken = dataclasses.replace(
+ BOOLEAN, conditions=(WatchedCondition(position=0, properties=(broken_filter(self.team),)),)
+ )
+ healthy = dataclasses.replace(BOOLEAN, id="healthy", conditions=(EVERYONE,))
+
+ exposure = exposure_by_bucket(self.team, [broken, healthy], START, START + BUCKET, BUCKET)
+
+ by_reason = {ConditionMatch.REASON: ConditionExposure(with_flag=1, exposed=1)}
+ by_reason_and_properties = {**by_reason, ConditionMatch.PROPERTIES: ConditionExposure(with_flag=1, exposed=1)}
+ self.assertEqual(
+ {edit_id: buckets[0].in_bucket for edit_id, buckets in exposure.items()},
+ {
+ "boolean": ExposureCounts(with_flag=1, exposed=1, by_variant={}, in_condition=by_reason),
+ "healthy": ExposureCounts(
+ with_flag=1,
+ exposed=1,
+ by_variant={},
+ in_condition=by_reason_and_properties if healthy_keeps_properties else by_reason,
+ ),
+ },
+ )
+
+ def test_counts_in_the_watched_conditions_of_a_group_flag_with_variants_next_to_a_person_flag(self) -> None:
+ def called(group: str, reason: str, value: str | bool) -> dict[str, Any]:
+ return {
+ "$feature_flag": "org-beta",
+ "$feature_flag_reason": reason,
+ "$feature/org-beta": value,
+ "$group_0": group,
+ }
+
+ # HogQL hides the group of an event sent before the group type existed.
+ create_group_type_mapping(
+ team=self.team,
+ project_id=self.team.project_id,
+ group_type="organization",
+ group_type_index=0,
+ created_at=START - timedelta(days=1),
+ )
+ # A group filter reads the ClickHouse groups table, which `create_group` leaves empty in tests.
+ for group_key, tier in (("org-a", "enterprise"), ("org-b", "enterprise"), ("org-c", "free")):
+ raw_create_group_ch(self.team.pk, 0, group_key, {"tier": tier}, created_at=START - timedelta(days=1))
+ self._send(
+ [
+ ("person-1", 1, called("org-a", "Matched condition set 1", "control")),
+ ("person-2", 2, called("org-a", "Matched condition set 1", "control")),
+ ("person-3", 3, called("org-b", "Out of rollout bound", False)),
+ ("person-4", 4, called("org-c", "Matched condition set 2", "test")),
+ (
+ "person-5",
+ 5,
+ {
+ "$feature_flag": "new-checkout",
+ "$feature_flag_reason": "Matched condition set 1",
+ "$feature/new-checkout": True,
+ },
+ ),
+ ],
+ event="$feature_flag_called",
+ )
+ enterprise = WatchedCondition(
+ position=0,
+ properties=(
+ {"key": "tier", "type": "group", "group_type_index": 0, "value": ["enterprise"], "operator": "exact"},
+ ),
+ )
+ group_edit = dataclasses.replace(GROUP, variants=("control", "test"), conditions=(enterprise,))
+ person_edit = dataclasses.replace(BOOLEAN, conditions=(EVERYONE,))
+
+ exposure = exposure_by_bucket(self.team, [group_edit, person_edit], START, START + BUCKET, BUCKET)
+
+ in_condition = {
+ ConditionMatch.REASON: ConditionExposure(with_flag=1, exposed=1),
+ ConditionMatch.PROPERTIES: ConditionExposure(with_flag=1, exposed=1),
+ }
+ self.assertEqual(
+ {edit_id: buckets[0].in_bucket for edit_id, buckets in exposure.items()},
+ {
+ "group": ExposureCounts(
+ with_flag=3,
+ exposed=2,
+ by_variant={"control": 1, "test": 1},
+ in_condition={
+ ConditionMatch.REASON: ConditionExposure(with_flag=2, exposed=1),
+ ConditionMatch.PROPERTIES: ConditionExposure(with_flag=2, exposed=1),
+ },
+ ),
+ "boolean": ExposureCounts(with_flag=1, exposed=1, by_variant={}, in_condition=in_condition),
+ },
+ )
+
+ @parameterized.expand(
+ [
+ # Ingestion still writes every flag call to events, and to flag_evaluations only for some teams.
+ ("read_flag_evaluations", FlagEvaluationsMode.READ_FLAG_EVALUATIONS, False),
+ ("flag_evaluations_only", FlagEvaluationsMode.FLAG_EVALUATIONS_ONLY, True),
+ ]
+ )
+ def test_reads_the_flag_reasons_where_the_mode_keeps_every_flag_call(
+ self, _name: str, mode: FlagEvaluationsMode, calls_in_flag_evaluations: bool
+ ) -> None:
+ OrganizationFeatureFlagsConfig.objects.update_or_create(
+ organization=self.organization, defaults={"flag_evaluations_mode": mode}
+ )
+ person = create_person(team=self.team, distinct_ids=["person-1"])
+ call = {
+ "$feature_flag": "new-checkout",
+ "$feature_flag_reason": "Matched condition set 1",
+ "$feature/new-checkout": True,
+ }
+ _create_event(
+ team=self.team,
+ event="$pageview" if calls_in_flag_evaluations else "$feature_flag_called",
+ distinct_id="person-1",
+ timestamp=START + timedelta(minutes=1),
+ properties={"$feature/new-checkout": True} if calls_in_flag_evaluations else call,
+ )
+ flush_persons_and_events()
+ if calls_in_flag_evaluations:
+ _create_flag_evaluations(
+ self.team.pk,
+ "new-checkout",
+ timestamp=START + timedelta(minutes=1),
+ distinct_id="person-1",
+ person_id=person.uuid,
+ properties={"$feature_flag_reason": "Matched condition set 1"},
+ )
+ edit = dataclasses.replace(BOOLEAN, conditions=(EVERYONE,))
+
+ exposure = exposure_by_bucket(self.team, [edit], START, START + BUCKET, BUCKET)
+
+ self.assertEqual(
+ exposure[edit.id][0].in_bucket.in_condition[ConditionMatch.REASON],
+ ConditionExposure(with_flag=1, exposed=1),
+ )
+
def test_one_query_keeps_the_counts_of_each_edit_apart(self) -> None:
self._send(
[
diff --git a/products/feature_flags/backend/test/test_user_blast_radius.py b/products/feature_flags/backend/test/test_user_blast_radius.py
index 9c3c7191a94e..256b600c9771 100644
--- a/products/feature_flags/backend/test/test_user_blast_radius.py
+++ b/products/feature_flags/backend/test/test_user_blast_radius.py
@@ -5,10 +5,12 @@ from parameterized import parameterized
from rest_framework.exceptions import ValidationError
from posthog.errors import CLICKHOUSE_ERROR_CODE_LOOKUP, InternalCHQueryError, wrap_clickhouse_query_error
+from posthog.models import Team
from posthog.models.property import PropertyValidationError
from products.feature_flags.backend.user_blast_radius import (
UNEVALUABLE_FILTERS_MESSAGE,
+ replace_proxy_properties,
unevaluable_filters_as_validation_errors,
)
@@ -62,3 +64,19 @@ class TestUnevaluableFiltersAsValidationErrors(SimpleTestCase):
err = wrap_clickhouse_query_error(ServerException("Logical error: invariant violated", code=49))
with self.assertRaises(InternalCHQueryError), unevaluable_filters_as_validation_errors():
raise err
+
+
+class TestReplaceProxyProperties(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("boolean_list", [True, False], ["true", "false"]),
+ ("boolean", True, "true"),
+ ("number_list", [1, 2.5], ["1", "2.5"]),
+ ]
+ )
+ def test_person_values_become_the_strings_hogql_compares(self, _name, value, expected):
+ filters = {"properties": [{"key": "beta", "type": "person", "value": value, "operator": "exact"}]}
+
+ [prop] = replace_proxy_properties(Team(), filters).flat
+
+ self.assertEqual(prop.value, expected)
diff --git a/products/feature_flags/backend/user_blast_radius.py b/products/feature_flags/backend/user_blast_radius.py
index 09890447b03f..d48f2ac1fad4 100644
--- a/products/feature_flags/backend/user_blast_radius.py
+++ b/products/feature_flags/backend/user_blast_radius.py
@@ -1,5 +1,6 @@
from collections.abc import Iterator
from contextlib import contextmanager
+from datetime import datetime
from typing import Optional
from django.core.exceptions import ObjectDoesNotExist
@@ -159,12 +160,20 @@ def _normalize_property_value(prop: Property) -> None:
if prop.type in ("person", "group"):
if isinstance(prop.value, list):
- prop.value = [str(v) for v in prop.value]
+ prop.value = [_as_property_string(v) for v in prop.value]
elif not isinstance(prop.value, str | list | dict | type(None)):
- prop.value = str(prop.value)
+ prop.value = _as_property_string(prop.value)
-def replace_proxy_properties(team: Team, feature_flag_condition: dict) -> PropertyGroup:
+def _as_property_string(value: object) -> str:
+ # HogQL reads only "true" and "false" as booleans, so str(True) would match no stored true value.
+ if isinstance(value, bool):
+ return "true" if value else "false"
+ return str(value)
+
+
+def replace_proxy_properties(team: Team, feature_flag_condition: dict, now: Optional[datetime] = None) -> PropertyGroup:
+ """`now` is the time that relative dates such as "-7d" count back from. It defaults to the current time."""
# Parse phase: everything here derives directly from the caller's filter JSON, so a
# ValueError is caller input (malformed property shape, non-numeric cohort id) and becomes
# a 400. Cohort ids are cast eagerly so a bad id fails here instead of surfacing as a bare
@@ -176,9 +185,10 @@ def replace_proxy_properties(team: Team, feature_flag_condition: dict) -> Proper
if prop.type in ("cohort", "static-cohort", "precalculated-cohort"):
Cohort._meta.pk.get_prep_value(prop.value)
if prop.operator in ("is_date_before", "is_date_after"):
- relative_date = relative_date_parse_for_feature_flag_matching(str(prop.value))
+ relative_date = relative_date_parse_for_feature_flag_matching(str(prop.value), now=now)
if relative_date:
- prop.value = relative_date.strftime("%Y-%m-%d %H:%M:%S")
+ # The offset keeps the time in UTC. HogQL reads a time without one in the team's timezone.
+ prop.value = relative_date.isoformat()
else:
_normalize_property_value(prop)