: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): make a calendar-day date_to cover the whole day (#104976)
posthog[bot]a669184e2cad
diff --git a/posthog/hogql_queries/ai/test/__snapshots__/test_traces_query_runner.ambr b/posthog/hogql_queries/ai/test/__snapshots__/test_traces_query_runner.ambrindex 87d101c616d5..389cad8c6754 100644--- a/posthog/hogql_queries/ai/test/__snapshots__/test_traces_query_runner.ambr+++ b/posthog/hogql_queries/ai/test/__snapshots__/test_traces_query_runner.ambr@@ -11,7 +11,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC'))))))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -83,7 +83,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC'))))))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -155,7 +155,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC')))), equals(events.`$group_0`, 'org:acme')))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -227,7 +227,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC')))), equals(events.`$group_0`, 'org:widgets')))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -299,7 +299,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC')))), equals(events.`$group_1`, 'project:alpha')))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -371,7 +371,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC')))), equals(events.`$group_0`, 'nonexistent')))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -400,7 +400,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC'))))))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 5)LIMIT 100 SETTINGS readonly=2,@@ -472,7 +472,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC'))))))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 9)LIMIT 100 SETTINGS readonly=2,@@ -544,7 +544,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-08 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2025-01-16 00:10:59', 'UTC'))))))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-16 00:00:59', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('today', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2025-01-09 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 13)LIMIT 100 SETTINGS readonly=2,@@ -616,7 +616,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC')))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'bar'), 0)))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:10:00', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('2024-12-01 00:10:00.000000', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -688,7 +688,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC')))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'baz'), 0)))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:10:00', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('2024-12-01 00:10:00.000000', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -760,7 +760,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC')))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'barz'), 0)))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:10:00', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('2024-12-01 00:10:00.000000', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -789,7 +789,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC')))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_span_name'), ''), 'null'), '^"|"$', ''), 'runnable'), 0)))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:10:00', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('2024-12-01 00:10:00.000000', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -861,7 +861,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC')))), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'bar'), 0)))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:10:00', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('2024-12-01 00:10:00.000000', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,@@ -933,7 +933,7 @@FROM eventsWHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_span', '$ai_generation', '$ai_embedding', '$ai_metric', '$ai_feedback', '$ai_trace')), and(isNotNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')), ifNull(notEquals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', ''), ''), 1), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-11-30 23:50:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-12-01 00:20:00', 'UTC')))), and(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_span_name'), ''), 'null'), '^"|"$', ''), 'runnable'), 0), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'foo'), ''), 'null'), '^"|"$', ''), 'bar'), 0))))GROUP BY trace_id- HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:10:00', 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))+ HAVING and(lessOrEquals(min(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime64('2024-12-01 00:10:00.000000', 6, 'UTC'))), greaterOrEquals(max(toTimeZone(events.timestamp, 'UTC')), assumeNotNull(toDateTime('2024-12-01 00:00:00', 'UTC'))))ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 101)LIMIT 100 SETTINGS readonly=2,diff --git a/posthog/hogql_queries/ai/test/test_trace_neighbors_query_runner.py b/posthog/hogql_queries/ai/test/test_trace_neighbors_query_runner.pyindex 2a44fa2e51b6..8bebfc8a3dd4 100644--- a/posthog/hogql_queries/ai/test/test_trace_neighbors_query_runner.py+++ b/posthog/hogql_queries/ai/test/test_trace_neighbors_query_runner.py@@ -290,6 +290,35 @@ class TestTraceNeighborsQueryRunner(ClickhouseTestMixin, BaseTest):self.assertIsNone(response.olderTraceId)self.assertEqual(response.newerTraceId, "trace_recent")+ def test_calendar_day_date_to_covers_the_whole_day(self):+ """A `date_to` naming a calendar day reaches the traces recorded later that day."""+ _create_person(distinct_ids=["person1"], team=self.team)++ _create_ai_generation_event(+ distinct_id="person1",+ trace_id="trace_morning",+ team=self.team,+ timestamp=datetime(2025, 1, 15, 9, 0, tzinfo=UTC),+ )+ _create_ai_generation_event(+ distinct_id="person1",+ trace_id="trace_evening",+ team=self.team,+ timestamp=datetime(2025, 1, 15, 20, 0, tzinfo=UTC),+ )++ response = TraceNeighborsQueryRunner(+ team=self.team,+ query=TraceNeighborsQuery(+ traceId="trace_morning",+ timestamp=datetime(2025, 1, 15, 9, 0, tzinfo=UTC).isoformat(),+ dateRange=DateRange(date_from="2025-01-15", date_to="2025-01-15"),+ ),+ ).calculate()++ self.assertEqual(response.newerTraceId, "trace_evening")+ self.assertIsNone(response.olderTraceId)+def test_default_date_range_window(self):"""Test that default date range is ±3 days around trace timestamp."""_create_person(distinct_ids=["person1"], team=self.team)diff --git a/posthog/hogql_queries/ai/test/test_trace_query_runner.py b/posthog/hogql_queries/ai/test/test_trace_query_runner.pyindex 2875edac9e6a..d1cd51295b16 100644--- a/posthog/hogql_queries/ai/test/test_trace_query_runner.py+++ b/posthog/hogql_queries/ai/test/test_trace_query_runner.py@@ -1189,6 +1189,34 @@ class TestTraceQueryRunner(ClickhouseTestMixin, BaseTest):self.assertEqual(bounded.inputTokens, 10)self.assertEqual(bounded.outputTokens, 10)+ def test_bound_events_to_date_range_keeps_the_whole_calendar_day(self):+ trace_id = str(uuid.uuid4())+ bulk_create_ai_events(+ [+ {+ "event": "$ai_generation",+ "team": self.team,+ "distinct_id": "person1",+ "timestamp": datetime(2024, 12, 1, 14, 30, tzinfo=UTC),+ "properties": {"$ai_trace_id": trace_id, "$ai_parent_id": trace_id, "$ai_latency": 1.0},+ }+ ]+ )++ trace = (+ TraceQueryRunner(+ team=self.team,+ query=TraceQuery(+ traceId=trace_id,+ dateRange=DateRange(date_from="2024-12-01", date_to="2024-12-01"),+ ),+ bound_events_to_date_range=True,+ )+ .calculate()+ .results[0]+ )+ self.assertEqual(len(trace.events), 1)+def test_bound_events_to_date_range_keeps_sub_second_events(self):trace_id = str(uuid.uuid4())# A cutoff rounded down to the whole second would drop this event.diff --git a/posthog/hogql_queries/ai/test/test_traces_query_runner.py b/posthog/hogql_queries/ai/test/test_traces_query_runner.pyindex 956459fe37e9..511d926116e5 100644--- a/posthog/hogql_queries/ai/test/test_traces_query_runner.py+++ b/posthog/hogql_queries/ai/test/test_traces_query_runner.py@@ -669,6 +669,30 @@ class TestTracesQueryRunner(ClickhouseTestMixin, BaseTest):self.assertEqual(len(response.results), 1)self.assertEqual(response.results[0].id, "trace3")+ @time_machine.travel("2025-01-16T00:00:00Z", tick=False)+ def test_single_calendar_day_range_covers_the_whole_day(self):+ _create_person(distinct_ids=["person1"], team=self.team)+ _create_ai_generation_event(+ distinct_id="person1",+ trace_id="trace1",+ team=self.team,+ timestamp=datetime(2025, 1, 15, 14, 30),+ )+ _create_ai_generation_event(+ distinct_id="person1",+ trace_id="trace2",+ team=self.team,+ timestamp=datetime(2025, 1, 15, 23, 59, 59, 500000),+ )++ response = TracesQueryRunner(+ team=self.team,+ query=TracesQuery(dateRange=DateRange(date_from="2025-01-15", date_to="2025-01-15")),+ ).calculate()+ self.assertEqual(len(response.results), 2)+ self.assertEqual(response.results[0].id, "trace2")+ self.assertEqual(response.results[1].id, "trace1")+def test_capture_range(self):_create_person(distinct_ids=["person1"], team=self.team)_create_ai_generation_event(diff --git a/posthog/hogql_queries/ai/trace_neighbors_query_runner.py b/posthog/hogql_queries/ai/trace_neighbors_query_runner.pyindex 38e2ac99dcc0..3cf767ea50d8 100644--- a/posthog/hogql_queries/ai/trace_neighbors_query_runner.py+++ b/posthog/hogql_queries/ai/trace_neighbors_query_runner.py@@ -19,6 +19,12 @@ from posthog.hogql_queries.query_runner import AnalyticsQueryRunnerfrom posthog.hogql_queries.utils.query_date_range import QueryDateRange+class TraceNeighborsQueryDateRange(QueryDateRange):+ # Navigation has to reach every trace the list shows, and the list ends a calendar-day+ # `date_to` at the last moment of that day.+ CALENDAR_DAY_DATE_TO_IS_INCLUSIVE = True++class TraceNeighborsQueryRunner(AnalyticsQueryRunner[TraceNeighborsQueryResponse]):"""Query runner to find the older and newer traces relative to a given trace.@@ -137,14 +143,14 @@ class TraceNeighborsQueryRunner(AnalyticsQueryRunner[TraceNeighborsQueryResponsedef _date_range(self):# Use the provided date range, or fall back to a range centered around the trace timestampif self.query.dateRange and (self.query.dateRange.date_from or self.query.dateRange.date_to):- return QueryDateRange(self.query.dateRange, self.team, IntervalType.MINUTE, datetime.now())+ return TraceNeighborsQueryDateRange(self.query.dateRange, self.team, IntervalType.MINUTE, datetime.now())# Default: 3 days before and after the trace timestamp for finding neighbors# This is more performant for large customers while still covering most navigation scenariosfrom posthog.schema import DateRangetrace_ts = datetime.fromisoformat(self.query.timestamp.replace("Z", "+00:00"))- return QueryDateRange(+ return TraceNeighborsQueryDateRange(DateRange(date_from=(trace_ts - timedelta(days=3)).isoformat(),date_to=(trace_ts + timedelta(days=3)).isoformat(),diff --git a/posthog/hogql_queries/ai/trace_query_runner.py b/posthog/hogql_queries/ai/trace_query_runner.pyindex 754026fa3f31..1c630d579f7f 100644--- a/posthog/hogql_queries/ai/trace_query_runner.py+++ b/posthog/hogql_queries/ai/trace_query_runner.py@@ -24,7 +24,12 @@ from posthog.hogql_queries.ai.sentiment_evaluations import (get_sentiment_for_generation,load_generation_sentiment_evaluations_for_traces,)-from posthog.hogql_queries.ai.utils import filled_property_filters, merge_heavy_properties, parse_ai_property_value+from posthog.hogql_queries.ai.utils import (+ filled_property_filters,+ merge_heavy_properties,+ parse_ai_property_value,+ timestamp_bound_as_hogql,+)from posthog.hogql_queries.query_runner import AnalyticsQueryRunnerfrom posthog.hogql_queries.utils.query_date_range import QueryDateRange@@ -61,6 +66,8 @@ class TraceQueryDateRange(QueryDateRange):# Forward buffer: an upper bound on a single trace's duration. A trace that maps to a chat can# stay open across days, so a sub-day bound silently truncates it.FORWARD_CAPTURE_RANGE_MINUTES = 7 * 24 * 60+ # Callers name calendar days: the trace detail tool takes a date-only `date_to`.+ CALENDAR_DAY_DATE_TO_IS_INCLUSIVE = Truedef date_from_for_filtering(self) -> datetime:return super().date_from()@@ -75,20 +82,7 @@ class TraceQueryDateRange(QueryDateRange):return super().date_to() + timedelta(minutes=self.FORWARD_CAPTURE_RANGE_MINUTES)def date_to_for_filtering_as_hogql(self) -> ast.Expr:- # `format_date` rounds down to a whole second, which would drop the events inside the final- # second of the bound. Event timestamps carry microseconds, so the bound carries them too.- return ast.Call(- name="assumeNotNull",- args=[- ast.Call(- name="toDateTime64",- args=[- ast.Constant(value=self.date_to_for_filtering().strftime("%Y-%m-%d %H:%M:%S.%f")),- ast.Constant(value=6),- ],- )- ],- )+ return timestamp_bound_as_hogql(self.date_to_for_filtering())class TraceQueryRunner(AnalyticsQueryRunner[TraceQueryResponse]):diff --git a/posthog/hogql_queries/ai/traces_query_runner.py b/posthog/hogql_queries/ai/traces_query_runner.pyindex 461f6053e0b5..8e918a6d420e 100644--- a/posthog/hogql_queries/ai/traces_query_runner.py+++ b/posthog/hogql_queries/ai/traces_query_runner.py@@ -31,7 +31,12 @@ from posthog.hogql_queries.ai.sentiment_evaluations import (get_sentiment_for_generation,load_trace_sentiment_evaluations,)-from posthog.hogql_queries.ai.utils import filled_property_filters, parse_ai_properties, parse_ai_property_value+from posthog.hogql_queries.ai.utils import (+ filled_property_filters,+ parse_ai_properties,+ parse_ai_property_value,+ timestamp_bound_as_hogql,+)from posthog.hogql_queries.paginators import HogQLHasMorePaginatorfrom posthog.hogql_queries.query_runner import AnalyticsQueryRunnerfrom posthog.hogql_queries.utils.query_date_range import QueryDateRange@@ -54,6 +59,8 @@ class TracesQueryDateRange(QueryDateRange):"""CAPTURE_RANGE_MINUTES = 10+ # Callers name calendar days: the trace list tool takes a date-only `date_to`.+ CALENDAR_DAY_DATE_TO_IS_INCLUSIVE = Truedef date_from_for_filtering(self) -> datetime:return super().date_from()@@ -70,12 +77,7 @@ class TracesQueryDateRange(QueryDateRange):)def date_to_for_filtering_as_hogql(self) -> ast.Expr:- return ast.Call(- name="assumeNotNull",- args=[- ast.Call(name="toDateTime", args=[ast.Constant(value=self.format_date(self.date_to_for_filtering()))])- ],- )+ return timestamp_bound_as_hogql(self.date_to_for_filtering())def date_from(self) -> datetime:return super().date_from() - timedelta(minutes=self.CAPTURE_RANGE_MINUTES)diff --git a/posthog/hogql_queries/ai/utils.py b/posthog/hogql_queries/ai/utils.pyindex 570d51414956..d11449f56920 100644--- a/posthog/hogql_queries/ai/utils.py+++ b/posthog/hogql_queries/ai/utils.py@@ -7,6 +7,8 @@ import orjsonfrom posthog.schema import PropertyOperator+from posthog.hogql import ast+from posthog.caching.utils import ThresholdMode, is_stale# Mapping from ai_events dedicated columns to their original property names.@@ -96,6 +98,23 @@ def filled_property_filters(properties: Sequence[Any] | None) -> list[Any]:return [prop for prop in (properties or []) if _is_filled_property_filter(prop)]+def timestamp_bound_as_hogql(bound: datetime) -> ast.Expr:+ """A non-null DateTime64(6) constant for `bound`, keeping its microseconds.++ `QueryDateRange.format_date` rounds down to a whole second, which would drop the events inside+ the final second of an upper bound. Event timestamps carry microseconds, so the bound does too.+ """+ return ast.Call(+ name="assumeNotNull",+ args=[+ ast.Call(+ name="toDateTime64",+ args=[ast.Constant(value=bound.strftime("%Y-%m-%d %H:%M:%S.%f")), ast.Constant(value=6)],+ )+ ],+ )++def parse_ai_property_value(value: Any) -> Any:if value is None or value == "":return Nonediff --git a/posthog/hogql_queries/utils/query_date_range.py b/posthog/hogql_queries/utils/query_date_range.pyindex 4ad3e0d9d6bb..99434a145d78 100644--- a/posthog/hogql_queries/utils/query_date_range.py+++ b/posthog/hogql_queries/utils/query_date_range.py@@ -1,3 +1,4 @@+import refrom datetime import datetime, timedeltafrom functools import cached_propertyfrom typing import Literal, Optional, cast@@ -14,6 +15,9 @@ from posthog.interval_specs import ORDERED_INTERVALS, PERIOD_MAP, IntervalLiterafrom posthog.models.team import Team, WeekStartDayfrom posthog.utils import DEFAULT_DATE_FROM_DAYS, relative_date_parse, relative_date_parse_with_delta_mapping+# The date-only forms `relative_date_parse_with_delta_mapping` accepts: extended, then basic ISO.+CALENDAR_DAY_RE = re.compile(r"\d{4}-\d{1,2}-\d{1,2}|\d{8}")+@frozenclass DateRangeBounds:@@ -47,6 +51,12 @@ class QueryDateRange:_now_without_timezone: datetime_earliest_timestamp_fallback: Optional[datetime]+ # Below an hour interval a `date_to` naming a calendar day ends the range at midnight, so a+ # range asking for a single day returns nothing. Subclasses whose callers name calendar days opt+ # in. The default stays False because the app date picker writes a bare day for every custom+ # range, so a wider rule would move every existing hour-granularity chart.+ CALENDAR_DAY_DATE_TO_IS_INCLUSIVE = False+def __init__(self,date_range: Optional[DateRange],@@ -113,7 +123,9 @@ class QueryDateRange:if not self._date_range or not self._date_range.explicitDate:is_relative = not self._date_range or not self._date_range.date_to or delta_mapping is not None- if compare_interval_length(self.interval_type, ">", IntervalType.HOUR):+ if compare_interval_length(self.interval_type, ">", IntervalType.HOUR) or (+ self.CALENDAR_DAY_DATE_TO_IS_INCLUSIVE and self._date_to_is_calendar_day+ ):date_to = date_to.replace(hour=23, minute=59, second=59, microsecond=999999)elif is_relative:if self.interval_type == IntervalType.HOUR:@@ -125,6 +137,12 @@ class QueryDateRange:return self._clip_incomplete_period(date_to)+ @cached_property+ def _date_to_is_calendar_day(self) -> bool:+ """Whether `date_to` names a calendar day with no time of day, such as `2026-09-01`."""+ date_to = self._date_range.date_to if self._date_range else None+ return bool(date_to and CALENDAR_DAY_RE.fullmatch(date_to.strip()))+def _clip_incomplete_period(self, date_to: datetime) -> datetime:"""Clip date_to to the end of the last complete interval when the range reaches into thecurrent, still-collecting one (DateRange.excludeIncompletePeriods)."""diff --git a/posthog/hogql_queries/utils/test/test_query_date_range.py b/posthog/hogql_queries/utils/test/test_query_date_range.pyindex 5e14a80038d8..f4c3d12aac71 100644--- a/posthog/hogql_queries/utils/test/test_query_date_range.py+++ b/posthog/hogql_queries/utils/test/test_query_date_range.py@@ -17,6 +17,10 @@ from posthog.interval_specs import PERIOD_MAPfrom posthog.models.team import WeekStartDay+class CalendarDayInclusiveDateRange(QueryDateRange):+ CALENDAR_DAY_DATE_TO_IS_INCLUSIVE = True++class TestQueryDateRange(APIBaseTest):def test_parsed_date(self):now = parser.isoparse("2021-08-25T00:00:00.000Z")@@ -306,12 +310,24 @@ class TestQueryDateRange(APIBaseTest):self.assertEqual(query_date_range.date_from(), parser.isoparse("2021-02-25T12:25:23.000Z"))self.assertEqual(query_date_range.date_to(), parser.isoparse("2021-04-25T10:59:23.000Z"))- def test_bare_calendar_date_to_is_inclusive(self):+ @parameterized.expand(+ [+ (QueryDateRange, IntervalType.DAY, "2021-04-25", "2021-04-25T23:59:59.999999Z"),+ (QueryDateRange, IntervalType.HOUR, "2021-04-25", "2021-04-25T00:00:00Z"),+ (QueryDateRange, IntervalType.MINUTE, "2021-04-25", "2021-04-25T00:00:00Z"),+ (QueryDateRange, IntervalType.MINUTE, "20210425", "2021-04-25T00:00:00Z"),+ (CalendarDayInclusiveDateRange, IntervalType.MINUTE, "2021-04-25", "2021-04-25T23:59:59.999999Z"),+ (CalendarDayInclusiveDateRange, IntervalType.MINUTE, "2021-4-25", "2021-04-25T23:59:59.999999Z"),+ (CalendarDayInclusiveDateRange, IntervalType.MINUTE, "20210425", "2021-04-25T23:59:59.999999Z"),+ (CalendarDayInclusiveDateRange, IntervalType.MINUTE, "2021-04-25T10:30:00Z", "2021-04-25T10:30:00Z"),+ ]+ )+ def test_bare_calendar_date_to_is_inclusive(self, date_range_class, interval, date_to, expected):now = parser.isoparse("2021-08-25T00:00:00.000Z")- date_range = DateRange(date_from="2021-04-01", date_to="2021-04-25")- query_date_range = QueryDateRange(team=self.team, date_range=date_range, interval=IntervalType.DAY, now=now)+ date_range = DateRange(date_from="2021-04-01", date_to=date_to)+ query_date_range = date_range_class(team=self.team, date_range=date_range, interval=interval, now=now)- self.assertEqual(query_date_range.date_to(), parser.isoparse("2021-04-25T23:59:59.999999Z"))+ self.assertEqual(query_date_range.date_to(), parser.isoparse(expected))def test_yesterday(self):now = parser.isoparse("2021-08-25T00:00:00.000Z")