diff --git a/app/vmselect/promql/aggr.go b/app/vmselect/promql/aggr.go index 2890ef349..2732fb029 100644 --- a/app/vmselect/promql/aggr.go +++ b/app/vmselect/promql/aggr.go @@ -431,7 +431,7 @@ func newAggrFuncTopK(isReverse bool) aggrFunc { ts.Values[n] = nan } } - return rvs + return removeNaNs(rvs) } return aggrFuncExt(afe, args[1], &afa.ae.Modifier, true) } diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index 8f702c122..a21fc56be 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -2483,6 +2483,21 @@ func TestExecSuccess(t *testing.T) { resultExpected := []netstorage.Result{r1, r2} f(q, resultExpected) }) + t.Run(`topk(1, nan_timeseries)`, func(t *testing.T) { + t.Parallel() + q := `topk(1, label_set(NaN, "foo", "bar") or label_set(time()/150, "baz", "sss")) default 0` + r1 := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{6.666666666666667, 8, 9.333333333333334, 10.666666666666666, 12, 13.333333333333334}, + Timestamps: timestampsExpected, + } + r1.MetricName.Tags = []storage.Tag{{ + Key: []byte("baz"), + Value: []byte("sss"), + }} + resultExpected := []netstorage.Result{r1} + f(q, resultExpected) + }) t.Run(`topk(2)`, func(t *testing.T) { t.Parallel() q := `sort(topk(2, label_set(10, "foo", "bar") or label_set(time()/150, "baz", "sss")))`