-
Notifications
You must be signed in to change notification settings - Fork 324
/
cnosql_eval.go
284 lines (276 loc) · 5.32 KB
/
cnosql_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package reads
import (
"math"
"regexp"
"github.com/cnosdb/cnosdb/vend/cnosql"
)
// evalExpr evaluates expr against a map.
func evalExpr(expr cnosql.Expr, m Valuer) interface{} {
if expr == nil {
return nil
}
switch expr := expr.(type) {
case *cnosql.BinaryExpr:
return evalBinaryExpr(expr, m)
case *cnosql.BooleanLiteral:
return expr.Val
case *cnosql.IntegerLiteral:
return expr.Val
case *cnosql.UnsignedLiteral:
return expr.Val
case *cnosql.NumberLiteral:
return expr.Val
case *cnosql.ParenExpr:
return evalExpr(expr.Expr, m)
case *cnosql.RegexLiteral:
return expr.Val
case *cnosql.StringLiteral:
return expr.Val
case *cnosql.VarRef:
v, _ := m.Value(expr.Val)
return v
default:
return nil
}
}
func evalBinaryExpr(expr *cnosql.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 cnosql.AND:
return ok && (lhs && rhs)
case cnosql.OR:
return ok && (lhs || rhs)
case cnosql.BITWISE_AND:
return ok && (lhs && rhs)
case cnosql.BITWISE_OR:
return ok && (lhs || rhs)
case cnosql.BITWISE_XOR:
return ok && (lhs != rhs)
case cnosql.EQ:
return ok && (lhs == rhs)
case cnosql.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 cnosql.EQ:
return ok && (lhs == rhs)
case cnosql.NEQ:
return ok && (lhs != rhs)
case cnosql.LT:
return ok && (lhs < rhs)
case cnosql.LTE:
return ok && (lhs <= rhs)
case cnosql.GT:
return ok && (lhs > rhs)
case cnosql.GTE:
return ok && (lhs >= rhs)
case cnosql.ADD:
if !ok {
return nil
}
return lhs + rhs
case cnosql.SUB:
if !ok {
return nil
}
return lhs - rhs
case cnosql.MUL:
if !ok {
return nil
}
return lhs * rhs
case cnosql.DIV:
if !ok {
return nil
} else if rhs == 0 {
return float64(0)
}
return lhs / rhs
case cnosql.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 cnosql.EQ:
return lhs == rhs
case cnosql.NEQ:
return lhs != rhs
case cnosql.LT:
return lhs < rhs
case cnosql.LTE:
return lhs <= rhs
case cnosql.GT:
return lhs > rhs
case cnosql.GTE:
return lhs >= rhs
case cnosql.ADD:
return lhs + rhs
case cnosql.SUB:
return lhs - rhs
case cnosql.MUL:
return lhs * rhs
case cnosql.DIV:
if rhs == 0 {
return float64(0)
}
return lhs / rhs
case cnosql.MOD:
return math.Mod(lhs, rhs)
}
} else {
rhs, ok := rhs.(int64)
switch expr.Op {
case cnosql.EQ:
return ok && (lhs == rhs)
case cnosql.NEQ:
return ok && (lhs != rhs)
case cnosql.LT:
return ok && (lhs < rhs)
case cnosql.LTE:
return ok && (lhs <= rhs)
case cnosql.GT:
return ok && (lhs > rhs)
case cnosql.GTE:
return ok && (lhs >= rhs)
case cnosql.ADD:
if !ok {
return nil
}
return lhs + rhs
case cnosql.SUB:
if !ok {
return nil
}
return lhs - rhs
case cnosql.MUL:
if !ok {
return nil
}
return lhs * rhs
case cnosql.DIV:
if !ok {
return nil
} else if rhs == 0 {
return float64(0)
}
return lhs / rhs
case cnosql.MOD:
if !ok {
return nil
} else if rhs == 0 {
return int64(0)
}
return lhs % rhs
case cnosql.BITWISE_AND:
if !ok {
return nil
}
return lhs & rhs
case cnosql.BITWISE_OR:
if !ok {
return nil
}
return lhs | rhs
case cnosql.BITWISE_XOR:
if !ok {
return nil
}
return lhs ^ rhs
}
}
case string:
switch expr.Op {
case cnosql.EQ:
rhs, ok := rhs.(string)
if !ok {
return nil
}
return lhs == rhs
case cnosql.NEQ:
rhs, ok := rhs.(string)
if !ok {
return nil
}
return lhs != rhs
case cnosql.EQREGEX:
rhs, ok := rhs.(*regexp.Regexp)
if !ok {
return nil
}
return rhs.MatchString(lhs)
case cnosql.NEQREGEX:
rhs, ok := rhs.(*regexp.Regexp)
if !ok {
return nil
}
return !rhs.MatchString(lhs)
}
case []byte:
switch expr.Op {
case cnosql.EQ:
rhs, ok := rhs.(string)
if !ok {
return nil
}
return string(lhs) == rhs
case cnosql.NEQ:
rhs, ok := rhs.(string)
if !ok {
return nil
}
return string(lhs) != rhs
case cnosql.EQREGEX:
rhs, ok := rhs.(*regexp.Regexp)
if !ok {
return nil
}
return rhs.Match(lhs)
case cnosql.NEQREGEX:
rhs, ok := rhs.(*regexp.Regexp)
if !ok {
return nil
}
return !rhs.Match(lhs)
}
}
return nil
}
func EvalExprBool(expr cnosql.Expr, m Valuer) bool {
v, _ := evalExpr(expr, m).(bool)
return v
}