:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(hogql): rebuild flag keys in events_json documents and mark true variants (#109395)
Sandy Spicer0ac0d7cddc3a
diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.goindex 072a216c8249..793c2d9598c3 100644--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go@@ -142,10 +142,10 @@ func makeEventPropertyRules() *pathRule {return root}-// The typed map stores every flag value as a string, so a variant named "false" would read the same as a flag that-// was evaluated and switched off (JSON false). The variant is stored under this sentinel instead. The query layer-// maps it back to "false" and the flag API refuses it as a variant key.-const falseVariantSentinel = "$false"+// The typed map stores every flag value as a string, so a variant named "false" or "true" would read the same as a+// boolean flag that evaluated to JSON false or true. Such a variant is stored under its sentinel instead. The query+// layer maps the sentinels back to the variant names and the flag API refuses them as variant keys.+var variantSentinels = map[string]string{"false": "$false", "true": "$true"}type valueKind byte@@ -380,8 +380,8 @@ func (p *processor) cleanEventProperties(v *value) (*value, error) {for _, property := range cleaned.entries {if property.key == "$feature_flags" && property.value.kind == kindObject {for _, flag := range property.value.entries {- if flag.value.kind == kindString && flag.value.s == "false" {- flag.value.s = falseVariantSentinel+ if sentinel, ok := variantSentinels[flag.value.s]; ok && flag.value.kind == kindString {+ flag.value.s = sentinelp.mutated = true}}diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.goindex 59d83431f8ae..c688b33c829e 100644--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go@@ -69,6 +69,8 @@ func TestProcessLineGroupsFeaturePropertiesAndPreservesExistingFlagValues(t *tes`{"$feature/a.b":1,"$feature/a":{"b":2},"$feature/Z":true}`: `{"$feature_flags":{"Z":true,"a":{"b":2},"a.b":1}}`,`{"$feature/named-false":"false","$feature/off":false}`: `{"$feature_flags":{"named-false":"$false","off":false}}`,`{"$feature_flags":{"off":false,"named-false":"false"}}`: `{"$feature_flags":{"named-false":"$false","off":false}}`,+ `{"$feature/named-true":"true","$feature/on":true}`: `{"$feature_flags":{"named-true":"$true","on":true}}`,+ `{"$feature_flags":{"on":true,"named-true":"true"}}`: `{"$feature_flags":{"named-true":"$true","on":true}}`,}for input, want := range tests {diff --git a/docs/internal/clickhouse-utility-udfs.md b/docs/internal/clickhouse-utility-udfs.mdindex d3986cfaa869..c9e6566b61d6 100644--- a/docs/internal/clickhouse-utility-udfs.md+++ b/docs/internal/clickhouse-utility-udfs.md@@ -3,8 +3,8 @@`JSONCleanPostHogEventProperties` groups `$feature/<key>` event properties into `$feature_flags`.Before emitting JSON for insertion, it sorts the keys in `$feature_flags` alphabetically using case-sensitive string order.This also applies to existing `$feature_flags` objects, after cleanup resolves duplicates.-A flag value that is the JSON string `"false"` (a variant named false) is stored as `$false`, so it stays distinct from a flag that was evaluated and switched off (JSON `false`, which the typed map stores as `false`).-`$false` is a reserved variant key; the flag API rejects it.+A flag value that is the JSON string `"false"` or `"true"` (a variant named false or true) is stored as `$false` or `$true`, so it stays distinct from a boolean flag (JSON `false` or `true`, which the typed map stores as `false` or `true`).+`$false` and `$true` are reserved variant keys; the flag API rejects them.Flag values and person-property ordering follow the existing cleanup rules.Invalid scalar and array `$feature_flags` values are replaced with an empty map and retained in@@ -107,7 +107,7 @@ Whole-document reads of `properties`, such as `SELECT properties`, do not includ`is_temporary_event_property` in `posthog/clickhouse/events_json.py` mirrors the allowlist, so update both together.Fresh installations use the updated schema definitions. Existing tables require a manual schema rollout and feature-flag query compatibility before native reads are enabled.-Native-event queries derive `$active_feature_flags` from the `$feature_flags` map, excluding empty and `false` values and restricted flags. `$false` counts as active, and reads of `$feature/<key>`, `$feature_flags.<key>` and the `$feature_flags` map return it as `false`. Whole-document reads of `properties`, and batch exports of the whole `$feature_flags` map, return it as stored; single-flag export fields and filters map it back. Array order follows the stored map rather than the original SDK evaluation order. No separate active-flags column is required.+Queries on `events_json` derive `$active_feature_flags` from the `$feature_flags` map, excluding empty and `false` values and restricted flags. `$false` and `$true` count as active, and reads of `$feature/<key>`, `$feature_flags.<key>` and the `$feature_flags` map return them as `false` and `true`. A whole-document read of `properties` rebuilds the document the SDK sent: one `$feature/<key>` per map entry, a boolean flag as JSON `true` or `false` and a variant as a string, plus `$active_feature_flags`, and no `$feature_flags` object. A flag sent as a number, object or array comes back as a JSON string, because the map stores every value as text. Restricted flags are left out of both. A JSON function over `properties`, such as `JSONExtractArrayRaw(properties, '$active_feature_flags')`, reads that rebuilt document. Batch exports of the whole `$feature_flags` map return the sentinels as stored; single-flag export fields and filters map them back. Array order follows the stored map rather than the original SDK evaluation order. No separate active-flags column is required.Feature-flag scalar reads still use JSON string encoding when requested: a `control` variantbecomes `"control"` through `toJSONString`, and `JSONExtractString` returns `control`.diff --git a/docs/internal/feature-flags/api-writes.md b/docs/internal/feature-flags/api-writes.mdindex bc6c863d83ed..410890391a81 100644--- a/docs/internal/feature-flags/api-writes.md+++ b/docs/internal/feature-flags/api-writes.md@@ -77,8 +77,8 @@ A stored version 2 row that is not remote-configured and carries no encrypted pa- A new row is created disabled with server-owned rule IDs and seeds; the request document is resolved against an empty stored document and validated strictly before it is persisted exactly. No v1 row is converted.- A supplied `filters` object replaces the whole configuration. There is no top-level merge, no rule merge by index, no filling of omitted fields from the stored document, and no v1 normalization. `{}`, `null`, a list, and an incomplete object are invalid; a complete document with `rules: []` is valid.-- `return_type` is `boolean`, `string`, `number` or `object` and cannot change after creation. `default_value` is null or the declared type; every rule `value` is the declared type. Strings are non-empty and not `$false`, the event-storage sentinel that v1 also reserves as a variant key; numbers lie within ±(2^53 − 1); objects nest at most 20 object or array levels with numbers in the same range.-- Writer-only rules (`check_writer_rules` in `facade/config_writes.py`, currently the `$false` reservation and the regex check below) apply to a created or replacing document and to the stored document being enabled. The shared validator, and with it the flags caches and the stored side of an update, does not apply them: tightening a writer rule never hides a stored row from evaluation or blocks replacing it.+- `return_type` is `boolean`, `string`, `number` or `object` and cannot change after creation. `default_value` is null or the declared type; every rule `value` is the declared type. Strings are non-empty and not `$false` or `$true`, the event-storage sentinels that v1 also reserves as variant keys; numbers lie within ±(2^53 − 1); objects nest at most 20 object or array levels with numbers in the same range.+- Writer-only rules (`check_writer_rules` in `facade/config_writes.py`, currently the `$false` and `$true` reservation and the regex check below) apply to a created or replacing document and to the stored document being enabled. The shared validator, and with it the flags caches and the stored side of an update, does not apply them: tightening a writer rule never hides a stored row from evaluation or blocks replacing it.- `regex` and `not_regex` patterns must compile with Python's `re`, as on the v1 write path. This is a writer-only rule: readers store an uncompilable pattern and report an evaluation error when evaluation reaches it. The flags service compiles with `fancy_regex`, whose dialect differs at the edges, so, as in v1, a pattern the stored document already holds is not checked again, and enabling checks no pattern.- Rule IDs and percentage-rollout seeds are server-owned and identify rules rather than list positions. An echoed ID keeps its seed through reordering and unrelated edits; a rule that omits its ID is new and receives fresh identity. An unknown ID, an ID used twice, a client-chosen seed, and a changed seed are all rejected.- `FeatureFlag.version` is required on every update and must equal the locked row's value. Unlike v1 there is no `original_flag` merge: a stale token is a conflict even when no individual field clashes. The token applies to disabling and deleting too.diff --git a/posthog/api/test/__snapshots__/test_query.new_events_schema.ambr b/posthog/api/test/__snapshots__/test_query.new_events_schema.ambrindex 213e7115bc6d..9e0f2d6bb5fe 100644--- a/posthog/api/test/__snapshots__/test_query.new_events_schema.ambr+++ b/posthog/api/test/__snapshots__/test_query.new_events_schema.ambr@@ -497,16 +497,19 @@# name: TestQuery.test_select_hogql_expressions[new_events_schema.1]'''/* user_id:0 request:_snapshot_ */- SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`,+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`,events.event AS eventFROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuidFROM events_json AS eventsWHERE and(equals(events.team_id, 99999), less(events.timestamp, toDateTime64('2020-01-10 12:14:05.000000', 6, 'UTC')), greater(events.timestamp, toDateTime64('2020-01-09 12:14:00.000000', 6, 'UTC')))- ORDER BY tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) ASC+ ORDER BY tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) ASCLIMIT 101)), and(less(events.timestamp, toDateTime64('2020-01-10 12:14:05.000000', 6, 'UTC')), greater(events.timestamp, toDateTime64('2020-01-09 12:14:00.000000', 6, 'UTC'))))- ORDER BY tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) ASC+ ORDER BY tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) ASCLIMIT 101OFFSET 0 SETTINGS readonly=2,max_execution_time=60,diff --git a/posthog/hogql/constants.py b/posthog/hogql/constants.pyindex ea9b7d41eb5b..1baa49239c68 100644--- a/posthog/hogql/constants.py+++ b/posthog/hogql/constants.py@@ -20,10 +20,21 @@ KEYWORDS = ["true", "false", "null"]# Keywords you can't alias toRESERVED_KEYWORDS = [*KEYWORDS, "team_id"]-# The ingest cleaner stores a feature flag variant named "false" under this sentinel in the `$feature_flags` map, because-# the map holds strings and a flag that was evaluated and switched off is already stored as 'false'. Reads map the-# sentinel back to "false", and the flag API refuses it as a variant key.-FEATURE_FLAG_FALSE_VARIANT_SENTINEL = "$false"+# The ingest cleaner stores a feature flag variant named "false" or "true" under a sentinel in the `$feature_flags` map,+# because the map holds strings and a boolean flag is already stored as 'false' or 'true'. Reads map each sentinel back+# to its variant name, and the flag API refuses the sentinels as variant keys.+FEATURE_FLAG_VARIANT_SENTINELS: dict[str, str] = {"$false": "false", "$true": "true"}++# `$feature_flags` map values of a flag evaluated to off, which `$active_feature_flags` leaves out.+INACTIVE_FEATURE_FLAG_VALUES: tuple[str, ...] = ("", "false")++FEATURE_FLAG_PROPERTY_PREFIX = "$feature/"+++def is_virtual_feature_flag_key(key: str) -> bool:+ """Whether an events_json property is rebuilt from the `$feature_flags` map instead of read under its own name."""+ return key in ("$active_feature_flags", "$feature_flags") or key.startswith(FEATURE_FLAG_PROPERTY_PREFIX)+# Limit applied to SELECT statements without LIMIT clause when queried via the APIDEFAULT_RETURNED_ROWS = 100diff --git a/posthog/hogql/printer/clickhouse.py b/posthog/hogql/printer/clickhouse.pyindex 4660fd6c9e8b..7d61c6feaa53 100644--- a/posthog/hogql/printer/clickhouse.py+++ b/posthog/hogql/printer/clickhouse.py@@ -7,7 +7,12 @@ from django.conf import settings as django_settingsfrom posthog.hogql import astfrom posthog.hogql.ast import AST, Constant, StringType-from posthog.hogql.constants import HogQLDialect+from posthog.hogql.constants import (+ FEATURE_FLAG_PROPERTY_PREFIX,+ FEATURE_FLAG_VARIANT_SENTINELS,+ INACTIVE_FEATURE_FLAG_VALUES,+ HogQLDialect,+)from posthog.hogql.context import HogQLContextfrom posthog.hogql.database.direct_sql_table import DirectSQLTablefrom posthog.hogql.database.models import (@@ -41,7 +46,11 @@ from posthog.hogql.functions.udfs import (from posthog.hogql.helpers.timestamp_visitor import parse_zoned_datetime_stringfrom posthog.hogql.printer.base import BasePrinter, get_channel_definition_dict, resolve_field_typefrom posthog.hogql.printer.hogql import HogQLPrinter-from posthog.hogql.restricted_properties import RESTRICTABLE_JSON_BLOB_COLUMNS, restricted_property_keys_for_table_type+from posthog.hogql.restricted_properties import (+ RESTRICTABLE_JSON_BLOB_COLUMNS,+ restricted_feature_flag_names,+ restricted_property_keys_for_table_type,+)from posthog.hogql.type_system import parse_sql_runtime_typefrom posthog.hogql.visitor import GetFieldsTraverser, clone_expr@@ -558,14 +567,10 @@ class ClickHousePrinter(BasePrinter):field_sql = f"{prefix}properties.{self._print_identifier(path)}"if name == "$session_id_uuid":field_sql = f"toUInt128(toUUIDOrNull({field_sql}))"- field_sql = self._maybe_stringify_events_json_field(type, field_sql)- return self._maybe_apply_json_drop_keys(type, field_sql)-- def _maybe_stringify_events_json_field(self, type: ast.FieldType, field_sql: str) -> str:serialized = self._serialize_events_json_field(type, field_sql)if serialized is not None:- return serialized- return field_sql+ return self._maybe_apply_json_drop_keys(type, serialized)+ return self._maybe_apply_json_drop_keys(type, field_sql, mask_feature_flags=True)def _serialize_events_json_field(self, type: ast.FieldType, field_sql: str) -> str | None:if not self.context.uses_new_events_schema():@@ -584,12 +589,54 @@ class ClickHousePrinter(BasePrinter):if not isinstance(type.table_type.resolve_database_table(self.context), EVENTS_TABLE_TYPES):return None- serialized = (- "concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), "- f"arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString({field_sql})))), ','), '}}')"+ key_filter = " and kv.1 != '$feature_flags'" if resolved_field.name == "properties" else ""+ pairs = (+ "arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), "+ f"arrayFilter(kv -> kv.2 != '[]'{key_filter}, JSONExtractKeysAndValuesRaw(toJSONString({field_sql}))))")+ if resolved_field.name == "properties":+ flag_pairs = self._serialized_feature_flag_pairs(type, field_sql)+ if flag_pairs is not None:+ pairs = f"arrayConcat({pairs}, {flag_pairs})"+ serialized = f"concat('{{', arrayStringConcat({pairs}, ','), '}}')"return f"{JSON_STRIP_EMPTY_STRINGS_AND_NULLS_CLICKHOUSE_NAME}({serialized})"+ def _serialized_feature_flag_pairs(self, type: ast.FieldType, field_sql: str) -> str | None:+ """The `"$feature/<key>":<value>` pairs and the `"$active_feature_flags":[...]` pair for the events_json document.++ The cleaner folds the `$feature/<key>` properties an SDK sends into the typed `$feature_flags` map and drops+ `$active_feature_flags`, so the legacy document is rebuilt from that map. A boolean flag is stored as 'true' or+ 'false' and comes back as a JSON boolean; a variant comes back as a JSON string, with the cleaner sentinels+ mapped to the variant names. `$active_feature_flags` lists the flags not evaluated to off, in map order.+ Restricted flags stay out of both, and a restricted `$feature_flags` hides every flag.+ """+ keys_to_drop = restricted_property_keys_for_table_type(type.table_type, self.context)+ if "$feature_flags" in keys_to_drop:+ return None+ flags = self._visible_feature_flags_sql(field_sql, restricted_feature_flag_names(keys_to_drop))+ variant_branches = ", ".join(+ f"value = {escape_clickhouse_string(sentinel)}, {escape_clickhouse_string(variant)}"+ for sentinel, variant in FEATURE_FLAG_VARIANT_SENTINELS.items()+ )+ flag_value = f"if(value in ('true', 'false'), value, toJSONString(multiIf({variant_branches}, value)))"+ prefix = escape_clickhouse_string(FEATURE_FLAG_PROPERTY_PREFIX)+ flag_pairs = (+ f"arrayMap((key, value) -> concat(toJSONString(concat({prefix}, key)), ':', {flag_value}), "+ f"mapKeys({flags}), mapValues({flags}))"+ )+ inactive_values = ", ".join(escape_clickhouse_string(value) for value in INACTIVE_FEATURE_FLAG_VALUES)+ active_flags = f"toJSONString(mapKeys(mapFilter((key, value) -> value not in ({inactive_values}), {flags})))"+ active_pair = f"if(empty({flags}), [], [concat('\"$active_feature_flags\":', {active_flags})])"+ return f"arrayConcat({flag_pairs}, {active_pair})"++ def _visible_feature_flags_sql(self, field_sql: str, restricted_flags: list[str]) -> str:+ """The `$feature_flags` map of `field_sql` without the restricted flags."""+ flags = f"{field_sql}.{escape_clickhouse_identifier('$feature_flags')}"+ if not restricted_flags:+ return flags+ placeholder = self.context.add_sensitive_value(restricted_flags)+ return f"mapFilter((key, value) -> not(has({placeholder}, key)), {flags})"+def _serialize_to_json_string_call(self, node: ast.Call) -> str | None:if node.name != "toJSONString" or len(node.args) != 1:return None@@ -613,10 +660,15 @@ class ClickHousePrinter(BasePrinter):finally:self._json_function_argument_depth = depth- def _maybe_apply_json_drop_keys(self, type: ast.FieldType, field_sql: str) -> str:+ def _maybe_apply_json_drop_keys(self, type: ast.FieldType, field_sql: str, mask_feature_flags: bool = False) -> str:"""Wraps a StringJSONDatabaseField in JSONDropKeys() to strip restricted property keyswhen the raw JSON blob is selected directly (e.g., `SELECT properties FROM events`).++ `mask_feature_flags` is for an events_json `properties` column printed as stored rather than as the rebuilt+ document, which happens under `toJSONString(if(..., properties, ...))`. That document holds the flags in the+ `$feature_flags` map, where JSONDropKeys cannot reach a restricted `$feature/<key>`, so the map is dropped and+ merged back without the restricted flags."""if not self.context.restricted_properties:return field_sql@@ -645,33 +697,29 @@ class ClickHousePrinter(BasePrinter):if not keys_to_drop:return field_sql- restricted_feature_flags = sorted(- key.removeprefix("$feature/") for key in keys_to_drop if key.startswith("$feature/")- )- filter_native_feature_flags = (- self.context.uses_new_events_schema()+ restricted_flags = restricted_feature_flag_names(keys_to_drop)+ mask_stored_feature_flags = (+ mask_feature_flags+ and bool(restricted_flags)+ and "$feature_flags" not in keys_to_dropand resolved_field.name == "properties"+ and self.context.uses_new_events_schema()and isinstance(type.table_type, ast.BaseTableType)and isinstance(type.table_type.resolve_database_table(self.context), EVENTS_TABLE_TYPES)- and bool(restricted_feature_flags)- and "$feature_flags" not in keys_to_drop)- if filter_native_feature_flags:- keys_to_drop = {key for key in keys_to_drop if not key.startswith("$feature/")} | {"$feature_flags"}+ if mask_stored_feature_flags:+ keys_to_drop = {key for key in keys_to_drop if not key.startswith(FEATURE_FLAG_PROPERTY_PREFIX)} | {+ "$feature_flags"+ }keys_placeholder = self.context.add_sensitive_value(sorted(keys_to_drop))stripped = f"{JSON_DROP_KEYS_CLICKHOUSE_NAME}({keys_placeholder})({field_sql})"- if not filter_native_feature_flags:+ if not mask_stored_feature_flags:return stripped- feature_keys_placeholder = self.context.add_sensitive_value(restricted_feature_flags)- physical_field = super().visit_field_type(type)- feature_flags = f"{physical_field}.{escape_clickhouse_identifier('$feature_flags')}"- filtered_feature_flags = (- f"mapFilter((key, value) -> not(has({feature_keys_placeholder}, key)), {feature_flags})"- )- feature_flags_patch = f"concat('{{\"$feature_flags\":', toJSONString({filtered_feature_flags}), '}}')"- return f"if(empty({filtered_feature_flags}), {stripped}, JSONMergePatch({stripped}, {feature_flags_patch}))"+ filtered_flags = self._visible_feature_flags_sql(field_sql, restricted_flags)+ flags_patch = f"concat('{{\"$feature_flags\":', toJSONString({filtered_flags}), '}}')"+ return f"if(empty({filtered_flags}), {stripped}, JSONMergePatch({stripped}, {flags_patch}))"def _get_optimized_session_id_compare_operation(self, node: ast.CompareOperation) -> str | None:"""Rewrite $session_id comparisons against UUID constants to use the $session_id_uuid column."""diff --git a/posthog/hogql/printer/test/__snapshots__/test_printer.new_events_schema.ambr b/posthog/hogql/printer/test/__snapshots__/test_printer.new_events_schema.ambrindex ce5b6cd8cb7b..0d276cff3355 100644--- a/posthog/hogql/printer/test/__snapshots__/test_printer.new_events_schema.ambr+++ b/posthog/hogql/printer/test/__snapshots__/test_printer.new_events_schema.ambr@@ -3480,7 +3480,7 @@"SELECT outer.event AS event FROM (SELECT inner.event AS event FROM (SELECT events.event AS event, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), '[]'), '{}'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_1)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), '[]'), '{}'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS inner) AS outer LIMIT 50000"# ---# name: TestPrinter.test_projection_pushdown_nested_subqueries_1_without_optimize_projections[new_events_schema]- "SELECT outer.event AS event FROM (SELECT inner.uuid AS uuid, inner.event AS event, inner.properties AS properties, inner.timestamp AS timestamp, inner.distinct_id AS distinct_id, inner.elements_chain AS elements_chain, inner.created_at AS created_at, inner.`$session_id` AS `$session_id`, inner.`$window_id` AS `$window_id`, inner.person_mode AS person_mode, inner.person_id AS person_id, inner.`$group_0` AS `$group_0`, inner.`$group_1` AS `$group_1`, inner.`$group_2` AS `$group_2`, inner.`$group_3` AS `$group_3`, inner.`$group_4` AS `$group_4`, inner.elements_chain_href AS elements_chain_href, inner.elements_chain_texts AS elements_chain_texts, inner.elements_chain_ids AS elements_chain_ids, inner.elements_chain_elements AS elements_chain_elements, inner.event_issue_id AS event_issue_id, inner.issue_id AS issue_id, inner.issue_id_v2 AS issue_id_v2, inner.issue_name AS issue_name, inner.issue_description AS issue_description, inner.issue_status AS issue_status, inner.issue_assigned_user_id AS issue_assigned_user_id, inner.issue_assigned_role_id AS issue_assigned_role_id, inner.issue_first_seen AS issue_first_seen FROM (SELECT events.uuid AS uuid, events.event AS event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')) AS properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp, events.distinct_id AS distinct_id, events.elements_chain AS elements_chain, toTimeZone(events.created_at, %(hogql_val_2)s) AS created_at, events.properties.`$session_id` AS `$session_id`, events.properties.`$window_id` AS `$window_id`, events.person_mode AS person_mode, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events.properties.`$group_0` AS `$group_0`, events.properties.`$group_1` AS `$group_1`, events.properties.`$group_2` AS `$group_2`, events.properties.`$group_3` AS `$group_3`, events.properties.`$group_4` AS `$group_4`, events.elements_chain_href AS elements_chain_href, events.elements_chain_texts AS elements_chain_texts, events.elements_chain_ids AS elements_chain_ids, events.elements_chain_elements AS elements_chain_elements, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), '[]'), '{}'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_3)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), '[]'), '{}'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS inner) AS outer LIMIT 50000"+ 'SELECT outer.event AS event FROM (SELECT inner.uuid AS uuid, inner.event AS event, inner.properties AS properties, inner.timestamp AS timestamp, inner.distinct_id AS distinct_id, inner.elements_chain AS elements_chain, inner.created_at AS created_at, inner.`$session_id` AS `$session_id`, inner.`$window_id` AS `$window_id`, inner.person_mode AS person_mode, inner.person_id AS person_id, inner.`$group_0` AS `$group_0`, inner.`$group_1` AS `$group_1`, inner.`$group_2` AS `$group_2`, inner.`$group_3` AS `$group_3`, inner.`$group_4` AS `$group_4`, inner.elements_chain_href AS elements_chain_href, inner.elements_chain_texts AS elements_chain_texts, inner.elements_chain_ids AS elements_chain_ids, inner.elements_chain_elements AS elements_chain_elements, inner.event_issue_id AS event_issue_id, inner.issue_id AS issue_id, inner.issue_id_v2 AS issue_id_v2, inner.issue_name AS issue_name, inner.issue_description AS issue_description, inner.issue_status AS issue_status, inner.issue_assigned_user_id AS issue_assigned_user_id, inner.issue_assigned_role_id AS issue_assigned_role_id, inner.issue_first_seen AS issue_first_seen FROM (SELECT events.uuid AS uuid, events.event AS event, JSONStripEmptyStringsAndNulls(concat(\'{\', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), \':\', kv.2), arrayFilter(kv -> kv.2 != \'[]\' and kv.1 != \'$feature_flags\', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat(\'$feature/\', key)), \':\', if(value in (\'true\', \'false\'), value, toJSONString(multiIf(value = \'$false\', \'false\', value = \'$true\', \'true\', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat(\'"$active_feature_flags":\', toJSONString(mapKeys(mapFilter((key, value) -> value not in (\'\', \'false\'), events.properties.`$feature_flags`))))]))), \',\'), \'}\')) AS properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp, events.distinct_id AS distinct_id, events.elements_chain AS elements_chain, toTimeZone(events.created_at, %(hogql_val_2)s) AS created_at, events.properties.`$session_id` AS `$session_id`, events.properties.`$window_id` AS `$window_id`, events.person_mode AS person_mode, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events.properties.`$group_0` AS `$group_0`, events.properties.`$group_1` AS `$group_1`, events.properties.`$group_2` AS `$group_2`, events.properties.`$group_3` AS `$group_3`, events.properties.`$group_4` AS `$group_4`, events.elements_chain_href AS elements_chain_href, events.elements_chain_texts AS elements_chain_texts, events.elements_chain_ids AS elements_chain_ids, events.elements_chain_elements AS elements_chain_elements, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), \'{}\'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), \'\')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), \'[]\'), \'{}\'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_3)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), \'{}\'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), \'\')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), \'[]\'), \'{}\'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS inner) AS outer LIMIT 50000'# ---# name: TestPrinter.test_projection_pushdown_no_asterisk_unchanged_0_with_optimize_projections[new_events_schema]'SELECT sub.event AS event FROM (SELECT events.event AS event, events.distinct_id AS distinct_id FROM events_json AS events WHERE equals(events.team_id, 99999)) AS sub LIMIT 50000'@@ -3492,19 +3492,19 @@"SELECT e.event AS event FROM (SELECT events.event AS event, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), '[]'), '{}'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_1)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), '[]'), '{}'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS e LEFT JOIN (SELECT person.id AS id FROM person WHERE equals(person.team_id, 99999) GROUP BY person.id HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_2)s), person.version), plus(now64(6, %(hogql_val_3)s), toIntervalDay(1)))) SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, e.person_id) LIMIT 50000"# ---# name: TestPrinter.test_projection_pushdown_preserves_join_columns_1_without_optimize_projections[new_events_schema]- "SELECT e.event AS event FROM (SELECT events.uuid AS uuid, events.event AS event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')) AS properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp, events.distinct_id AS distinct_id, events.elements_chain AS elements_chain, toTimeZone(events.created_at, %(hogql_val_2)s) AS created_at, events.properties.`$session_id` AS `$session_id`, events.properties.`$window_id` AS `$window_id`, events.person_mode AS person_mode, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events.properties.`$group_0` AS `$group_0`, events.properties.`$group_1` AS `$group_1`, events.properties.`$group_2` AS `$group_2`, events.properties.`$group_3` AS `$group_3`, events.properties.`$group_4` AS `$group_4`, events.elements_chain_href AS elements_chain_href, events.elements_chain_texts AS elements_chain_texts, events.elements_chain_ids AS elements_chain_ids, events.elements_chain_elements AS elements_chain_elements, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), '[]'), '{}'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_3)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), '[]'), '{}'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS e LEFT JOIN (SELECT person.id AS id FROM person WHERE equals(person.team_id, 99999) GROUP BY person.id HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_4)s), person.version), plus(now64(6, %(hogql_val_5)s), toIntervalDay(1)))) SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, e.person_id) LIMIT 50000"+ 'SELECT e.event AS event FROM (SELECT events.uuid AS uuid, events.event AS event, JSONStripEmptyStringsAndNulls(concat(\'{\', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), \':\', kv.2), arrayFilter(kv -> kv.2 != \'[]\' and kv.1 != \'$feature_flags\', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat(\'$feature/\', key)), \':\', if(value in (\'true\', \'false\'), value, toJSONString(multiIf(value = \'$false\', \'false\', value = \'$true\', \'true\', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat(\'"$active_feature_flags":\', toJSONString(mapKeys(mapFilter((key, value) -> value not in (\'\', \'false\'), events.properties.`$feature_flags`))))]))), \',\'), \'}\')) AS properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp, events.distinct_id AS distinct_id, events.elements_chain AS elements_chain, toTimeZone(events.created_at, %(hogql_val_2)s) AS created_at, events.properties.`$session_id` AS `$session_id`, events.properties.`$window_id` AS `$window_id`, events.person_mode AS person_mode, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events.properties.`$group_0` AS `$group_0`, events.properties.`$group_1` AS `$group_1`, events.properties.`$group_2` AS `$group_2`, events.properties.`$group_3` AS `$group_3`, events.properties.`$group_4` AS `$group_4`, events.elements_chain_href AS elements_chain_href, events.elements_chain_texts AS elements_chain_texts, events.elements_chain_ids AS elements_chain_ids, events.elements_chain_elements AS elements_chain_elements, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), \'{}\'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), \'\')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), \'[]\'), \'{}\'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_3)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), \'{}\'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), \'\')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), \'[]\'), \'{}\'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS e LEFT JOIN (SELECT person.id AS id FROM person WHERE equals(person.team_id, 99999) GROUP BY person.id HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_4)s), person.version), plus(now64(6, %(hogql_val_5)s), toIntervalDay(1)))) SETTINGS optimize_aggregation_in_order=1) AS persons ON equals(persons.id, e.person_id) LIMIT 50000'# ---# name: TestPrinter.test_projection_pushdown_preserves_where_columns_0_with_optimize_projections[new_events_schema]"SELECT sub.event AS event FROM (SELECT events.event AS event, events.distinct_id AS distinct_id, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), '[]'), '{}'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_1)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), '[]'), '{}'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS sub WHERE equals(sub.distinct_id, %(hogql_val_2)s) LIMIT 50000"# ---# name: TestPrinter.test_projection_pushdown_preserves_where_columns_1_without_optimize_projections[new_events_schema]- "SELECT sub.event AS event FROM (SELECT events.uuid AS uuid, events.event AS event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')) AS properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp, events.distinct_id AS distinct_id, events.elements_chain AS elements_chain, toTimeZone(events.created_at, %(hogql_val_2)s) AS created_at, events.properties.`$session_id` AS `$session_id`, events.properties.`$window_id` AS `$window_id`, events.person_mode AS person_mode, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events.properties.`$group_0` AS `$group_0`, events.properties.`$group_1` AS `$group_1`, events.properties.`$group_2` AS `$group_2`, events.properties.`$group_3` AS `$group_3`, events.properties.`$group_4` AS `$group_4`, events.elements_chain_href AS elements_chain_href, events.elements_chain_texts AS elements_chain_texts, events.elements_chain_ids AS elements_chain_ids, events.elements_chain_elements AS elements_chain_elements, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), '[]'), '{}'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_3)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), '[]'), '{}'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS sub WHERE equals(sub.distinct_id, %(hogql_val_4)s) LIMIT 50000"+ 'SELECT sub.event AS event FROM (SELECT events.uuid AS uuid, events.event AS event, JSONStripEmptyStringsAndNulls(concat(\'{\', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), \':\', kv.2), arrayFilter(kv -> kv.2 != \'[]\' and kv.1 != \'$feature_flags\', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat(\'$feature/\', key)), \':\', if(value in (\'true\', \'false\'), value, toJSONString(multiIf(value = \'$false\', \'false\', value = \'$true\', \'true\', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat(\'"$active_feature_flags":\', toJSONString(mapKeys(mapFilter((key, value) -> value not in (\'\', \'false\'), events.properties.`$feature_flags`))))]))), \',\'), \'}\')) AS properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp, events.distinct_id AS distinct_id, events.elements_chain AS elements_chain, toTimeZone(events.created_at, %(hogql_val_2)s) AS created_at, events.properties.`$session_id` AS `$session_id`, events.properties.`$window_id` AS `$window_id`, events.person_mode AS person_mode, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events.properties.`$group_0` AS `$group_0`, events.properties.`$group_1` AS `$group_1`, events.properties.`$group_2` AS `$group_2`, events.properties.`$group_3` AS `$group_3`, events.properties.`$group_4` AS `$group_4`, events.elements_chain_href AS elements_chain_href, events.elements_chain_texts AS elements_chain_texts, events.elements_chain_ids AS elements_chain_ids, events.elements_chain_elements AS elements_chain_elements, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), \'{}\'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), \'\')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), \'[]\'), \'{}\'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_3)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), \'{}\'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), \'\')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), \'[]\'), \'{}\'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS sub WHERE equals(sub.distinct_id, %(hogql_val_4)s) LIMIT 50000'# ---# name: TestPrinter.test_projection_pushdown_simple_asterisk_subquery_0_with_optimize_projections[new_events_schema]"SELECT sub.event AS event FROM (SELECT events.event AS event, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), '[]'), '{}'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_1)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), '[]'), '{}'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS sub LIMIT 50000"# ---# name: TestPrinter.test_projection_pushdown_simple_asterisk_subquery_1_without_optimize_projections[new_events_schema]- "SELECT sub.event AS event FROM (SELECT events.uuid AS uuid, events.event AS event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')) AS properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp, events.distinct_id AS distinct_id, events.elements_chain AS elements_chain, toTimeZone(events.created_at, %(hogql_val_2)s) AS created_at, events.properties.`$session_id` AS `$session_id`, events.properties.`$window_id` AS `$window_id`, events.person_mode AS person_mode, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events.properties.`$group_0` AS `$group_0`, events.properties.`$group_1` AS `$group_1`, events.properties.`$group_2` AS `$group_2`, events.properties.`$group_3` AS `$group_3`, events.properties.`$group_4` AS `$group_4`, events.elements_chain_href AS elements_chain_href, events.elements_chain_texts AS elements_chain_texts, events.elements_chain_ids AS elements_chain_ids, events.elements_chain_elements AS elements_chain_elements, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), '[]'), '{}'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_3)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), '')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), '[]'), '{}'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS sub LIMIT 50000"+ 'SELECT sub.event AS event FROM (SELECT events.uuid AS uuid, events.event AS event, JSONStripEmptyStringsAndNulls(concat(\'{\', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), \':\', kv.2), arrayFilter(kv -> kv.2 != \'[]\' and kv.1 != \'$feature_flags\', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat(\'$feature/\', key)), \':\', if(value in (\'true\', \'false\'), value, toJSONString(multiIf(value = \'$false\', \'false\', value = \'$true\', \'true\', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat(\'"$active_feature_flags":\', toJSONString(mapKeys(mapFilter((key, value) -> value not in (\'\', \'false\'), events.properties.`$feature_flags`))))]))), \',\'), \'}\')) AS properties, toTimeZone(events.timestamp, %(hogql_val_1)s) AS timestamp, events.distinct_id AS distinct_id, events.elements_chain AS elements_chain, toTimeZone(events.created_at, %(hogql_val_2)s) AS created_at, events.properties.`$session_id` AS `$session_id`, events.properties.`$window_id` AS `$window_id`, events.person_mode AS person_mode, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, events.properties.`$group_0` AS `$group_0`, events.properties.`$group_1` AS `$group_1`, events.properties.`$group_2` AS `$group_2`, events.properties.`$group_3` AS `$group_3`, events.properties.`$group_4` AS `$group_4`, events.elements_chain_href AS elements_chain_href, events.elements_chain_texts AS elements_chain_texts, events.elements_chain_ids AS elements_chain_ids, events.elements_chain_elements AS elements_chain_elements, accurateCastOrNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), \'{}\'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_issue_id`)), if(isNull(nullIf(toString(events.properties.`$exception_issue_id`), \'\')), NULL, if(and(in(ascii(toString(events.properties.`$exception_issue_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_issue_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_issue_id`), \'[]\'), \'{}\'), toString(events.properties.`$exception_issue_id`)))), %(hogql_val_3)s) AS event_issue_id, events__fingerprint_issue_state.issue_id AS issue_id, events__fingerprint_issue_state.issue_id AS issue_id_v2, events__fingerprint_issue_state.issue_name AS issue_name, events__fingerprint_issue_state.issue_description AS issue_description, events__fingerprint_issue_state.issue_status AS issue_status, events__fingerprint_issue_state.assigned_user_id AS issue_assigned_user_id, events__fingerprint_issue_state.assigned_role_id AS issue_assigned_role_id, events__fingerprint_issue_state.first_seen AS issue_first_seen FROM events_json AS events LEFT OUTER JOIN (SELECT cityHash64(error_tracking_fingerprint_issue_state.fingerprint) AS fp_hash, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_id), error_tracking_fingerprint_issue_state.version), 1)) AS issue_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_name), error_tracking_fingerprint_issue_state.version), 1)) AS issue_name, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_description), error_tracking_fingerprint_issue_state.version), 1)) AS issue_description, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.issue_status), error_tracking_fingerprint_issue_state.version), 1)) AS issue_status, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_user_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_user_id, toNullable(tupleElement(argMax(tuple(error_tracking_fingerprint_issue_state.assigned_role_id), error_tracking_fingerprint_issue_state.version), 1)) AS assigned_role_id, toNullable(tupleElement(argMax(tuple(toTimeZone(error_tracking_fingerprint_issue_state.first_seen, %(hogql_val_0)s)), error_tracking_fingerprint_issue_state.version), 1)) AS first_seen FROM error_tracking_fingerprint_issue_state WHERE equals(error_tracking_fingerprint_issue_state.team_id, 99999) GROUP BY fp_hash HAVING equals(argMax(error_tracking_fingerprint_issue_state.is_deleted, error_tracking_fingerprint_issue_state.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__fingerprint_issue_state ON equals(cityHash64(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), \'{}\'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$exception_fingerprint`)), if(isNull(nullIf(toString(events.properties.`$exception_fingerprint`), \'\')), NULL, if(and(in(ascii(toString(events.properties.`$exception_fingerprint`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$exception_fingerprint`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$exception_fingerprint`), \'[]\'), \'{}\'), toString(events.properties.`$exception_fingerprint`))))), events__fingerprint_issue_state.fp_hash) LEFT OUTER JOIN (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id, person_distinct_id_overrides.distinct_id AS distinct_id FROM person_distinct_id_overrides WHERE equals(person_distinct_id_overrides.team_id, 99999) GROUP BY person_distinct_id_overrides.distinct_id HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) WHERE equals(events.team_id, 99999)) AS sub LIMIT 50000'# ---# name: TestPrinter.test_s3_tables_global_join_anonymous_tables_0_global_joins_with_optimize[new_events_schema]"SELECT e.event AS event, ij.remote_id AS remote_id FROM events_json AS e GLOBAL INNER JOIN (SELECT remote_id AS remote_id FROM (SELECT p.id AS person_id, rt.id AS remote_id FROM (SELECT person.id AS id FROM person WHERE equals(person.team_id, 99999) GROUP BY person.id HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, %(hogql_val_0)s), person.version), plus(now64(6, %(hogql_val_1)s), toIntervalDay(1)))) SETTINGS optimize_aggregation_in_order=1) AS p LEFT JOIN (SELECT test_table.id AS id FROM (SELECT * FROM s3(%(hogql_val_2_sensitive)s, %(hogql_val_4_sensitive)s, %(hogql_val_5_sensitive)s, 'Parquet', %(hogql_val_3)s) SETTINGS optimize_move_to_prewhere = 0) AS test_table) AS rt ON equals(rt.id, p.id))) AS ij ON equals(e.event, ij.remote_id) WHERE equals(e.team_id, 99999) LIMIT 50000"diff --git a/posthog/hogql/restricted_properties.py b/posthog/hogql/restricted_properties.pyindex c4ca63e19e9e..0cadaeb5a258 100644--- a/posthog/hogql/restricted_properties.py+++ b/posthog/hogql/restricted_properties.py@@ -1,6 +1,9 @@+from collections.abc import Iterable+import structlogfrom posthog.hogql import ast+from posthog.hogql.constants import FEATURE_FLAG_PROPERTY_PREFIXfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.postgres_table import PostgresTablefrom posthog.hogql.database.schema.ai_events import AI_PROPERTY_TO_COLUMN, AiEventsTable@@ -70,6 +73,13 @@ def mirrored_property_for_column(table_type: ast.Type, column_name: str, contextreturn None+def restricted_feature_flag_names(keys: Iterable[str]) -> list[str]:+ """The `$feature_flags` map keys of the restricted `$feature/<key>` properties in `keys`, sorted for stable SQL."""+ return sorted(+ key.removeprefix(FEATURE_FLAG_PROPERTY_PREFIX) for key in keys if key.startswith(FEATURE_FLAG_PROPERTY_PREFIX)+ )++def restricted_property_keys_for_table_type(table_type: ast.Type, context: HogQLContext, *, group_type_index: int | None = None) -> set[str]:diff --git a/posthog/hogql/transforms/clickhouse_property_resolution.py b/posthog/hogql/transforms/clickhouse_property_resolution.pyindex 507aa3c94497..013145cfea0b 100644--- a/posthog/hogql/transforms/clickhouse_property_resolution.py+++ b/posthog/hogql/transforms/clickhouse_property_resolution.py@@ -28,7 +28,13 @@ from typing import Literal, castfrom posthog.hogql import astfrom posthog.hogql.base import _T_AST-from posthog.hogql.constants import EXCEPTION_STRING_ARRAY_PROPERTIES, FEATURE_FLAG_FALSE_VARIANT_SENTINEL+from posthog.hogql.constants import (+ EXCEPTION_STRING_ARRAY_PROPERTIES,+ FEATURE_FLAG_PROPERTY_PREFIX,+ FEATURE_FLAG_VARIANT_SENTINELS,+ INACTIVE_FEATURE_FLAG_VALUES,+ is_virtual_feature_flag_key,+)from posthog.hogql.context import HogQLContextfrom posthog.hogql.database.models import DatabaseField, MapStringDatabaseField, StringJSONDatabaseFieldfrom posthog.hogql.errors import QueryError@@ -38,6 +44,7 @@ from posthog.hogql.printer.clickhouse import AI_BLOOM_FILTER_PROPERTIES, COLUMNSfrom posthog.hogql.restricted_properties import (mirrored_property_for_column,native_property_path_overlaps_restriction,+ restricted_feature_flag_names,restricted_property_keys_for_table_type,)from posthog.hogql.type_system import (@@ -568,29 +575,28 @@ def _mirrored_source_property(field_type: ast.FieldType, context: HogQLContext)return mirrored_property_for_column(field_type.table_type, resolved_field.name, context)-def _false_variant_read(value: ast.Expr) -> ast.Expr:- """`value` with the `$false` sentinel read back as the variant name "false".+def feature_flag_variant_read(value: ast.Expr) -> ast.Expr:+ """`value` with each cleaner sentinel read back as the variant name it stands for.- The cleaner stores a variant named "false" as `$false` so it stays apart from a flag that was evaluated and switched- off, which the typed map holds as 'false'.+ The cleaner stores a variant named "false" or "true" as `$false` or `$true`, so it stays apart from a boolean flag,+ which the typed map holds as 'false' or 'true'."""- return _call(- "if",- [- _call("equals", [clone_expr(value), _const(FEATURE_FLAG_FALSE_VARIANT_SENTINEL)]),- _sentinel("false"),- value,- ],- )+ branches: list[ast.Expr] = []+ for sentinel, variant in FEATURE_FLAG_VARIANT_SENTINELS.items():+ branches.append(_call("equals", [clone_expr(value), _const(sentinel)]))+ branches.append(_sentinel(variant))+ return _call("multiIf", [*branches, value])def _feature_flag_value_read(feature_flags: ast.Expr, key: str) -> ast.Expr:- """`has(map, key) ? map[key] : null`, with the `$false` sentinel mapped back to "false"."""+ """`has(map, key) ? map[key] : null`, with the cleaner sentinels mapped back to their variant names."""return ast.Call(name="if",args=[ast.Call(name="has", args=[clone_expr(feature_flags), ast.Constant(value=key)]),- _false_variant_read(ast.ArrayAccess(array=clone_expr(feature_flags), property=ast.Constant(value=key))),+ feature_flag_variant_read(+ ast.ArrayAccess(array=clone_expr(feature_flags), property=ast.Constant(value=key))+ ),ast.Constant(value=None),],)@@ -616,8 +622,6 @@ def _is_moved_events_property(field_type: ast.FieldType, key: str, context: HogQ)-FEATURE_FLAG_PROPERTY_PREFIX = "$feature/"-# JSON functions that take a key path and that nothing earlier rewrites to read one property. The printer passes them# the serialized `properties` document, which never holds a key the native cleaner moves to `temporary_properties`._TEMPORARY_PROPERTY_JSON_PATH_FUNCTIONS = frozenset(@@ -691,11 +695,6 @@ def _json_path_first_member(path: str) -> str | None:return next(member for member in match.groups() if member is not None)-def _is_virtual_feature_flag_key(key: str) -> bool:- """Whether a native events property is rebuilt from the `$feature_flags` map instead of read under its own name."""- return key in ("$active_feature_flags", "$feature_flags") or key.startswith(FEATURE_FLAG_PROPERTY_PREFIX)--def _feature_flags_map(field_type: ast.FieldType, context: HogQLContext) -> ast.Expr | None:source = resolve_json_subcolumn_source(field_type, DISTRIBUTED_EVENTS_JSON_TABLE, "properties", "$feature_flags", context@@ -708,12 +707,7 @@ def _feature_flags_map(field_type: ast.FieldType, context: HogQLContext) -> ast.def _restricted_feature_flag_keys(field_type: ast.FieldType, context: HogQLContext) -> list[str]:- keys = (- key- for key in restricted_property_keys_for_table_type(field_type.table_type, context)- if key.startswith(FEATURE_FLAG_PROPERTY_PREFIX)- )- return sorted(key.removeprefix(FEATURE_FLAG_PROPERTY_PREFIX) for key in keys)+ return restricted_feature_flag_names(restricted_property_keys_for_table_type(field_type.table_type, context))def _not_in_lambda_values(name: str, values: list[str], *, is_sensitive: bool = False) -> ast.Call:@@ -742,7 +736,7 @@ def _filter_feature_flags(feature_flags: ast.Expr, restricted_keys: list[str]) -def _compact_feature_flags_map(feature_flags: ast.Expr, restricted_keys: list[str], *, map_values: bool = True) -> ast.Expr:- """The visible flags map, with `$false` read back as "false".+ """The visible flags map, with the cleaner sentinels read back as their variant names.Presence checks pass `map_values=False`: the mapping cannot change the key set, so they skip the per-row `mapApply`."""@@ -754,7 +748,9 @@ def _compact_feature_flags_map(args=[ast.Lambda(args=["key", "value"],- expr=ast.Tuple(exprs=[_lambda_string_arg("key"), _false_variant_read(_lambda_string_arg("value"))]),+ expr=ast.Tuple(+ exprs=[_lambda_string_arg("key"), feature_flag_variant_read(_lambda_string_arg("value"))]+ ),),filtered,],@@ -775,9 +771,9 @@ def _nonempty_container_json(value: ast.Expr, empty_json: str) -> ast.Expr:def _active_flag_lambda(restricted_keys: list[str] | None, key_predicate: ast.Expr | None = None) -> ast.Lambda:"""`(key, value) -> value is active, key is not restricted, and `key_predicate` holds.- A variant named "false" is stored as `$false`, so it counts as active here.+ The cleaner stores variants named "false" and "true" as `$false` and `$true`, so both count as active here."""- predicates: list[ast.Expr] = [_not_in_lambda_values("value", ["", "false"])]+ predicates: list[ast.Expr] = [_not_in_lambda_values("value", list(INACTIVE_FEATURE_FLAG_VALUES))]if restricted_keys:predicates.append(_not_in_lambda_values("key", restricted_keys, is_sensitive=True))if key_predicate is not None:@@ -830,7 +826,7 @@ def _feature_flag_compatibility_read(deeper_keys = list(node.keys[1:])if (not context.uses_new_events_schema()- or not _is_virtual_feature_flag_key(first_key)+ or not is_virtual_feature_flag_key(first_key)or not _is_events_properties(field_type, context)):return None@@ -1227,7 +1223,7 @@ class ClickHousePropertyResolver(CloningVisitor):def _is_virtual_feature_flag_property(self, field_type: ast.FieldType, property_name: str) -> bool:return (self.context.uses_new_events_schema()- and _is_virtual_feature_flag_key(property_name)+ and is_virtual_feature_flag_key(property_name)and _is_events_properties(field_type, self.context))@@ -1513,7 +1509,7 @@ class ClickHousePropertyResolver(CloningVisitor):return Noneif first_key in restricted_property_keys_for_table_type(field_type.table_type, self.context):return ast.Constant(value=False, type=ast.BooleanType(nullable=False))- if not _is_virtual_feature_flag_key(first_key):+ if not is_virtual_feature_flag_key(first_key):return Nonerestricted_properties = restricted_property_keys_for_table_type(field_type.table_type, self.context)diff --git a/posthog/hogql/transforms/property_types.py b/posthog/hogql/transforms/property_types.pyindex 418445716d30..4438fbcd2a78 100644--- a/posthog/hogql/transforms/property_types.py+++ b/posthog/hogql/transforms/property_types.py@@ -2,7 +2,7 @@ from datetime import datetimefrom typing import Literal, Optionalfrom posthog.hogql import ast-from posthog.hogql.constants import EXCEPTION_STRING_ARRAY_PROPERTIES+from posthog.hogql.constants import EXCEPTION_STRING_ARRAY_PROPERTIES, is_virtual_feature_flag_keyfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.models import BooleanDatabaseField, DateTimeDatabaseField, StringJSONDatabaseFieldfrom posthog.hogql.database.s3_table import S3Table@@ -348,14 +348,16 @@ class PropertySwapper(CloningVisitor):if table_name not in MATERIALIZATION_VALID_TABLES:return None- # On native events, property resolution rebuilds this virtual map for every JSONExtract* function.+ # events_json holds no `$feature/<key>` subcolumns: the rebuilt document, or the resolver's map read, serves+ # these keys with the boolean or string type the SDK sent.if (self.context.uses_new_events_schema()and table_name == "events"and database_field.name == "properties"and len(node.args) > 1and isinstance(node.args[1], ast.Constant)- and node.args[1].value == "$feature_flags"+ and isinstance(node.args[1].value, str)+ and is_virtual_feature_flag_key(node.args[1].value)):return Nonediff --git a/posthog/hogql/transforms/test/test_property_types.py b/posthog/hogql/transforms/test/test_property_types.pyindex a866b4d36437..3fc35caab3ed 100644--- a/posthog/hogql/transforms/test/test_property_types.py+++ b/posthog/hogql/transforms/test/test_property_types.py@@ -322,14 +322,32 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):("SELECT NULL AS `$active_feature_flags`, NULL AS `$feature/checkout`, NULL, 0 ",),("events.properties.`$feature_flags`",),),+ (+ "restricted_map_document",+ "SELECT properties FROM events",+ True,+ {RestrictedProperty(name="$feature_flags", property_type=PropertyDefinition.Type.EVENT)},+ None,+ ("JSONExtractKeysAndValuesRaw(",),+ ("concat('$feature/', key)", "'\"$active_feature_flags\":'"),+ ),("dynamic_restricted","SELECT properties, JSONHas(properties, '$feature_flags', concat('sec', 'ret')) FROM events",True,{RestrictedProperty(name="$feature/secret", property_type=PropertyDefinition.Type.EVENT)},None,- ("JSONMergePatch(", "mapFilter("),- (),+ ("mapFilter((key, value) -> not(has(", "'\"$active_feature_flags\":'"),+ ("JSONMergePatch(",),+ ),+ (+ "stored_document_restricted",+ "SELECT toJSONString(if(1, properties, properties)) FROM events",+ True,+ {RestrictedProperty(name="$feature/secret", property_type=PropertyDefinition.Type.EVENT)},+ None,+ ("JSONMergePatch(", "mapFilter((key, value) -> not(has("),+ ("'\"$active_feature_flags\":'",),),])@@ -1654,19 +1672,28 @@ class TestEventsSchemaPropertyParity(ClickhouseTestMixin, BaseTest):}restricted = execute_hogql_query("SELECT properties, JSONHas(properties, '$feature_flags', concat('sec', 'ret')), "- "properties.$active_feature_flags "+ "properties.$active_feature_flags, toJSONString(if(1, properties, properties)) "f"FROM events WHERE uuid = '{native_uuid}'",team=self.team,context=restricted_context,)assert restricted.results is not None- assert json.loads(restricted.results[0][0])["$feature_flags"] == {- "checkout": "true",- "disabled": "false",- "false-variant": "$false",- "only-in-map": "true",- "variant": "control",+ # The masked document is a String, so toJSONString quotes it, as it quotes the legacy String column.+ stored_document = json.loads(json.loads(restricted.results[0][3]))+ assert "$feature/secret" not in stored_document+ assert "secret" not in stored_document["$feature_flags"]+ assert stored_document["$feature_flags"]["variant"] == "control"+ restricted_document = json.loads(restricted.results[0][0])+ assert "$feature_flags" not in restricted_document+ # The fixture sends "true" as a string, which the cleaner stores as `$true`, so it comes back as a string.+ assert {key: value for key, value in restricted_document.items() if key.startswith("$feature/")} == {+ "$feature/checkout": "true",+ "$feature/disabled": False,+ "$feature/false-variant": "false",+ "$feature/only-in-map": "true",+ "$feature/variant": "control",}+ assert restricted_document["$active_feature_flags"] == ["checkout", "false-variant", "only-in-map", "variant"]assert restricted.results[0][1] == 0assert json.loads(restricted.results[0][2]) == ["checkout", "false-variant", "only-in-map", "variant"]@@ -1697,6 +1724,88 @@ class TestEventsSchemaPropertyParity(ClickhouseTestMixin, BaseTest):)assert inactive.results == [("[]", 1, 1)]+ def test_sdk_flag_properties_read_the_same_on_both_schemas(self) -> None:+ sdk_uuid = _create_event(+ team=self.team,+ distinct_id="sdk-flags",+ event="schema-parity",+ properties={+ "$browser": "Firefox",+ "$feature/checkout": True,+ "$feature/disabled": False,+ "$feature/variant": "control",+ "$feature/named-false": "false",+ "$feature/named-true": "true",+ "$active_feature_flags": ["checkout", "variant", "named-false", "named-true"],+ },+ )+ flush_persons_and_events()++ query = (+ "SELECT properties, toString(properties), arraySort(JSONExtractKeys(properties)), "+ "JSONLength(properties, '$active_feature_flags'), "+ "arraySort(JSONExtractArrayRaw(properties, '$active_feature_flags')), "+ "JSONExtractBool(properties, '$feature/checkout'), JSONExtractBool(properties, '$feature/named-true'), "+ "JSONExtractRaw(properties, '$feature/named-true'), JSONType(properties, '$feature/named-false'), "+ "JSONType(properties, '$feature/disabled'), JSONExtractString(properties, '$feature/variant'), "+ "properties.`$feature/named-true`, properties.`$feature/checkout`, "+ "arraySort(JSONExtractArrayRaw(toString(properties), '$active_feature_flags')) "+ f"FROM events WHERE uuid = '{sdk_uuid}'"+ )+ exploded_query = (+ "SELECT arrayJoin(JSONExtractArrayRaw(properties, '$active_feature_flags')) AS flag "+ f"FROM events WHERE uuid = '{sdk_uuid}' ORDER BY flag"+ )++ def read(use_new_events_schema: bool) -> tuple[dict[str, Any], dict[str, Any], tuple[Any, ...], list[Any]]:+ context = HogQLContext(+ team_id=self.team.pk, enable_select_queries=True, use_new_events_schema=use_new_events_schema+ )+ reads = execute_hogql_query(query, team=self.team, context=context)+ exploded = execute_hogql_query(exploded_query, team=self.team, context=context)+ assert reads.results is not None and exploded.results is not None+ document = json.loads(reads.results[0][0])+ document["$active_feature_flags"] = sorted(document["$active_feature_flags"])+ stringified = json.loads(reads.results[0][1])+ stringified["$active_feature_flags"] = sorted(stringified["$active_feature_flags"])+ return document, stringified, tuple(reads.results[0][2:]), exploded.results++ native = read(use_new_events_schema=True)+ assert native == read(use_new_events_schema=False)+ active_flags = ['"checkout"', '"named-false"', '"named-true"', '"variant"']+ assert native[0] == {+ "$browser": "Firefox",+ "$feature/checkout": True,+ "$feature/disabled": False,+ "$feature/variant": "control",+ "$feature/named-false": "false",+ "$feature/named-true": "true",+ "$active_feature_flags": ["checkout", "named-false", "named-true", "variant"],+ }+ assert native[2] == (+ [+ "$active_feature_flags",+ "$browser",+ "$feature/checkout",+ "$feature/disabled",+ "$feature/named-false",+ "$feature/named-true",+ "$feature/variant",+ ],+ 4,+ active_flags,+ 1,+ 0,+ '"true"',+ "String",+ "Bool",+ "control",+ "true",+ "true",+ active_flags,+ )+ assert native[3] == [(flag,) for flag in active_flags]+def test_moved_mutation_properties_read_the_same_on_both_schemas(self) -> None:event_uuid = _create_event(team=self.team,diff --git a/posthog/hogql_queries/ai/event_taxonomy_query_runner.py b/posthog/hogql_queries/ai/event_taxonomy_query_runner.pyindex 0b9d2d6d2e6f..f7cde1ab24f1 100644--- a/posthog/hogql_queries/ai/event_taxonomy_query_runner.py+++ b/posthog/hogql_queries/ai/event_taxonomy_query_runner.py@@ -111,6 +111,7 @@ class EventTaxonomyQueryRunner(TaxonomyCacheMixin, AnalyticsQueryRunner[EventTax# feature flags and experimentsr"\$feature\/",r"^\$feature_flags$",+ r"^\$active_feature_flags$",r"\$feature_enrollment\/",r"\$feature_interaction\/",# product toursdiff --git a/posthog/hogql_queries/ai/test/__snapshots__/test_event_taxonomy_query_runner.ambr b/posthog/hogql_queries/ai/test/__snapshots__/test_event_taxonomy_query_runner.ambrindex f5261069f3a7..816751b55d43 100644--- a/posthog/hogql_queries/ai/test/__snapshots__/test_event_taxonomy_query_runner.ambr+++ b/posthog/hogql_queries/ai/test/__snapshots__/test_event_taxonomy_query_runner.ambr@@ -19,7 +19,7 @@LIMIT 100) ARRAYJOIN (kv).1 AS key,(kv).2 AS value- WHERE and(not(match(key, '(\\$set|\\$time|\\$set_once|\\$sent_at|distinct_id|\\$ip|\\$feature\\/|^\\$feature_flags$|\\$feature_enrollment\\/|\\$feature_interaction\\/|\\$product_tour|__|survey_dismiss|survey_responded|phjs|partial_filter_chosen|changed_action|window-id|changed_event|partial_filter)')), isNotNull(value), ifNull(notEquals(value, ''), 1))+ WHERE and(not(match(key, '(\\$set|\\$time|\\$set_once|\\$sent_at|distinct_id|\\$ip|\\$feature\\/|^\\$feature_flags$|^\\$active_feature_flags$|\\$feature_enrollment\\/|\\$feature_interaction\\/|\\$product_tour|__|survey_dismiss|survey_responded|phjs|partial_filter_chosen|changed_action|window-id|changed_event|partial_filter)')), isNotNull(value), ifNull(notEquals(value, ''), 1))GROUP BY key,value)GROUP BY key@@ -99,7 +99,7 @@LIMIT 100) ARRAYJOIN (kv).1 AS key,(kv).2 AS value- WHERE and(not(match(key, '(\\$set|\\$time|\\$set_once|\\$sent_at|distinct_id|\\$ip|\\$feature\\/|^\\$feature_flags$|\\$feature_enrollment\\/|\\$feature_interaction\\/|\\$product_tour|__|survey_dismiss|survey_responded|phjs|partial_filter_chosen|changed_action|window-id|changed_event|partial_filter)')), isNotNull(value), ifNull(notEquals(value, ''), 1))+ WHERE and(not(match(key, '(\\$set|\\$time|\\$set_once|\\$sent_at|distinct_id|\\$ip|\\$feature\\/|^\\$feature_flags$|^\\$active_feature_flags$|\\$feature_enrollment\\/|\\$feature_interaction\\/|\\$product_tour|__|survey_dismiss|survey_responded|phjs|partial_filter_chosen|changed_action|window-id|changed_event|partial_filter)')), isNotNull(value), ifNull(notEquals(value, ''), 1))GROUP BY key,value)GROUP BY keydiff --git a/posthog/hogql_queries/ai/test/__snapshots__/test_event_taxonomy_query_runner.new_events_schema.ambr b/posthog/hogql_queries/ai/test/__snapshots__/test_event_taxonomy_query_runner.new_events_schema.ambrindex 8d39498ad218..98f03989e6d2 100644--- a/posthog/hogql_queries/ai/test/__snapshots__/test_event_taxonomy_query_runner.new_events_schema.ambr+++ b/posthog/hogql_queries/ai/test/__snapshots__/test_event_taxonomy_query_runner.new_events_schema.ambr@@ -11,7 +11,8 @@count() AS value_count,max(timestamp) AS latest_seenFROM- (SELECT JSONExtractKeysAndValues(JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), 'String') AS kv,+ (SELECT JSONExtractKeysAndValues(JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), 'String') AS kv,toTimeZone(events.timestamp, 'UTC') AS timestampFROM events_json AS eventsWHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, toDateTime64(minus(now64(6, 'UTC'), toIntervalDay(30)), 6, 'UTC')), equals(events.event, 'event1'))@@ -19,7 +20,7 @@LIMIT 100) ARRAYJOIN (kv).1 AS key,(kv).2 AS value- WHERE and(not(match(key, '(\\$set|\\$time|\\$set_once|\\$sent_at|distinct_id|\\$ip|\\$feature\\/|^\\$feature_flags$|\\$feature_enrollment\\/|\\$feature_interaction\\/|\\$product_tour|__|survey_dismiss|survey_responded|phjs|partial_filter_chosen|changed_action|window-id|changed_event|partial_filter)')), isNotNull(value), ifNull(notEquals(value, ''), 1))+ WHERE and(not(match(key, '(\\$set|\\$time|\\$set_once|\\$sent_at|distinct_id|\\$ip|\\$feature\\/|^\\$feature_flags$|^\\$active_feature_flags$|\\$feature_enrollment\\/|\\$feature_interaction\\/|\\$product_tour|__|survey_dismiss|survey_responded|phjs|partial_filter_chosen|changed_action|window-id|changed_event|partial_filter)')), isNotNull(value), ifNull(notEquals(value, ''), 1))GROUP BY key,value)GROUP BY key@@ -93,7 +94,8 @@count() AS value_count,max(timestamp) AS latest_seenFROM- (SELECT JSONExtractKeysAndValues(JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), 'String') AS kv,+ (SELECT JSONExtractKeysAndValues(JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), 'String') AS kv,toTimeZone(events.timestamp, 'UTC') AS timestampFROM events_json AS eventsWHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, toDateTime64(minus(now64(6, 'UTC'), toIntervalDay(30)), 6, 'UTC')), equals(events.event, '$pageview'))@@ -101,7 +103,7 @@LIMIT 100) ARRAYJOIN (kv).1 AS key,(kv).2 AS value- WHERE and(not(match(key, '(\\$set|\\$time|\\$set_once|\\$sent_at|distinct_id|\\$ip|\\$feature\\/|^\\$feature_flags$|\\$feature_enrollment\\/|\\$feature_interaction\\/|\\$product_tour|__|survey_dismiss|survey_responded|phjs|partial_filter_chosen|changed_action|window-id|changed_event|partial_filter)')), isNotNull(value), ifNull(notEquals(value, ''), 1))+ WHERE and(not(match(key, '(\\$set|\\$time|\\$set_once|\\$sent_at|distinct_id|\\$ip|\\$feature\\/|^\\$feature_flags$|^\\$active_feature_flags$|\\$feature_enrollment\\/|\\$feature_interaction\\/|\\$product_tour|__|survey_dismiss|survey_responded|phjs|partial_filter_chosen|changed_action|window-id|changed_event|partial_filter)')), isNotNull(value), ifNull(notEquals(value, ''), 1))GROUP BY key,value)GROUP BY keydiff --git a/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.new_events_schema.ambr b/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.new_events_schema.ambrindex 059bfa8bf6cf..eef08676f144 100644--- a/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.new_events_schema.ambr+++ b/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.new_events_schema.ambr@@ -22,7 +22,8 @@__ai_events_fallback.event AS event,toTimeZone(__ai_events_fallback.timestamp, 'UTC') AS timestamp,__ai_events_fallback.distinct_id AS distinct_id,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(__ai_events_fallback.properties.`$feature_flags`), mapValues(__ai_events_fallback.properties.`$feature_flags`)), if(empty(__ai_events_fallback.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), __ai_events_fallback.properties.`$feature_flags`))))]))), ','), '}')) AS properties,if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(__ai_events_fallback.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(__ai_events_fallback.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(__ai_events_fallback.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(__ai_events_fallback.properties.`$ai_trace_id`), '[]'), '{}'), toString(__ai_events_fallback.properties.`$ai_trace_id`)))) AS trace_id,if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_session_id`)), if(isNull(nullIf(toString(__ai_events_fallback.properties.`$ai_session_id`), '')), NULL, if(and(in(ascii(toString(__ai_events_fallback.properties.`$ai_session_id`)), tuple(91, 123)), in(ascii(toJSONString(__ai_events_fallback.properties.`$ai_session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(__ai_events_fallback.properties.`$ai_session_id`), '[]'), '{}'), toString(__ai_events_fallback.properties.`$ai_session_id`)))) AS session_id,if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(__ai_events_fallback.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(__ai_events_fallback.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(__ai_events_fallback.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(__ai_events_fallback.properties.`$ai_parent_id`), '[]'), '{}'), toString(__ai_events_fallback.properties.`$ai_parent_id`)))) AS parent_id,@@ -82,7 +83,8 @@__ai_events_fallback.event AS event,toTimeZone(__ai_events_fallback.timestamp, 'UTC') AS timestamp,__ai_events_fallback.distinct_id AS distinct_id,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(__ai_events_fallback.properties.`$feature_flags`), mapValues(__ai_events_fallback.properties.`$feature_flags`)), if(empty(__ai_events_fallback.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), __ai_events_fallback.properties.`$feature_flags`))))]))), ','), '}')) AS properties,if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(__ai_events_fallback.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(__ai_events_fallback.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(__ai_events_fallback.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(__ai_events_fallback.properties.`$ai_trace_id`), '[]'), '{}'), toString(__ai_events_fallback.properties.`$ai_trace_id`)))) AS trace_id,if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_session_id`)), if(isNull(nullIf(toString(__ai_events_fallback.properties.`$ai_session_id`), '')), NULL, if(and(in(ascii(toString(__ai_events_fallback.properties.`$ai_session_id`)), tuple(91, 123)), in(ascii(toJSONString(__ai_events_fallback.properties.`$ai_session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(__ai_events_fallback.properties.`$ai_session_id`), '[]'), '{}'), toString(__ai_events_fallback.properties.`$ai_session_id`)))) AS session_id,if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(__ai_events_fallback.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(__ai_events_fallback.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(__ai_events_fallback.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(__ai_events_fallback.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(__ai_events_fallback.properties.`$ai_parent_id`), '[]'), '{}'), toString(__ai_events_fallback.properties.`$ai_parent_id`)))) AS parent_id,diff --git a/posthog/hogql_queries/ai/test/__snapshots__/test_traces_query_runner.new_events_schema.ambr b/posthog/hogql_queries/ai/test/__snapshots__/test_traces_query_runner.new_events_schema.ambrindex 3b1b793ad59e..af7cff0d76d7 100644--- a/posthog/hogql_queries/ai/test/__snapshots__/test_traces_query_runner.new_events_schema.ambr+++ b/posthog/hogql_queries/ai/test/__snapshots__/test_traces_query_runner.new_events_schema.ambr@@ -14,7 +14,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -86,7 +87,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -158,7 +160,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -230,7 +233,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -302,7 +306,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -404,7 +409,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -476,7 +482,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -548,7 +555,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -620,7 +628,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -692,7 +701,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -794,7 +804,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,@@ -866,7 +877,8 @@round(sumIf(accurateCastOrNull(events.properties.`$ai_request_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS request_cost,round(sumIf(accurateCastOrNull(events.properties.`$ai_web_search_cost_usd`, 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS web_search_cost,round(sumIf(accurateCastOrNull(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(events.event, tuple('$ai_generation', '$ai_embedding'))), 10) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(events.uuid, events.event, toTimeZone(events.timestamp, 'UTC'), JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}'))), or(in(events.event, tuple('$ai_metric', '$ai_feedback')), equals(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_parent_id`)), if(isNull(nullIf(toString(events.properties.`$ai_parent_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_parent_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_parent_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_parent_id`), '[]'), '{}'), toString(events.properties.`$ai_parent_id`))))), toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_id`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_id`), '[]'), '{}'), toString(events.properties.`$ai_trace_id`)))))))))) AS events,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_input_state`)), if(isNull(nullIf(toString(events.properties.`$ai_input_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_input_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_input_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_input_state`), '[]'), '{}'), toString(events.properties.`$ai_input_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS input_state,argMinIf(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_output_state`)), if(isNull(nullIf(toString(events.properties.`$ai_output_state`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_output_state`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_output_state`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_output_state`), '[]'), '{}'), toString(events.properties.`$ai_output_state`)))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')) AS output_state,ifNull(argMinIf(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'), equals(events.event, '$ai_trace')), argMin(ifNull(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_span_name`)), if(isNull(nullIf(toString(events.properties.`$ai_span_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_span_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_span_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_span_name`), '[]'), '{}'), toString(events.properties.`$ai_span_name`)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$ai_trace_name`)), if(isNull(nullIf(toString(events.properties.`$ai_trace_name`), '')), NULL, if(and(in(ascii(toString(events.properties.`$ai_trace_name`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$ai_trace_name`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$ai_trace_name`), '[]'), '{}'), toString(events.properties.`$ai_trace_name`))))), toTimeZone(events.timestamp, 'UTC'))) AS trace_name,diff --git a/posthog/hogql_queries/ai/test/test_event_taxonomy_query_runner.py b/posthog/hogql_queries/ai/test/test_event_taxonomy_query_runner.pyindex 9754b9a28dc3..972533ecf178 100644--- a/posthog/hogql_queries/ai/test/test_event_taxonomy_query_runner.py+++ b/posthog/hogql_queries/ai/test/test_event_taxonomy_query_runner.py@@ -493,7 +493,12 @@ class TestEventTaxonomyQueryRunner(ClickhouseTestMixin, APIBaseTest):_create_event(event="event1",distinct_id="person2",- properties={"prop": "3", "$feature/dashboard": "0", "$feature_flags": {"another_flag": "true"}},+ properties={+ "prop": "3",+ "$feature/dashboard": "0",+ "$feature_flags": {"another_flag": "true"},+ "$active_feature_flags": ["another_flag"],+ },team=self.team,)diff --git a/posthog/hogql_queries/test/__snapshots__/test_events_query_runner.new_events_schema.ambr b/posthog/hogql_queries/test/__snapshots__/test_events_query_runner.new_events_schema.ambrindex fa8c4fc1145a..18a80a7548bf 100644--- a/posthog/hogql_queries/test/__snapshots__/test_events_query_runner.new_events_schema.ambr+++ b/posthog/hogql_queries/test/__snapshots__/test_events_query_runner.new_events_schema.ambr@@ -1,7 +1,8 @@# serializer version: 1# name: TestEventsQueryRunner.test_absolute_date_range[new_events_schema]'''- SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -248,7 +249,8 @@# ---# name: TestEventsQueryRunner.test_cursor_with_star_select[new_events_schema]'''- SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -275,7 +277,8 @@# ---# name: TestEventsQueryRunner.test_element_chain_property_filter[new_events_schema]'''- SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -302,7 +305,8 @@# ---# name: TestEventsQueryRunner.test_presorted_events_table[new_events_schema]'''- SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -329,7 +333,8 @@# ---# name: TestEventsQueryRunner.test_presorted_events_table_multiple_order_by[new_events_schema]'''- SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -357,7 +362,8 @@# ---# name: TestEventsQueryRunner.test_presorted_events_table_order_by_event[new_events_schema]'''- SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -384,7 +390,8 @@# ---# name: TestEventsQueryRunner.test_presorted_events_table_order_by_property[new_events_schema]'''- SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')), toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC'), events.person_mode) AS `tuple(uuid, event, properties, toTimeZone(timestamp, 'UTC'), team_id, distinct_id, elements_chain, toTimeZone(created_at, 'UTC'), person_mode)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuiddiff --git a/posthog/hogql_queries/test/__snapshots__/test_sessions_timeline_query_runner.new_events_schema.ambr b/posthog/hogql_queries/test/__snapshots__/test_sessions_timeline_query_runner.new_events_schema.ambrindex ea46227b98af..43bd49f57c50 100644--- a/posthog/hogql_queries/test/__snapshots__/test_sessions_timeline_query_runner.new_events_schema.ambr+++ b/posthog/hogql_queries/test/__snapshots__/test_sessions_timeline_query_runner.new_events_schema.ambr@@ -28,7 +28,8 @@if(not(empty(event_source__override.distinct_id)), event_source__override.person_id, event_source.person_id) AS person_id,toTimeZone(event_source.timestamp, 'UTC') AS timestamp,event_source.event AS event,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(event_source.properties.`$feature_flags`), mapValues(event_source.properties.`$feature_flags`)), if(empty(event_source.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), event_source.properties.`$feature_flags`))))]))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -101,7 +102,8 @@if(not(empty(event_source__override.distinct_id)), event_source__override.person_id, event_source.person_id) AS person_id,toTimeZone(event_source.timestamp, 'UTC') AS timestamp,event_source.event AS event,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(event_source.properties.`$feature_flags`), mapValues(event_source.properties.`$feature_flags`)), if(empty(event_source.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), event_source.properties.`$feature_flags`))))]))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -174,7 +176,8 @@if(not(empty(event_source__override.distinct_id)), event_source__override.person_id, event_source.person_id) AS person_id,toTimeZone(event_source.timestamp, 'UTC') AS timestamp,event_source.event AS event,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(event_source.properties.`$feature_flags`), mapValues(event_source.properties.`$feature_flags`)), if(empty(event_source.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), event_source.properties.`$feature_flags`))))]))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -247,7 +250,8 @@if(not(empty(event_source__override.distinct_id)), event_source__override.person_id, event_source.person_id) AS person_id,toTimeZone(event_source.timestamp, 'UTC') AS timestamp,event_source.event AS event,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(event_source.properties.`$feature_flags`), mapValues(event_source.properties.`$feature_flags`)), if(empty(event_source.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), event_source.properties.`$feature_flags`))))]))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -320,7 +324,8 @@if(not(empty(event_source__override.distinct_id)), event_source__override.person_id, event_source.person_id) AS person_id,toTimeZone(event_source.timestamp, 'UTC') AS timestamp,event_source.event AS event,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(event_source.properties.`$feature_flags`), mapValues(event_source.properties.`$feature_flags`)), if(empty(event_source.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), event_source.properties.`$feature_flags`))))]))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -393,7 +398,8 @@if(not(empty(event_source__override.distinct_id)), event_source__override.person_id, event_source.person_id) AS person_id,toTimeZone(event_source.timestamp, 'UTC') AS timestamp,event_source.event AS event,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(event_source.properties.`$feature_flags`), mapValues(event_source.properties.`$feature_flags`)), if(empty(event_source.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), event_source.properties.`$feature_flags`))))]))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -466,7 +472,8 @@if(not(empty(event_source__override.distinct_id)), event_source__override.person_id, event_source.person_id) AS person_id,toTimeZone(event_source.timestamp, 'UTC') AS timestamp,event_source.event AS event,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(event_source.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(event_source.properties.`$feature_flags`), mapValues(event_source.properties.`$feature_flags`)), if(empty(event_source.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), event_source.properties.`$feature_flags`))))]))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,diff --git a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_by_top_level_event_property.new_events_schema.ambr b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_by_top_level_event_property.new_events_schema.ambrindex 266e2abc53e8..dfe0fd19918b 100644--- a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_by_top_level_event_property.new_events_schema.ambr+++ b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_by_top_level_event_property.new_events_schema.ambr@@ -124,7 +124,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,(SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL), 'true'), 0)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), multiIf(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$true'), 0), 'true', events.properties.`$feature_flags`['target-flag']), NULL), 'true'), 0)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))HAVING 1LIMIT 1000000)))@@ -174,7 +174,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,(SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL), 'false'), 0)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), multiIf(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$true'), 0), 'true', events.properties.`$feature_flags`['target-flag']), NULL), 'false'), 0)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))HAVING 1LIMIT 1000000)))@@ -224,7 +224,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,(SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL))), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), multiIf(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$true'), 0), 'true', events.properties.`$feature_flags`['target-flag']), NULL))), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))HAVING 1LIMIT 1000000)))@@ -274,7 +274,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalNotIn(s.session_id,(SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), multiIf(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$true'), 0), 'true', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))LIMIT 1000000)))GROUP BY s.session_id@@ -470,7 +470,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,(SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL), 'true'), 0), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), multiIf(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$true'), 0), 'true', events.properties.`$feature_flags`['target-flag']), NULL), 'true'), 0), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))HAVING 1LIMIT 1000000)))@@ -520,7 +520,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,(SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL), 'false'), 0), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), multiIf(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$true'), 0), 'true', events.properties.`$feature_flags`['target-flag']), NULL), 'false'), 0), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))HAVING 1LIMIT 1000000)))@@ -570,7 +570,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,(SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), multiIf(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$true'), 0), 'true', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))HAVING 1LIMIT 1000000)))@@ -620,7 +620,7 @@WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalNotIn(s.session_id,(SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))), lessOrEquals(events.timestamp, toDateTime64(now64(6, 'UTC'), 6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), multiIf(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$true'), 0), 'true', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$session_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$session_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))LIMIT 1000000)))GROUP BY s.session_iddiff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64index 2abcf3b62b8e..c7ae68068ad0 100755Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 differdiff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64index e7eb1fb54e0f..bc7755669056 100755Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 differdiff --git a/products/batch_exports/backend/hogql_source.py b/products/batch_exports/backend/hogql_source.pyindex 7f501eb5ee48..41c20939c2b5 100644--- a/products/batch_exports/backend/hogql_source.py+++ b/products/batch_exports/backend/hogql_source.py@@ -13,7 +13,6 @@ from pydantic import ValidationErrorfrom posthog.schema import HogQLQueryModifiersfrom posthog.hogql import ast-from posthog.hogql.constants import FEATURE_FLAG_FALSE_VARIANT_SENTINELfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.database import Databasefrom posthog.hogql.errors import ExposedHogQLError, QueryError@@ -22,7 +21,8 @@ from posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.placeholders import find_placeholders, replace_placeholdersfrom posthog.hogql.printer import prepare_ast_for_printing, print_prepared_ast-from posthog.hogql.visitor import CloningVisitor, clone_expr+from posthog.hogql.transforms.clickhouse_property_resolution import feature_flag_variant_read+from posthog.hogql.visitor import CloningVisitorfrom posthog.clickhouse.events_json import UNPARSEABLE_PROPERTIES_KEY@@ -257,21 +257,14 @@ def native_event_property_chain(property_chain: list[str | int]) -> list[str | idef native_feature_flag_read(field: ast.Field, property_chain: list[str | int]) -> ast.Expr:- """A native `$feature_flags.<key>` read with the `$false` sentinel mapped back to the variant name "false".+ """A native `$feature_flags.<key>` read with the cleaner sentinels mapped back to their variant names.The hidden alias carries the column name the resolver would give the bare field, so an un-aliased select columnstill serializes to a stable name instead of the parameterized expression."""if len(property_chain) != 2 or property_chain[0] != "$feature_flags":return field- mapped = ast.Call(- name="if",- args=[- ast.Call(name="equals", args=[clone_expr(field), ast.Constant(value=FEATURE_FLAG_FALSE_VARIANT_SENTINEL)]),- ast.Constant(value="false"),- field,- ],- )+ mapped = feature_flag_variant_read(field)return ast.Alias(alias="__".join(str(part) for part in property_chain), expr=mapped, hidden=True)diff --git a/products/batch_exports/backend/tests/temporal/test_filters.py b/products/batch_exports/backend/tests/temporal/test_filters.pyindex 1674840995ea..d7d5d7921b93 100644--- a/products/batch_exports/backend/tests/temporal/test_filters.py+++ b/products/batch_exports/backend/tests/temporal/test_filters.py@@ -139,24 +139,28 @@ def test_compose_filters_clause_reads_flags_from_the_native_map(ateam):return f"""replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_{flag_key})s, %(hogql_val_{flag_name})s), ''), 'null'), '^"|"$', '')"""assert result_clause == (- f"""and(ifNull(equals(if(ifNull(equals({map_read(0, 1)}, %(hogql_val_2)s), 0), %(hogql_val_3)s, {map_read(4, 5)}), %(hogql_val_6)s), 0), """- f"""ifNull(equals(if(ifNull(equals({map_read(7, 8)}, %(hogql_val_9)s), 0), %(hogql_val_10)s, {map_read(11, 12)}), %(hogql_val_13)s), 0))"""+ f"""and(ifNull(equals(multiIf(ifNull(equals({map_read(0, 1)}, %(hogql_val_2)s), 0), 'false', ifNull(equals({map_read(3, 4)}, %(hogql_val_5)s), 0), 'true', {map_read(6, 7)}), %(hogql_val_8)s), 0), """+ f"""ifNull(equals(multiIf(ifNull(equals({map_read(9, 10)}, %(hogql_val_11)s), 0), 'false', ifNull(equals({map_read(12, 13)}, %(hogql_val_14)s), 0), 'true', {map_read(15, 16)}), %(hogql_val_17)s), 0))""")assert result_values == {"hogql_val_0": "$feature_flags","hogql_val_1": "some-feature","hogql_val_2": "$false",- "hogql_val_3": "false",- "hogql_val_4": "$feature_flags",- "hogql_val_5": "some-feature",- "hogql_val_6": "true",- "hogql_val_7": "$feature_flags",- "hogql_val_8": "other-feature",- "hogql_val_9": "$false",- "hogql_val_10": "false",- "hogql_val_11": "$feature_flags",- "hogql_val_12": "other-feature",- "hogql_val_13": "control",+ "hogql_val_3": "$feature_flags",+ "hogql_val_4": "some-feature",+ "hogql_val_5": "$true",+ "hogql_val_6": "$feature_flags",+ "hogql_val_7": "some-feature",+ "hogql_val_8": "true",+ "hogql_val_9": "$feature_flags",+ "hogql_val_10": "other-feature",+ "hogql_val_11": "$false",+ "hogql_val_12": "$feature_flags",+ "hogql_val_13": "other-feature",+ "hogql_val_14": "$true",+ "hogql_val_15": "$feature_flags",+ "hogql_val_16": "other-feature",+ "hogql_val_17": "control",}diff --git a/products/batch_exports/backend/tests/temporal/test_record_batch_model.py b/products/batch_exports/backend/tests/temporal/test_record_batch_model.pyindex 7c6744ac4f45..91f0bd7c8849 100644--- a/products/batch_exports/backend/tests/temporal/test_record_batch_model.py+++ b/products/batch_exports/backend/tests/temporal/test_record_batch_model.py@@ -745,7 +745,7 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables("$browser": "Firefox","amount": 2.5,"person": {"properties": "event value"},- "$feature_flags": {"named-false": "$false", "some-feature": "true"},+ "$feature_flags": {"named-false": "$false", "named-true": "$true", "some-feature": "true"},},"person_properties": {"email": "[email protected]"},"temporary_properties": {"$set": {"email": "[email protected]"}},@@ -755,10 +755,10 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables(+ "\n".join(json.dumps(value) for value in [row, row, {**row, "team_id": ateam.pk + 1}]))schema: BatchExportSchema = {- "hogql_query": "SELECT e.properties.$browser AS browser, e.properties.amount AS amount, e.person.properties.email AS email, e.properties.person.properties AS nested, e.properties.`$feature/some-feature` AS flag, e.properties.`$feature/named-false` AS named_false FROM events AS e",+ "hogql_query": "SELECT e.properties.$browser AS browser, e.properties.amount AS amount, e.person.properties.email AS email, e.properties.person.properties AS nested, e.properties.`$feature/some-feature` AS flag, e.properties.`$feature/named-false` AS named_false, e.properties.`$feature/named-true` AS named_true FROM events AS e","fields": [{"expression": "events.mat_removed_column", "alias": alias}- for alias in ("browser", "amount", "email", "nested", "flag", "named_false")+ for alias in ("browser", "amount", "email", "nested", "flag", "named_false", "named_true")],"values": {"unused_old_parameter": "stale"},}@@ -798,6 +798,7 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables(assert rows[0]["nested"] == "event value"assert rows[0]["flag"] == "true"assert rows[0]["named_false"] == "false"+ assert rows[0]["named_true"] == "true"assert json.loads(rows[0]["properties"])["$browser"] == "Firefox"assert json.loads(rows[0]["person_properties"])["email"] == "[email protected]"assert json.loads(rows[0]["set"]) == {"email": "[email protected]"}diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.new_events_schema.ambrindex 190939e208ca..f5cba123d9fa 100644--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.new_events_schema.ambr+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.new_events_schema.ambr@@ -794,7 +794,7 @@'''WITH exposures AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,- if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,@@ -807,7 +807,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, 'purchase'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(and(in(ascii(toString(events.properties.plan)), tuple(91, 123)), in(ascii(toJSONString(events.properties.plan)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan)))), 'premium'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, 'purchase'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(and(in(ascii(toString(events.properties.plan)), tuple(91, 123)), in(ascii(toJSONString(events.properties.plan)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan)))), 'premium'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))GROUP BY entity_id),metric_events AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,@@ -857,7 +857,7 @@'''WITH exposures AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,- if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,@@ -870,7 +870,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$pageview'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(and(in(ascii(toString(events.properties.plan)), tuple(91, 123)), in(ascii(toJSONString(events.properties.plan)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan)))), 'free'), 1)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$pageview'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(and(in(ascii(toString(events.properties.plan)), tuple(91, 123)), in(ascii(toJSONString(events.properties.plan)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan)))), 'free'), 1)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))GROUP BY entity_id),metric_events AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,@@ -983,7 +983,7 @@'''WITH exposures AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,- if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,@@ -996,7 +996,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), equals(events.event, '$pageview'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), equals(events.event, '$pageview'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))GROUP BY entity_id),metric_events AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,@@ -1563,7 +1563,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(and(in(ascii(toString(events.properties.`$feature_flag`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$feature_flag`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`)))), 'test-experiment'), 0)), in(if(has(events.properties.`$feature_flags`, 'flag_doesnt_exist'), if(ifNull(equals(events.properties.`$feature_flags`['flag_doesnt_exist'], '$false'), 0), 'false', events.properties.`$feature_flags`['flag_doesnt_exist']), NULL), tuple('test', 'control')), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(and(in(ascii(toString(events.properties.`$feature_flag_response`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$feature_flag_response`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(and(in(ascii(toString(events.properties.`$feature_flag`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$feature_flag`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`)))), 'test-experiment'), 0)), in(if(has(events.properties.`$feature_flags`, 'flag_doesnt_exist'), multiIf(ifNull(equals(events.properties.`$feature_flags`['flag_doesnt_exist'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['flag_doesnt_exist'], '$true'), 0), 'true', events.properties.`$feature_flags`['flag_doesnt_exist']), NULL), tuple('test', 'control')), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(and(in(ascii(toString(events.properties.`$feature_flag_response`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$feature_flag_response`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))), ['control', 'test']))GROUP BY entity_id),metric_events AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_data_warehouse_metric.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_data_warehouse_metric.new_events_schema.ambrindex 00b112dbbe4d..75212c5aa06d 100644--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_data_warehouse_metric.new_events_schema.ambr+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_data_warehouse_metric.new_events_schema.ambr@@ -910,7 +910,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(and(in(ascii(toString(events.properties.`$feature_flag`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$feature_flag`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`)))), 'test-experiment'), 0)), in(if(has(events.properties.`$feature_flags`, 'flag_doesnt_exist'), if(ifNull(equals(events.properties.`$feature_flags`['flag_doesnt_exist'], '$false'), 0), 'false', events.properties.`$feature_flags`['flag_doesnt_exist']), NULL), tuple('test', 'control')), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(and(in(ascii(toString(events.properties.`$feature_flag_response`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$feature_flag_response`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(and(in(ascii(toString(events.properties.`$feature_flag`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$feature_flag`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`)))), 'test-experiment'), 0)), in(if(has(events.properties.`$feature_flags`, 'flag_doesnt_exist'), multiIf(ifNull(equals(events.properties.`$feature_flags`['flag_doesnt_exist'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['flag_doesnt_exist'], '$true'), 0), 'true', events.properties.`$feature_flags`['flag_doesnt_exist']), NULL), tuple('test', 'control')), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(and(in(ascii(toString(events.properties.`$feature_flag_response`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$feature_flag_response`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))), ['control', 'test']))GROUP BY entity_id),metric_events AS(SELECT posthog_test_usage.userid AS entity_id,diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_experiment_exposures_query_runner.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_experiment_exposures_query_runner.new_events_schema.ambrindex 10fce88da1c8..870258448e74 100644--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_experiment_exposures_query_runner.new_events_schema.ambr+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_experiment_exposures_query_runner.new_events_schema.ambr@@ -230,7 +230,7 @@'''WITH first_exposures AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,- if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS dayFROM events_json AS eventsLEFT OUTER JOIN@@ -240,7 +240,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, 'purchase'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(and(in(ascii(toString(events.properties.plan)), tuple(91, 123)), in(ascii(toJSONString(events.properties.plan)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan)))), 'premium'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, 'purchase'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(and(in(ascii(toString(events.properties.plan)), tuple(91, 123)), in(ascii(toJSONString(events.properties.plan)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan)))), 'premium'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))GROUP BY entity_id)SELECT first_exposures.day AS day,first_exposures.variant AS variant,@@ -269,7 +269,7 @@'''WITH first_exposures AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,- if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS dayFROM events_json AS eventsLEFT OUTER JOIN@@ -279,7 +279,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$pageview'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(and(in(ascii(toString(events.properties.plan)), tuple(91, 123)), in(ascii(toJSONString(events.properties.plan)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan)))), 'free'), 1)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$pageview'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(and(in(ascii(toString(events.properties.plan)), tuple(91, 123)), in(ascii(toJSONString(events.properties.plan)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan)))), 'free'), 1)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))GROUP BY entity_id)SELECT first_exposures.day AS day,first_exposures.variant AS variant,diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_retention_metric.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_retention_metric.new_events_schema.ambrindex 82aeb640a99e..ffdff137acd5 100644--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_retention_metric.new_events_schema.ambr+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_retention_metric.new_events_schema.ambr@@ -248,7 +248,7 @@'''WITH exposures AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,- if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,@@ -261,7 +261,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))GROUP BY entity_id),start_events AS(SELECT exposures.entity_id AS entity_id,@@ -402,7 +402,7 @@'''WITH exposures AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,- if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,@@ -415,7 +415,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))GROUP BY entity_id),start_events AS(SELECT exposures.entity_id AS entity_id,@@ -555,7 +555,7 @@'''WITH exposures AS(SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,- if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,@@ -568,7 +568,7 @@WHERE equals(person_distinct_id_overrides.team_id, 99999)GROUP BY person_distinct_id_overrides.distinct_idHAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)- WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(and(in(ascii(toString(events.person_properties.email)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.email)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), multiIf(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$true'), 0), 'true', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))GROUP BY entity_id),start_events AS(SELECT exposures.entity_id AS entity_id,diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.pyindex 6a7fead922cc..fe052be24e92 100644--- a/products/feature_flags/backend/api/feature_flag.py+++ b/products/feature_flags/backend/api/feature_flag.py@@ -38,7 +38,7 @@ from rest_framework.response import Responsefrom posthog.schema import ProductKey-from posthog.hogql.constants import FEATURE_FLAG_FALSE_VARIANT_SENTINEL+from posthog.hogql.constants import FEATURE_FLAG_VARIANT_SENTINELSfrom posthog.api.cohort import CohortSerializerfrom posthog.api.documentation import FeatureFlagFiltersSchemaSerializer, extend_schema@@ -927,20 +927,28 @@ class EvaluationContextSerializerMixin(serializers.Serializer):_RUST_PROPERTY_TYPES: frozenset[str] = frozenset({*FEATURE_FLAG_PROPERTY_TYPES, "person_metadata"})-def _uses_reserved_variant_key(filters: dict) -> bool:- """Whether a `multivariate.variants[].key` is the sentinel the ingest cleaner stores a variant named "false" under.+def _reserved_variant_key(filters: dict) -> str | None:+ """The first `multivariate.variants[].key` that is a sentinel the ingest cleaner stores a variant named "false" or+ "true" under, if any.Checked on the raw request shape ahead of every validation tier, so the rejection does not depend on the #50084rollout switch."""multivariate = filters.get("multivariate")if not isinstance(multivariate, dict):- return False+ return Nonevariants = multivariate.get("variants")if not isinstance(variants, list):- return False- return any(- isinstance(variant, dict) and variant.get("key") == FEATURE_FLAG_FALSE_VARIANT_SENTINEL for variant in variants+ return None+ return next(+ (+ variant["key"]+ for variant in variants+ if isinstance(variant, dict)+ and isinstance(variant.get("key"), str)+ and variant["key"] in FEATURE_FLAG_VARIANT_SENTINELS+ ),+ None,)@@ -1962,9 +1970,10 @@ class FeatureFlagSerializer(raise self._v2_validation_error(exc) from excdef _validate_filters_inner(self, filters, operation: str):- if _uses_reserved_variant_key(filters):+ reserved_variant_key = _reserved_variant_key(filters)+ if reserved_variant_key is not None:raise serializers.ValidationError(- f"The variant key {FEATURE_FLAG_FALSE_VARIANT_SENTINEL} is reserved. Choose another key.",+ f"The variant key {reserved_variant_key} is reserved. Choose another key.",code="reserved_variant_key",)diff --git a/products/feature_flags/backend/api/test/test_feature_flag_config_v2_updates.py b/products/feature_flags/backend/api/test/test_feature_flag_config_v2_updates.pyindex e9e0822af836..cf04b000895c 100644--- a/products/feature_flags/backend/api/test/test_feature_flag_config_v2_updates.py+++ b/products/feature_flags/backend/api/test/test_feature_flag_config_v2_updates.py@@ -730,18 +730,18 @@ def string_config(value: str, default: str | None, rule_id: str | None = RULE_A)RESERVED_STRING_VALUES = [- ("rule_value", "$false", None, "filters.rules[0].value: Must be a non-empty string other than $false."),+ ("rule_value", "$false", None, "filters.rules[0].value: Must be a non-empty string other than $false or $true."),("default_value","compact",- "$false",- "filters.default_value: Must be a non-empty string other than $false, or null.",+ "$true",+ "filters.default_value: Must be a non-empty string other than $false or $true, or null.",),]class TestWriterOnlyRules(AdmittedV2TestCase):- """The writer reserves `$false` as a string value; the caches accept it, so a stored one stays replaceable."""+ """The writer reserves `$false` and `$true` as string values; the caches accept them, so a stored one stays replaceable."""@parameterized.expand(RESERVED_STRING_VALUES)def test_create_rejects_a_reserved_value(self, _name: str, value: str, default: str | None, detail: str) -> None:diff --git a/products/feature_flags/backend/facade/config_writes.py b/products/feature_flags/backend/facade/config_writes.pyindex ae5dc1df4e29..86d2a066a282 100644--- a/products/feature_flags/backend/facade/config_writes.py+++ b/products/feature_flags/backend/facade/config_writes.py@@ -32,7 +32,7 @@ from django.conf import settingsimport structlog-from posthog.hogql.constants import FEATURE_FLAG_FALSE_VARIANT_SENTINEL+from posthog.hogql.constants import FEATURE_FLAG_VARIANT_SENTINELSfrom posthog.dataclasses import frozenfrom posthog.ph_client import feature_enabled_or_false@@ -280,14 +280,14 @@ def check_writer_rules(document: Mapping[str, Any], *, stored: Mapping[str, Any]def _reserved_string_values(document: Mapping[str, Any], _stored: Mapping[str, Any]) -> Iterator[ConfigError]:- # A string value is served as the variant, and `$false` is the event-storage sentinel that v1 reserves as a variant key.+ # A string value is served as the variant, and the event-storage sentinels are the keys v1 reserves as variant keys.if document["return_type"] != "string":return- detail = f"Must be a non-empty string other than {FEATURE_FLAG_FALSE_VARIANT_SENTINEL}"- if document["default_value"] == FEATURE_FLAG_FALSE_VARIANT_SENTINEL:+ detail = f"Must be a non-empty string other than {' or '.join(FEATURE_FLAG_VARIANT_SENTINELS)}"+ if document["default_value"] in FEATURE_FLAG_VARIANT_SENTINELS:yield ConfigError(code="invalid", detail=f"{detail}, or null.", attr="filters.default_value")for index, rule in enumerate(document["rules"]):- if rule.get("value") == FEATURE_FLAG_FALSE_VARIANT_SENTINEL:+ if rule.get("value") in FEATURE_FLAG_VARIANT_SENTINELS:yield ConfigError(code="invalid", detail=f"{detail}.", attr=f"filters.rules[{index}].value")diff --git a/products/feature_flags/backend/test/test_facade_config_validation.py b/products/feature_flags/backend/test/test_facade_config_validation.pyindex 8a50aa45a4c6..5d6a2a192f76 100644--- a/products/feature_flags/backend/test/test_facade_config_validation.py+++ b/products/feature_flags/backend/test/test_facade_config_validation.py@@ -180,8 +180,8 @@ VALID_DOCUMENTS: list[tuple[str, dict[str, Any]]] = [config(targeted(value="compact"), rollout(value="wide"), return_type="string", default_value="standard"),),("string_null_default", config(targeted(value="compact"), return_type="string", default_value=None)),- # Readers accept the event-storage sentinel; only the writer reserves it.- ("string_false_sentinel", config(targeted(value="$false"), return_type="string", default_value="$false")),+ # Readers accept the event-storage sentinels; only the writer reserves them.+ ("string_sentinels", config(targeted(value="$false"), return_type="string", default_value="$true")),("number_values", config(targeted(value=1.25), rollout(value=-40), return_type="number", default_value=0)),("number_safe_integer_bounds",diff --git a/products/feature_flags/backend/test/test_filters_validation.py b/products/feature_flags/backend/test/test_filters_validation.pyindex ac07f37e91f5..9880d7a64eac 100644--- a/products/feature_flags/backend/test/test_filters_validation.py+++ b/products/feature_flags/backend/test/test_filters_validation.py@@ -6,7 +6,7 @@ from parameterized import parameterizedfrom rest_framework import serializersfrom rest_framework.exceptions import ErrorDetail-from posthog.hogql.constants import FEATURE_FLAG_FALSE_VARIANT_SENTINEL+from posthog.hogql.constants import FEATURE_FLAG_VARIANT_SENTINELSfrom products.feature_flags.backend.api.feature_flag import FeatureFlagSerializer, _reject_serde_unsafe_filtersfrom products.feature_flags.backend.api.filters_schema import FEATURE_FLAG_PROPERTY_TYPES, FeatureFlagFiltersSerializer@@ -474,14 +474,20 @@ class TestRejectSerdeUnsafeFilters(SimpleTestCase):class TestReservedVariantKey(SimpleTestCase):- @parameterized.expand([("log_only", set()), ("full", {"*"})])- def test_false_sentinel_is_rejected_as_a_variant_key(self, _name: str, enforced_rules: set[str]) -> None:+ @parameterized.expand(+ [+ (f"{sentinel}_{name}", sentinel, enforced_rules)+ for sentinel in FEATURE_FLAG_VARIANT_SENTINELS+ for name, enforced_rules in (("log_only", set()), ("full", {"*"}))+ ]+ )+ def test_sentinel_is_rejected_as_a_variant_key(self, _name: str, sentinel: str, enforced_rules: set[str]) -> None:filters = {"groups": [{"properties": [], "rollout_percentage": 100}],"multivariate": {"variants": [{"key": "control", "rollout_percentage": 50},- {"key": FEATURE_FLAG_FALSE_VARIANT_SENTINEL, "rollout_percentage": 50},+ {"key": sentinel, "rollout_percentage": 50},]},}@@ -489,3 +495,15 @@ class TestReservedVariantKey(SimpleTestCase):serializer = FeatureFlagSerializer(data={"filters": filters}, partial=True)assert not serializer.is_valid()assert serializer.errors["filters"][0].code == "reserved_variant_key"+ assert sentinel in str(serializer.errors["filters"][0])++ @parameterized.expand([("list", ["$true"]), ("dict", {"key": "$true"})])+ def test_non_string_key_is_a_validation_error(self, _name: str, key: Any) -> None:+ filters = {+ "groups": [{"properties": [], "rollout_percentage": 100}],+ "multivariate": {"variants": [{"key": key, "rollout_percentage": 100}]},+ }+ with override_settings(FEATURE_FLAG_FILTERS_ENFORCED_RULES={"*"}):+ serializer = FeatureFlagSerializer(data={"filters": filters}, partial=True)+ assert not serializer.is_valid()+ assert serializer.errors["filters"][0].code == "structural.multivariate.variants[].key.invalid"diff --git a/products/product_analytics/backend/hogql_queries/funnels/test/__snapshots__/test_funnel_correlation.new_events_schema.ambr b/products/product_analytics/backend/hogql_queries/funnels/test/__snapshots__/test_funnel_correlation.new_events_schema.ambrindex 25f796134134..a10ac43da21f 100644--- a/products/product_analytics/backend/hogql_queries/funnels/test/__snapshots__/test_funnel_correlation.new_events_schema.ambr+++ b/products/product_analytics/backend/hogql_queries/funnels/test/__snapshots__/test_funnel_correlation.new_events_schema.ambr@@ -484,7 +484,8 @@ORDER BY aggregation_target ASC SETTINGS join_algorithm='auto'),filtered_events AS(SELECT events.event AS event,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]'+ and kv.1 != '$feature_flags', JSONExtractKeysAndValuesRaw(toJSONString(events.properties)))), arrayConcat(arrayMap((key, value) -> concat(toJSONString(concat('$feature/', key)), ':', if(value in ('true', 'false'), value, toJSONString(multiIf(value = '$false', 'false', value = '$true', 'true', value)))), mapKeys(events.properties.`$feature_flags`), mapValues(events.properties.`$feature_flags`)), if(empty(events.properties.`$feature_flags`), [], [concat('"$active_feature_flags":', toJSONString(mapKeys(mapFilter((key, value) -> value not in ('', 'false'), events.properties.`$feature_flags`))))]))), ','), '}')) AS properties,toTimeZone(events.timestamp, 'UTC') AS timestamp,if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id,events.properties.`$group_1` AS `$group_1`,