: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(aio): deduplicate ai_events rows in the trace view (#73382)
Eva Wuuuae38ec739789
diff --git a/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.ambr b/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.ambrindex b1a593810e88..f1c3525477fb 100644--- a/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.ambr+++ b/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.ambr@@ -1,25 +1,49 @@# serializer version: 1# name: TestTraceQueryRunner.test_event_property_filters'''- SELECT ai_events.trace_id AS id,- any(ai_events.session_id) AS ai_session_id,- min(toTimeZone(ai_events.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(ai_events.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), ''), argMin(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(ai_events.latency, 0), 0), notEquals(ai_events.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(ai_events.latency, 0), 0), equals(ai_events.event, '$ai_generation'))), 0)), sumIf(ai_events.latency, and(equals(ai_events.event, '$ai_generation'), ifNull(greater(ai_events.latency, 0), 0))), sumIf(ai_events.latency, or(isNull(ai_events.parent_id), ifNull(equals(ai_events.parent_id, ai_events.trace_id), isNull(ai_events.parent_id)- and isNull(ai_events.trace_id))))), 2) AS total_latency,- nullIf(sumIf(ai_events.input_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(ai_events.output_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(ai_events.input_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(ai_events.output_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(ai_events.total_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(ai_events.uuid, ai_events.event, toTimeZone(ai_events.timestamp, 'UTC'), ai_events.properties, ai_events.input, ai_events.output, ai_events.output_choices, ai_events.input_state, ai_events.output_state, ai_events.tools), notEquals(ai_events.event, '$ai_trace')))) AS events,- argMinIf(ai_events.input_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS input_state,- argMinIf(ai_events.output_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), argMin(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'))) AS trace_name- FROM ai_events- WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(ai_events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'bar'), 0)))- GROUP BY ai_events.trace_id+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT ai_events.uuid AS uuid,+ ai_events.event AS event,+ toTimeZone(ai_events.timestamp, 'UTC') AS timestamp,+ ai_events.distinct_id AS distinct_id,+ ai_events.properties AS properties,+ ai_events.trace_id AS trace_id,+ ai_events.session_id AS session_id,+ ai_events.parent_id AS parent_id,+ ai_events.span_name AS span_name,+ ai_events.trace_name AS trace_name,+ ai_events.latency AS latency,+ ai_events.input_tokens AS input_tokens,+ ai_events.output_tokens AS output_tokens,+ ai_events.input_cost_usd AS input_cost_usd,+ ai_events.output_cost_usd AS output_cost_usd,+ ai_events.total_cost_usd AS total_cost_usd,+ ai_events.input AS input,+ ai_events.output AS output,+ ai_events.output_choices AS output_choices,+ ai_events.input_state AS input_state,+ ai_events.output_state AS output_state,+ ai_events.tools AS tools+ FROM ai_events+ WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(ai_events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'bar'), 0)))+ LIMIT 1 BY ai_events.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,@@ -36,25 +60,49 @@# ---# name: TestTraceQueryRunner.test_event_property_filters.1'''- SELECT ai_events.trace_id AS id,- any(ai_events.session_id) AS ai_session_id,- min(toTimeZone(ai_events.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(ai_events.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), ''), argMin(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(ai_events.latency, 0), 0), notEquals(ai_events.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(ai_events.latency, 0), 0), equals(ai_events.event, '$ai_generation'))), 0)), sumIf(ai_events.latency, and(equals(ai_events.event, '$ai_generation'), ifNull(greater(ai_events.latency, 0), 0))), sumIf(ai_events.latency, or(isNull(ai_events.parent_id), ifNull(equals(ai_events.parent_id, ai_events.trace_id), isNull(ai_events.parent_id)- and isNull(ai_events.trace_id))))), 2) AS total_latency,- nullIf(sumIf(ai_events.input_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(ai_events.output_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(ai_events.input_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(ai_events.output_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(ai_events.total_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(ai_events.uuid, ai_events.event, toTimeZone(ai_events.timestamp, 'UTC'), ai_events.properties, ai_events.input, ai_events.output, ai_events.output_choices, ai_events.input_state, ai_events.output_state, ai_events.tools), notEquals(ai_events.event, '$ai_trace')))) AS events,- argMinIf(ai_events.input_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS input_state,- argMinIf(ai_events.output_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), argMin(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'))) AS trace_name- FROM ai_events- WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(ai_events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'baz'), 0)))- GROUP BY ai_events.trace_id+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT ai_events.uuid AS uuid,+ ai_events.event AS event,+ toTimeZone(ai_events.timestamp, 'UTC') AS timestamp,+ ai_events.distinct_id AS distinct_id,+ ai_events.properties AS properties,+ ai_events.trace_id AS trace_id,+ ai_events.session_id AS session_id,+ ai_events.parent_id AS parent_id,+ ai_events.span_name AS span_name,+ ai_events.trace_name AS trace_name,+ ai_events.latency AS latency,+ ai_events.input_tokens AS input_tokens,+ ai_events.output_tokens AS output_tokens,+ ai_events.input_cost_usd AS input_cost_usd,+ ai_events.output_cost_usd AS output_cost_usd,+ ai_events.total_cost_usd AS total_cost_usd,+ ai_events.input AS input,+ ai_events.output AS output,+ ai_events.output_choices AS output_choices,+ ai_events.input_state AS input_state,+ ai_events.output_state AS output_state,+ ai_events.tools AS tools+ FROM ai_events+ WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(ai_events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'baz'), 0)))+ LIMIT 1 BY ai_events.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,@@ -71,25 +119,49 @@# ---# name: TestTraceQueryRunner.test_event_property_filters.2'''- SELECT ai_events.trace_id AS id,- any(ai_events.session_id) AS ai_session_id,- min(toTimeZone(ai_events.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(ai_events.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), ''), argMin(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(ai_events.latency, 0), 0), notEquals(ai_events.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(ai_events.latency, 0), 0), equals(ai_events.event, '$ai_generation'))), 0)), sumIf(ai_events.latency, and(equals(ai_events.event, '$ai_generation'), ifNull(greater(ai_events.latency, 0), 0))), sumIf(ai_events.latency, or(isNull(ai_events.parent_id), ifNull(equals(ai_events.parent_id, ai_events.trace_id), isNull(ai_events.parent_id)- and isNull(ai_events.trace_id))))), 2) AS total_latency,- nullIf(sumIf(ai_events.input_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(ai_events.output_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(ai_events.input_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(ai_events.output_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(ai_events.total_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(ai_events.uuid, ai_events.event, toTimeZone(ai_events.timestamp, 'UTC'), ai_events.properties, ai_events.input, ai_events.output, ai_events.output_choices, ai_events.input_state, ai_events.output_state, ai_events.tools), notEquals(ai_events.event, '$ai_trace')))) AS events,- argMinIf(ai_events.input_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS input_state,- argMinIf(ai_events.output_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), argMin(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'))) AS trace_name- FROM ai_events- WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(ai_events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'barz'), 0)))- GROUP BY ai_events.trace_id+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT ai_events.uuid AS uuid,+ ai_events.event AS event,+ toTimeZone(ai_events.timestamp, 'UTC') AS timestamp,+ ai_events.distinct_id AS distinct_id,+ ai_events.properties AS properties,+ ai_events.trace_id AS trace_id,+ ai_events.session_id AS session_id,+ ai_events.parent_id AS parent_id,+ ai_events.span_name AS span_name,+ ai_events.trace_name AS trace_name,+ ai_events.latency AS latency,+ ai_events.input_tokens AS input_tokens,+ ai_events.output_tokens AS output_tokens,+ ai_events.input_cost_usd AS input_cost_usd,+ ai_events.output_cost_usd AS output_cost_usd,+ ai_events.total_cost_usd AS total_cost_usd,+ ai_events.input AS input,+ ai_events.output AS output,+ ai_events.output_choices AS output_choices,+ ai_events.input_state AS input_state,+ ai_events.output_state AS output_state,+ ai_events.tools AS tools+ FROM ai_events+ WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(ai_events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'barz'), 0)))+ LIMIT 1 BY ai_events.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,@@ -106,25 +178,49 @@# ---# name: TestTraceQueryRunner.test_event_property_filters.3'''- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '') AS id,- any(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_session_id'), ''), 'null'), '^"|"$', '')) AS ai_session_id,- min(toTimeZone(__ai_events_fallback.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(__ai_events_fallback.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(__ai_events_fallback.distinct_id, toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')), ''), argMin(__ai_events_fallback.distinct_id, toTimeZone(__ai_events_fallback.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), 0), 0), notEquals(__ai_events_fallback.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), 0), 0), equals(__ai_events_fallback.event, '$ai_generation'))), 0)), sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), and(equals(__ai_events_fallback.event, '$ai_generation'), ifNull(greater(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), 0), 0))), sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), or(isNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_parent_id'), ''), 'null'), '^"|"$', '')), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_parent_id'), ''), 'null'), '^"|"$', ''), replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), isNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_parent_id'), ''), 'null'), '^"|"$', ''))- and isNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')))))), 2) AS total_latency,- nullIf(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_tokens'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_tokens'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_total_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(__ai_events_fallback.uuid, __ai_events_fallback.event, toTimeZone(__ai_events_fallback.timestamp, 'UTC'), __ai_events_fallback.properties, JSONExtractRaw(__ai_events_fallback.properties, '$ai_input'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_output'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_choices'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_state'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_state'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_tools')), notEquals(__ai_events_fallback.event, '$ai_trace')))) AS events,- argMinIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_state'), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')) AS input_state,- argMinIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_state'), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_span_name'), ''), 'null'), '^"|"$', ''), ''), nullIf(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_name'), ''), 'null'), '^"|"$', ''), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')), argMin(ifNull(nullIf(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_span_name'), ''), 'null'), '^"|"$', ''), ''), nullIf(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_name'), ''), 'null'), '^"|"$', ''), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'))) AS trace_name- FROM events AS __ai_events_fallback- WHERE and(equals(__ai_events_fallback.team_id, 99999), in(__ai_events_fallback.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), 'trace1'), 0), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'barz'), 0)))- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT __ai_events_fallback.uuid AS uuid,+ __ai_events_fallback.event AS event,+ toTimeZone(__ai_events_fallback.timestamp, 'UTC') AS timestamp,+ __ai_events_fallback.distinct_id AS distinct_id,+ __ai_events_fallback.properties AS properties,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '') AS trace_id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_session_id'), ''), 'null'), '^"|"$', '') AS session_id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_parent_id'), ''), 'null'), '^"|"$', '') AS parent_id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_span_name'), ''), 'null'), '^"|"$', '') AS span_name,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_name'), ''), 'null'), '^"|"$', '') AS trace_name,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS latency,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_tokens'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS input_tokens,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_tokens'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS output_tokens,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS input_cost_usd,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS output_cost_usd,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_total_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS total_cost_usd,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_input') AS input,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_output') AS output,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_choices') AS output_choices,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_state') AS input_state,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_state') AS output_state,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_tools') AS tools+ FROM events AS __ai_events_fallback+ WHERE and(equals(__ai_events_fallback.team_id, 99999), in(__ai_events_fallback.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), 'trace1'), 0), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'barz'), 0)))+ LIMIT 1 BY __ai_events_fallback.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,@@ -141,26 +237,51 @@# ---# name: TestTraceQueryRunner.test_event_property_filters[new_events_schema]'''- SELECT __ai_events_fallback.properties.`$ai_trace_id` AS id,- any(__ai_events_fallback.properties.`$ai_session_id`) AS ai_session_id,- min(toTimeZone(__ai_events_fallback.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(__ai_events_fallback.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(__ai_events_fallback.distinct_id, toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')), ''), argMin(__ai_events_fallback.distinct_id, toTimeZone(__ai_events_fallback.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), 0), 0), notEquals(__ai_events_fallback.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), 0), 0), equals(__ai_events_fallback.event, '$ai_generation'))), 0)), sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), and(equals(__ai_events_fallback.event, '$ai_generation'), ifNull(greater(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), 0), 0))), sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), or(isNull(__ai_events_fallback.properties.`$ai_parent_id`), equals(__ai_events_fallback.properties.`$ai_parent_id`, __ai_events_fallback.properties.`$ai_trace_id`)))), 2) AS total_latency,- nullIf(sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_tokens`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_tokens`), if(isNull(__ai_events_fallback.properties.`$ai_input_tokens`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_input_tokens`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_input_tokens`), toString(__ai_events_fallback.properties.`$ai_input_tokens`))))), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_tokens`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_tokens`), if(isNull(__ai_events_fallback.properties.`$ai_output_tokens`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_output_tokens`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_output_tokens`), toString(__ai_events_fallback.properties.`$ai_output_tokens`))))), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_cost_usd`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_cost_usd`), if(isNull(__ai_events_fallback.properties.`$ai_input_cost_usd`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_input_cost_usd`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_input_cost_usd`), toString(__ai_events_fallback.properties.`$ai_input_cost_usd`))))), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_cost_usd`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_cost_usd`), if(isNull(__ai_events_fallback.properties.`$ai_output_cost_usd`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_output_cost_usd`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_output_cost_usd`), toString(__ai_events_fallback.properties.`$ai_output_cost_usd`))))), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(__ai_events_fallback.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(__ai_events_fallback.uuid, __ai_events_fallback.event, toTimeZone(__ai_events_fallback.timestamp, 'UTC'), concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != 'null'- AND NOT (kv.2 = '[]'- AND has(['$active_feature_flags', '$exception_functions', '$exception_sources', '$exception_types', '$exception_values'], kv.1)), JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.properties)))), ','), '}'), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input`), if(isNull(__ai_events_fallback.properties.`$ai_input`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output`), if(isNull(__ai_events_fallback.properties.`$ai_output`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_choices`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_choices`), if(isNull(__ai_events_fallback.properties.`$ai_output_choices`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_choices`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_choices`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_choices`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), if(isNull(__ai_events_fallback.properties.`$ai_input_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input_state`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), if(isNull(__ai_events_fallback.properties.`$ai_output_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_state`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_tools`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_tools`), if(isNull(__ai_events_fallback.properties.`$ai_tools`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_tools`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_tools`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_tools`)))), ''))), notEquals(__ai_events_fallback.event, '$ai_trace')))) AS events,- argMinIf(JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), if(isNull(__ai_events_fallback.properties.`$ai_input_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input_state`)))), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')) AS input_state,- argMinIf(JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), if(isNull(__ai_events_fallback.properties.`$ai_output_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_state`)))), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), if(isNull(__ai_events_fallback.properties.`$ai_span_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_span_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_span_name`), toString(__ai_events_fallback.properties.`$ai_span_name`))))), ''), nullIf(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), if(isNull(__ai_events_fallback.properties.`$ai_trace_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_trace_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_trace_name`), toString(__ai_events_fallback.properties.`$ai_trace_name`))))), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')), argMin(ifNull(nullIf(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), if(isNull(__ai_events_fallback.properties.`$ai_span_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_span_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_span_name`), toString(__ai_events_fallback.properties.`$ai_span_name`))))), ''), nullIf(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), if(isNull(__ai_events_fallback.properties.`$ai_trace_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_trace_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_trace_name`), toString(__ai_events_fallback.properties.`$ai_trace_name`))))), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'))) AS trace_name- FROM events_json AS __ai_events_fallback- WHERE and(equals(__ai_events_fallback.team_id, 99999), in(__ai_events_fallback.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(__ai_events_fallback.properties.`$ai_trace_id`, 'trace1'), ifNull(equals(if(notEquals(toJSONString(__ai_events_fallback.properties.^foo), '{}'), toJSONString(__ai_events_fallback.properties.^foo), if(isNull(__ai_events_fallback.properties.foo), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.foo), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.foo), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.foo), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.foo), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.foo), 'Tuple')), toJSONString(__ai_events_fallback.properties.foo), toString(__ai_events_fallback.properties.foo))))), 'barz'), 0)))- GROUP BY __ai_events_fallback.properties.`$ai_trace_id`+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT __ai_events_fallback.uuid AS uuid,+ __ai_events_fallback.event AS event,+ toTimeZone(__ai_events_fallback.timestamp, 'UTC') AS timestamp,+ __ai_events_fallback.distinct_id AS distinct_id,+ concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != 'null'+ AND NOT (kv.2 = '[]'+ AND has(['$active_feature_flags', '$exception_functions', '$exception_sources', '$exception_types', '$exception_values'], kv.1)), JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.properties)))), ','), '}') AS properties,+ __ai_events_fallback.properties.`$ai_trace_id` AS trace_id,+ __ai_events_fallback.properties.`$ai_session_id` AS session_id,+ __ai_events_fallback.properties.`$ai_parent_id` AS parent_id,+ if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), if(isNull(__ai_events_fallback.properties.`$ai_span_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_span_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_span_name`), toString(__ai_events_fallback.properties.`$ai_span_name`))))) AS span_name,+ if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), if(isNull(__ai_events_fallback.properties.`$ai_trace_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_trace_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_trace_name`), toString(__ai_events_fallback.properties.`$ai_trace_name`))))) AS trace_name,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64') AS latency,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_tokens`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_tokens`), if(isNull(__ai_events_fallback.properties.`$ai_input_tokens`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_input_tokens`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_input_tokens`), toString(__ai_events_fallback.properties.`$ai_input_tokens`))))), 'Float64'), 'Float64') AS input_tokens,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_tokens`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_tokens`), if(isNull(__ai_events_fallback.properties.`$ai_output_tokens`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_output_tokens`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_output_tokens`), toString(__ai_events_fallback.properties.`$ai_output_tokens`))))), 'Float64'), 'Float64') AS output_tokens,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_cost_usd`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_cost_usd`), if(isNull(__ai_events_fallback.properties.`$ai_input_cost_usd`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_input_cost_usd`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_input_cost_usd`), toString(__ai_events_fallback.properties.`$ai_input_cost_usd`))))), 'Float64'), 'Float64') AS input_cost_usd,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_cost_usd`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_cost_usd`), if(isNull(__ai_events_fallback.properties.`$ai_output_cost_usd`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_output_cost_usd`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_output_cost_usd`), toString(__ai_events_fallback.properties.`$ai_output_cost_usd`))))), 'Float64'), 'Float64') AS output_cost_usd,+ accurateCastOrNull(accurateCastOrNull(__ai_events_fallback.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64') AS total_cost_usd,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input`), if(isNull(__ai_events_fallback.properties.`$ai_input`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input`)))), '')) AS input,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output`), if(isNull(__ai_events_fallback.properties.`$ai_output`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output`)))), '')) AS output,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_choices`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_choices`), if(isNull(__ai_events_fallback.properties.`$ai_output_choices`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_choices`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_choices`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_choices`)))), '')) AS output_choices,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), if(isNull(__ai_events_fallback.properties.`$ai_input_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input_state`)))), '')) AS input_state,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), if(isNull(__ai_events_fallback.properties.`$ai_output_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_state`)))), '')) AS output_state,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_tools`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_tools`), if(isNull(__ai_events_fallback.properties.`$ai_tools`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_tools`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_tools`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_tools`)))), '')) AS tools+ FROM events_json AS __ai_events_fallback+ WHERE and(equals(__ai_events_fallback.team_id, 99999), in(__ai_events_fallback.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(__ai_events_fallback.properties.`$ai_trace_id`, 'trace1'), ifNull(equals(if(notEquals(toJSONString(__ai_events_fallback.properties.^foo), '{}'), toJSONString(__ai_events_fallback.properties.^foo), if(isNull(__ai_events_fallback.properties.foo), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.foo), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.foo), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.foo), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.foo), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.foo), 'Tuple')), toJSONString(__ai_events_fallback.properties.foo), toString(__ai_events_fallback.properties.foo))))), 'barz'), 0)))+ LIMIT 1 BY __ai_events_fallback.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,@@ -177,43 +298,67 @@# ---# name: TestTraceQueryRunner.test_person_property_filters'''- SELECT ai_events.trace_id AS id,- any(ai_events.session_id) AS ai_session_id,- min(toTimeZone(ai_events.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(ai_events.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), ''), argMin(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(ai_events.latency, 0), 0), notEquals(ai_events.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(ai_events.latency, 0), 0), equals(ai_events.event, '$ai_generation'))), 0)), sumIf(ai_events.latency, and(equals(ai_events.event, '$ai_generation'), ifNull(greater(ai_events.latency, 0), 0))), sumIf(ai_events.latency, or(isNull(ai_events.parent_id), ifNull(equals(ai_events.parent_id, ai_events.trace_id), isNull(ai_events.parent_id)- and isNull(ai_events.trace_id))))), 2) AS total_latency,- nullIf(sumIf(ai_events.input_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(ai_events.output_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(ai_events.input_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(ai_events.output_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(ai_events.total_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(ai_events.uuid, ai_events.event, toTimeZone(ai_events.timestamp, 'UTC'), ai_events.properties, ai_events.input, ai_events.output, ai_events.output_choices, ai_events.input_state, ai_events.output_state, ai_events.tools), notEquals(ai_events.event, '$ai_trace')))) AS events,- argMinIf(ai_events.input_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS input_state,- argMinIf(ai_events.output_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), argMin(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'))) AS trace_name- FROM ai_events- INNER JOIN- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS ai_events__pdi___person_id,- argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id,- person_distinct_id2.distinct_id AS distinct_id- FROM person_distinct_id2- WHERE equals(person_distinct_id2.team_id, 99999)- GROUP BY person_distinct_id2.distinct_id- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS ai_events__pdi ON equals(ai_events.distinct_id, ai_events__pdi.distinct_id)- LEFT JOIN- (SELECT person.id AS id,- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'bar'), ''), 'null'), '^"|"$', '') AS properties___bar- FROM person- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),- (SELECT person.id AS id, max(person.version) AS version- 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, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS ai_events__pdi__person ON equals(ai_events__pdi.ai_events__pdi___person_id, ai_events__pdi__person.id)- WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(ai_events__pdi__person.properties___bar, 'baz'), 0)))- GROUP BY ai_events.trace_id+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT ai_events.uuid AS uuid,+ ai_events.event AS event,+ toTimeZone(ai_events.timestamp, 'UTC') AS timestamp,+ ai_events.distinct_id AS distinct_id,+ ai_events.properties AS properties,+ ai_events.trace_id AS trace_id,+ ai_events.session_id AS session_id,+ ai_events.parent_id AS parent_id,+ ai_events.span_name AS span_name,+ ai_events.trace_name AS trace_name,+ ai_events.latency AS latency,+ ai_events.input_tokens AS input_tokens,+ ai_events.output_tokens AS output_tokens,+ ai_events.input_cost_usd AS input_cost_usd,+ ai_events.output_cost_usd AS output_cost_usd,+ ai_events.total_cost_usd AS total_cost_usd,+ ai_events.input AS input,+ ai_events.output AS output,+ ai_events.output_choices AS output_choices,+ ai_events.input_state AS input_state,+ ai_events.output_state AS output_state,+ ai_events.tools AS tools+ FROM ai_events+ INNER JOIN+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS ai_events__pdi___person_id,+ argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id,+ person_distinct_id2.distinct_id AS distinct_id+ FROM person_distinct_id2+ WHERE equals(person_distinct_id2.team_id, 99999)+ GROUP BY person_distinct_id2.distinct_id+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS ai_events__pdi ON equals(ai_events.distinct_id, ai_events__pdi.distinct_id)+ LEFT JOIN+ (SELECT person.id AS id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'bar'), ''), 'null'), '^"|"$', '') AS properties___bar+ FROM person+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),+ (SELECT person.id AS id, max(person.version) AS version+ 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, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS ai_events__pdi__person ON equals(ai_events__pdi.ai_events__pdi___person_id, ai_events__pdi__person.id)+ WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(ai_events__pdi__person.properties___bar, 'baz'), 0)))+ LIMIT 1 BY ai_events.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,@@ -230,43 +375,67 @@# ---# name: TestTraceQueryRunner.test_person_property_filters.1'''- SELECT ai_events.trace_id AS id,- any(ai_events.session_id) AS ai_session_id,- min(toTimeZone(ai_events.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(ai_events.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), ''), argMin(ai_events.distinct_id, toTimeZone(ai_events.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(ai_events.latency, 0), 0), notEquals(ai_events.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(ai_events.latency, 0), 0), equals(ai_events.event, '$ai_generation'))), 0)), sumIf(ai_events.latency, and(equals(ai_events.event, '$ai_generation'), ifNull(greater(ai_events.latency, 0), 0))), sumIf(ai_events.latency, or(isNull(ai_events.parent_id), ifNull(equals(ai_events.parent_id, ai_events.trace_id), isNull(ai_events.parent_id)- and isNull(ai_events.trace_id))))), 2) AS total_latency,- nullIf(sumIf(ai_events.input_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(ai_events.output_tokens, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(ai_events.input_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(ai_events.output_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(ai_events.total_cost_usd, in(ai_events.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(ai_events.uuid, ai_events.event, toTimeZone(ai_events.timestamp, 'UTC'), ai_events.properties, ai_events.input, ai_events.output, ai_events.output_choices, ai_events.input_state, ai_events.output_state, ai_events.tools), notEquals(ai_events.event, '$ai_trace')))) AS events,- argMinIf(ai_events.input_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS input_state,- argMinIf(ai_events.output_state, toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'), equals(ai_events.event, '$ai_trace')), argMin(ifNull(nullIf(ai_events.span_name, ''), nullIf(ai_events.trace_name, '')), toTimeZone(ai_events.timestamp, 'UTC'))) AS trace_name- FROM ai_events- INNER JOIN- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS ai_events__pdi___person_id,- argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id,- person_distinct_id2.distinct_id AS distinct_id- FROM person_distinct_id2- WHERE equals(person_distinct_id2.team_id, 99999)- GROUP BY person_distinct_id2.distinct_id- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS ai_events__pdi ON equals(ai_events.distinct_id, ai_events__pdi.distinct_id)- LEFT JOIN- (SELECT person.id AS id,- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'bar'), ''), 'null'), '^"|"$', '') AS properties___bar- FROM person- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),- (SELECT person.id AS id, max(person.version) AS version- 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, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS ai_events__pdi__person ON equals(ai_events__pdi.ai_events__pdi___person_id, ai_events__pdi__person.id)- WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(ai_events__pdi__person.properties___bar, 'foo'), 0)))- GROUP BY ai_events.trace_id+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT ai_events.uuid AS uuid,+ ai_events.event AS event,+ toTimeZone(ai_events.timestamp, 'UTC') AS timestamp,+ ai_events.distinct_id AS distinct_id,+ ai_events.properties AS properties,+ ai_events.trace_id AS trace_id,+ ai_events.session_id AS session_id,+ ai_events.parent_id AS parent_id,+ ai_events.span_name AS span_name,+ ai_events.trace_name AS trace_name,+ ai_events.latency AS latency,+ ai_events.input_tokens AS input_tokens,+ ai_events.output_tokens AS output_tokens,+ ai_events.input_cost_usd AS input_cost_usd,+ ai_events.output_cost_usd AS output_cost_usd,+ ai_events.total_cost_usd AS total_cost_usd,+ ai_events.input AS input,+ ai_events.output AS output,+ ai_events.output_choices AS output_choices,+ ai_events.input_state AS input_state,+ ai_events.output_state AS output_state,+ ai_events.tools AS tools+ FROM ai_events+ INNER JOIN+ (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS ai_events__pdi___person_id,+ argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id,+ person_distinct_id2.distinct_id AS distinct_id+ FROM person_distinct_id2+ WHERE equals(person_distinct_id2.team_id, 99999)+ GROUP BY person_distinct_id2.distinct_id+ HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS ai_events__pdi ON equals(ai_events.distinct_id, ai_events__pdi.distinct_id)+ LEFT JOIN+ (SELECT person.id AS id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'bar'), ''), 'null'), '^"|"$', '') AS properties___bar+ FROM person+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),+ (SELECT person.id AS id, max(person.version) AS version+ 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, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS ai_events__pdi__person ON equals(ai_events__pdi.ai_events__pdi___person_id, ai_events__pdi__person.id)+ WHERE and(equals(ai_events.team_id, 99999), in(ai_events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(ai_events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(ai_events.trace_id, 'trace1'), ifNull(equals(ai_events__pdi__person.properties___bar, 'foo'), 0)))+ LIMIT 1 BY ai_events.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,@@ -283,42 +452,66 @@# ---# name: TestTraceQueryRunner.test_person_property_filters.2'''- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '') AS id,- any(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_session_id'), ''), 'null'), '^"|"$', '')) AS ai_session_id,- min(toTimeZone(__ai_events_fallback.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(__ai_events_fallback.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(__ai_events_fallback.distinct_id, toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')), ''), argMin(__ai_events_fallback.distinct_id, toTimeZone(__ai_events_fallback.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), 0), 0), notEquals(__ai_events_fallback.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), 0), 0), equals(__ai_events_fallback.event, '$ai_generation'))), 0)), sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), and(equals(__ai_events_fallback.event, '$ai_generation'), ifNull(greater(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), 0), 0))), sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), or(isNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_parent_id'), ''), 'null'), '^"|"$', '')), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_parent_id'), ''), 'null'), '^"|"$', ''), replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), isNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_parent_id'), ''), 'null'), '^"|"$', ''))- and isNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')))))), 2) AS total_latency,- nullIf(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_tokens'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_tokens'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_total_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(__ai_events_fallback.uuid, __ai_events_fallback.event, toTimeZone(__ai_events_fallback.timestamp, 'UTC'), __ai_events_fallback.properties, JSONExtractRaw(__ai_events_fallback.properties, '$ai_input'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_output'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_choices'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_state'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_state'), JSONExtractRaw(__ai_events_fallback.properties, '$ai_tools')), notEquals(__ai_events_fallback.event, '$ai_trace')))) AS events,- argMinIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_state'), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')) AS input_state,- argMinIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_state'), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_span_name'), ''), 'null'), '^"|"$', ''), ''), nullIf(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_name'), ''), 'null'), '^"|"$', ''), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')), argMin(ifNull(nullIf(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_span_name'), ''), 'null'), '^"|"$', ''), ''), nullIf(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_name'), ''), 'null'), '^"|"$', ''), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'))) AS trace_name- FROM events AS __ai_events_fallback- 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 __ai_events_fallback__override ON equals(__ai_events_fallback.distinct_id, __ai_events_fallback__override.distinct_id)- LEFT JOIN- (SELECT person.id AS id,- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'bar'), ''), 'null'), '^"|"$', '') AS properties___bar- FROM person- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),- (SELECT person.id AS id, max(person.version) AS version- 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, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS __ai_events_fallback__person ON equals(if(not(empty(__ai_events_fallback__override.distinct_id)), __ai_events_fallback__override.person_id, __ai_events_fallback.person_id), __ai_events_fallback__person.id)- WHERE and(equals(__ai_events_fallback.team_id, 99999), in(__ai_events_fallback.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), 'trace1'), 0), ifNull(equals(__ai_events_fallback__person.properties___bar, 'foo'), 0)))- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT __ai_events_fallback.uuid AS uuid,+ __ai_events_fallback.event AS event,+ toTimeZone(__ai_events_fallback.timestamp, 'UTC') AS timestamp,+ __ai_events_fallback.distinct_id AS distinct_id,+ __ai_events_fallback.properties AS properties,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '') AS trace_id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_session_id'), ''), 'null'), '^"|"$', '') AS session_id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_parent_id'), ''), 'null'), '^"|"$', '') AS parent_id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_span_name'), ''), 'null'), '^"|"$', '') AS span_name,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_name'), ''), 'null'), '^"|"$', '') AS trace_name,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_latency'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS latency,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_tokens'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS input_tokens,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_tokens'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS output_tokens,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS input_cost_usd,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS output_cost_usd,+ accurateCastOrNull(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_total_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), 'Float64') AS total_cost_usd,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_input') AS input,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_output') AS output,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_choices') AS output_choices,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_input_state') AS input_state,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_output_state') AS output_state,+ JSONExtractRaw(__ai_events_fallback.properties, '$ai_tools') AS tools+ FROM events AS __ai_events_fallback+ 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 __ai_events_fallback__override ON equals(__ai_events_fallback.distinct_id, __ai_events_fallback__override.distinct_id)+ LEFT JOIN+ (SELECT person.id AS id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'bar'), ''), 'null'), '^"|"$', '') AS properties___bar+ FROM person+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),+ (SELECT person.id AS id, max(person.version) AS version+ 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, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS __ai_events_fallback__person ON equals(if(not(empty(__ai_events_fallback__override.distinct_id)), __ai_events_fallback__override.person_id, __ai_events_fallback.person_id), __ai_events_fallback__person.id)+ WHERE and(equals(__ai_events_fallback.team_id, 99999), in(__ai_events_fallback.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(__ai_events_fallback.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), 'trace1'), 0), ifNull(equals(__ai_events_fallback__person.properties___bar, 'foo'), 0)))+ LIMIT 1 BY __ai_events_fallback.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,@@ -335,43 +528,68 @@# ---# name: TestTraceQueryRunner.test_person_property_filters[new_events_schema]'''- SELECT __ai_events_fallback.properties.`$ai_trace_id` AS id,- any(__ai_events_fallback.properties.`$ai_session_id`) AS ai_session_id,- min(toTimeZone(__ai_events_fallback.timestamp, 'UTC')) AS first_timestamp,- max(toTimeZone(__ai_events_fallback.timestamp, 'UTC')) AS last_timestamp,- ifNull(nullIf(argMinIf(__ai_events_fallback.distinct_id, toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')), ''), argMin(__ai_events_fallback.distinct_id, toTimeZone(__ai_events_fallback.timestamp, 'UTC'))) AS first_distinct_id,- round(if(and(equals(countIf(and(ifNull(greater(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), 0), 0), notEquals(__ai_events_fallback.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), 0), 0), equals(__ai_events_fallback.event, '$ai_generation'))), 0)), sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), and(equals(__ai_events_fallback.event, '$ai_generation'), ifNull(greater(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), 0), 0))), sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64'), or(isNull(__ai_events_fallback.properties.`$ai_parent_id`), equals(__ai_events_fallback.properties.`$ai_parent_id`, __ai_events_fallback.properties.`$ai_trace_id`)))), 2) AS total_latency,- nullIf(sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_tokens`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_tokens`), if(isNull(__ai_events_fallback.properties.`$ai_input_tokens`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_input_tokens`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_input_tokens`), toString(__ai_events_fallback.properties.`$ai_input_tokens`))))), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,- nullIf(sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_tokens`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_tokens`), if(isNull(__ai_events_fallback.properties.`$ai_output_tokens`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_output_tokens`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_output_tokens`), toString(__ai_events_fallback.properties.`$ai_output_tokens`))))), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_cost_usd`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_cost_usd`), if(isNull(__ai_events_fallback.properties.`$ai_input_cost_usd`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_input_cost_usd`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_input_cost_usd`), toString(__ai_events_fallback.properties.`$ai_input_cost_usd`))))), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_cost_usd`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_cost_usd`), if(isNull(__ai_events_fallback.properties.`$ai_output_cost_usd`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_output_cost_usd`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_output_cost_usd`), toString(__ai_events_fallback.properties.`$ai_output_cost_usd`))))), 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,- nullIf(round(sumIf(accurateCastOrNull(accurateCastOrNull(__ai_events_fallback.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64'), in(__ai_events_fallback.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,- arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(__ai_events_fallback.uuid, __ai_events_fallback.event, toTimeZone(__ai_events_fallback.timestamp, 'UTC'), concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != 'null'- AND NOT (kv.2 = '[]'- AND has(['$active_feature_flags', '$exception_functions', '$exception_sources', '$exception_types', '$exception_values'], kv.1)), JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.properties)))), ','), '}'), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input`), if(isNull(__ai_events_fallback.properties.`$ai_input`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output`), if(isNull(__ai_events_fallback.properties.`$ai_output`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_choices`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_choices`), if(isNull(__ai_events_fallback.properties.`$ai_output_choices`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_choices`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_choices`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_choices`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), if(isNull(__ai_events_fallback.properties.`$ai_input_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input_state`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), if(isNull(__ai_events_fallback.properties.`$ai_output_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_state`)))), '')), JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_tools`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_tools`), if(isNull(__ai_events_fallback.properties.`$ai_tools`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_tools`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_tools`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_tools`)))), ''))), notEquals(__ai_events_fallback.event, '$ai_trace')))) AS events,- argMinIf(JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), if(isNull(__ai_events_fallback.properties.`$ai_input_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input_state`)))), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')) AS input_state,- argMinIf(JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), if(isNull(__ai_events_fallback.properties.`$ai_output_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_state`)))), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')) AS output_state,- ifNull(argMinIf(ifNull(nullIf(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), if(isNull(__ai_events_fallback.properties.`$ai_span_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_span_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_span_name`), toString(__ai_events_fallback.properties.`$ai_span_name`))))), ''), nullIf(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), if(isNull(__ai_events_fallback.properties.`$ai_trace_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_trace_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_trace_name`), toString(__ai_events_fallback.properties.`$ai_trace_name`))))), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'), equals(__ai_events_fallback.event, '$ai_trace')), argMin(ifNull(nullIf(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), if(isNull(__ai_events_fallback.properties.`$ai_span_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_span_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_span_name`), toString(__ai_events_fallback.properties.`$ai_span_name`))))), ''), nullIf(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), if(isNull(__ai_events_fallback.properties.`$ai_trace_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_trace_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_trace_name`), toString(__ai_events_fallback.properties.`$ai_trace_name`))))), '')), toTimeZone(__ai_events_fallback.timestamp, 'UTC'))) AS trace_name- FROM events_json AS __ai_events_fallback- 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 __ai_events_fallback__override ON equals(__ai_events_fallback.distinct_id, __ai_events_fallback__override.distinct_id)- LEFT JOIN- (SELECT person.id AS id,- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'bar'), ''), 'null'), '^"|"$', '') AS properties___bar- FROM person- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),- (SELECT person.id AS id, max(person.version) AS version- 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, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS __ai_events_fallback__person ON equals(if(not(empty(__ai_events_fallback__override.distinct_id)), __ai_events_fallback__override.person_id, __ai_events_fallback.person_id), __ai_events_fallback__person.id)- WHERE and(equals(__ai_events_fallback.team_id, 99999), in(__ai_events_fallback.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(__ai_events_fallback.properties.`$ai_trace_id`, 'trace1'), ifNull(equals(__ai_events_fallback__person.properties___bar, 'foo'), 0)))- GROUP BY __ai_events_fallback.properties.`$ai_trace_id`+ SELECT deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,+ ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,+ round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)+ and isNull(deduped.trace_id))))), 2) AS total_latency,+ nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,+ nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,+ nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,+ nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,+ nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,+ arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,+ argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,+ argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,+ ifNull(argMinIf(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp, equals(deduped.event, '$ai_trace')), argMin(ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')), deduped.timestamp)) AS trace_name+ FROM+ (SELECT __ai_events_fallback.uuid AS uuid,+ __ai_events_fallback.event AS event,+ toTimeZone(__ai_events_fallback.timestamp, 'UTC') AS timestamp,+ __ai_events_fallback.distinct_id AS distinct_id,+ concat('{', arrayStringConcat(arrayMap(kv -> concat(toJSONString(kv.1), ':', kv.2), arrayFilter(kv -> kv.2 != 'null'+ AND NOT (kv.2 = '[]'+ AND has(['$active_feature_flags', '$exception_functions', '$exception_sources', '$exception_types', '$exception_values'], kv.1)), JSONExtractKeysAndValuesRaw(toJSONString(__ai_events_fallback.properties)))), ','), '}') AS properties,+ __ai_events_fallback.properties.`$ai_trace_id` AS trace_id,+ __ai_events_fallback.properties.`$ai_session_id` AS session_id,+ __ai_events_fallback.properties.`$ai_parent_id` AS parent_id,+ if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_span_name`), if(isNull(__ai_events_fallback.properties.`$ai_span_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_span_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_span_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_span_name`), toString(__ai_events_fallback.properties.`$ai_span_name`))))) AS span_name,+ if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_trace_name`), if(isNull(__ai_events_fallback.properties.`$ai_trace_name`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_trace_name`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_trace_name`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_trace_name`), toString(__ai_events_fallback.properties.`$ai_trace_name`))))) AS trace_name,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_latency`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_latency`), if(isNull(__ai_events_fallback.properties.`$ai_latency`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_latency`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_latency`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_latency`), toString(__ai_events_fallback.properties.`$ai_latency`))))), 'Float64'), 'Float64') AS latency,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_tokens`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_tokens`), if(isNull(__ai_events_fallback.properties.`$ai_input_tokens`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_input_tokens`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_tokens`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_input_tokens`), toString(__ai_events_fallback.properties.`$ai_input_tokens`))))), 'Float64'), 'Float64') AS input_tokens,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_tokens`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_tokens`), if(isNull(__ai_events_fallback.properties.`$ai_output_tokens`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_output_tokens`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_tokens`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_output_tokens`), toString(__ai_events_fallback.properties.`$ai_output_tokens`))))), 'Float64'), 'Float64') AS output_tokens,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_cost_usd`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_cost_usd`), if(isNull(__ai_events_fallback.properties.`$ai_input_cost_usd`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_input_cost_usd`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_cost_usd`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_input_cost_usd`), toString(__ai_events_fallback.properties.`$ai_input_cost_usd`))))), 'Float64'), 'Float64') AS input_cost_usd,+ accurateCastOrNull(accurateCastOrNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_cost_usd`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_cost_usd`), if(isNull(__ai_events_fallback.properties.`$ai_output_cost_usd`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'DateTime'), replaceOne(toString(__ai_events_fallback.properties.`$ai_output_cost_usd`), ' ', 'T'), if(or(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Array'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Map'), startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_cost_usd`), 'Tuple')), toJSONString(__ai_events_fallback.properties.`$ai_output_cost_usd`), toString(__ai_events_fallback.properties.`$ai_output_cost_usd`))))), 'Float64'), 'Float64') AS output_cost_usd,+ accurateCastOrNull(accurateCastOrNull(__ai_events_fallback.properties.`$ai_total_cost_usd`, 'Float64'), 'Float64') AS total_cost_usd,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input`), if(isNull(__ai_events_fallback.properties.`$ai_input`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input`)))), '')) AS input,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output`), if(isNull(__ai_events_fallback.properties.`$ai_output`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output`)))), '')) AS output,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_choices`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_choices`), if(isNull(__ai_events_fallback.properties.`$ai_output_choices`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_choices`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_choices`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_choices`)))), '')) AS output_choices,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_input_state`), if(isNull(__ai_events_fallback.properties.`$ai_input_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_input_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_input_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_input_state`)))), '')) AS input_state,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_output_state`), if(isNull(__ai_events_fallback.properties.`$ai_output_state`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_output_state`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_output_state`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_output_state`)))), '')) AS output_state,+ JSONExtractRaw(ifNull(if(notEquals(toJSONString(__ai_events_fallback.properties.^`$ai_tools`), '{}'), toJSONString(__ai_events_fallback.properties.^`$ai_tools`), if(isNull(__ai_events_fallback.properties.`$ai_tools`), NULL, if(startsWith(dynamicType(__ai_events_fallback.properties.`$ai_tools`), 'DateTime'), concat('"', ifNull(toString(replaceOne(toString(__ai_events_fallback.properties.`$ai_tools`), ' ', 'T')), ''), '"'), toJSONString(__ai_events_fallback.properties.`$ai_tools`)))), '')) AS tools+ FROM events_json AS __ai_events_fallback+ 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 __ai_events_fallback__override ON equals(__ai_events_fallback.distinct_id, __ai_events_fallback__override.distinct_id)+ LEFT JOIN+ (SELECT person.id AS id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'bar'), ''), 'null'), '^"|"$', '') AS properties___bar+ FROM person+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),+ (SELECT person.id AS id, max(person.version) AS version+ 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, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS __ai_events_fallback__person ON equals(if(not(empty(__ai_events_fallback__override.distinct_id)), __ai_events_fallback__override.person_id, __ai_events_fallback.person_id), __ai_events_fallback__person.id)+ WHERE and(equals(__ai_events_fallback.team_id, 99999), in(__ai_events_fallback.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(greaterOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(__ai_events_fallback.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC'))), equals(__ai_events_fallback.properties.`$ai_trace_id`, 'trace1'), ifNull(equals(__ai_events_fallback__person.properties___bar, 'foo'), 0)))+ LIMIT 1 BY __ai_events_fallback.uuid) AS deduped+ GROUP BY deduped.trace_idLIMIT 1 SETTINGS readonly=2,max_execution_time=60,allow_experimental_object_type=1,diff --git a/posthog/hogql_queries/ai/test/test_trace_query_runner.py b/posthog/hogql_queries/ai/test/test_trace_query_runner.pyindex 7e33d140810e..30adad56eccb 100644--- a/posthog/hogql_queries/ai/test/test_trace_query_runner.py+++ b/posthog/hogql_queries/ai/test/test_trace_query_runner.py@@ -20,6 +20,7 @@ from posthog.schema import (from posthog.hogql_queries.ai.trace_query_runner import TraceQueryRunnerfrom posthog.models import PropertyDefinition, Team+from posthog.models.ai_events.test_util import bulk_create_ai_eventsfrom products.event_definitions.backend.models.property_definition import PropertyType@@ -874,6 +875,70 @@ class TestTraceQueryRunner(ClickhouseTestMixin, BaseTest):self.assertEqual(len(response.results), 1)self.assertEqual(len(response.results[0].events), 1)+ def test_deduplicates_ai_events_rows_for_the_same_generation(self):+ trace_id = str(uuid.uuid4())+ generation_uuid = str(uuid.uuid4())+ timestamp = datetime(2024, 12, 1, 0, 0, tzinfo=UTC)+ generation_properties = {+ "$ai_trace_id": trace_id,+ "$ai_parent_id": trace_id,+ "$ai_latency": 3.9,+ "$ai_input_tokens": 582,+ "$ai_output_tokens": 353,+ "$ai_input_cost_usd": 0.4,+ "$ai_output_cost_usd": 0.6,+ "$ai_total_cost_usd": 1.0,+ "$ai_input": [{"role": "user", "content": "Foo"}],+ }++ # At-least-once delivery lands one logical generation twice. The events table absorbs+ # this (uuid is in its sorting key), but ai_events is a plain MergeTree that never+ # dedupes, so scalar totals must be deduplicated before aggregation.+ bulk_create_ai_events(+ [+ {+ "event": "$ai_trace",+ "team": self.team,+ "distinct_id": "person1",+ "timestamp": timestamp,+ "properties": {"$ai_trace_id": trace_id, "$ai_trace_name": "runnable"},+ },+ {+ "event": "$ai_generation",+ "team": self.team,+ "distinct_id": "person1",+ "timestamp": timestamp,+ "event_uuid": generation_uuid,+ "properties": generation_properties,+ },+ {+ "event": "$ai_generation",+ "team": self.team,+ "distinct_id": "person1",+ "timestamp": timestamp,+ "event_uuid": generation_uuid,+ "properties": generation_properties,+ },+ ]+ )++ response = TraceQueryRunner(+ team=self.team,+ query=TraceQuery(+ traceId=trace_id,+ dateRange=DateRange(date_from="2024-12-01T00:00:00Z", date_to="2024-12-01T00:10:00Z"),+ ),+ ).calculate()++ self.assertEqual(len(response.results), 1)+ trace = response.results[0]+ self.assertEqual(len(trace.events), 1)+ self.assertEqual(trace.inputTokens, 582)+ self.assertEqual(trace.outputTokens, 353)+ self.assertEqual(trace.totalCost, 1.0)+ self.assertEqual(trace.totalLatency, 3.9)+ self.assertEqual(trace.events[0].properties.get("$ai_input"), [{"role": "user", "content": "Foo"}])+def test_trace_name_from_trace_event(self):"""Test that trace_name comes from $ai_trace events when they exist."""_create_person(distinct_ids=["person1"], team=self.team)diff --git a/posthog/hogql_queries/ai/trace_query_runner.py b/posthog/hogql_queries/ai/trace_query_runner.pyindex b2b84eb9e6d7..17abd91f0767 100644--- a/posthog/hogql_queries/ai/trace_query_runner.py+++ b/posthog/hogql_queries/ai/trace_query_runner.py@@ -113,86 +113,100 @@ class TraceQueryRunner(AnalyticsQueryRunner[TraceQueryResponse]):return self._build_query()def _build_query(self) -> ast.SelectQuery:+ # ai_events is a plain MergeTree fed by at-least-once ingestion, so one logical event+ # can land as several rows (the shared events table absorbs this via ReplacingMergeTree,+ # this table cannot). Collapse to one row per uuid before aggregating: otherwise sumIf+ # double-counts tokens/cost and the tree renders a node per duplicate row.query = parse_select("""SELECT- trace_id AS id,- any(session_id) AS ai_session_id,- min(timestamp) AS first_timestamp,- max(timestamp) AS last_timestamp,+ deduped.trace_id AS id,+ any(deduped.session_id) AS ai_session_id,+ min(deduped.timestamp) AS first_timestamp,+ max(deduped.timestamp) AS last_timestamp,ifNull(- nullIf(argMinIf(distinct_id, timestamp, event = '$ai_trace'), ''),- argMin(distinct_id, timestamp)+ nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, deduped.event = '$ai_trace'), ''),+ argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,round(CASE-- If all events with latency are generations, sum them all- WHEN countIf(latency > 0 AND event != '$ai_generation') = 0- AND countIf(latency > 0 AND event = '$ai_generation') > 0- THEN sumIf(latency,- event = '$ai_generation' AND latency > 0+ WHEN countIf(deduped.latency > 0 AND deduped.event != '$ai_generation') = 0+ AND countIf(deduped.latency > 0 AND deduped.event = '$ai_generation') > 0+ THEN sumIf(deduped.latency,+ deduped.event = '$ai_generation' AND deduped.latency > 0)-- Otherwise sum the direct children of the trace- ELSE sumIf(latency,- parent_id IS NULL- OR parent_id = trace_id+ ELSE sumIf(deduped.latency,+ deduped.parent_id IS NULL+ OR deduped.parent_id = deduped.trace_id)END, 2) AS total_latency,- nullIf(sumIf(input_tokens,- event IN ('$ai_generation', '$ai_embedding')+ nullIf(sumIf(deduped.input_tokens,+ deduped.event IN ('$ai_generation', '$ai_embedding')), 0) AS input_tokens,- nullIf(sumIf(output_tokens,- event IN ('$ai_generation', '$ai_embedding')+ nullIf(sumIf(deduped.output_tokens,+ deduped.event IN ('$ai_generation', '$ai_embedding')), 0) AS output_tokens,nullIf(round(- sumIf(input_cost_usd,- event IN ('$ai_generation', '$ai_embedding')+ sumIf(deduped.input_cost_usd,+ deduped.event IN ('$ai_generation', '$ai_embedding')), 10), 0) AS input_cost,nullIf(round(- sumIf(output_cost_usd,- event IN ('$ai_generation', '$ai_embedding')+ sumIf(deduped.output_cost_usd,+ deduped.event IN ('$ai_generation', '$ai_embedding')), 10), 0) AS output_cost,nullIf(round(- sumIf(total_cost_usd,- event IN ('$ai_generation', '$ai_embedding')+ sumIf(deduped.total_cost_usd,+ deduped.event IN ('$ai_generation', '$ai_embedding')), 10), 0) AS total_cost,arrayDistinct(arraySort(x -> x.3,groupArrayIf(- tuple(uuid, event, timestamp, properties,- input, output, output_choices, input_state, output_state, tools),- event != '$ai_trace'+ tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties,+ deduped.input, deduped.output, deduped.output_choices,+ deduped.input_state, deduped.output_state, deduped.tools),+ deduped.event != '$ai_trace'))) AS events,- argMinIf(input_state,- timestamp, event = '$ai_trace'+ argMinIf(deduped.input_state,+ deduped.timestamp, deduped.event = '$ai_trace') AS input_state,- argMinIf(output_state,- timestamp, event = '$ai_trace'+ argMinIf(deduped.output_state,+ deduped.timestamp, deduped.event = '$ai_trace') AS output_state,ifNull(argMinIf(- ifNull(nullIf(span_name, ''), nullIf(trace_name, '')),- timestamp,- event = '$ai_trace'+ ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')),+ deduped.timestamp,+ deduped.event = '$ai_trace'),argMin(- ifNull(nullIf(span_name, ''), nullIf(trace_name, '')),- timestamp,+ ifNull(nullIf(deduped.span_name, ''), nullIf(deduped.trace_name, '')),+ deduped.timestamp,)) AS trace_name- FROM posthog.ai_events AS ai_events- WHERE event IN (- '$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace'- )- AND {filter_conditions}- GROUP BY trace_id+ FROM (+ SELECT+ uuid, event, timestamp, distinct_id, properties,+ trace_id, session_id, parent_id, span_name, trace_name,+ latency, input_tokens, output_tokens,+ input_cost_usd, output_cost_usd, total_cost_usd,+ input, output, output_choices, input_state, output_state, tools+ FROM posthog.ai_events AS ai_events+ WHERE event IN (+ '$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace'+ )+ AND {filter_conditions}+ LIMIT 1 BY uuid+ ) AS deduped+ GROUP BY deduped.trace_idLIMIT 1""",)@@ -202,7 +216,7 @@ class TraceQueryRunner(AnalyticsQueryRunner[TraceQueryResponse]):return {**super().get_cache_payload(),# When the response schema changes, increment this version to invalidate the cache.- "schema_version": 9,+ "schema_version": 10,}@cached_property