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 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