From 4ef7158e897f7bab5c136d0589e715c44bc9691c Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 2 Dec 2020 00:28:16 +0200 Subject: [PATCH] app/vmselect/promql: return `nan` from `a >bool b` if `a` is `nan` in the same way as Prometheus does --- app/vmselect/promql/binary_op.go | 3 +++ app/vmselect/promql/exec_test.go | 26 ++++++++++++++++++++++++-- docs/CHANGELOG.md | 2 ++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/vmselect/promql/binary_op.go b/app/vmselect/promql/binary_op.go index d26282330..7d827607a 100644 --- a/app/vmselect/promql/binary_op.go +++ b/app/vmselect/promql/binary_op.go @@ -62,6 +62,9 @@ func newBinaryOpCmpFunc(cf func(left, right float64) bool) binaryOpFunc { if cf(left, right) { return 1 } + if math.IsNaN(left) { + return nan + } return 0 } return newBinaryOpFunc(cfe) diff --git a/app/vmselect/promql/exec_test.go b/app/vmselect/promql/exec_test.go index 1aac301db..d0cccb5ec 100644 --- a/app/vmselect/promql/exec_test.go +++ b/app/vmselect/promql/exec_test.go @@ -1782,10 +1782,32 @@ func TestExecSuccess(t *testing.T) { }) t.Run(`time() > scalar`, func(t *testing.T) { t.Parallel() - q := `time() > 123` + q := `time() > 1234` r := netstorage.Result{ MetricName: metricNameExpected, - Values: []float64{1000, 1200, 1400, 1600, 1800, 2000}, + Values: []float64{nan, nan, 1400, 1600, 1800, 2000}, + Timestamps: timestampsExpected, + } + resultExpected := []netstorage.Result{r} + f(q, resultExpected) + }) + t.Run(`time() >bool scalar`, func(t *testing.T) { + t.Parallel() + q := `time() >bool 1234` + r := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{0, 0, 1, 1, 1, 1}, + Timestamps: timestampsExpected, + } + resultExpected := []netstorage.Result{r} + f(q, resultExpected) + }) + t.Run(`nan >bool scalar1`, func(t *testing.T) { + t.Parallel() + q := `(time() > 1234) >bool 1450` + r := netstorage.Result{ + MetricName: metricNameExpected, + Values: []float64{nan, nan, 0, 1, 1, 1}, Timestamps: timestampsExpected, } resultExpected := []netstorage.Result{r} diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b26e74778..1354d263e 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,6 +4,8 @@ * BUGFIX: properly parse timestamps in OpenMetrics format - they are exposed as floating-point number in seconds instead of integer milliseconds unlike in Prometheus exposition format. See [the docs](https://github.com/OpenObservability/OpenMetrics/blob/master/specification/OpenMetrics.md#timestamps). +* BUGFIX: return `nan` for `a >bool b` query when `a` equals to `nan` like Prometheus does. Previously `0` was returned in this case. This applies to any comparison operation + with `bool` modifier. See [these docs](https://prometheus.io/docs/prometheus/latest/querying/operators/#comparison-binary-operators) for details. # [v1.48.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.48.0)