forked from c4pt0r/kvql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
331 lines (304 loc) · 8.56 KB
/
checker.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package kvql
func (e *BinaryOpExpr) Check(ctx *CheckCtx) error {
if err := e.Left.Check(ctx); err != nil {
return err
}
if err := e.Right.Check(ctx); err != nil {
return err
}
e.tryRewriteExpr(ctx)
switch e.Op {
case And, Or:
return e.checkWithAndOr(ctx)
case Not:
return NewSyntaxError(e.GetPos(), "Invalid operator !")
case Add, Sub, Mul, Div:
return e.checkWithMath(ctx)
case In:
return e.checkWithIn(ctx)
case Between:
return e.checkWithBetween(ctx)
default:
return e.checkWithCompares(ctx)
}
}
func (e *BinaryOpExpr) tryRewriteExpr(ctx *CheckCtx) {
switch lexp := e.Left.(type) {
case *NameExpr:
if nexpr, have := ctx.GetNamedExpr(lexp.Data); have {
e.Left = &FieldReferenceExpr{
Name: lexp,
FieldExpr: nexpr,
}
}
}
switch rexp := e.Right.(type) {
case *NameExpr:
if nexpr, have := ctx.GetNamedExpr(rexp.Data); have {
e.Right = &FieldReferenceExpr{
Name: rexp,
FieldExpr: nexpr,
}
}
}
}
func (e *BinaryOpExpr) checkWithAndOr(ctx *CheckCtx) error {
op := OperatorToString[e.Op]
switch exp := e.Left.(type) {
case *BinaryOpExpr, *FunctionCallExpr, *NotExpr, *FieldReferenceExpr:
if e.Left.ReturnType() != TBOOL {
return NewSyntaxError(e.Left.GetPos(), "%s operator has wrong type of left expression %s", op, exp)
}
default:
return NewSyntaxError(e.Left.GetPos(), "%s operator with invalid left expression %s", op, exp)
}
switch exp := e.Right.(type) {
case *BinaryOpExpr, *FunctionCallExpr, *NotExpr, *FieldReferenceExpr:
if exp.ReturnType() != TBOOL {
return NewSyntaxError(e.Right.GetPos(), "%s operator has wrong type of right expression %s", op, exp)
}
default:
return NewSyntaxError(e.Right.GetPos(), "%s operator with invalid right expression %s", op, exp)
}
return nil
}
func (e *BinaryOpExpr) checkWithMath(ctx *CheckCtx) error {
op := OperatorToString[e.Op]
lstring := false
rstring := false
switch exp := e.Left.(type) {
case *BinaryOpExpr, *FunctionCallExpr, *NumberExpr, *FloatExpr, *FieldReferenceExpr:
if e.Left.ReturnType() != TNUMBER {
if e.Left.ReturnType() == TSTR {
lstring = true
} else {
return NewSyntaxError(e.Left.GetPos(), "%s operator has wrong type of left expression %s", op, exp)
}
}
case *StringExpr, *FieldExpr, *FieldAccessExpr:
lstring = true
default:
return NewSyntaxError(e.Left.GetPos(), "%s operator with invalid left expression %s", op, exp)
}
switch exp := e.Right.(type) {
case *BinaryOpExpr, *FunctionCallExpr, *NumberExpr, *FloatExpr, *FieldReferenceExpr:
if e.Right.ReturnType() != TNUMBER {
if e.Right.ReturnType() == TSTR {
rstring = true
} else {
return NewSyntaxError(e.Right.GetPos(), "%s operator has wrong type of right expression %s", op, exp)
}
}
case *StringExpr, *FieldExpr, *FieldAccessExpr:
rstring = true
default:
return NewSyntaxError(e.Right.GetPos(), "%s operator with invalid right expression %s", op, exp)
}
if op == "+" && lstring && rstring {
} else {
if lstring {
return NewSyntaxError(e.Left.GetPos(), "%s operator with invalid left expression %s", op, e.Left)
}
if rstring {
return NewSyntaxError(e.Right.GetPos(), "%s operator with invalid right expression %s", op, e.Left)
}
}
if op == "/" {
switch rval := e.Right.(type) {
case *NumberExpr:
if rval.Int == 0 {
return NewSyntaxError(e.Right.GetPos(), "/ operator divide by zero")
}
case *FloatExpr:
if rval.Float == 0.0 {
return NewSyntaxError(e.Right.GetPos(), "/ operator divide by zero")
}
}
}
return nil
}
func (e *BinaryOpExpr) checkWithCompares(ctx *CheckCtx) error {
var (
numKeyFieldExpr = 0
numValueFieldExpr = 0
numCallExpr = 0
)
op := OperatorToString[e.Op]
switch exp := e.Left.(type) {
case *FieldExpr:
switch exp.Field {
case KeyKW:
numKeyFieldExpr++
case ValueKW:
numValueFieldExpr++
}
case *FunctionCallExpr, *FieldReferenceExpr:
numCallExpr++
case *StringExpr, *BoolExpr, *NumberExpr, *FloatExpr, *BinaryOpExpr, *FieldAccessExpr:
default:
return NewSyntaxError(e.Left.GetPos(), "%s operator with invalid left expression", op)
}
switch exp := e.Right.(type) {
case *FieldExpr:
switch exp.Field {
case KeyKW:
numKeyFieldExpr++
case ValueKW:
numValueFieldExpr++
}
case *FunctionCallExpr, *FieldReferenceExpr:
numCallExpr++
case *StringExpr, *BoolExpr, *NumberExpr, *FloatExpr, *BinaryOpExpr, *FieldAccessExpr:
default:
return NewSyntaxError(e.Right.GetPos(), "%s operator with invalid right expression", op)
}
if numKeyFieldExpr == 2 || numValueFieldExpr == 2 {
return NewSyntaxError(e.GetPos(), "%s operator with two same field", op)
}
ltype := e.Left.ReturnType()
rtype := e.Right.ReturnType()
if ltype != rtype {
return NewSyntaxError(e.GetPos(), "%s operator left and right type not same", op)
}
switch e.Op {
case Gt, Gte, Lt, Lte:
if ltype != TNUMBER && ltype != TSTR {
return NewSyntaxError(e.Left.GetPos(), "%s operator has wrong type of left expression", op)
}
case PrefixMatch, RegExpMatch:
if ltype != TSTR {
return NewSyntaxError(e.Left.GetPos(), "%s operator has wrong type of left expression", op)
}
}
return nil
}
func (e *BinaryOpExpr) checkWithIn(ctx *CheckCtx) error {
ltype := e.Left.ReturnType()
switch r := e.Right.(type) {
case *ListExpr:
for _, expr := range r.List {
if expr.ReturnType() != ltype {
return NewSyntaxError(expr.GetPos(), "in operator element has wrong type")
}
}
case *FunctionCallExpr, *FieldReferenceExpr:
if r.ReturnType() != TLIST {
return NewSyntaxError(r.GetPos(), "in operator element has wrong type")
}
default:
return NewSyntaxError(e.Right.GetPos(), "in operator right expression must be list expression")
}
return nil
}
func (e *BinaryOpExpr) checkWithBetween(ctx *CheckCtx) error {
ltype := e.Left.ReturnType()
rlist, ok := e.Right.(*ListExpr)
if !ok || len(rlist.List) != 2 {
return NewSyntaxError(e.Right.GetPos(), "between operator invalid right expression")
}
switch ltype {
case TSTR, TNUMBER:
default:
return NewSyntaxError(e.Left.GetPos(), "between operator only support string and number type")
}
lexpr := rlist.List[0]
uexpr := rlist.List[1]
if lexpr.ReturnType() != ltype || uexpr.ReturnType() != ltype {
return NewSyntaxError(e.Right.GetPos(), "between operator right expression with wrong type")
}
return nil
}
func (e *FieldExpr) Check(ctx *CheckCtx) error {
if e.Field == KeyKW && ctx.NotAllowKey {
return NewSyntaxError(e.Pos, "not allow key keyword in expression")
}
if e.Field == ValueKW && ctx.NotAllowValue {
return NewSyntaxError(e.Pos, "not allow value keyword in expression")
}
return nil
}
func (e *StringExpr) Check(ctx *CheckCtx) error {
return nil
}
func (e *NotExpr) Check(ctx *CheckCtx) error {
if e.Right.ReturnType() != TBOOL {
return NewSyntaxError(e.Right.GetPos(), "! operator right expression has wrong type")
}
return nil
}
func (e *FunctionCallExpr) Check(ctx *CheckCtx) error {
_, ok := e.Name.(*NameExpr)
if !ok {
return NewSyntaxError(e.Name.GetPos(), "Invalid function name")
}
if len(e.Args) > 0 {
for _, a := range e.Args {
if err := a.Check(ctx); err != nil {
return err
}
}
}
return nil
}
func (e *NameExpr) Check(ctx *CheckCtx) error {
return nil
}
func (e *FloatExpr) Check(ctx *CheckCtx) error {
return nil
}
func (e *NumberExpr) Check(ctx *CheckCtx) error {
return nil
}
func (e *BoolExpr) Check(ctx *CheckCtx) error {
return nil
}
func (e *ListExpr) Check(ctx *CheckCtx) error {
if len(e.List) == 0 {
return NewSyntaxError(e.GetPos(), "Empty list")
}
if len(e.List) > 1 {
ftype := e.List[0].ReturnType()
for i, item := range e.List[1:] {
if item.ReturnType() != ftype {
return NewSyntaxError(item.GetPos(), "List %d item has wrong type", i)
}
}
}
return nil
}
func (e *FieldAccessExpr) Check(ctx *CheckCtx) error {
_, leftIsFAE := e.Left.(*FieldAccessExpr)
lrType := e.Left.ReturnType()
switch lrType {
case TJSON, TLIST:
default:
if leftIsFAE {
// Support cascade field access such as:
// json(value)['x']['y']
return nil
}
return NewSyntaxError(e.Left.GetPos(), "Field access expression left require JSON or List type")
}
switch e.FieldName.(type) {
case *StringExpr:
if lrType == TJSON {
return nil
} else if leftIsFAE {
// Support cascade array index access such as:
// json(value)['list'][1]
return nil
}
case *NumberExpr:
if lrType == TLIST {
return nil
} else if leftIsFAE {
// Support cascade array index access such as:
// json(value)['list'][1]
return nil
}
}
return NewSyntaxError(e.FieldName.GetPos(), "Invalid field name")
}
func (e *FieldReferenceExpr) Check(ctx *CheckCtx) error {
return nil
}