app/vmselect/promql: add atan2 binary operator, which is going to be added in Prometheus 2.31

See https://github.com/prometheus/prometheus/pull/9248
This commit is contained in:
Aliaksandr Valialkin 2021-10-11 21:15:51 +03:00
parent 81c6720392
commit a5001b9c20
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
9 changed files with 49 additions and 13 deletions

View file

@ -19,6 +19,9 @@ var binaryOpFuncs = map[string]binaryOpFunc{
"%": newBinaryOpArithFunc(binaryop.Mod),
"^": newBinaryOpArithFunc(binaryop.Pow),
// See https://github.com/prometheus/prometheus/pull/9248
"atan2": newBinaryOpArithFunc(binaryop.Atan2),
// cmp ops
"==": newBinaryOpCmpFunc(binaryop.Eq),
"!=": newBinaryOpCmpFunc(binaryop.Neq),

View file

@ -1072,6 +1072,17 @@ func TestExecSuccess(t *testing.T) {
resultExpected := []netstorage.Result{r}
f(q, resultExpected)
})
t.Run("atan2()", func(t *testing.T) {
t.Parallel()
q := `time() atan2 time()/10`
r := netstorage.Result{
MetricName: metricNameExpected,
Values: []float64{0.07853981633974483, 0.07853981633974483, 0.07853981633974483, 0.07853981633974483, 0.07853981633974483, 0.07853981633974483},
Timestamps: timestampsExpected,
}
resultExpected := []netstorage.Result{r}
f(q, resultExpected)
})
t.Run("atan()", func(t *testing.T) {
t.Parallel()
q := `atan((2000-time())/1000)`

View file

@ -6,7 +6,7 @@ sort: 15
## tip
* FEATURE: add missing trigonometric functions, which are going to be added in [Prometheus 2.31](https://github.com/prometheus/prometheus/pull/9239): [acosh](https://docs.victoriametrics.com/MetricsQL.html#acosh), [asinh](https://docs.victoriametrics.com/MetricsQL.html#asinh), [atan](https://docs.victoriametrics.com/MetricsQL.html#atan), [atanh](https://docs.victoriametrics.com/MetricsQL.html#atanh), [cosh](https://docs.victoriametrics.com/MetricsQL.html#cosh), [deg](https://docs.victoriametrics.com/MetricsQL.html#deg), [rad](https://docs.victoriametrics.com/MetricsQL.html#rad), [sinh](https://docs.victoriametrics.com/MetricsQL.html#sinh), [tan](https://docs.victoriametrics.com/MetricsQL.html#tan), [tanh](https://docs.victoriametrics.com/MetricsQL.html#tanh).
* FEATURE: add trigonometric functions, which are going to be added in [Prometheus 2.31](https://github.com/prometheus/prometheus/pull/9239): [acosh](https://docs.victoriametrics.com/MetricsQL.html#acosh), [asinh](https://docs.victoriametrics.com/MetricsQL.html#asinh), [atan](https://docs.victoriametrics.com/MetricsQL.html#atan), [atanh](https://docs.victoriametrics.com/MetricsQL.html#atanh), [cosh](https://docs.victoriametrics.com/MetricsQL.html#cosh), [deg](https://docs.victoriametrics.com/MetricsQL.html#deg), [rad](https://docs.victoriametrics.com/MetricsQL.html#rad), [sinh](https://docs.victoriametrics.com/MetricsQL.html#sinh), [tan](https://docs.victoriametrics.com/MetricsQL.html#tan), [tanh](https://docs.victoriametrics.com/MetricsQL.html#tanh). Also add `atan2` binary operator. See [this pull request](https://github.com/prometheus/prometheus/pull/9248).
* FEATURE: consistently return the same set of time series from [limitk](https://docs.victoriametrics.com/MetricsQL.html#limitk) function. This improves the usability of periodically refreshed graphs.
* BUGFIX: vmstorage: fix `unaligned 64-bit atomic operation` panic on 32-bit architectures (arm and 386). The panic has been introduced in v1.67.0

2
go.mod
View file

@ -9,7 +9,7 @@ require (
// like https://github.com/valyala/fasthttp/commit/996610f021ff45fdc98c2ce7884d5fa4e7f9199b
github.com/VictoriaMetrics/fasthttp v1.1.0
github.com/VictoriaMetrics/metrics v1.18.0
github.com/VictoriaMetrics/metricsql v0.26.0
github.com/VictoriaMetrics/metricsql v0.27.0
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/aws/aws-sdk-go v1.40.58
github.com/cespare/xxhash/v2 v2.1.2

4
go.sum
View file

@ -107,8 +107,8 @@ github.com/VictoriaMetrics/fasthttp v1.1.0/go.mod h1:/7DMcogqd+aaD3G3Hg5kFgoFwlR
github.com/VictoriaMetrics/metrics v1.12.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
github.com/VictoriaMetrics/metrics v1.18.0 h1:vov5NxDHRSXFbdiH4dYLYEjKLoAXXSQ7hcnG8TSD9JQ=
github.com/VictoriaMetrics/metrics v1.18.0/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA=
github.com/VictoriaMetrics/metricsql v0.26.0 h1:lJBRn9vn9kst7hfNzSsQorulzNYQtX7JxWWWxh/udfI=
github.com/VictoriaMetrics/metricsql v0.26.0/go.mod h1:ylO7YITho/Iw6P71oEaGyHbO94bGoGtzWfLGqFhMIg8=
github.com/VictoriaMetrics/metricsql v0.27.0 h1:S6xWFKEyu+EbPS3tYr1cWeRza61L3e4tYcbBqMakuX0=
github.com/VictoriaMetrics/metricsql v0.27.0/go.mod h1:ylO7YITho/Iw6P71oEaGyHbO94bGoGtzWfLGqFhMIg8=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=

View file

@ -16,6 +16,9 @@ var binaryOps = map[string]bool{
"%": true,
"^": true,
// See https://github.com/prometheus/prometheus/pull/9248
"atan2": true,
// cmp ops
"==": true,
"!=": true,
@ -60,6 +63,7 @@ var binaryOpPriorities = map[string]int{
"*": 5,
"/": 5,
"%": 5,
"atan2": 5,
"^": 6,
}
@ -140,6 +144,7 @@ func isBinaryOpLogicalSet(op string) bool {
}
func binaryOpEvalNumber(op string, left, right float64, isBool bool) float64 {
op = strings.ToLower(op)
if IsBinaryOpCmp(op) {
evalCmp := func(cf func(left, right float64) bool) float64 {
if isBool {
@ -181,6 +186,8 @@ func binaryOpEvalNumber(op string, left, right float64, isBool bool) float64 {
left = binaryop.Div(left, right)
case "%":
left = binaryop.Mod(left, right)
case "atan2":
left = binaryop.Atan2(left, right)
case "^":
left = binaryop.Pow(left, right)
case "and":

View file

@ -79,6 +79,11 @@ func Pow(left, right float64) float64 {
return math.Pow(left, right)
}
// Atan2 returns atan2(left, right)
func Atan2(left, right float64) float64 {
return math.Atan2(left, right)
}
// Default returns left or right if left is NaN.
func Default(left, right float64) float64 {
if math.IsNaN(left) {

View file

@ -9,13 +9,22 @@ var transformFuncs = map[string]bool{
// See funcs accepting instant-vector on https://prometheus.io/docs/prometheus/latest/querying/functions/ .
"abs": true,
"absent": true,
"acos": true,
"acosh": true,
"asin": true,
"asinh": true,
"atan": true,
"atanh": true,
"ceil": true,
"clamp": true,
"clamp_max": true,
"clamp_min": true,
"cos": true,
"cosh": true,
"day_of_month": true,
"day_of_week": true,
"days_in_month": true,
"deg": true,
"exp": true,
"floor": true,
"histogram_quantile": true,
@ -27,12 +36,18 @@ var transformFuncs = map[string]bool{
"log10": true,
"minute": true,
"month": true,
"pi": true,
"rad": true,
"round": true,
"scalar": true,
"sgn": true,
"sin": true,
"sinh": true,
"sort": true,
"sort_desc": true,
"sqrt": true,
"tan": true,
"tanh": true,
"time": true,
// "timestamp" has been moved to rollup funcs. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/415
"vector": true,
@ -75,11 +90,6 @@ var transformFuncs = map[string]bool{
"rand": true,
"rand_normal": true,
"rand_exponential": true,
"pi": true,
"sin": true,
"cos": true,
"asin": true,
"acos": true,
"prometheus_buckets": true,
"buckets_limit": true,
"histogram_share": true,

2
vendor/modules.txt vendored
View file

@ -22,7 +22,7 @@ github.com/VictoriaMetrics/fasthttp/stackless
# github.com/VictoriaMetrics/metrics v1.18.0
## explicit
github.com/VictoriaMetrics/metrics
# github.com/VictoriaMetrics/metricsql v0.26.0
# github.com/VictoriaMetrics/metricsql v0.27.0
## explicit
github.com/VictoriaMetrics/metricsql
github.com/VictoriaMetrics/metricsql/binaryop