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.

perf(hogql): read flag keys from the map in JSON functions on events_json (#106152)

Sandy Spicer471f0ec53486
diff --git a/docs/internal/clickhouse-utility-udfs.md b/docs/internal/clickhouse-utility-udfs.md
index 26bb8dbb43ee..e6bc468d0152 100644
--- a/docs/internal/clickhouse-utility-udfs.md
+++ b/docs/internal/clickhouse-utility-udfs.md
@@ -112,8 +112,10 @@ Queries on `events_json` derive `$active_feature_flags` from the `$feature_flags
Feature-flag scalar reads still use JSON string encoding when requested: a `control` variant
becomes `"control"` through `toJSONString`, and `JSONExtractString` returns `control`.
-On native events, HogQL `JSONExtract*` calls with `$feature_flags` as their first property key read the same restricted-property-aware map as dotted `$feature_flags` access.
-The original extractor still determines the return type and missing-value default.
+A HogQL JSON function on `events_json` whose first key is `$feature/<key>`, `$active_feature_flags` or `$feature_flags` reads the flags map directly instead of the rebuilt document, also when the document is `toString(properties)`.
+The result is the same as reading the rebuilt document, because the value is serialized the way the SDK sent it: a boolean flag as JSON `true` or `false`, a variant as a JSON string, with the sentinels mapped back.
+The original function still determines the return type, the missing-value default and any deeper keys.
+Key listings of the whole document, JSON functions with a computed key, `properties` selected through a subquery, and `JSON_VALUE` (whose second argument is a JSONPath, not a key) keep reading the rebuilt document.
The legacy table stores flags as sent, so HogQL reads every flag property there as stored, `$feature_flags` included.
### Benchmarking the cleaner
diff --git a/posthog/hogql/transforms/clickhouse_property_resolution.py b/posthog/hogql/transforms/clickhouse_property_resolution.py
index 013145cfea0b..5ad9fd1262ee 100644
--- a/posthog/hogql/transforms/clickhouse_property_resolution.py
+++ b/posthog/hogql/transforms/clickhouse_property_resolution.py
@@ -602,6 +602,30 @@ def _feature_flag_value_read(feature_flags: ast.Expr, key: str) -> ast.Expr:
)
+def _feature_flag_json_read(feature_flags: ast.Expr, key: str) -> ast.Expr:
+ """The JSON text the legacy document holds for `$feature/<key>`, or NULL when the map has no such flag.
+
+ SDKs send a boolean flag as JSON `true`/`false` and a variant as a JSON string. The map stores the booleans as 'true'
+ and 'false' and a variant named "false" or "true" under its sentinel, so only the sentinels serialize back to strings.
+ """
+ value = ast.ArrayAccess(array=clone_expr(feature_flags), property=_const(key))
+ return _call(
+ "if",
+ [
+ _call("has", [clone_expr(feature_flags), _const(key)]),
+ _call(
+ "if",
+ [
+ _call("in", [clone_expr(value), ast.Tuple(exprs=[_sentinel("true"), _sentinel("false")])]),
+ clone_expr(value),
+ _call("toJSONString", [feature_flag_variant_read(value)]),
+ ],
+ ),
+ _const(None),
+ ],
+ )
+
+
def _is_events_properties(field_type: ast.FieldType, context: HogQLContext) -> bool:
table_type = _unwrap_to_table_type(field_type)
field = field_type.resolve_database_field(context)
@@ -816,11 +840,13 @@ def _active_feature_flags_present(feature_flags: ast.Expr, restricted_keys: list
def _feature_flag_compatibility_read(
- node: ast.PropertyAccess, field_type: ast.FieldType, context: HogQLContext
+ node: ast.PropertyAccess, field_type: ast.FieldType, context: HogQLContext, *, as_json: bool = False
) -> ast.Expr | None:
"""The read of a flag property the native table rebuilds from `$feature_flags`, or None to read it as stored.
The legacy table stores every flag property as sent, so it always reads them as stored.
+ `as_json` reads `$feature/<key>` as the JSON text a JSON function parses instead of the unquoted value.
+ `$active_feature_flags` and the whole `$feature_flags` map read as JSON text either way.
"""
first_key = str(node.keys[0])
deeper_keys = list(node.keys[1:])
@@ -841,7 +867,12 @@ def _feature_flag_compatibility_read(
restricted_keys = _restricted_feature_flag_keys(field_type, context)
if first_key.startswith(FEATURE_FLAG_PROPERTY_PREFIX):
- value = _feature_flag_value_read(feature_flags, first_key.removeprefix(FEATURE_FLAG_PROPERTY_PREFIX))
+ flag_key = first_key.removeprefix(FEATURE_FLAG_PROPERTY_PREFIX)
+ value = (
+ _feature_flag_json_read(feature_flags, flag_key)
+ if as_json
+ else _feature_flag_value_read(feature_flags, flag_key)
+ )
return ast.PropertyAccess(expr=value, keys=deeper_keys) if deeper_keys else value
if first_key == "$active_feature_flags":
@@ -1301,10 +1332,6 @@ class ClickHousePropertyResolver(CloningVisitor):
if json_string_on_events_json is not None:
return json_string_on_events_json
- feature_flag_extract = self._rewrite_feature_flag_json_extract(node)
- if feature_flag_extract is not None:
- return feature_flag_extract
-
temporary_property_json_function = self._rewrite_json_function_on_temporary_property(node)
if temporary_property_json_function is not None:
return temporary_property_json_function
@@ -1317,16 +1344,20 @@ class ClickHousePropertyResolver(CloningVisitor):
if temporary_property_json_value is not None:
return temporary_property_json_value
- json_extract_on_events_json = self._rewrite_json_extract_on_events_json_subcolumn(node)
- if json_extract_on_events_json is not None:
- return json_extract_on_events_json
-
optimized_json_has = self._rewrite_feature_flag_json_has(node)
if optimized_json_has is None:
optimized_json_has = self._optimize_json_has_on_events_json(node)
if optimized_json_has is not None:
return optimized_json_has
+ feature_flag_json = self._rewrite_feature_flag_json_call(node)
+ if feature_flag_json is not None:
+ return feature_flag_json
+
+ json_extract_on_events_json = self._rewrite_json_extract_on_events_json_subcolumn(node)
+ if json_extract_on_events_json is not None:
+ return json_extract_on_events_json
+
# `isNull` / `isNotNull` / `JSONHas` on a property-group property can be answered by `has(map, key)` alone,
# without reading the values subcolumn — so it stays eligible for the keys bloom-filter index.
optimized = self._optimize_property_group_call(node)
@@ -1352,23 +1383,49 @@ class ClickHousePropertyResolver(CloningVisitor):
return super().visit_call(node)
- def _rewrite_feature_flag_json_extract(self, node: ast.Call) -> ast.Expr | None:
+ def _rewrite_feature_flag_json_call(self, node: ast.Call) -> ast.Expr | None:
+ """A JSON function over a virtual flag key, rewritten on native events to parse the flag read.
+
+ The native stored document has no `$feature/<key>` or `$active_feature_flags` key, so a JSON function that reads
+ the document sees a missing key. The legacy document holds every flag key as sent, so it is read as stored.
+ """
+ # `JSON_VALUE` and the other SQL-standard `JSON_` functions take one JSONPath argument instead of keys, so
+ # dropping their second argument would leave them without a path.
if (
- not node.name.startswith("JSONExtract")
+ not self.context.uses_new_events_schema()
+ or not node.name.startswith("JSON")
+ or node.name.startswith("JSON_")
or len(node.args) < 2
- or not isinstance(node.args[1], ast.Constant)
- or node.args[1].value != "$feature_flags"
):
return None
- field_type = resolve_field_type(node.args[0])
- if not isinstance(field_type, ast.FieldType):
+ key_arg = node.args[1]
+ if not isinstance(key_arg, ast.Constant) or not isinstance(key_arg.value, str):
return None
- value = _feature_flag_compatibility_read(
- ast.PropertyAccess(expr=node.args[0], keys=["$feature_flags"]), field_type, self.context
- )
+ first_key = key_arg.value
+ if not is_virtual_feature_flag_key(first_key):
+ return None
+
+ document = node.args[0]
+ # On the legacy table `toString(properties)` is the stored document itself, so queries pass it to JSON
+ # functions in place of `properties`.
+ unwrapped = document.expr if isinstance(document, ast.Alias) else document
+ if isinstance(unwrapped, ast.Call) and unwrapped.name == "toString" and len(unwrapped.args) == 1:
+ document = unwrapped.args[0]
+ field_type = resolve_field_type(document)
+ if not isinstance(field_type, ast.FieldType) or not _is_events_properties(field_type, self.context):
+ return None
+
+ value: ast.Expr | None
+ if first_key in restricted_property_keys_for_table_type(field_type.table_type, self.context):
+ value = ast.Constant(value=None, type=ast.StringType(nullable=True))
+ else:
+ value = _feature_flag_compatibility_read(
+ ast.PropertyAccess(expr=document, keys=[first_key]), field_type, self.context, as_json=True
+ )
if value is None:
return None
- # Keep the extractor to preserve its return type and missing-value defaults.
+ # Keep the function and pass it the flag read with the first key dropped, so its return type, missing-value
+ # default and any deeper keys behave as they do over the legacy document.
return ast.Call(
start=node.start,
end=node.end,
diff --git a/posthog/hogql/transforms/test/test_property_types.py b/posthog/hogql/transforms/test/test_property_types.py
index c7774911fab3..5b686e58be34 100644
--- a/posthog/hogql/transforms/test/test_property_types.py
+++ b/posthog/hogql/transforms/test/test_property_types.py
@@ -202,6 +202,15 @@ class TestNewEventsSchemaArraySubcolumns(_NewEventsSchemaArraySubcolumnsHelpers,
("events.properties.`$feature_flags`", "mapFilter("),
("events.properties.`$active_feature_flags`", "events._active_feature_flags"),
),
+ (
+ "json_path_function_reads_document",
+ "SELECT JSON_VALUE(properties, '$feature_flags') FROM events",
+ True,
+ None,
+ None,
+ ("JSON_VALUE(", "JSONExtractKeysAndValuesRaw("),
+ ("mapApply(",),
+ ),
(
"native_restricted",
"SELECT properties.$feature_flags, properties.$active_feature_flags, "
@@ -1639,8 +1648,52 @@ class TestEventsSchemaPropertyParity(ClickhouseTestMixin, BaseTest):
event="schema-parity",
properties={"$active_feature_flags": ["secret"], "$feature_flags": {"secret": "false"}},
)
+ sdk_uuid = _create_event(
+ team=self.team,
+ distinct_id="sdk-flags",
+ event="schema-parity",
+ properties={
+ "$feature/checkout": True,
+ "$feature/disabled": False,
+ "$feature/variant": "control",
+ "$feature/named-false": "false",
+ "$active_feature_flags": ["checkout", "variant", "named-false"],
+ },
+ )
flush_persons_and_events()
+ def json_function_reads(use_new_events_schema: bool) -> tuple[list[Any] | None, list[Any] | None]:
+ context = HogQLContext(
+ team_id=self.team.pk, enable_select_queries=True, use_new_events_schema=use_new_events_schema
+ )
+ reads = execute_hogql_query(
+ "SELECT arraySort(JSONExtractArrayRaw(properties, '$active_feature_flags')), "
+ "JSONLength(properties, '$active_feature_flags'), JSONType(properties, '$active_feature_flags'), "
+ "JSONExtractBool(properties, '$feature/checkout'), JSONExtractBool(properties, '$feature/disabled'), "
+ "JSONExtractBool(properties, '$feature/named-false'), JSONType(properties, '$feature/named-false'), "
+ "JSONType(properties, '$feature/checkout'), JSONExtractRaw(properties, '$feature/disabled'), "
+ "JSONExtractString(toString(properties), '$feature/variant'), "
+ "arraySort(JSONExtractArrayRaw(toString(properties), '$active_feature_flags')) "
+ f"FROM events WHERE uuid = '{sdk_uuid}'",
+ team=self.team,
+ context=context,
+ )
+ exploded = execute_hogql_query(
+ "SELECT arrayJoin(JSONExtractArrayRaw(properties, '$active_feature_flags')) AS flag "
+ f"FROM events WHERE uuid = '{sdk_uuid}' ORDER BY flag",
+ team=self.team,
+ context=context,
+ )
+ return reads.results, exploded.results
+
+ active_flags = ['"checkout"', '"named-false"', '"variant"']
+ native_json_reads = json_function_reads(use_new_events_schema=True)
+ assert native_json_reads == json_function_reads(use_new_events_schema=False)
+ assert native_json_reads == (
+ [(active_flags, 3, "Array", 1, 0, 0, "String", "Bool", "false", "control", active_flags)],
+ [(flag,) for flag in active_flags],
+ )
+
native = execute_hogql_query(
"SELECT properties.`$feature/checkout`, properties.`$feature/variant`, "
"properties.$feature_flags.checkout, JSONHas(properties, '$feature_flags', 'checkout'), "