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): read property-group maps on flag_evaluations (#118157)

Phil Haack6220dab5382c
diff --git a/posthog/clickhouse/property_groups.py b/posthog/clickhouse/property_groups.py
index 6503c3b995f0..0c2df681486f 100644
--- a/posthog/clickhouse/property_groups.py
+++ b/posthog/clickhouse/property_groups.py
@@ -240,18 +240,14 @@ def without_materialization(definitions: PropertyGroupDefinitions) -> PropertyGr
# Flag calls carry no $ai_* keys, so the ai groups are left out. The feature_flags group is left out because a
-# $feature/* map would copy a large share of each flag call's JSON. No column on this table family carries a CODEC.
-# hidden=True stops HogQL from reading the maps, so it reads the JSON until every environment has the columns.
+# $feature/* map would copy a large share of each flag call's JSON. HogQL therefore reads $feature/* keys from the
+# JSON on this table. No column on this table family carries a CODEC.
flag_evaluations_property_group_definitions: PropertyGroupDefinitions = {
"properties": {
- "custom": dataclasses.replace(
- event_property_group_definitions["properties"]["custom"], codec=None, hidden=True
- ),
+ "custom": dataclasses.replace(event_property_group_definitions["properties"]["custom"], codec=None),
},
"person_properties": {
- "custom": dataclasses.replace(
- event_property_group_definitions["person_properties"]["custom"], codec=None, hidden=True
- ),
+ "custom": dataclasses.replace(event_property_group_definitions["person_properties"]["custom"], codec=None),
},
}
diff --git a/posthog/hogql/database/schema/flag_evaluations.py b/posthog/hogql/database/schema/flag_evaluations.py
index a47d92164f00..0d574ef171c4 100644
--- a/posthog/hogql/database/schema/flag_evaluations.py
+++ b/posthog/hogql/database/schema/flag_evaluations.py
@@ -94,7 +94,9 @@ class FlagEvaluationsTable(Table):
nullable=False,
description="JSON map of the event's properties. Access nested keys with `properties.$lib` etc. "
"Prefer the `flag_key`, `response`, `session_id`, `request_id` and `$group_0`..`$group_4` columns "
- "where they cover what you need: reading the same value out of this JSON scans far more data.",
+ "where they cover what you need: reading the same value out of this JSON scans far more data. "
+ "A property that holds an empty string reads as '' here, where `events` can return NULL. A value with "
+ "an escaped character, such as a quote, can also read differently than on `events`.",
),
"timestamp": DateTimeDatabaseField(
name="timestamp", nullable=False, description="When the flag was evaluated (client timestamp, in UTC)."
diff --git a/posthog/hogql/database/schema/test/test_flag_evaluations.py b/posthog/hogql/database/schema/test/test_flag_evaluations.py
index b455811478c8..412836b156d4 100644
--- a/posthog/hogql/database/schema/test/test_flag_evaluations.py
+++ b/posthog/hogql/database/schema/test/test_flag_evaluations.py
@@ -13,7 +13,9 @@ from posthog.schema import (
EventPropertyFilter,
GroupPropertyFilter,
HogQLPropertyFilter,
+ HogQLQueryModifiers,
PersonPropertyFilter,
+ PropertyGroupsMode,
PropertyOperator,
SessionPropertyFilter,
)
@@ -140,6 +142,7 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
self,
columns: str,
where: str = "",
+ modifiers: HogQLQueryModifiers | None = None,
source: str = "posthog.flag_evaluations",
*,
row_uuids: tuple[uuid.UUID, ...] = (),
@@ -158,6 +161,7 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
team=self.team,
placeholders=placeholders,
context=context,
+ modifiers=modifiers,
pretty=False,
)
@@ -242,6 +246,21 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
assert rows == [({"quote": 'a"b', "keep": "yes"}, {"email": "[email protected]"})]
+ def test_custom_property_filter_reads_the_property_group_map_for_an_ordinary_value(self):
+ where = f" AND properties.probe = '{EVENT_PROBE}'"
+ json_read = self._execute(
+ "properties.probe", where, modifiers=HogQLQueryModifiers(propertyGroupsMode=PropertyGroupsMode.DISABLED)
+ )
+ map_read = self._execute(
+ "properties.probe", where, modifiers=HogQLQueryModifiers(propertyGroupsMode=PropertyGroupsMode.OPTIMIZED)
+ )
+
+ # A read that falls back to the JSON returns the same rows. Only the printed SQL shows whether the
+ # filter reads the map. Only a map read can use the bloom filter indexes.
+ assert "equals(flag_evaluations.properties_group_custom[" in (map_read.clickhouse or "")
+ assert "properties_group_custom" not in (json_read.clickhouse or "")
+ assert map_read.results == json_read.results == [(EVENT_PROBE,)]
+
@parameterized.expand(
[
("json_null", {"$feature_flag_response": None}),
@@ -391,20 +410,34 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
@parameterized.expand(
[
- ("event", "properties", "probe", PropertyDefinition.Type.EVENT, "$feature_flag"),
- ("person", "person.properties", "email", PropertyDefinition.Type.PERSON, "plan"),
+ (f"{kind}_{read}", blob_field, restricted, stored_value, property_type, unrestricted, mode)
+ for kind, blob_field, restricted, stored_value, property_type, unrestricted in [
+ ("event", "properties", "probe", EVENT_PROBE, PropertyDefinition.Type.EVENT, "$feature_flag"),
+ ("person", "person.properties", "email", "[email protected]", PropertyDefinition.Type.PERSON, "plan"),
+ ]
+ for read, mode in [("json", PropertyGroupsMode.DISABLED), ("map", PropertyGroupsMode.OPTIMIZED)]
]
)
def test_restricted_property_is_hidden_from_both_read_paths(
- self, _name: str, blob_field: str, restricted: str, property_type, unrestricted: str
+ self,
+ _name: str,
+ blob_field: str,
+ restricted: str,
+ stored_value: str,
+ property_type,
+ unrestricted: str,
+ mode: PropertyGroupsMode,
):
# The explicit read lowers to a NULL constant and the blob read is scrubbed by the printer.
# Both consult the same dispatch, so a table dropped from it leaks through both.
+ # A filter that reads the map would reveal the value through the rows it returns.
self._restrict(restricted, property_type)
+ modifiers = HogQLQueryModifiers(propertyGroupsMode=mode)
- assert self._select(f"{blob_field}.{restricted}") == [(None,)]
+ assert self._execute(f"{blob_field}.{restricted}", modifiers=modifiers).results == [(None,)]
+ assert self._execute("uuid", f" AND {blob_field}.{restricted} = '{stored_value}'", modifiers).results == []
- blob = json.loads(self._select(blob_field)[0][0])
+ blob = json.loads(self._execute(blob_field, modifiers=modifiers).results[0][0])
assert restricted not in blob
assert unrestricted in blob