From 3127aa92b5faca403f27ae2163ba41e8ba931213 Mon Sep 17 00:00:00 2001 From: n4mine Date: Fri, 6 Nov 2020 07:29:24 +0800 Subject: [PATCH] app/vmselect/promql: fix when the parameter of `maxValue()`, `minValue()` leading by `NaN`. it will cause `{top,bottom}k_{max,min}` return inappropriate result (#883) --- app/vmselect/promql/aggr.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/app/vmselect/promql/aggr.go b/app/vmselect/promql/aggr.go index 050a8cbd48..f30313df7d 100644 --- a/app/vmselect/promql/aggr.go +++ b/app/vmselect/promql/aggr.go @@ -740,12 +740,21 @@ func minValue(values []float64) float64 { if len(values) == 0 { return nan } - min := values[0] - for _, v := range values[1:] { - if v < min { + aNumberFounded := false + var min float64 + for _, v := range values { + if !math.IsNaN(v) && !aNumberFounded { + min = v + aNumberFounded = true + continue + } + if !math.IsNaN(v) && v < min { min = v } } + if !aNumberFounded { + return nan + } return min } @@ -753,12 +762,21 @@ func maxValue(values []float64) float64 { if len(values) == 0 { return nan } - max := values[0] - for _, v := range values[1:] { - if v > max { + aNumberFounded := false + var max float64 + for _, v := range values { + if !math.IsNaN(v) && !aNumberFounded { + max = v + aNumberFounded = true + continue + } + if !math.IsNaN(v) && v > max { max = v } } + if !aNumberFounded { + return nan + } return max }