diff --git a/promql/engine.go b/promql/engine.go index ab1f2996d8..c762c1b6a5 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -3233,10 +3233,10 @@ func computeZeroBucketTrim(zeroBucket histogram.Bucket[float64], rhs float64, ha lower = zeroBucket.Lower upper = zeroBucket.Upper ) - if !hasPositive { + if hasNegative && !hasPositive { upper = 0 } - if !hasNegative { + if hasPositive && !hasNegative { lower = 0 } diff --git a/promql/promqltest/testdata/native_histograms.test b/promql/promqltest/testdata/native_histograms.test index 0e63e5b506..f356a18bea 100644 --- a/promql/promqltest/testdata/native_histograms.test +++ b/promql/promqltest/testdata/native_histograms.test @@ -1980,6 +1980,41 @@ eval instant at 1m h_negative_buckets >/ 0 h_negative_buckets {{schema:0 z_bucket_w:0.5}} +# Exponential buckets: trim zero bucket when there are no other buckets. +load 1m + zero_bucket_only {{schema:0 count:5 sum:0 z_bucket:5 z_bucket_w:0.1 }} + +eval instant at 1m zero_bucket_only >/ 0.1 + zero_bucket_only {{schema:0 count:0 sum:0 z_bucket:0 z_bucket_w:0.1 }} + +eval instant at 1m zero_bucket_only / 0.05 + zero_bucket_only {{schema:0 count:1.25 sum:0.09375000000000001 z_bucket:1.25 z_bucket_w:0.1 }} + +eval instant at 1m zero_bucket_only / 0 + zero_bucket_only {{schema:0 count:2.5 sum:0.125 z_bucket:2.5 z_bucket_w:0.1 }} + +eval instant at 1m zero_bucket_only / -0.05 + zero_bucket_only {{schema:0 count:3.75 sum:0.09375000000000001 z_bucket:3.75 z_bucket_w:0.1 }} + +eval instant at 1m zero_bucket_only / -0.1 + zero_bucket_only {{schema:0 count:5 sum:0 z_bucket:5 z_bucket_w:0.1 }} + + load 1m cbh {{schema:-53 sum:172.5 count:15 custom_values:[5 10 15 20] buckets:[1 6 4 3 1]}}