File: binary_op.go

package info (click to toggle)
golang-github-victoriametrics-metricsql 0.10.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 236 kB
  • sloc: makefile: 2
file content (205 lines) | stat: -rw-r--r-- 3,617 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package metricsql

import (
	"fmt"
	"math"
	"strings"

	"github.com/VictoriaMetrics/metricsql/binaryop"
)

var binaryOps = map[string]bool{
	"+": true,
	"-": true,
	"*": true,
	"/": true,
	"%": true,
	"^": true,

	// cmp ops
	"==": true,
	"!=": true,
	">":  true,
	"<":  true,
	">=": true,
	"<=": true,

	// logical set ops
	"and":    true,
	"or":     true,
	"unless": true,

	// New ops for MetricsQL
	"if":      true,
	"ifnot":   true,
	"default": true,
}

var binaryOpPriorities = map[string]int{
	"default": -1,

	"if":    0,
	"ifnot": 0,

	// See https://prometheus.io/docs/prometheus/latest/querying/operators/#binary-operator-precedence
	"or": 1,

	"and":    2,
	"unless": 2,

	"==": 3,
	"!=": 3,
	"<":  3,
	">":  3,
	"<=": 3,
	">=": 3,

	"+": 4,
	"-": 4,

	"*": 5,
	"/": 5,
	"%": 5,

	"^": 6,
}

func isBinaryOp(op string) bool {
	op = strings.ToLower(op)
	return binaryOps[op]
}

func binaryOpPriority(op string) int {
	op = strings.ToLower(op)
	return binaryOpPriorities[op]
}

func scanBinaryOpPrefix(s string) int {
	n := 0
	for op := range binaryOps {
		if len(s) < len(op) {
			continue
		}
		ss := strings.ToLower(s[:len(op)])
		if ss == op && len(op) > n {
			n = len(op)
		}
	}
	return n
}

func isRightAssociativeBinaryOp(op string) bool {
	// See https://prometheus.io/docs/prometheus/latest/querying/operators/#binary-operator-precedence
	return op == "^"
}

func isBinaryOpGroupModifier(s string) bool {
	s = strings.ToLower(s)
	switch s {
	// See https://prometheus.io/docs/prometheus/latest/querying/operators/#vector-matching
	case "on", "ignoring":
		return true
	default:
		return false
	}
}

func isBinaryOpJoinModifier(s string) bool {
	s = strings.ToLower(s)
	switch s {
	case "group_left", "group_right":
		return true
	default:
		return false
	}
}

func isBinaryOpBoolModifier(s string) bool {
	s = strings.ToLower(s)
	return s == "bool"
}

// IsBinaryOpCmp returns true if op is comparison operator such as '==', '!=', etc.
func IsBinaryOpCmp(op string) bool {
	switch op {
	case "==", "!=", ">", "<", ">=", "<=":
		return true
	default:
		return false
	}
}

func isBinaryOpLogicalSet(op string) bool {
	op = strings.ToLower(op)
	switch op {
	case "and", "or", "unless":
		return true
	default:
		return false
	}
}

func binaryOpEvalNumber(op string, left, right float64, isBool bool) float64 {
	if IsBinaryOpCmp(op) {
		evalCmp := func(cf func(left, right float64) bool) float64 {
			if isBool {
				if cf(left, right) {
					return 1
				}
				return 0
			}
			if cf(left, right) {
				return left
			}
			return nan
		}
		switch op {
		case "==":
			left = evalCmp(binaryop.Eq)
		case "!=":
			left = evalCmp(binaryop.Neq)
		case ">":
			left = evalCmp(binaryop.Gt)
		case "<":
			left = evalCmp(binaryop.Lt)
		case ">=":
			left = evalCmp(binaryop.Gte)
		case "<=":
			left = evalCmp(binaryop.Lte)
		default:
			panic(fmt.Errorf("BUG: unexpected comparison binaryOp: %q", op))
		}
	} else {
		switch op {
		case "+":
			left = binaryop.Plus(left, right)
		case "-":
			left = binaryop.Minus(left, right)
		case "*":
			left = binaryop.Mul(left, right)
		case "/":
			left = binaryop.Div(left, right)
		case "%":
			left = binaryop.Mod(left, right)
		case "^":
			left = binaryop.Pow(left, right)
		case "and":
			// Nothing to do
		case "or":
			// Nothing to do
		case "unless":
			left = nan
		case "default":
			left = binaryop.Default(left, right)
		case "if":
			left = binaryop.If(left, right)
		case "ifnot":
			left = binaryop.Ifnot(left, right)
		default:
			panic(fmt.Errorf("BUG: unexpected non-comparison binaryOp: %q", op))
		}
	}
	return left
}

var nan = math.NaN()