diff --git a/promql/engine.go b/promql/engine.go index c762c1b6a5..bb1536829e 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -3444,21 +3444,24 @@ func trimHistogram(trimmedHist *histogram.FloatHistogram, rhs float64, isUpperTr } } -func computeMidpoint(survivingIntervalBoundA, survivingIntervalBoundB float64, isPositive, isLinear bool) float64 { - if math.IsInf(survivingIntervalBoundA, 0) { - if math.IsInf(survivingIntervalBoundB, 0) { +func computeMidpoint(survivingIntervalLowerBound, survivingIntervalUpperBound float64, isPositive, isLinear bool) float64 { + if math.IsInf(survivingIntervalLowerBound, 0) { + if math.IsInf(survivingIntervalUpperBound, 0) { return 0 } - return survivingIntervalBoundB - } else if math.IsInf(survivingIntervalBoundB, 0) { - return survivingIntervalBoundA + if survivingIntervalUpperBound > 0 { + return survivingIntervalUpperBound / 2 + } + return survivingIntervalUpperBound + } else if math.IsInf(survivingIntervalUpperBound, 0) { + return survivingIntervalLowerBound } if isLinear { - return (survivingIntervalBoundA + survivingIntervalBoundB) / 2 + return (survivingIntervalLowerBound + survivingIntervalUpperBound) / 2 } - geoMean := math.Sqrt(math.Abs(survivingIntervalBoundA * survivingIntervalBoundB)) + geoMean := math.Sqrt(math.Abs(survivingIntervalLowerBound * survivingIntervalUpperBound)) if isPositive { return geoMean diff --git a/promql/promqltest/testdata/native_histograms.test b/promql/promqltest/testdata/native_histograms.test index d9eaf4e23f..614521b3af 100644 --- a/promql/promqltest/testdata/native_histograms.test +++ b/promql/promqltest/testdata/native_histograms.test @@ -2020,24 +2020,24 @@ load 1m # Custom buckets: trim on bucket boundary without interpolation eval instant at 1m cbh / 15 cbh{} {{schema:-53 count:4 sum:72.5 custom_values:[5 10 15 20] offset:3 buckets:[3 1]}} # Custom buckets: trim uses linear interpolation if cutoff is inside a bucket eval instant at 1m cbh / 13 cbh{} {{schema:-53 count:5.6 sum:94.9 custom_values:[5 10 15 20] offset:2 buckets:[1.6 3 1]}} eval instant at 1m cbh