Fix nan-to-int64 crash when avg/quantile runs on empty result set
All checks were successful
CI / build (push) Successful in 1m9s
CI / docker (push) Successful in 39s
CI / deploy (push) Successful in 29s

ClickHouse avg() and quantile() return nan/inf on zero rows, which
toInt64() cannot convert. Wrap with ifNotFinite(..., 0) to default to
zero. Applied to both stats and timeseries queries.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
hsiegeln
2026-03-14 09:37:19 +01:00
parent 3641dffecc
commit cb600be1f1

View File

@@ -91,8 +91,8 @@ public class ClickHouseSearchEngine implements SearchEngine {
public ExecutionStats stats(Instant from, Instant to) {
String aggregateSql = "SELECT count() AS total_count, " +
"countIf(status = 'FAILED') AS failed_count, " +
"toInt64(avg(duration_ms)) AS avg_duration_ms, " +
"toInt64(quantile(0.99)(duration_ms)) AS p99_duration_ms, " +
"toInt64(ifNotFinite(avg(duration_ms), 0)) AS avg_duration_ms, " +
"toInt64(ifNotFinite(quantile(0.99)(duration_ms), 0)) AS p99_duration_ms, " +
"countIf(status = 'RUNNING') AS active_count " +
"FROM route_executions WHERE start_time >= ? AND start_time <= ?";
@@ -143,8 +143,8 @@ public class ClickHouseSearchEngine implements SearchEngine {
"toDateTime(intDiv(toUInt32(toDateTime(start_time)), " + intervalSeconds + ") * " + intervalSeconds + ") AS bucket, " +
"count() AS total_count, " +
"countIf(status = 'FAILED') AS failed_count, " +
"toInt64(avg(duration_ms)) AS avg_duration_ms, " +
"toInt64(quantile(0.99)(duration_ms)) AS p99_duration_ms, " +
"toInt64(ifNotFinite(avg(duration_ms), 0)) AS avg_duration_ms, " +
"toInt64(ifNotFinite(quantile(0.99)(duration_ms), 0)) AS p99_duration_ms, " +
"countIf(status = 'RUNNING') AS active_count " +
"FROM route_executions " +
"WHERE start_time >= ? AND start_time <= ? " +