From 9fddb70b8c5e82a6665d15bd0c123f78cb6c7513 Mon Sep 17 00:00:00 2001 From: Jeanette Tan Date: Wed, 11 Feb 2026 05:06:53 +0800 Subject: [PATCH 1/2] promql: Add test case for avg_over_time single histogram fix Add regression test for the bug where avg_over_time with a single histogram sample would produce +Inf count/sum and NaN zero bucket due to division by zero. The test verifies that both regular exponential histograms and native histograms with custom buckets (NHCB) correctly return the histogram unchanged when averaging a single sample. Signed-off-by: Jeanette Tan --- promql/promqltest/testdata/native_histograms.test | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/promql/promqltest/testdata/native_histograms.test b/promql/promqltest/testdata/native_histograms.test index 07352eb59a..3b497e5ff4 100644 --- a/promql/promqltest/testdata/native_histograms.test +++ b/promql/promqltest/testdata/native_histograms.test @@ -1545,6 +1545,21 @@ eval instant at 3m avg_over_time(histogram_sum_over_time_incremental_6[4m:1m]) clear +# Test avg_over_time with a single histogram sample (regression test for division by zero bug). +load 1m + single_histogram_sample {{schema:3 sum:5 count:4 buckets:[1 2 1]}} + single_nhcb_sample {{schema:-53 sum:1 count:5 custom_values:[5 10] buckets:[1 4]}} + +# avg_over_time should return the histogram unchanged when there's only one sample, not Inf/NaN. +eval instant at 0m avg_over_time(single_histogram_sample[1m]) + {} {{schema:3 sum:5 count:4 buckets:[1 2 1]}} + +# Test with native histogram with custom buckets (NHCB). +eval instant at 0m avg_over_time(single_nhcb_sample[1m]) + {} {{schema:-53 sum:1 count:5 custom_values:[5 10] buckets:[1 4]}} + +clear + # Test native histograms with sub operator. load 10m histogram_sub_1{idx="0"} {{schema:0 count:41 sum:2345.6 z_bucket:5 z_bucket_w:0.001 buckets:[1 3 1 2 1 1 1] n_buckets:[0 1 4 2 7 0 0 0 0 5 5 2]}}x1 From d2bb5605dc2d9c67b8ee8efec20f2f193a26db87 Mon Sep 17 00:00:00 2001 From: Jeanette Tan Date: Wed, 11 Feb 2026 05:06:53 +0800 Subject: [PATCH 2/2] promql: Fix avg_over_time for single histogram avg_over_time would produce +Inf count/sum and NaN zero bucket when averaging a single histogram, because the count variable was initialized to 0 instead of 1. This caused a division by zero at the end of the function. The float version of avg_over_time already correctly initializes count to 1, this change makes the histogram version consistent with that. Signed-off-by: Jeanette Tan --- promql/functions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/promql/functions.go b/promql/functions.go index f02262ac40..2cb90a9b6c 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -848,7 +848,7 @@ func funcAvgOverTime(_ []Vector, matrixVal Matrix, args parser.Expressions, enh var ( sum = s.Histograms[0].H.Copy() mean, kahanC *histogram.FloatHistogram - count float64 + count = 1. incrementalMean bool nhcbBoundsReconciled bool err error