-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
1229 lines (1153 loc) · 32.5 KB
/
vm.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// VM implements the virtual machine runtime evaluator
// for the SQL, FilterQL, and Expression evalutors.
package vm
import (
"fmt"
"math"
"strconv"
"strings"
"time"
u "github.com/araddon/gou"
"github.com/mb0/glob"
"github.com/lytics/qlbridge/expr"
"github.com/lytics/qlbridge/lex"
"github.com/lytics/qlbridge/value"
)
var (
// MaxDepth acts as a guard against potentially recursive queries
MaxDepth = 1000
// ErrMaxDepth If we hit max depth on recursion
ErrMaxDepth = fmt.Errorf("Recursive Evaluation Error")
// ErrUnknownOp an unrecognized Operator in expression
ErrUnknownOp = fmt.Errorf("expr: unknown op type")
// ErrUnknownNodeType Unhandled Node type for expression evaluation
ErrUnknownNodeType = fmt.Errorf("expr: unknown node type")
// ErrExecute could not evaluate an expression
ErrExecute = fmt.Errorf("Could not execute")
)
// EvalBaseContext base context for expression evaluation
type EvalBaseContext struct {
expr.EvalContext
}
// Eval - Evaluate the given expression (arg Node) against the given context.
// @ctx is the evaluation context ie the variables/values which the expression will be
// evaluated against. It may be a simple reader of message/data or any
// object whhich implements EvalContext.
func Eval(ctx expr.EvalContext, arg expr.Node) (value.Value, bool) {
return evalDepth(ctx, arg, 0, make([]string, 0))
}
// creates a new Value with a nil group and given value.
func numberNodeToValue(t *expr.NumberNode) (value.Value, bool) {
if t.IsInt {
return value.NewIntValue(t.Int64), true
} else if t.IsFloat {
fv, ok := value.StringToFloat64(t.Text)
if !ok {
u.Debugf("Could not perform numeric conversion for %q", t.Text)
return value.NilValueVal, false
}
return value.NewNumberValue(fv), true
}
u.Debugf("Could not find numeric conversion for %#v", t)
return value.NilValueVal, false
}
// ResolveIncludes take an expression and resolve any includes so that
// it does not have to be resolved at runtime. There is also a
// InlineIncludes alternative in expr pkg which actually re-writes the expression
// to remove includes and embed the expressions they refer to as part of this expression.
func ResolveIncludes(ctx expr.Includer, arg expr.Node) error {
return resolveIncludesDepth(ctx, arg, 0, make([]string, 0))
}
func resolveIncludesDepth(ctx expr.Includer, arg expr.Node, depth int, visitedIncludes []string) error {
if depth > MaxDepth {
return ErrMaxDepth
}
// can we switch to arg.Type()
switch n := arg.(type) {
case *expr.BinaryNode:
for _, narg := range n.Args {
if err := resolveIncludesDepth(ctx, narg, depth+1, visitedIncludes); err != nil {
return err
}
}
case *expr.BooleanNode:
for _, narg := range n.Args {
if err := resolveIncludesDepth(ctx, narg, depth+1, visitedIncludes); err != nil {
return err
}
}
case *expr.UnaryNode:
if err := resolveIncludesDepth(ctx, n.Arg, depth+1, visitedIncludes); err != nil {
return err
}
case *expr.TriNode:
for _, narg := range n.Args {
if err := resolveIncludesDepth(ctx, narg, depth+1, visitedIncludes); err != nil {
return err
}
}
case *expr.ArrayNode:
for _, narg := range n.Args {
if err := resolveIncludesDepth(ctx, narg, depth+1, visitedIncludes); err != nil {
return err
}
}
case *expr.FuncNode:
for _, narg := range n.Args {
if err := resolveIncludesDepth(ctx, narg, depth+1, visitedIncludes); err != nil {
return err
}
}
case *expr.NumberNode, *expr.IdentityNode, *expr.StringNode, nil,
*expr.ValueNode, *expr.NullNode:
return nil
case *expr.IncludeNode:
return resolveInclude(ctx, n, depth+1, visitedIncludes)
}
return nil
}
func evalBool(ctx expr.EvalContext, arg expr.Node, depth int, visitedIncludes []string) (bool, bool) {
val, ok := evalDepth(ctx, arg, depth, visitedIncludes)
if !ok || val == nil {
return false, false
}
if bv, isBool := val.(value.BoolValue); isBool {
return bv.Val(), true
}
return false, false
}
func evalDepth(ctx expr.EvalContext, arg expr.Node, depth int, visitedIncludes []string) (value.Value, bool) {
if depth > MaxDepth {
return nil, false
}
switch argVal := arg.(type) {
case *expr.NumberNode:
return numberNodeToValue(argVal)
case *expr.BinaryNode:
return walkBinary(ctx, argVal, depth, visitedIncludes)
case *expr.BooleanNode:
return walkBoolean(ctx, argVal, depth, visitedIncludes)
case *expr.UnaryNode:
return walkUnary(ctx, argVal, depth)
case *expr.TriNode:
return walkTernary(ctx, argVal, depth)
case *expr.ArrayNode:
return walkArray(ctx, argVal, depth)
case *expr.FuncNode:
return walkFunc(ctx, argVal, depth)
case *expr.IdentityNode:
return walkIdentity(ctx, argVal)
case *expr.StringNode:
return value.NewStringValue(argVal.Text), true
case nil:
return nil, false
case *expr.NullNode:
// WHERE (`users.user_id` != NULL)
return value.NewNilValue(), true
case *expr.IncludeNode:
return walkInclude(ctx, argVal, depth+1, visitedIncludes)
case *expr.ValueNode:
if argVal.Value == nil {
return nil, false
}
switch val := argVal.Value.(type) {
case *value.NilValue, value.NilValue:
return nil, false
case value.SliceValue:
return val, true
}
u.Errorf("Unknonwn node type: %#v", argVal.Value)
panic(ErrUnknownNodeType)
default:
u.Errorf("Unknonwn node type: %#v", arg)
panic(ErrUnknownNodeType)
}
}
func resolveInclude(ctx expr.Includer, inc *expr.IncludeNode, depth int, visitedIncludes []string) error {
if inc.ExprNode != nil {
return nil
}
// check if we've already seen this node in our visit stack
currentNode := inc.Identity.Text
for _, visited := range visitedIncludes {
if currentNode == visited {
return fmt.Errorf("%w: cycle encountered: %s->%s", ErrMaxDepth, strings.Join(visitedIncludes, "->"), currentNode)
}
}
// add the node to our visit stack
visitedIncludes = append(visitedIncludes, currentNode)
incExpr, err := ctx.Include(inc.Identity.Text)
if err != nil {
if err == expr.ErrNoIncluder {
return err
}
u.Debugf("Could not find include for filter:%s err=%v", inc.String(), err)
return err
}
if incExpr == nil {
u.Debugf("Includer %T returned a nil filter statement!", inc)
return expr.ErrIncludeNotFound
}
if err = resolveIncludesDepth(ctx, incExpr, depth+1, visitedIncludes); err != nil {
return err
}
inc.ExprNode = incExpr
return nil
}
func walkInclude(ctx expr.EvalContext, inc *expr.IncludeNode, depth int, visitedIncludes []string) (value.Value, bool) {
if inc.ExprNode == nil {
incCtx, ok := ctx.(expr.EvalIncludeContext)
if !ok {
u.Errorf("No Includer context? %T stack:%v", ctx, u.PrettyStack(14))
return nil, false
}
if err := resolveInclude(incCtx, inc, depth, visitedIncludes); err != nil {
return nil, false
}
}
switch exp := inc.ExprNode.(type) {
case *expr.IdentityNode:
if exp.Text == "*" || exp.Text == "match_all" {
return value.NewBoolValue(true), true
}
}
matches, ok := evalBool(ctx, inc.ExprNode, depth+1, visitedIncludes)
if !ok {
if inc.Negated() {
return value.NewBoolValue(true), true
}
return nil, false
}
if inc.Negated() {
return value.NewBoolValue(!matches), true
}
return value.NewBoolValue(matches), true
}
func walkBoolean(ctx expr.EvalContext, n *expr.BooleanNode, depth int, visitedIncludes []string) (value.Value, bool) {
if depth > MaxDepth {
u.Warnf("Recursive query death? %v", n)
return nil, false
}
var and bool
switch n.Operator.T {
case lex.TokenAnd, lex.TokenLogicAnd:
and = true
case lex.TokenOr, lex.TokenLogicOr:
and = false
default:
u.Warnf("un-recognized operator %v", n.Operator)
return value.BoolValueFalse, false
}
for _, bn := range n.Args {
matches, ok := evalBool(ctx, bn, depth+1, visitedIncludes)
if !ok && and {
return nil, false
} else if !ok {
continue
}
if !and && matches {
// one of the expressions in an OR clause matched, shortcircuit true
if n.Negated() {
return value.BoolValueFalse, true
}
return value.BoolValueTrue, true
}
if and && !matches {
// one of the expressions in an AND clause did not match, shortcircuit false
if n.Negated() {
return value.BoolValueTrue, true
}
return value.BoolValueFalse, true
}
}
// no shortcircuiting, if and=true this means all expressions returned true...
// ...if and=false (OR) this means all expressions returned false.
if n.Negated() {
return value.NewBoolValue(!and), true
}
return value.NewBoolValue(and), true
}
// Binary operands: =, ==, !=, OR, AND, >, <, >=, <=, LIKE, contains
//
// x == y, x = y
// x != y
// x OR y
// x > y
// x < =
//
func walkBinary(ctx expr.EvalContext, node *expr.BinaryNode, depth int, visitedIncludes []string) (value.Value, bool) {
val, ok := evalBinary(ctx, node, depth, visitedIncludes)
if !ok {
return nil, ok
}
return val, ok
}
func evalBinary(ctx expr.EvalContext, node *expr.BinaryNode, depth int, visitedIncludes []string) (value.Value, bool) {
ar, aok := evalDepth(ctx, node.Args[0], depth+1, visitedIncludes)
br, bok := evalDepth(ctx, node.Args[1], depth+1, visitedIncludes)
// If we could not evaluate either we can shortcut
if !aok && !bok {
switch node.Operator.T {
case lex.TokenLogicOr, lex.TokenOr:
return value.NewBoolValue(false), true
case lex.TokenEqualEqual, lex.TokenEqual:
// We don't alllow nil == nil here bc we have a NilValue type
// that we would use for that
return value.NewBoolValue(false), true
case lex.TokenNE:
return value.NewBoolValue(false), true
case lex.TokenGT, lex.TokenGE, lex.TokenLT, lex.TokenLE, lex.TokenLike:
return value.NewBoolValue(false), true
}
return nil, false
}
// Else if we can only evaluate right
if !aok {
switch node.Operator.T {
case lex.TokenIntersects, lex.TokenIN, lex.TokenContains, lex.TokenLike:
return value.NewBoolValue(false), true
}
}
// Else if we can only evaluate one, we can short circuit as well
if !aok || !bok {
switch node.Operator.T {
case lex.TokenAnd, lex.TokenLogicAnd:
return value.NewBoolValue(false), true
case lex.TokenEqualEqual, lex.TokenEqual:
return value.NewBoolValue(false), true
case lex.TokenNE:
// they are technically not equal?
return value.NewBoolValue(true), true
case lex.TokenIN, lex.TokenIntersects:
return value.NewBoolValue(false), true
case lex.TokenGT, lex.TokenGE, lex.TokenLT, lex.TokenLE, lex.TokenLike:
return value.NewBoolValue(false), true
}
}
switch at := ar.(type) {
case value.IntValue:
switch bt := br.(type) {
case value.IntValue:
n := operateInts(node.Operator, at, bt)
return n, true
case value.StringValue:
// Try int first
bi, err := strconv.ParseInt(bt.Val(), 10, 64)
if err == nil {
n, err := operateIntVals(node.Operator, at.Val(), bi)
if err != nil {
return nil, false
}
return n, true
}
// Fallback to float
bf, err := strconv.ParseFloat(bt.Val(), 64)
if err == nil {
n := operateNumbers(node.Operator, at.NumberValue(), value.NewNumberValue(bf))
return n, true
}
case value.NumberValue:
n := operateNumbers(node.Operator, at.NumberValue(), bt)
return n, true
case value.SliceValue:
switch node.Operator.T {
case lex.TokenIN, lex.TokenIntersects:
for _, val := range bt.Val() {
rhi, rhok := value.ValueToInt64(val)
if rhok && rhi == at.Val() {
return value.BoolValueTrue, true
}
}
return value.NewBoolValue(false), true
default:
u.Debugf("unsupported op for SliceValue op:%v rhT:%T", node.Operator, br)
return nil, false
}
case nil, value.NilValue:
return nil, false
default:
u.Errorf("unknown type: %T %v", bt, bt)
}
case value.NumberValue:
switch bt := br.(type) {
case value.IntValue:
n := operateNumbers(node.Operator, at, bt.NumberValue())
return n, true
case value.NumberValue:
n := operateNumbers(node.Operator, at, bt)
return n, true
case value.SliceValue:
for _, val := range bt.Val() {
switch valt := val.(type) {
case value.StringValue:
if at.Val() == valt.NumberValue().Val() {
return value.BoolValueTrue, true
}
case value.IntValue:
if at.Val() == valt.NumberValue().Val() {
return value.BoolValueTrue, true
}
case value.NumberValue:
if at.Val() == valt.Val() {
return value.BoolValueTrue, true
}
default:
u.Debugf("Could not coerce to number: T:%T v:%v", val, val)
}
}
return value.BoolValueFalse, true
case value.StringValue:
// Try int first
if bf, err := strconv.ParseInt(bt.Val(), 10, 64); err == nil {
n := operateNumbers(node.Operator, at, value.NewNumberValue(float64(bf)))
return n, true
}
// Fallback to float
if bf, err := strconv.ParseFloat(bt.Val(), 64); err == nil {
n := operateNumbers(node.Operator, at, value.NewNumberValue(bf))
return n, true
}
case nil, value.NilValue:
return nil, false
default:
u.Errorf("unknown type: %T %v", bt, bt)
}
case value.BoolValue:
switch bt := br.(type) {
case value.BoolValue:
atv, btv := at.Value().(bool), bt.Value().(bool)
switch node.Operator.T {
case lex.TokenLogicAnd, lex.TokenAnd:
return value.NewBoolValue(atv && btv), true
case lex.TokenLogicOr, lex.TokenOr:
return value.NewBoolValue(atv || btv), true
case lex.TokenEqualEqual, lex.TokenEqual:
return value.NewBoolValue(atv == btv), true
case lex.TokenNE:
return value.NewBoolValue(atv != btv), true
default:
u.Warnf("bool binary?: %#v %v %v", node, at, bt)
}
case nil, value.NilValue:
switch node.Operator.T {
case lex.TokenLogicAnd:
return value.NewBoolValue(false), true
case lex.TokenLogicOr, lex.TokenOr:
return at, true
case lex.TokenEqualEqual, lex.TokenEqual:
return value.NewBoolValue(false), true
case lex.TokenNE:
return value.NewBoolValue(true), true
default:
u.Warnf("right side nil binary: %q", node)
return nil, false
}
default:
return nil, false
}
case value.StringValue:
switch bt := br.(type) {
case value.StringValue:
// Nice, both strings
return operateStrings(node.Operator, at, bt), true
case nil, value.NilValue:
switch node.Operator.T {
case lex.TokenEqualEqual, lex.TokenEqual:
if at.Nil() {
return value.NewBoolValue(true), true
}
return value.NewBoolValue(false), true
case lex.TokenNE:
if at.Nil() {
return value.NewBoolValue(false), true
}
return value.NewBoolValue(true), true
default:
u.Debugf("unsupported op: %v", node.Operator)
return nil, false
}
case value.Slice:
switch node.Operator.T {
case lex.TokenIN, lex.TokenIntersects:
for _, val := range bt.SliceValue() {
if at.Val() == val.ToString() {
return value.NewBoolValue(true), true
}
}
return value.NewBoolValue(false), true
case lex.TokenContains:
for _, val := range bt.SliceValue() {
if strings.Contains(at.Val(), val.ToString()) {
return value.NewBoolValue(true), true
}
}
return value.NewBoolValue(false), true
case lex.TokenLike: // a(value) LIKE b(pattern)
for _, val := range bt.SliceValue() {
bv, ok := LikeCompare(at.Val(), val.ToString())
if ok && bv.Val() {
return value.NewBoolValue(true), true
}
}
return value.NewBoolValue(false), true
default:
u.Debugf("unsupported op for SliceValue op:%v rhT:%T", node.Operator, br)
return nil, false
}
case value.BoolValue:
if value.IsBool(at.Val()) {
switch node.Operator.T {
case lex.TokenEqualEqual, lex.TokenEqual:
return value.NewBoolValue(value.BoolStringVal(at.Val()) == bt.Val()), true
case lex.TokenNE:
return value.NewBoolValue(value.BoolStringVal(at.Val()) != bt.Val()), true
default:
u.Debugf("unsupported op: %v", node.Operator)
}
}
switch node.Operator.T {
case lex.TokenLogicOr, lex.TokenOr, lex.TokenEqualEqual, lex.TokenEqual, lex.TokenLogicAnd,
lex.TokenAnd, lex.TokenIN, lex.TokenIntersects, lex.TokenContains, lex.TokenLike:
return value.NewBoolValue(false), true
}
// Should we evaluate strings that are non-nil to be = true?
u.Debugf("not handled: boolean %v %T=%v expr: %s", node.Operator, at.Value(), at.Val(), node.String())
return nil, false
case value.Map:
switch node.Operator.T {
case lex.TokenIN, lex.TokenIntersects:
_, hasKey := bt.Get(at.Val())
if hasKey {
return value.NewBoolValue(true), true
}
return value.NewBoolValue(false), true
default:
u.Debugf("unsupported op for Map op:%v rhT:%T", node.Operator, br)
return nil, false
}
case value.IntValue:
n := operateNumbers(node.Operator, at.NumberValue(), bt.NumberValue())
return n, true
case value.NumberValue:
n := operateNumbers(node.Operator, at.NumberValue(), bt)
return n, true
case value.TimeValue:
lht, ok := value.ValueToTime(at)
if !ok {
return value.BoolValueFalse, false
}
return operateTime(node.Operator.T, lht, bt.Val())
default:
u.Errorf("at?%T %v bt? %T %v", at, at.Value(), bt, bt.Value())
}
return nil, false
case value.SliceValue:
switch node.Operator.T {
case lex.TokenGT, lex.TokenGE, lex.TokenLT, lex.TokenLE, lex.TokenEqualEqual, lex.TokenEqual, lex.TokenNE:
if at.Len() == 0 {
return value.NewBoolValue(false), true
}
// Lets look at first arg, all in slice must be of same type
switch at.Val()[0].(type) {
case value.TimeValue:
rht, ok := value.ValueToTime(br)
if !ok {
return value.BoolValueFalse, false
}
for _, arg := range at.Val() {
lht, ok := arg.(value.TimeValue)
if !ok {
return value.NewErrorValue(fmt.Errorf("All values of slice must be same type %v", at)), false
}
if isTrue, _ := operateTime(node.Operator.T, lht.Val(), rht); isTrue.Val() {
return value.BoolValueTrue, true
}
}
return value.BoolValueFalse, true
}
case lex.TokenContains:
switch bval := br.(type) {
case nil, value.NilValue:
return nil, false
case value.StringValue:
// [x,y,z] contains str
for _, val := range at.Val() {
if strings.Contains(val.ToString(), bval.Val()) {
return value.BoolValueTrue, true
}
}
return value.BoolValueFalse, true
case value.IntValue:
// [a,b,c] contains int
for _, val := range at.Val() {
sliceInt, ok := value.ValueToInt64(val)
if ok && sliceInt == bval.Val() {
return value.BoolValueTrue, true
}
}
return value.BoolValueFalse, true
}
case lex.TokenLike:
switch bv := br.(type) {
case value.StringValue:
// [x,y,z] LIKE str
for _, val := range at.Val() {
if boolVal, ok := LikeCompare(val.ToString(), bv.Val()); ok && boolVal.Val() == true {
return boolVal, true
}
}
return value.BoolValueFalse, true
}
case lex.TokenIntersects, lex.TokenIN:
switch bt := br.(type) {
case nil, value.NilValue:
return nil, false
case value.SliceValue:
for _, aval := range at.Val() {
for _, bval := range bt.Val() {
if eq, _ := value.Equal(aval, bval); eq {
return value.BoolValueTrue, true
}
}
}
return value.BoolValueFalse, true
case value.StringsValue:
for _, aval := range at.Val() {
for _, bstr := range bt.Val() {
if aval.ToString() == bstr {
return value.BoolValueTrue, true
}
}
}
return value.BoolValueFalse, true
}
case lex.TokenLogicOr, lex.TokenOr, lex.TokenLogicAnd, lex.TokenAnd:
return value.NewBoolValue(false), true
}
return nil, false
case value.StringsValue:
switch node.Operator.T {
case lex.TokenContains:
switch bv := br.(type) {
case value.StringValue:
// [x,y,z] contains str
for _, val := range at.Val() {
if strings.Contains(val, bv.Val()) {
return value.BoolValueTrue, true
}
}
return value.BoolValueFalse, true
}
case lex.TokenLike:
switch bv := br.(type) {
case value.StringValue:
// [x,y,z] LIKE str
for _, val := range at.Val() {
boolVal, ok := LikeCompare(val, bv.Val())
if ok && boolVal.Val() == true {
return boolVal, true
}
}
return value.BoolValueFalse, true
}
case lex.TokenIntersects, lex.TokenIN:
switch bt := br.(type) {
case nil, value.NilValue:
return nil, false
case value.SliceValue:
for _, astr := range at.Val() {
for _, bval := range bt.Val() {
if astr == bval.ToString() {
return value.BoolValueTrue, true
}
}
}
return value.BoolValueFalse, true
case value.StringsValue:
for _, astr := range at.Val() {
for _, bstr := range bt.Val() {
if astr == bstr {
return value.BoolValueTrue, true
}
}
}
return value.BoolValueFalse, true
}
case lex.TokenLogicOr, lex.TokenOr, lex.TokenEqualEqual, lex.TokenEqual, lex.TokenLogicAnd,
lex.TokenAnd:
return value.NewBoolValue(false), true
}
return nil, false
case value.TimeValue:
lht := at.Val()
rht, ok := value.ValueToTime(br)
if !ok {
return value.BoolValueFalse, false
}
return operateTime(node.Operator.T, lht, rht)
case value.Map:
rhvals := make([]string, 0)
switch bv := br.(type) {
case value.StringsValue:
rhvals = bv.Val()
case value.Slice:
for _, arg := range bv.SliceValue() {
rhvals = append(rhvals, arg.ToString())
}
default:
u.Debugf("un-handled? %T", bv)
return nil, false
}
switch node.Operator.T {
case lex.TokenIN, lex.TokenIntersects:
for _, val := range rhvals {
if _, ok := at.Get(val); ok {
return value.NewBoolValue(true), true
}
}
return value.NewBoolValue(false), true
}
return nil, false
case nil, value.NilValue:
switch node.Operator.T {
case lex.TokenLogicAnd:
return value.NewBoolValue(false), true
case lex.TokenLogicOr, lex.TokenOr:
switch bt := br.(type) {
case value.BoolValue:
return bt, true
default:
return value.NewBoolValue(false), true
}
case lex.TokenEqualEqual, lex.TokenEqual:
// does nil==nil = true ??
switch br.(type) {
case nil, value.NilValue:
return value.NewBoolValue(true), true
default:
return value.NewBoolValue(false), true
}
case lex.TokenNE:
return value.NewBoolValue(true), true
case lex.TokenContains, lex.TokenLike, lex.TokenIN, lex.TokenIntersects:
return value.NewBoolValue(false), false
default:
return nil, false
}
default:
u.Debugf("Unknown op? %T %T %v", ar, at, ar)
return value.NewErrorValue(fmt.Errorf("unsupported left side value: %T in %s", at, node)), false
}
return value.NewErrorValue(fmt.Errorf("unsupported binary expression: %s", node)), false
}
func walkIdentity(ctx expr.EvalContext, node *expr.IdentityNode) (value.Value, bool) {
if node.IsBooleanIdentity() {
return value.NewBoolValue(node.Bool()), true
}
if ctx == nil {
return nil, false
}
if node.HasLeftRight() {
return ctx.Get(node.OriginalText())
}
return ctx.Get(node.Text)
}
func walkUnary(ctx expr.EvalContext, node *expr.UnaryNode, depth int) (value.Value, bool) {
a, ok := Eval(ctx, node.Arg)
if !ok {
switch node.Operator.T {
case lex.TokenExists:
return value.NewBoolValue(false), true
case lex.TokenNegate:
return value.NewBoolValue(false), false
}
u.Debugf("unary could not evaluate for[ %s ] and %#v", node.String(), node)
return a, false
}
switch node.Operator.T {
case lex.TokenNegate:
switch argVal := a.(type) {
case value.BoolValue:
return value.NewBoolValue(!argVal.Val()), true
case nil, value.NilValue:
return value.NewBoolValue(false), false
default:
u.LogThrottle(u.WARN, 5, "unary type not implemented. Unknonwn node type: %T:%v node=%s", argVal, argVal, node.String())
return value.NewNilValue(), false
}
case lex.TokenMinus:
if an, aok := a.(value.NumericValue); aok {
return value.NewNumberValue(-an.Float()), true
}
case lex.TokenExists:
switch a.(type) {
case nil, value.NilValue:
return value.NewBoolValue(false), true
}
if a.Nil() {
return value.NewBoolValue(false), true
}
return value.NewBoolValue(true), true
default:
u.Warnf("urnary not implemented for type %s %#v", node.Operator.T.String(), node)
}
return value.NewNilValue(), false
}
// walkTernary ternary evaluator
//
// A BETWEEN B AND C
//
func walkTernary(ctx expr.EvalContext, node *expr.TriNode, depth int) (value.Value, bool) {
a, aok := Eval(ctx, node.Args[0])
b, bok := Eval(ctx, node.Args[1])
c, cok := Eval(ctx, node.Args[2])
if !aok {
return nil, false
}
if !bok || !cok {
return nil, false
}
if a == nil || b == nil || c == nil {
return nil, false
}
switch node.Operator.T {
case lex.TokenBetween:
switch at := a.(type) {
case value.IntValue:
av := at.Val()
bv, ok := value.ValueToInt64(b)
if !ok {
return nil, false
}
cv, ok := value.ValueToInt64(c)
if !ok {
return nil, false
}
if av > bv && av < cv {
return value.NewBoolValue(true), true
}
return value.NewBoolValue(false), true
case value.NumberValue:
av := at.Val()
bv, ok := value.ValueToFloat64(b)
if !ok {
return nil, false
}
cv, ok := value.ValueToFloat64(c)
if !ok {
return nil, false
}
if av > bv && av < cv {
return value.NewBoolValue(true), true
}
return value.NewBoolValue(false), true
case value.TimeValue:
av := at.Val()
bv, ok := value.ValueToTime(b)
if !ok {
return nil, false
}
cv, ok := value.ValueToTime(c)
if !ok {
return nil, false
}
if av.Unix() > bv.Unix() && av.Unix() < cv.Unix() {
return value.NewBoolValue(true), true
}
return value.NewBoolValue(false), true
default:
u.Warnf("between not implemented for type %s %#v", a.Type().String(), node)
}
default:
u.Warnf("ternary node walk not implemented for node %#v", node)
}
return nil, false
}
// walkArray Array evaluator: evaluate multiple values into an array
//
// (b,c,d)
//
func walkArray(ctx expr.EvalContext, node *expr.ArrayNode, depth int) (value.Value, bool) {
vals := make([]value.Value, len(node.Args))
for i := 0; i < len(node.Args); i++ {
v, _ := Eval(ctx, node.Args[i])
vals[i] = v
}
// we are returning an array of evaluated nodes
return value.NewSliceValues(vals), true
}
// walkFunc evaluates a function
func walkFunc(ctx expr.EvalContext, node *expr.FuncNode, depth int) (value.Value, bool) {
if node.F.CustomFunc == nil {
return nil, false
}
if node.Eval == nil {
u.LogThrottle(u.WARN, 10, "No Eval() for %s", node.Name)
return nil, false
}
args := make([]value.Value, len(node.Args))
for i, a := range node.Args {
v, ok := Eval(ctx, a)
if !ok {
v = value.NewNilValue()
}
args[i] = v
}
return node.Eval(ctx, args)
}
func operateNumbers(op lex.Token, av, bv value.NumberValue) value.Value {
switch op.T {
case lex.TokenPlus, lex.TokenStar, lex.TokenMultiply, lex.TokenDivide, lex.TokenMinus,
lex.TokenModulus:
if math.IsNaN(av.Val()) || math.IsNaN(bv.Val()) {
return value.NewNumberValue(math.NaN())
}
}
a, b := av.Val(), bv.Val()
switch op.T {
case lex.TokenPlus: // +
return value.NewNumberValue(a + b)
case lex.TokenStar, lex.TokenMultiply: // *
return value.NewNumberValue(a * b)
case lex.TokenMinus: // -
return value.NewNumberValue(a - b)
case lex.TokenDivide: //
return value.NewNumberValue(a / b)
case lex.TokenModulus: // %
// is this even valid? modulus on floats?
return value.NewNumberValue(float64(int64(a) % int64(b)))
// Below here are Boolean Returns
case lex.TokenEqualEqual, lex.TokenEqual: // ==
if a == b {
return value.BoolValueTrue
} else {
return value.BoolValueFalse
}
case lex.TokenGT: // >
if a > b {
//r = 1
return value.BoolValueTrue