From 17096b575066f03bba6c1b63a825b4bbd867f29b Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 25 Jul 2019 22:02:03 +0300 Subject: [PATCH] app/vmselect/promql: return NaN from `count()` over zero time series This aligns `count` behavior with Prometheus. --- app/vmselect/promql/aggr.go | 6 +++++- app/vmselect/promql/aggr_incremental.go | 7 ++++++- app/vmselect/promql/aggr_incremental_test.go | 2 +- app/vmselect/promql/exec_test.go | 4 ++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/vmselect/promql/aggr.go b/app/vmselect/promql/aggr.go index 51eaec99a..a2bfdaad0 100644 --- a/app/vmselect/promql/aggr.go +++ b/app/vmselect/promql/aggr.go @@ -312,7 +312,11 @@ func aggrFuncCount(tss []*timeseries) []*timeseries { } count++ } - dst.Values[i] = float64(count) + v := float64(count) + if count == 0 { + v = nan + } + dst.Values[i] = v } return tss[:1] } diff --git a/app/vmselect/promql/aggr_incremental.go b/app/vmselect/promql/aggr_incremental.go index 8b0fe4a66..c4651a541 100644 --- a/app/vmselect/promql/aggr_incremental.go +++ b/app/vmselect/promql/aggr_incremental.go @@ -348,7 +348,12 @@ func mergeAggrCount(dst, src *incrementalAggrContext) { } func finalizeAggrCount(iac *incrementalAggrContext) { - // Nothing to do + dstValues := iac.ts.Values + for i, v := range dstValues { + if v == 0 { + dstValues[i] = nan + } + } } func updateAggrSum2(iac *incrementalAggrContext, values []float64) { diff --git a/app/vmselect/promql/aggr_incremental_test.go b/app/vmselect/promql/aggr_incremental_test.go index 7e779a124..f9a78b136 100644 --- a/app/vmselect/promql/aggr_incremental_test.go +++ b/app/vmselect/promql/aggr_incremental_test.go @@ -81,7 +81,7 @@ func TestIncrementalAggr(t *testing.T) { }) t.Run("count", func(t *testing.T) { t.Parallel() - valuesExpected := []float64{6, 0, 5, 5} + valuesExpected := []float64{6, nan, 5, 5} f("count", valuesExpected) }) t.Run("sum2", func(t *testing.T) { diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index 7b950e6f4..c22b8fa41 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -2343,10 +2343,10 @@ func TestExecSuccess(t *testing.T) { }) t.Run(`count(multi-vector)`, func(t *testing.T) { t.Parallel() - q := `count(label_set(10, "foo", "bar") or label_set((15-time()/100)^0.5, "baz", "sss"))` + q := `count(label_set(time()<1500, "foo", "bar") or label_set(time()<1800, "baz", "sss"))` r := netstorage.Result{ MetricName: metricNameExpected, - Values: []float64{2, 2, 2, 1, 1, 1}, + Values: []float64{2, 2, 2, 1, nan, nan}, Timestamps: timestampsExpected, } resultExpected := []netstorage.Result{r}