forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.go
257 lines (249 loc) · 4.96 KB
/
eval.go
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package storage
import (
"math"
"regexp"
"github.com/influxdata/influxql"
)
// evalExpr evaluates expr against a map.
func evalExpr(expr influxql.Expr, m valuer) interface{} {
if expr == nil {
return nil
}
switch expr := expr.(type) {
case *influxql.BinaryExpr:
return evalBinaryExpr(expr, m)
case *influxql.BooleanLiteral:
return expr.Val
case *influxql.IntegerLiteral:
return expr.Val
case *influxql.UnsignedLiteral:
return expr.Val
case *influxql.NumberLiteral:
return expr.Val
case *influxql.ParenExpr:
return evalExpr(expr.Expr, m)
case *influxql.RegexLiteral:
return expr.Val
case *influxql.StringLiteral:
return expr.Val
case *influxql.VarRef:
v, _ := m.Value(expr.Val)
return v
default:
return nil
}
}
func evalBinaryExpr(expr *influxql.BinaryExpr, m valuer) interface{} {
lhs := evalExpr(expr.LHS, m)
rhs := evalExpr(expr.RHS, m)
if lhs == nil && rhs != nil {
// When the LHS is nil and the RHS is a boolean, implicitly cast the
// nil to false.
if _, ok := rhs.(bool); ok {
lhs = false
}
} else if lhs != nil && rhs == nil {
// Implicit cast of the RHS nil to false when the LHS is a boolean.
if _, ok := lhs.(bool); ok {
rhs = false
}
}
// Evaluate if both sides are simple types.
switch lhs := lhs.(type) {
case bool:
rhs, ok := rhs.(bool)
switch expr.Op {
case influxql.AND:
return ok && (lhs && rhs)
case influxql.OR:
return ok && (lhs || rhs)
case influxql.BITWISE_AND:
return ok && (lhs && rhs)
case influxql.BITWISE_OR:
return ok && (lhs || rhs)
case influxql.BITWISE_XOR:
return ok && (lhs != rhs)
case influxql.EQ:
return ok && (lhs == rhs)
case influxql.NEQ:
return ok && (lhs != rhs)
}
case float64:
// Try the rhs as a float64 or int64
rhsf, ok := rhs.(float64)
if !ok {
var rhsi int64
if rhsi, ok = rhs.(int64); ok {
rhsf = float64(rhsi)
}
}
rhs := rhsf
switch expr.Op {
case influxql.EQ:
return ok && (lhs == rhs)
case influxql.NEQ:
return ok && (lhs != rhs)
case influxql.LT:
return ok && (lhs < rhs)
case influxql.LTE:
return ok && (lhs <= rhs)
case influxql.GT:
return ok && (lhs > rhs)
case influxql.GTE:
return ok && (lhs >= rhs)
case influxql.ADD:
if !ok {
return nil
}
return lhs + rhs
case influxql.SUB:
if !ok {
return nil
}
return lhs - rhs
case influxql.MUL:
if !ok {
return nil
}
return lhs * rhs
case influxql.DIV:
if !ok {
return nil
} else if rhs == 0 {
return float64(0)
}
return lhs / rhs
case influxql.MOD:
if !ok {
return nil
}
return math.Mod(lhs, rhs)
}
case int64:
// Try as a float64 to see if a float cast is required.
rhsf, ok := rhs.(float64)
if ok {
lhs := float64(lhs)
rhs := rhsf
switch expr.Op {
case influxql.EQ:
return lhs == rhs
case influxql.NEQ:
return lhs != rhs
case influxql.LT:
return lhs < rhs
case influxql.LTE:
return lhs <= rhs
case influxql.GT:
return lhs > rhs
case influxql.GTE:
return lhs >= rhs
case influxql.ADD:
return lhs + rhs
case influxql.SUB:
return lhs - rhs
case influxql.MUL:
return lhs * rhs
case influxql.DIV:
if rhs == 0 {
return float64(0)
}
return lhs / rhs
case influxql.MOD:
return math.Mod(lhs, rhs)
}
} else {
rhs, ok := rhs.(int64)
switch expr.Op {
case influxql.EQ:
return ok && (lhs == rhs)
case influxql.NEQ:
return ok && (lhs != rhs)
case influxql.LT:
return ok && (lhs < rhs)
case influxql.LTE:
return ok && (lhs <= rhs)
case influxql.GT:
return ok && (lhs > rhs)
case influxql.GTE:
return ok && (lhs >= rhs)
case influxql.ADD:
if !ok {
return nil
}
return lhs + rhs
case influxql.SUB:
if !ok {
return nil
}
return lhs - rhs
case influxql.MUL:
if !ok {
return nil
}
return lhs * rhs
case influxql.DIV:
if !ok {
return nil
} else if rhs == 0 {
return float64(0)
}
return lhs / rhs
case influxql.MOD:
if !ok {
return nil
} else if rhs == 0 {
return int64(0)
}
return lhs % rhs
case influxql.BITWISE_AND:
if !ok {
return nil
}
return lhs & rhs
case influxql.BITWISE_OR:
if !ok {
return nil
}
return lhs | rhs
case influxql.BITWISE_XOR:
if !ok {
return nil
}
return lhs ^ rhs
}
}
case string:
switch expr.Op {
case influxql.EQ:
rhs, ok := rhs.(string)
if !ok {
return nil
}
return lhs == rhs
case influxql.NEQ:
rhs, ok := rhs.(string)
if !ok {
return nil
}
return lhs != rhs
case influxql.EQREGEX:
rhs, ok := rhs.(*regexp.Regexp)
if !ok {
return nil
}
return rhs.MatchString(lhs)
case influxql.NEQREGEX:
rhs, ok := rhs.(*regexp.Regexp)
if !ok {
return nil
}
return !rhs.MatchString(lhs)
}
}
return nil
}
func evalExprBool(expr influxql.Expr, m valuer) bool {
v, _ := evalExpr(expr, m).(bool)
return v
}