: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): add moved keys back to events_json documents while retained (#109486)
Sandy Spicer1afafb8898f1
diff --git a/docs/internal/clickhouse-utility-udfs.md b/docs/internal/clickhouse-utility-udfs.mdindex c9e6566b61d6..43b1ca946143 100644--- a/docs/internal/clickhouse-utility-udfs.md+++ b/docs/internal/clickhouse-utility-udfs.md@@ -103,7 +103,7 @@ Documents exceeding the shared depth limit produce `{}` in the temporary output;Native events retain `temporary_properties` for 60 days after insertion, including historical events; TTL merges clear the column asynchronously.On native events, HogQL reads a property in this allowlist from `temporary_properties`: property access such as `properties.$set.email`, filters, `JSONHas`, `JSONLength`, `JSONType`, the `JSONExtract*` functions with the property as their first key, and `JSON_VALUE` with the property as the first member of its path.These functions reject a first key computed per row, because such a key can name a moved property.-Whole-document reads of `properties`, such as `SELECT properties`, do not include these properties, because rebuilding them would serialize `temporary_properties` on every row read.+A whole-document read of `properties`, such as `SELECT properties`, `toString(properties)`, a batch export or the events API, adds these properties back from `temporary_properties`, so the document matches what the SDK sent for 60 days after insertion. After that the properties are absent from every read.`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.diff --git a/posthog/api/test/__snapshots__/test_properties_timeline.new_events_schema.ambr b/posthog/api/test/__snapshots__/test_properties_timeline.new_events_schema.ambrindex 776e9c16a853..42fb1f13aa02 100644--- a/posthog/api/test/__snapshots__/test_properties_timeline.new_events_schema.ambr+++ b/posthog/api/test/__snapshots__/test_properties_timeline.new_events_schema.ambr@@ -13,7 +13,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -54,7 +54,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -95,7 +95,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,'' AS relevant_property_values,lagInFrame('') OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -136,7 +136,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -177,7 +177,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -218,7 +218,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -259,7 +259,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -300,7 +300,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -341,7 +341,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -382,7 +382,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -423,7 +423,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^foo)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^foo)), if(isNull(nullIf(toString(events.person_properties.foo), '')), NULL, if(and(in(ascii(toString(events.person_properties.foo)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.foo)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.foo), '[]'), '{}'), toString(events.person_properties.foo)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^foo)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^foo)), if(isNull(nullIf(toString(events.person_properties.foo), '')), NULL, if(and(in(ascii(toString(events.person_properties.foo)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.foo)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.foo), '[]'), '{}'), toString(events.person_properties.foo))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,@@ -464,7 +464,7 @@ORDER BY timestamp ASC ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS end_event_numberFROM(SELECT toTimeZone(events.timestamp, 'UTC') AS timestamp,- JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS properties,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS properties,toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^foo)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^foo)), if(isNull(nullIf(toString(events.person_properties.foo), '')), NULL, if(and(in(ascii(toString(events.person_properties.foo)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.foo)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.foo), '[]'), '{}'), toString(events.person_properties.foo)))))) AS relevant_property_values,lagInFrame(toString(tuple(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^bar)), if(isNull(nullIf(toString(events.person_properties.bar), '')), NULL, if(and(in(ascii(toString(events.person_properties.bar)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.bar)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.bar), '[]'), '{}'), toString(events.person_properties.bar)))), if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^foo)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^foo)), if(isNull(nullIf(toString(events.person_properties.foo), '')), NULL, if(and(in(ascii(toString(events.person_properties.foo)), tuple(91, 123)), in(ascii(toJSONString(events.person_properties.foo)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.person_properties.foo), '[]'), '{}'), toString(events.person_properties.foo))))))) OVER (ORDER BY toTimeZone(events.timestamp, 'UTC') ASC ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_relevant_property_values,diff --git a/posthog/api/test/__snapshots__/test_query.new_events_schema.ambr b/posthog/api/test/__snapshots__/test_query.new_events_schema.ambrindex 9e0f2d6bb5fe..f4ebf67eebc2 100644--- a/posthog/api/test/__snapshots__/test_query.new_events_schema.ambr+++ b/posthog/api/test/__snapshots__/test_query.new_events_schema.ambr@@ -497,19 +497,22 @@# name: TestQuery.test_select_hogql_expressions[new_events_schema.1]'''/* user_id:0 request:_snapshot_ */- 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)`,+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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)`,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(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) ASC+ ORDER BY tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_properties)))), ','), '}')), 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(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) ASC+ ORDER BY tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_properties)))), ','), '}')), 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/printer/clickhouse.py b/posthog/hogql/printer/clickhouse.pyindex 7d61c6feaa53..b130cafbc5f0 100644--- a/posthog/hogql/printer/clickhouse.py+++ b/posthog/hogql/printer/clickhouse.py@@ -7,12 +7,7 @@ from django.conf import settings as django_settingsfrom posthog.hogql import astfrom posthog.hogql.ast import AST, Constant, StringType-from posthog.hogql.constants import (- FEATURE_FLAG_PROPERTY_PREFIX,- FEATURE_FLAG_VARIANT_SENTINELS,- INACTIVE_FEATURE_FLAG_VALUES,- HogQLDialect,-)+from posthog.hogql.constants import FEATURE_FLAG_PROPERTY_PREFIX, HogQLDialectfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.direct_sql_table import DirectSQLTablefrom posthog.hogql.database.models import (@@ -45,6 +40,7 @@ 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_type+from posthog.hogql.printer.events_json_document import event_document_sql, json_member_pairs_sqlfrom posthog.hogql.printer.hogql import HogQLPrinterfrom posthog.hogql.restricted_properties import (RESTRICTABLE_JSON_BLOB_COLUMNS,@@ -589,45 +585,22 @@ class ClickHousePrinter(BasePrinter):if not isinstance(type.table_type.resolve_database_table(self.context), EVENTS_TABLE_TYPES):return None- 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}, ','), '}}')"+ prefix = field_sql.removesuffix(self._print_identifier("properties"))+ temporary_properties_sql = f"{prefix}{self._print_identifier(TEMPORARY_PROPERTIES_COLUMN)}"+ serialized = event_document_sql(+ field_sql, temporary_properties_sql, self._document_feature_flags_sql(type, field_sql)+ )+ else:+ serialized = f"concat('{{', arrayStringConcat({json_member_pairs_sql(field_sql)}, ','), '}}')"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.- """+ def _document_feature_flags_sql(self, type: ast.FieldType, field_sql: str) -> str | None:+ """The `$feature_flags` map without restricted flags, or None when a restricted `$feature_flags` hides them all."""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})"+ return self._visible_feature_flags_sql(field_sql, restricted_feature_flag_names(keys_to_drop))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."""diff --git a/posthog/hogql/printer/events_json_document.py b/posthog/hogql/printer/events_json_document.pynew file mode 100644index 000000000000..4a29b82c6dfc--- /dev/null+++ b/posthog/hogql/printer/events_json_document.py@@ -0,0 +1,90 @@+"""The SQL that rebuilds the document an SDK sent from the columns of one events_json row.++The cleaner splits event properties at insert: each `$feature/<key>` value goes into the typed `$feature_flags` map,+`$active_feature_flags` is dropped, and the keys `is_temporary_event_property` names go into `temporary_properties`,+which ClickHouse clears 60 days after insertion. A whole-document read puts those parts back so the document matches+the legacy table while the parts exist. The HogQL printer and the batch export source share this builder so every+whole-document reader returns the same document.+"""++from collections.abc import Iterable++from posthog.hogql.constants import (+ FEATURE_FLAG_PROPERTY_PREFIX,+ FEATURE_FLAG_VARIANT_SENTINELS,+ INACTIVE_FEATURE_FLAG_VALUES,+)+from posthog.hogql.escape_sql import escape_clickhouse_string++from posthog.clickhouse.events_json import EVENTS_PROPERTIES_JSON_SUBCOLUMNS+++def declared_array_paths(subcolumns: dict[str, str]) -> tuple[str, ...]:+ """The declared paths of a JSON column whose type is an array."""+ return tuple(sorted(path for path, declared_type in subcolumns.items() if declared_type.startswith("Array(")))+++EVENTS_PROPERTIES_DECLARED_ARRAY_PATHS = declared_array_paths(EVENTS_PROPERTIES_JSON_SUBCOLUMNS)+++def json_member_pairs_sql(+ document_sql: str, *, exclude_key: str | None = None, declared_array_paths: Iterable[str] = ()+) -> str:+ """`"key":<raw json>` strings for the members of a JSON column.++ The JSON type materializes a declared array path as `[]` on every row, so for those paths an empty array means+ the key was absent and the pair is left out. An empty array under any other key was sent and stays.+ """+ conditions = []+ array_paths = ", ".join(escape_clickhouse_string(path) for path in declared_array_paths)+ if array_paths:+ conditions.append(f"not (kv.2 = '[]' and kv.1 in ({array_paths}))")+ if exclude_key is not None:+ conditions.append(f"kv.1 != {escape_clickhouse_string(exclude_key)}")+ members = f"JSONExtractKeysAndValuesRaw(toJSONString({document_sql}))"+ if conditions:+ members = f"arrayFilter(kv -> {' and '.join(conditions)}, {members})"+ return f"arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), {members})"+++def feature_flag_pairs_sql(feature_flags_sql: str) -> str:+ """`"$feature/<key>":<value>` pairs plus the `"$active_feature_flags":[...]` pair, from a `$feature_flags` 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, and is present whenever the map has an entry.+ """+ 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({feature_flags_sql}), mapValues({feature_flags_sql}))"+ )+ 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}), {feature_flags_sql})))"+ )+ active_pair = f"if(empty({feature_flags_sql}), [], [concat('\"$active_feature_flags\":', {active_flags})])"+ return f"arrayConcat({flag_pairs}, {active_pair})"+++def event_document_sql(properties_sql: str, temporary_properties_sql: str, feature_flags_sql: str | None) -> str:+ """The rebuilt document as JSON text. The caller wraps it in the strip UDF and any restricted-key masking.++ `feature_flags_sql` is the flags map to rebuild from, already filtered of restricted flags, or None when every+ flag is hidden. The `$feature_flags` member of `properties` is left out because its entries come back as+ `$feature/<key>` pairs.+ """+ pairs = [+ json_member_pairs_sql(+ properties_sql, exclude_key="$feature_flags", declared_array_paths=EVENTS_PROPERTIES_DECLARED_ARRAY_PATHS+ )+ ]+ if feature_flags_sql is not None:+ pairs.append(feature_flag_pairs_sql(feature_flags_sql))+ pairs.append(json_member_pairs_sql(temporary_properties_sql))+ return f"concat('{{', arrayStringConcat(arrayConcat({', '.join(pairs)}), ','), '}}')"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 0d276cff3355..9098e07a6abf 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(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'+ '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 -> not (kv.2 = \'[]\' and kv.1 in (\'$exception_functions\', \'$exception_list\', \'$exception_sources\', \'$exception_types\', \'$exception_values\', \'$mcp_listed_tool_names\')) 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), \':\', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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'# ---# 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(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'+ '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 -> not (kv.2 = \'[]\' and kv.1 in (\'$exception_functions\', \'$exception_list\', \'$exception_sources\', \'$exception_types\', \'$exception_values\', \'$mcp_listed_tool_names\')) 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), \':\', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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'# ---# 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(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'+ '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 -> not (kv.2 = \'[]\' and kv.1 in (\'$exception_functions\', \'$exception_list\', \'$exception_sources\', \'$exception_types\', \'$exception_values\', \'$mcp_listed_tool_names\')) 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), \':\', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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'# ---# 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(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'+ '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 -> not (kv.2 = \'[]\' and kv.1 in (\'$exception_functions\', \'$exception_list\', \'$exception_sources\', \'$exception_types\', \'$exception_values\', \'$mcp_listed_tool_names\')) 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), \':\', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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'# ---# 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/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex 65243e585f37..7d467d7f3430 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -61,6 +61,7 @@ from posthog.hogql.query import execute_hogql_queryfrom posthog.hogql.test.utils import json_dynamic_read_sql, json_dynamic_read_sql_from_partsfrom posthog.clickhouse.client.execute import sync_execute+from posthog.clickhouse.events_json import TEMPORARY_PROPERTIES_JSON_TYPEfrom posthog.models import PropertyDefinitionfrom posthog.models.event.sql import EVENTS_JSON_DATA_TABLE, EVENTS_PROPERTIES_JSON_TYPEfrom posthog.models.exchange_rate.sql import EXCHANGE_RATE_DICTIONARY_NAME@@ -1422,21 +1423,33 @@ class TestPrinter(BaseTest):@parameterized.expand([- (expression, properties)+ (expression, properties, expected)for expression in ["properties", "toJSONString(properties)"]- for properties in [{}, {"$exception_types": ["TypeError"], "items": [None, "", {}, []], "custom_empty": []}]+ for properties, expected in [+ ({}, {}),+ (+ {"$exception_types": ["TypeError"], "items": [None, "", {}, []], "custom_empty": []},+ {"$exception_types": ["TypeError"], "items": [None, "", {}, []], "custom_empty": []},+ ),+ ({"$exception_types": [], "custom_empty": []}, {"custom_empty": []}),+ ]])@override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=True)def test_new_events_schema_to_json_string_strips_empty_values(- self, expression: str, properties: dict[str, object]+ self, expression: str, properties: dict[str, object], expected: dict[str, object]) -> None:printed = self._expr(expression)[(serialized,)] = sync_execute(- f"SELECT {printed} FROM (SELECT CAST(%(raw)s, %(json_type)s) AS properties) AS events",- {"raw": json.dumps(properties), "json_type": EVENTS_PROPERTIES_JSON_TYPE()},+ f"SELECT {printed} FROM (SELECT CAST(%(raw)s, %(json_type)s) AS properties, "+ "CAST('{}', %(temporary_type)s) AS temporary_properties) AS events",+ {+ "raw": json.dumps(properties),+ "json_type": EVENTS_PROPERTIES_JSON_TYPE(),+ "temporary_type": TEMPORARY_PROPERTIES_JSON_TYPE,+ },)- self.assertEqual(json.loads(serialized), {key: value for key, value in properties.items() if value != []})+ self.assertEqual(json.loads(serialized), expected)@parameterized.expand([diff --git a/posthog/hogql/transforms/test/__snapshots__/test_events_predicate_pushdown.new_events_schema.ambr b/posthog/hogql/transforms/test/__snapshots__/test_events_predicate_pushdown.new_events_schema.ambrindex 5c31b5345913..6db91ba08fab 100644--- a/posthog/hogql/transforms/test/__snapshots__/test_events_predicate_pushdown.new_events_schema.ambr+++ b/posthog/hogql/transforms/test/__snapshots__/test_events_predicate_pushdown.new_events_schema.ambr@@ -106,7 +106,7 @@SELECT events.event AS event, if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS id, events.person_properties AS properties, events.__pd_expr_0 AS created_at, events__session.`$session_duration` AS `$session_duration`FROM (- SELECT toTimeZone(events.person_created_at, %(hogql_val_0)s) AS __pd_expr_0, toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_1)s)) AS __pd_expr_1, events.distinct_id AS distinct_id, events.event AS event, events.person_id AS person_id, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS person_properties+ SELECT toTimeZone(events.person_created_at, %(hogql_val_0)s) AS __pd_expr_0, toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_1)s)) AS __pd_expr_1, events.distinct_id AS distinct_id, events.event AS event, events.person_id AS person_id, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS person_propertiesFROM events_json AS eventsWHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_2)s, 6, %(hogql_val_3)s)))LIMIT 50000) AS events LEFT JOIN (@@ -273,7 +273,7 @@SELECT events.event AS event, events.person_properties AS properties, events__session.`$session_duration` AS `$session_duration`FROM (- SELECT toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_0)s)) AS __pd_expr_0, events.event AS event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != '[]', JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties)))), ','), '}')) AS person_properties+ SELECT toUInt128(accurateCastOrNull(events.properties.`$session_id`, %(hogql_val_0)s)) AS __pd_expr_0, events.event AS event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.person_properties))), ','), '}')) AS person_propertiesFROM events_json AS eventsWHERE and(equals(events.team_id, 420), greaterOrEquals(events.timestamp, toDateTime64(%(hogql_val_1)s, 6, %(hogql_val_2)s)))LIMIT 50000) AS events LEFT JOIN (diff --git a/posthog/hogql/transforms/test/test_property_types.py b/posthog/hogql/transforms/test/test_property_types.pyindex 3fc35caab3ed..2c9dc6006c60 100644--- a/posthog/hogql/transforms/test/test_property_types.py+++ b/posthog/hogql/transforms/test/test_property_types.py@@ -55,7 +55,7 @@ from posthog.hogql.transforms.property_types import PropertySwapper, build_propefrom posthog.hogql.type_system import ComparisonCompatibilityfrom posthog.clickhouse.client import sync_execute-from posthog.clickhouse.events_json import EVENTS_JSON_DATA_TABLE+from posthog.clickhouse.events_json import EVENTS_JSON_DATA_TABLE, TEMPORARY_PROPERTIES_JSON_TYPEfrom posthog.errors import ExposedCHQueryErrorfrom posthog.models import PropertyDefinition, Teamfrom posthog.models.event.sql import EVENTS_DATA_TABLE, EVENTS_PROPERTIES_JSON_TYPE, PERSON_PROPERTIES_JSON_TYPE@@ -349,6 +349,15 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):("JSONMergePatch(", "mapFilter((key, value) -> not(has("),("'\"$active_feature_flags\":'",),),+ (+ "whole_document_adds_temporary_keys",+ "SELECT properties, toString(properties) FROM events",+ True,+ None,+ None,+ ("toJSONString(events.temporary_properties)",),+ (),+ ),])def test_feature_flag_property_compatibility_uses_the_schema_backed_map(@@ -538,7 +547,8 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):"""WITH events_json AS (SELECT 1 AS team_id, 'synthetic' AS event,CAST(%(document)s, %(event_type)s) AS properties,- CAST(%(document)s, %(person_type)s) AS person_properties+ CAST(%(document)s, %(person_type)s) AS person_properties,+ CAST('{}', %(temporary_type)s) AS temporary_properties) """+ printed,{@@ -546,6 +556,7 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):"document": '{"$groups":{"organization":"hidden","project":"visible"}}',"event_type": EVENTS_PROPERTIES_JSON_TYPE(),"person_type": PERSON_PROPERTIES_JSON_TYPE(),+ "temporary_type": TEMPORARY_PROPERTIES_JSON_TYPE,},)assert row == ('{"project":"visible"}', None, "visible", 0, 0, 1)@@ -1860,6 +1871,52 @@ class TestEventsSchemaPropertyParity(ClickhouseTestMixin, BaseTest):)], use_new_events_schema+ document_query = (+ "SELECT properties, toString(properties), arraySort(JSONExtractKeys(properties)), "+ "(SELECT properties.$set.email FROM (SELECT * FROM events WHERE uuid = {uuid})) "+ f"FROM events WHERE uuid = '{event_uuid}'"+ ).replace("{uuid}", f"'{event_uuid}'")+ restricted_plan = {RestrictedProperty(name="$set.plan", property_type=PropertyDefinition.Type.EVENT)}+ documents: dict[tuple[bool, bool], tuple[Any, ...]] = {}+ for use_new_events_schema in (False, True):+ for with_restriction in (False, True):+ context = HogQLContext(+ team_id=self.team.pk,+ enable_select_queries=True,+ use_new_events_schema=use_new_events_schema,+ restricted_properties=restricted_plan if with_restriction else set(),+ )+ response = execute_hogql_query(document_query, team=self.team, context=context)+ assert response.results is not None+ document, stringified, keys, from_subquery = response.results[0]+ documents[(use_new_events_schema, with_restriction)] = (+ json.loads(document),+ json.loads(stringified),+ keys,+ from_subquery,+ )+ assert documents[(True, False)] == documents[(False, False)]+ assert documents[(True, True)] == documents[(False, True)]+ assert documents[(True, False)] == (+ {+ "$set": {"email": "[email protected]", "plan": {"tier": "pro"}},+ "$set_once": {"initial_referrer": "https://example.com", "is_beta": True},+ "$unset": ["old_key"],+ "$sdk_debug_replay_flushed_size": 42,+ "$browser": "Firefox",+ },+ {+ "$set": {"email": "[email protected]", "plan": {"tier": "pro"}},+ "$set_once": {"initial_referrer": "https://example.com", "is_beta": True},+ "$unset": ["old_key"],+ "$sdk_debug_replay_flushed_size": 42,+ "$browser": "Firefox",+ },+ ["$browser", "$sdk_debug_replay_flushed_size", "$set", "$set_once", "$unset"],+ "[email protected]",+ )+ assert documents[(True, True)][0]["$set"] == {"email": "[email protected]"}+# One call per registered JSON function, so a newly registered function fails here until native reads the# moved key. isValidJSON and JSONArrayLength take no key path.moved_key_calls = {diff --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 98f03989e6d2..ab3456578f7b 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,8 +11,9 @@count() AS value_count,max(timestamp) AS latest_seenFROM- (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,+ (SELECT JSONExtractKeysAndValues(JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_properties)))), ','), '}')), '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'))@@ -94,8 +95,9 @@count() AS value_count,max(timestamp) AS latest_seenFROM- (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,+ (SELECT JSONExtractKeysAndValues(JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_properties)))), ','), '}')), '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'))diff --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 eef08676f144..3cdaa5dbd2b0 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,8 +22,9 @@__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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.temporary_properties)))), ','), '}')) 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,@@ -83,8 +84,9 @@__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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.temporary_properties)))), ','), '}')) 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 af7cff0d76d7..8793db5abd23 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,8 +14,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -87,8 +88,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -160,8 +162,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -233,8 +236,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -306,8 +310,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -409,8 +414,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -482,8 +488,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -555,8 +562,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -628,8 +636,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -701,8 +710,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -804,8 +814,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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,@@ -877,8 +888,9 @@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(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,+ 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 -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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,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/test/__snapshots__/test_events_query_runner.new_events_schema.ambr b/posthog/hogql_queries/test/__snapshots__/test_events_query_runner.new_events_schema.ambrindex 18a80a7548bf..cd18bc96ae5e 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,8 +1,9 @@# serializer version: 1# name: TestEventsQueryRunner.test_absolute_date_range[new_events_schema]'''- 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)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -249,8 +250,9 @@# ---# name: TestEventsQueryRunner.test_cursor_with_star_select[new_events_schema]'''- 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)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -277,8 +279,9 @@# ---# name: TestEventsQueryRunner.test_element_chain_property_filter[new_events_schema]'''- 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)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -305,8 +308,9 @@# ---# name: TestEventsQueryRunner.test_presorted_events_table[new_events_schema]'''- 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)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -333,8 +337,9 @@# ---# name: TestEventsQueryRunner.test_presorted_events_table_multiple_order_by[new_events_schema]'''- 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)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -362,8 +367,9 @@# ---# name: TestEventsQueryRunner.test_presorted_events_table_order_by_event[new_events_schema]'''- 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)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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)`FROM events_json AS eventsWHERE and(equals(events.team_id, 99999), globalIn(events.uuid,(SELECT events.uuid AS uuid@@ -390,8 +396,9 @@# ---# name: TestEventsQueryRunner.test_presorted_events_table_order_by_property[new_events_schema]'''- 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)`+ SELECT tuple(events.uuid, events.event, JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_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)`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 43bd49f57c50..378dc2d89d84 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,8 +28,9 @@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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(event_source.temporary_properties)))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -102,8 +103,9 @@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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(event_source.temporary_properties)))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -176,8 +178,9 @@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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(event_source.temporary_properties)))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -250,8 +253,9 @@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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(event_source.temporary_properties)))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -324,8 +328,9 @@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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(event_source.temporary_properties)))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -398,8 +403,9 @@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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(event_source.temporary_properties)))), ','), '}')) AS properties,event_source.distinct_id AS distinct_id,event_source.elements_chain AS elements_chain,event_source.properties.`$session_id` AS session_id,@@ -472,8 +478,9 @@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(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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(event_source.temporary_properties)))), ','), '}')) 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/products/batch_exports/backend/debug/debugger.py b/products/batch_exports/backend/debug/debugger.pyindex 5350aa54af83..060ad60e15d6 100644--- a/products/batch_exports/backend/debug/debugger.py+++ b/products/batch_exports/backend/debug/debugger.py@@ -487,7 +487,7 @@ class BatchExportsDebugger:filters_str = ""if filters is not None and len(filters) > 0:filters_str, extra_query_parameters = compose_filters_clause(- filters, team_id=team_id, values=extra_query_parameters, native_events_source=native_source+ filters, team_id=team_id, values=extra_query_parameters)if native_source:diff --git a/products/batch_exports/backend/hogql_source.py b/products/batch_exports/backend/hogql_source.pyindex 41c20939c2b5..bd891b703859 100644--- a/products/batch_exports/backend/hogql_source.py+++ b/products/batch_exports/backend/hogql_source.py@@ -21,7 +21,6 @@ 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.transforms.clickhouse_property_resolution import feature_flag_variant_readfrom posthog.hogql.visitor import CloningVisitorfrom posthog.clickhouse.events_json import UNPARSEABLE_PROPERTIES_KEY@@ -244,30 +243,6 @@ def load_hogql_modifiers(stored: dict[str, typing.Any] | None) -> HogQLQueryModiraise UnsupportedHogQLQueryError(f"Invalid HogQL modifiers: {e}") from e-def native_event_property_chain(property_chain: list[str | int]) -> list[str | int]:- """The path an event property has in the native source's JSON.-- The native table keeps `$feature/<key>` flags in the `$feature_flags` map, so a `$feature/<key>` read must- become `$feature_flags.<key>` there. Every other property keeps its path.- """- key = property_chain[0] if property_chain else None- if isinstance(key, str) and key.startswith("$feature/"):- return ["$feature_flags", key.removeprefix("$feature/"), *property_chain[1:]]- return list(property_chain)---def native_feature_flag_read(field: ast.Field, property_chain: list[str | int]) -> ast.Expr:- """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 column- still serializes to a stable name instead of the parameterized expression.- """- if len(property_chain) != 2 or property_chain[0] != "$feature_flags":- return field- mapped = feature_flag_variant_read(field)- return ast.Alias(alias="__".join(str(part) for part in property_chain), expr=mapped, hidden=True)--class SerializedExportProperties(CloningVisitor):"""Rebind event fields while preserving property access restrictions."""@@ -279,7 +254,6 @@ class SerializedExportProperties(CloningVisitor):split_restricted_property_names,)- self.use_native_schema = context.uses_new_events_schema()restrictions = context.restricted_properties or set()restricted_names = split_restricted_property_names(restrictions)self.event_restrictions = set(restricted_names.event)@@ -298,36 +272,21 @@ class SerializedExportProperties(CloningVisitor):if node.chain[index : index + 2] == ["person", "properties"]:# `poe.properties` is the events table's own copy of the person properties.node.chain[index : index + 2] = ["poe", "properties"]- is_event_property = Falseif node.chain[index : index + 2] == ["poe", "properties"]:restrictions, property_chain = self.person_restrictions, node.chain[index + 2 :]elif str(node.chain[index]) == "properties":restrictions, property_chain = self.event_restrictions, node.chain[index + 1 :]- is_event_property = Trueelse:return nodeif restrictions:property_path = ".".join(str(part) for part in property_chain)if not property_path:raise QueryError("Batch export queries cannot select a restricted properties object")- checked_paths = [property_path]- if self.use_native_schema and is_event_property and property_chain[0] == "$feature_flags":- if len(property_chain) == 1:- # The serialized map cannot drop single entries, so any restricted flag hides the whole map.- if any(key.startswith("$feature/") for key in restrictions):- return ast.Constant(value=None)- else:- # A restriction names the flag as `$feature/<key>`, the spelling the native map entry replaces.- checked_paths.append("$feature/" + ".".join(str(part) for part in property_chain[1:]))if any(- path == key or path.startswith(key + ".") or key.startswith(path + ".")- for path in checked_paths+ property_path == key or property_path.startswith(key + ".") or key.startswith(property_path + ".")for key in restrictions):return ast.Constant(value=None)- if self.use_native_schema and is_event_property:- node.chain[index + 1 :] = native_event_property_chain(property_chain)- return native_feature_flag_read(node, node.chain[index + 1 :])return nodediff --git a/products/batch_exports/backend/temporal/destinations/http_batch_export.py b/products/batch_exports/backend/temporal/destinations/http_batch_export.pyindex 25aac8d21e7f..5dd77b4d1eba 100644--- a/products/batch_exports/backend/temporal/destinations/http_batch_export.py+++ b/products/batch_exports/backend/temporal/destinations/http_batch_export.py@@ -234,7 +234,7 @@ async def insert_into_http_activity(inputs: HttpInsertInputs) -> BatchExportResufilters = inputs.batch_export_model.filters if inputs.batch_export_model is not None else Noneif filters is not None and len(filters) > 0:filters_str, extra_query_parameters = await database_sync_to_async(compose_filters_clause)(- filters, team_id=inputs.team_id, values=None, native_events_source=native_source+ filters, team_id=inputs.team_id, values=None)else:filters_str = ""diff --git a/products/batch_exports/backend/temporal/filters.py b/products/batch_exports/backend/temporal/filters.pyindex db74d6253dc9..c70ee6cf4beb 100644--- a/products/batch_exports/backend/temporal/filters.py+++ b/products/batch_exports/backend/temporal/filters.py@@ -16,12 +16,11 @@ from posthog.hogql.hogql import astfrom posthog.hogql.parser import parse_exprfrom posthog.hogql.printer import prepare_ast_for_printing, print_prepared_astfrom posthog.hogql.property import property_to_expr-from posthog.hogql.visitor import CloningVisitor, TraversingVisitor+from posthog.hogql.visitor import TraversingVisitorfrom posthog.models import Teamfrom products.batch_exports.backend.filters import SUPPORTED_FILTER_TYPES-from products.batch_exports.backend.hogql_source import native_event_property_chain, native_feature_flag_readclass UpdatePropertiesToPersonProperties(TraversingVisitor):@@ -32,18 +31,6 @@ class UpdatePropertiesToPersonProperties(TraversingVisitor):node.chain = ["events", "poe", "properties", *node.chain[1:]]-class UpdatePropertiesToNativePaths(CloningVisitor):- """Move event property paths to where the native events source's JSON keeps them."""-- def visit_field(self, node: ast.Field) -> ast.Expr:- node = super().visit_field(node)- index = 1 if node.chain and node.chain[0] == "events" else 0- if node.chain[index : index + 1] != ["properties"]:- return node- node.chain[index + 1 :] = native_event_property_chain(node.chain[index + 1 :])- return native_feature_flag_read(node, node.chain[index + 1 :])--class InvalidFilterError(Exception):"""Error raised when an invalid filter is used."""@@ -61,8 +48,6 @@ def compose_filters_clause(filters: list[dict[str, str | bool | list[str] | None]],team_id: int,values: dict[str, str] | None = None,- *,- native_events_source: bool = False,) -> tuple[str, dict[str, str]]:"""Compose a clause of matching filters for a batch exports query.@@ -73,8 +58,6 @@ def compose_filters_clause(filters: A list of serialized HogQL filters.team_id: Team we are running for.values: HogQL placeholder values already in use.- native_events_source: Whether the clause filters the native events source, whose- `properties` JSON keeps `$feature/<key>` flags under `$feature_flags`.Returns:A printed string with the ClickHouse SQL clause, and a dictionary@@ -133,8 +116,6 @@ def compose_filters_clause(raise TypeError(f"Unhandled filter type: '{filter_type}'")and_expr: ast.Expr = ast.And(exprs=exprs)- if native_events_source:- and_expr = UpdatePropertiesToNativePaths().visit(and_expr)# This query only supports events at the moment.# TODO: Extend for other models that also wish to implement property filtering.select_query = ast.SelectQuery(diff --git a/products/batch_exports/backend/temporal/pipeline/internal_stage.py b/products/batch_exports/backend/temporal/pipeline/internal_stage.pyindex b1f11f4f5ea3..14b8933f8460 100644--- a/products/batch_exports/backend/temporal/pipeline/internal_stage.py+++ b/products/batch_exports/backend/temporal/pipeline/internal_stage.py@@ -642,7 +642,7 @@ async def _get_query(filters_str = ""if filters is not None and len(filters) > 0:filters_str, extra_query_parameters = await database_sync_to_async(compose_filters_clause)(- filters, team_id=team_id, values=extra_query_parameters, native_events_source=native_source+ filters, team_id=team_id, values=extra_query_parameters)if native_source:diff --git a/products/batch_exports/backend/temporal/sql/events.py b/products/batch_exports/backend/temporal/sql/events.pyindex 5c86ed100acf..4806aa3d9187 100644--- a/products/batch_exports/backend/temporal/sql/events.py+++ b/products/batch_exports/backend/temporal/sql/events.py@@ -2,6 +2,8 @@from string import Template+from posthog.hogql.printer.events_json_document import event_document_sql+# Projected columns a HogQL query can select.# NOTE: this is just for the 'old' version of our HogQL support, where a user can define a custom# SELECT FROM clause, useful for defining a custom schema. It's not related to the new HogQL model,@@ -502,14 +504,20 @@ SETTINGS# The `{{}}` literals below are not a typo. `ClickHouseClient.prepare_query` runs `str.format` over# the whole query, which turns `{{}}` back into `{}`. A bare `{}` would parse as a positional field-# and reach ClickHouse as `{0}`, so the `nullIf` would never match an empty object.+# and reach ClickHouse as `{0}`, so the `nullIf` would never match an empty object. The rebuilt document+# gets the same doubling for the same reason.# `toJSONString` prints a stored dotted key as `a%2Eb` unless the query sets `json_type_escape_dots_in_keys`,# which is why the native query's SETTINGS carry it.+_PROPERTIES_DOCUMENT = (+ event_document_sql("properties", "temporary_properties", "properties.`$feature_flags`")+ .replace("{", "{{")+ .replace("}", "}}")+)SERIALIZED_EVENTS_JSON_SOURCE = """(SELECT * REPLACE (toString(uuid) AS uuid,toString(person_id) AS person_id,- JSONStripEmptyStringsAndNulls(toJSONString(properties)) AS properties,+ JSONStripEmptyStringsAndNulls(__PROPERTIES_DOCUMENT__) AS properties,JSONStripEmptyStringsAndNulls(toJSONString(person_properties)) AS person_properties),nullIf(toJSONString(temporary_properties.^`$set`), '{{}}') AS set,@@ -517,7 +525,7 @@ SERIALIZED_EVENTS_JSON_SOURCE = """(nullIf(toJSONString(temporary_properties.^`$unset`), '[]') AS unset,nullIf(toJSONString(temporary_properties.^`$group_set`), '{{}}') AS group_setFROM events_json-)"""+)""".replace("__PROPERTIES_DOCUMENT__", _PROPERTIES_DOCUMENT)def native_events_export_query(diff --git a/products/batch_exports/backend/tests/api/test_serializers.py b/products/batch_exports/backend/tests/api/test_serializers.pyindex feef7ed11e14..38d4108a401e 100644--- a/products/batch_exports/backend/tests/api/test_serializers.py+++ b/products/batch_exports/backend/tests/api/test_serializers.py@@ -211,16 +211,14 @@ class TestSerializeHogQLQueryToBatchExportSchema(BaseTest):@parameterized.expand([- ("legacy_name", "SELECT properties.`$feature/secret` AS flag FROM events", "NULL"),- ("map_entry", "SELECT properties.$feature_flags.secret AS flag FROM events", "NULL"),- ("whole_map", "SELECT properties.$feature_flags AS flags FROM events", "NULL"),- ("visible_entry", "SELECT properties.$feature_flags.checkout AS flag FROM events", "JSONExtractRaw("),+ ("restricted", "SELECT properties.`$feature/secret` AS flag FROM events", "NULL"),+ ("visible", "SELECT properties.`$feature/checkout` AS flag FROM events", "JSONExtractRaw("),])@override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=True)@patch("products.batch_exports.backend.api.batch_export.get_restricted_properties_with_group_type_index_for_team")@patch("posthog.models.event.new_events_schema.use_new_events_schema", return_value=True)- def test_native_export_hides_a_restricted_flag_under_every_spelling(+ def test_native_export_hides_a_restricted_flag(self, _name: str, query: str, expected: str, _use_new_events_schema, get_restricted_properties):get_restricted_properties.return_value = {@@ -247,8 +245,7 @@ class TestSerializeHogQLQueryToBatchExportSchema(BaseTest):)field = schema["fields"][0]- assert field["alias"] == "`$feature_flags__checkout`", field- assert "%(" not in field["alias"]+ assert field["alias"] == "`$feature/checkout`", fieldassert "JSONExtractRaw(" in field["expression"], fielddiff --git a/products/batch_exports/backend/tests/temporal/test_filters.py b/products/batch_exports/backend/tests/temporal/test_filters.pyindex d7d5d7921b93..d3fb7d454120 100644--- a/products/batch_exports/backend/tests/temporal/test_filters.py+++ b/products/batch_exports/backend/tests/temporal/test_filters.py@@ -125,45 +125,6 @@ def test_compose_filters_clause_uses_legacy_events_schema(settings, ateam):assert result_values == {"hogql_val_0": "$browser", "hogql_val_1": "Chrome"}-def test_compose_filters_clause_reads_flags_from_the_native_map(ateam):- result_clause, result_values = compose_filters_clause(- [- {"key": "$feature/some-feature", "type": "event", "operator": "exact", "value": ["true"]},- {"key": "properties.`$feature/other-feature` = 'control'", "type": "hogql"},- ],- team_id=ateam.id,- native_events_source=True,- )-- def map_read(flag_key: int, flag_name: int) -> str:- 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(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": "$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",- }--@pytest.mark.parametrize("property_type,document,predicate",[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 91f0bd7c8849..095361b44017 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@@ -771,7 +771,6 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables([{"key": "$feature/some-feature", "type": "event", "operator": "exact", "value": ["true"]}],team_id=ateam.pk,values=values,- native_events_source=True,)batches: list[pa.RecordBatch] = await database_sync_to_async(lambda: list(@@ -799,7 +798,14 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables(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"+ exported_properties = json.loads(rows[0]["properties"])+ assert exported_properties["$browser"] == "Firefox"+ assert exported_properties["$set"] == {"email": "[email protected]"}+ assert "$feature_flags" not in exported_properties+ assert exported_properties["$feature/some-feature"] is True+ assert exported_properties["$feature/named-false"] == "false"+ assert exported_properties["$feature/named-true"] == "true"+ assert exported_properties["$active_feature_flags"] == ["named-false", "named-true", "some-feature"]assert json.loads(rows[0]["person_properties"])["email"] == "[email protected]"assert json.loads(rows[0]["set"]) == {"email": "[email protected]"}assert rows[0]["_inserted_at"] == dt.datetime(2024, 1, 1, 12, tzinfo=dt.UTC)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 a10ac43da21f..d2f0cc0e2487 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,8 +484,9 @@ORDER BY aggregation_target ASC SETTINGS join_algorithm='auto'),filtered_events AS(SELECT 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,+ JSONStripEmptyStringsAndNulls(concat('{', arrayStringConcat(arrayConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> not (kv.2 = '[]'+ and kv.1 in ('$exception_functions', '$exception_list', '$exception_sources', '$exception_types', '$exception_values', '$mcp_listed_tool_names'))+ 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`))))])), arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), JSONExtractKeysAndValuesRaw(toJSONString(events.temporary_properties)))), ','), '}')) 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`,