-
Notifications
You must be signed in to change notification settings - Fork 8
/
vfilter_test.go
1595 lines (1327 loc) · 48.1 KB
/
vfilter_test.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
package vfilter
import (
"context"
"fmt"
"log"
"os"
"sync"
"testing"
"github.com/Velocidex/ordereddict"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/assert"
"www.velocidex.com/golang/vfilter/arg_parser"
"www.velocidex.com/golang/vfilter/plugins"
"www.velocidex.com/golang/vfilter/protocols"
"www.velocidex.com/golang/vfilter/types"
"www.velocidex.com/golang/vfilter/utils"
"www.velocidex.com/golang/vfilter/utils/dict"
)
const (
PARSE_ERROR = "PARSE ERROR"
)
type execTest struct {
clause string
result Any
}
var compareOptions = cmpopts.IgnoreUnexported(
_Value{}, Plugin{}, _SymbolRef{}, _AliasedExpression{})
var execTestsSerialization = []execTest{
{"1 or sleep(a=100)", true},
// Arithmetic
{"1", 1},
{"0 or 3", true},
{"1 and 3", true},
{"1 = TRUE", true},
{"0 = FALSE", true},
// This should not parse properly. Previously this was parsed
// like -2.
{"'-' 2", PARSE_ERROR},
{"1.5", 1.5},
{"2 - 1", 1},
{"1 + 2", 3}, // int
{"1 + 2.0", 3}, // float
{"1 + 2.5", 3.5}, // float
{"2.5 + 1", 3.5}, // float
{"1 + -2", -1},
{"1 + (1 + 2) * 5", 16},
{"1 + (2 + 2) / 2", 3},
{"(1 + 2 + 3) + 1", 7},
{"(1 + 2 - 3) + 1", 1},
// Subtraction
{"4 - 2", 2},
{"4 - 2.0", 2.0},
{"4 - 2.1", 1.9},
{"4.0 - 2", 2},
{"4.0 - 2.0", 2.0},
// Division
{"4 / 2", 2},
{"4 / 2.0", 2.0},
{"4.0 / 2", 2.0},
{"4.0 / 2.0", 2.0},
// Divide by zero
{"4.0 / 0", &Null{}},
// Fractions
{"4 / 3", 4.0 / 3},
// Precedence
{"1 + 2 * 4", 9},
{"1 and 2 * 4", true},
{"1 and 2 * 0", false},
// and is higher than OR
{"false and 5 or 4", false},
{"(false and 5) or 4", true},
// Division by 0 silently trapped.
{"10 / 0", Null{}},
// Arithmetic on incompatible types silently trapped.
{"1 + 'foo'", Null{}},
{"'foo' - 'bar'", Null{}},
// Logical operators
{"1 and 2 and 3 and 4", true},
{"1 and (2 = 1 + 1) and 3", true},
{"1 and (2 = 1 + 2) and 3", false},
{"1 and func_foo(return=FALSE) and 3", false},
{"func_foo(return=FALSE) or func_foo(return=2) or func_foo(return=FALSE)", true},
// String concat
{"'foo' + 'bar'", "foobar"},
{"'foo' + 'bar' = 'foobar'", true},
{"5 * func_foo()", 5},
// Equality
{"const_foo = 1", true},
{"const_foo != 2", true},
{"func_foo() = 1", true},
{"func_foo() = func_foo()", true},
{"1 = const_foo", true},
{"1 = TRUE", true},
{"dict(X=1, Y=2) = dict(Y=2, X=1)", true},
// Comparing int to float
{"1 = 1.0", true},
{"1.0 = 1", true},
{"1.1 = 1", false},
{"1 = 1.1", false},
{"1 = 'foo'", false},
// Floats do not compare with integers properly.
{"281462092005375 = 65535 * 65535 * 65535", true},
// Greater than
{"const_foo > 1", false},
{"const_foo < 2", true},
{"func_foo() >= 1", true},
{"func_foo() > 1", false},
{"func_foo() < func_foo()", false},
{"1 <= const_foo", true},
{"1 >= TRUE", true},
{"2 > 1", true},
{"2 > 1.5", true},
{"2 > 2.5", false},
{"2 < 1", false},
{"2 < 1.5", false},
// Floats
{"2.1 < three_int64", true},
{"2.1 < 2.5", true},
{"3.5 < three_int64", false},
{"three_int64 < 3.6", true},
{"three_int64 < 2.1", false},
{"2.1 > three_int64", false},
{"2.1 > 2.5", false},
{"3.5 > three_int64", true},
{"three_int64 > 3.6", false},
{"three_int64 > 2.1", true},
// Non matching types
{"2 > 'hello'", false},
{"2 < 'hello'", false},
// Callables
{"func_foo(return =1)", 1},
{"func_foo(return =1) = 1", true},
{"func_foo(return =1 + 2)", 3},
{"func_foo(return = (1 + (2 + 3) * 3))", 16},
// Previously this was misparsed as the - sign (e.g. -2).
{"func_foo(return='-')", "-"},
// Nested callables.
{"func_foo(return = (1 + func_foo(return=2 + 3)))", 6},
// Arrays
{"(1, 2, 3, 4)", []int64{1, 2, 3, 4}},
{"(1, 2.2, 3, 4)", []float64{1, 2.2, 3, 4}},
{"2 in (1, 2, 3, 4)", true},
{"(1, 2, 3) = (1, 2, 3)", true},
{"(1, 2, 3) != (2, 3)", true},
// Array additions means concatenate the array
{"(1, 2) + (3, 4)", []int64{1, 2, 3, 4}},
// Coercing single members into the array
{"1 + (3, 4)", []int64{1, 3, 4}},
{"(1, 2) + 3", []int64{1, 2, 3}},
// Null
{"1 + NULL", types.Null{}},
{"NULL + 1", types.Null{}},
{"1 - NULL", types.Null{}},
{"NULL - 1", types.Null{}},
{"1 * NULL", types.Null{}},
{"NULL * 1", types.Null{}},
{"1 / NULL", types.Null{}},
{"NULL / 1", types.Null{}},
{"1 =~ NULL", false},
{"NULL =~ 1", false},
{"1 in NULL", false},
{"NULL in 1", false},
// Dicts
{"dict(foo=1) = dict(foo=1)", true},
{"dict(foo=1)", ordereddict.NewDict().Set("foo", int64(1))},
{"dict(foo=1.0)", ordereddict.NewDict().Set("foo", 1.0)},
{"dict(foo=1, bar=2)", ordereddict.NewDict().
Set("foo", int64(1)).
Set("bar", int64(2))},
{"dict(foo=1, bar=2, baz=3)", ordereddict.NewDict().
Set("foo", int64(1)).
Set("bar", int64(2)).
Set("baz", int64(3))},
{"dict(foo=[1, 2])", ordereddict.NewDict().
Set("foo", []int64{1, 2})},
{"dict(`key with spaces`='Value')",
ordereddict.NewDict().Set("key with spaces", "Value")},
// Using the trailing comma notation indicates an array.
{"dict(foo=[1,])", ordereddict.NewDict().
Set("foo", []int64{1})},
// Tuple notation can also be used
{"dict(foo=(1,))", ordereddict.NewDict().
Set("foo", []int64{1})},
{"dict(foo=len(list=[1,]))", ordereddict.NewDict().
Set("foo", 1)},
// Without it a single item array is silently converted to a
// single value.
{"dict(foo=[1])", ordereddict.NewDict().
Set("foo", 1)},
// Expression as parameter.
{"dict(foo=1, bar=( 2 + 3 ))", ordereddict.NewDict().
Set("foo", int64(1)).Set("bar", int64(5))},
// Mixing floats and ints.
{"dict(foo=1.0, bar=( 2.1 + 3 ))", ordereddict.NewDict().
Set("foo", float64(1)).Set("bar", 5.1)},
// List as parameter.
{"dict(foo=1, bar= [2 , 3] )", ordereddict.NewDict().
Set("foo", int64(1)).
Set("bar", []Any{int64(2), int64(3)})},
// Associative
// Relies on pre-populating the scope with a Dict.
{"foo.bar.baz, foo.bar2", []float64{5, 7}},
{"dict(foo=dict(bar=5)).foo.bar", 5},
{"1, dict(foo=5).foo", []float64{1, 5}},
// Support array indexes.
{"my_list_obj.my_list[2]", 3},
{"my_list_obj.my_list[1]", 2},
{"(my_list_obj.my_list[3]).Foo", "Bar"},
{"dict(x=(my_list_obj.my_list[3]).Foo + 'a')",
ordereddict.NewDict().Set("x", "Bara")},
// Support index of strings
{"'Hello'[1]", 101},
{"'Hello'[-1]", 111},
{"'Hello'[:3]", "Hel"},
// Indexing past the end of the array should clamp to end.
{"'Hello'[2:300]", "llo"},
{"'Hello'[-2:300]", "lo"},
{"'Hello'[-3:]", "llo"},
// Rgexp operator
{"'Hello' =~ '.'", true},
// . matches anything including the empty string (it is optimized away).
{"'' =~ '.'", true},
{"'Hello' =~ 'he[lo]+'", true},
// Null also matches "." because it is optimized away.
{"NULL =~ '.'", true},
{"NULL =~ '.*'", true},
{"NULL =~ ''", true},
// Arrays match any element
{"('Hello', 'World') =~ 'he'", true},
{"('Hello', 'World') =~ 'xx'", false},
// For now dicts are not regexable
{"dict(x='Hello', y='World') =~ 'he'", false},
}
// These tests are excluded from serialization tests.
var execTests = append(execTestsSerialization, []execTest{
// We now support hex and octal integers directly.
{"(0x10, 0x20, 070, 0xea, -4)", []int64{16, 32, 56, 234, -4}},
// Spurious line breaks should be ignored.
{"1 +\n2", 3},
{"1 AND\n 2", true},
{"NOT\nTRUE", false},
{"2 IN\n(1,2)", true},
}...)
// Function that returns a value.
type TestFunction struct {
return_value Any
}
func (self TestFunction) Copy() types.FunctionInterface {
return &TestFunction{self.return_value}
}
func (self TestFunction) Call(ctx context.Context, scope types.Scope, args *ordereddict.Dict) Any {
if value, pres := args.Get("return"); pres {
lazy_value, ok := value.(types.LazyExpr)
if ok {
return lazy_value.Reduce(ctx)
}
return value
}
return self.return_value
}
func (self TestFunction) Info(scope types.Scope, type_map *TypeMap) *FunctionInfo {
return &FunctionInfo{
Name: "func_foo",
}
}
var CounterFunctionCount = 0
type CounterFunction struct{}
func (self CounterFunction) Call(ctx context.Context, scope types.Scope, args *ordereddict.Dict) Any {
CounterFunctionCount += 1
return CounterFunctionCount
}
func (self CounterFunction) Info(scope types.Scope, type_map *TypeMap) *FunctionInfo {
return &FunctionInfo{
Name: "counter",
}
}
type PanicFunction struct{}
type PanicFunctionArgs struct {
Column Any `vfilter:"optional,field=column"`
Value Any `vfilter:"optional,field=value"`
}
// Panic if we get an arg of a=2
func (self PanicFunction) Call(ctx context.Context, scope types.Scope, args *ordereddict.Dict) Any {
arg := PanicFunctionArgs{}
err := arg_parser.ExtractArgsWithContext(ctx, scope, args, &arg)
if err != nil {
scope.Log("Panic: %v", err)
return types.Null{}
}
if scope.Eq(arg.Value, arg.Column) {
fmt.Printf("Panic because I got %v = %v! \n", arg.Column, arg.Value)
panic(fmt.Sprintf("Panic because I got %v = %v!", arg.Column, arg.Value))
}
return arg.Value
}
func (self PanicFunction) Info(scope types.Scope, type_map *TypeMap) *FunctionInfo {
return &FunctionInfo{
Name: "panic",
}
}
type SetEnvFunctionArgs struct {
Column string `vfilter:"required,field=column"`
Value Any `vfilter:"optional,field=value"`
}
type SetEnvFunction struct{}
func (self SetEnvFunction) Call(ctx context.Context, scope types.Scope, args *ordereddict.Dict) Any {
arg := SetEnvFunctionArgs{}
err := arg_parser.ExtractArgsWithContext(ctx, scope, args, &arg)
if err != nil {
panic(err)
}
env_any, pres := scope.Resolve("RootEnv")
if !pres {
panic("Can not find env")
}
env, ok := env_any.(*ordereddict.Dict)
if !ok {
panic("Can not find env")
}
env.Set(arg.Column, arg.Value)
return true
}
func (self SetEnvFunction) Info(scope types.Scope, type_map *TypeMap) *FunctionInfo {
return &FunctionInfo{
Name: "set_env",
}
}
func makeScope() types.Scope {
env := ordereddict.NewDict().
Set("const_foo", 1).
Set("three_int64", int64(3)).
Set("my_list_obj", ordereddict.NewDict().
Set("my_list", []interface{}{
1, 2, 3,
ordereddict.NewDict().Set("Foo", "Bar")})).
Set("env_var", "EnvironmentData").
Set("foo", ordereddict.NewDict().
Set("bar", ordereddict.NewDict().Set("baz", 5)).
Set("bar2", 7))
result := NewScope().AppendVars(env).AppendFunctions(
TestFunction{1},
CounterFunction{}, SetEnvFunction{},
PanicFunction{},
).AppendPlugins(
plugins.GenericListPlugin{
PluginName: "range",
Function: func(ctx context.Context, scope types.Scope, args *ordereddict.Dict) []Row {
return []Row{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
},
},
)
env.Set("RootEnv", env)
return result
}
func TestValue(t *testing.T) {
scope := makeScope()
ctx, cancel := context.WithCancel(context.Background())
foo := "'foo'"
value := _Value{
// String now contains quotes to preserve quoting
// style on serialization.
String: &foo,
}
result := value.Reduce(ctx, scope)
defer cancel()
if !scope.Eq(result, "foo") {
t.Fatalf("Expected %v, got %v", "foo", foo)
}
}
func TestEvalWhereClause(t *testing.T) {
scope := makeScope()
for idx, test := range execTests {
preamble := "select * from plugin() where \n"
vql, err := Parse(preamble + test.clause)
if err != nil {
if test.result == PARSE_ERROR {
continue
}
t.Fatalf("Failed to parse %v: %v", test.clause, err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
value := vql.Query.Where.Reduce(ctx, scope)
if !scope.Eq(value, test.result) {
utils.Debug(test.clause)
utils.Debug(test.result)
utils.Debug(value)
t.Fatalf("%v: %v: Expected %v, got %v", idx, test.clause, test.result, value)
}
}
}
// Check that ToString() methods work properly - convert an AST back
// to VQL. Since ToString() will produce normalized VQL, we ensure
// that re-parsing this will produce the same AST.
func TestSerializaition(t *testing.T) {
scope := makeScope()
for _, test := range execTestsSerialization {
preamble := "select * from plugin() where "
vql, err := Parse(preamble + test.clause)
if err != nil {
// If we expect a parse error then its ok.
if test.result == PARSE_ERROR {
continue
}
t.Fatalf("Failed to parse %v: %v", test.clause, err)
}
vql_string := FormatToString(scope, vql)
parsed_vql, err := Parse(vql_string)
if err != nil {
utils.Debug(vql)
t.Fatalf("Failed to parse stringified VQL %v: %v (%v)",
vql_string, err, test.clause)
}
FormatToString(scope, parsed_vql)
diff := cmp.Diff(parsed_vql, vql, compareOptions)
if diff != "" {
t.Fatalf("Parsed generated VQL not equivalent: %v vs %v: \n%v",
preamble+test.clause, vql_string, diff)
}
}
}
type vqlTest struct {
name string
vql string
}
var vqlTests = []vqlTest{
{"query with dicts", "select * from test()"},
{"query with ints", "select * from range(start=10, end=12)"},
{"query with wild card followed by comma",
"select *, 1 AS Extra from test()"},
// The environment contains a 'foo' and the plugin emits 'foo'
// which should shadow it.
{"aliases with shadowed var", "select env_var as EnvVar, foo as FooColumn from test()"},
{"aliases with non-shadowed var", "select foo as FooColumn from range(start=1, end=2)"},
{"condition on aliases", "select foo as FooColumn from test() where FooColumn = 2"},
{"condition on aliases with not", "select foo as FooColumn from test() where NOT FooColumn = 2"},
{"condition on non aliases", "select foo as FooColumn from test() where foo = 4"},
{"dict plugin", "select * from dict(env_var=15, foo=5, `field with space`='value')"},
{"dict plugin with invalid column",
"select no_such_column from dict(env_var=15, foo=5)"},
{"dict plugin with invalid column in expression",
"select no_such_column + 'foo' from dict(env_var=15, foo=5)"},
{"mix from env and plugin", "select env_var + param as ConCat from dict(param='param')"},
{"subselects", "select param from dict(param={select * from range(start=3, end=5)})"},
{"empty subselects should produce null", "select {select * from range(start=3, end=5) WHERE 0} AS Value FROM scope()"},
// Add two subselects - Adding sequences makes one longer sequence.
{"subselects addition",
`select q1.value + q2.value as Sum from
dict(q1={select * from range(start=3, end=5)},
q2={select * from range(start=10, end=14)})`},
{"Functions in select expression",
"select func_foo(return=q1 + 4) from dict(q1=3)"},
// This query shows the power of VQL:
// 1. First the test() plugin is called to return a set of rows.
// 2. For each of these rows, the query() function is run with
// the subselect specified. Note how the subselect can use
// the values returned from the first query.
{"Subselect in column.",
`select bar, {select * from dict(column=bar)} as Query
from test()`},
// The below query demonstrates that the query() function is
// run on every row returned from the filter, and then the
// output is filtered by the the where clause. Be aware that
// this may be expensive if test() returns many rows.
{"Subselect functions in filter.",
`select bar, {select * from dict(column=bar)} as Query
from test() where 1 in Query.column`},
{"Subselect in columns",
`select bar, { select column from dict(column=bar) } AS subquery from test()
`},
{"Foreach plugin", `
select * from foreach(
row={
select * from test()
}, query={
select bar, foo, value from range(start=bar, end=foo)
})`},
{"Foreach plugin with array", `
select * from foreach(
row=[dict(bar=1, foo=2), dict(foo=1, bar=2)],
query={
select bar, foo from scope()
})`},
{"Foreach plugin with single object", `
select * from foreach(
row=dict(bar=1, foo=2),
query={
select bar, foo from scope()
})`},
{"Foreach fully materializes row before passing to query ", `
SELECT Evaluated FROM foreach(row={
SELECT value,
set_env(column="Evaluated", value=TRUE)
FROM range(start=1, end=10)
},
query={
SELECT value from scope()
}) LIMIT 1
`},
{"Foreach with non row elements",
"SELECT * FROM foreach(row=1, query='hello')"},
{"Foreach with non row elements",
"SELECT * FROM foreach(row=1, query=[1,2,3,4])"},
{"Foreach with non row elements",
"SELECT * FROM foreach(row=[1,2,3], query={SELECT _value FROM scope()})"},
{"Foreach with no query - single object",
"SELECT * FROM foreach(row=dict(X=1))"},
{"Foreach with no query - array of objects",
"SELECT * FROM foreach(row=[dict(X=1), dict(X=2)])"},
{"Foreach with no query - select with column",
"SELECT * FROM foreach(row={ SELECT dict(X=1) AS X FROM scope()}, column='X')"},
// Foreach ignores NULL rows.
{"Foreach with no query - with null",
"SELECT * FROM foreach(row=NULL)"},
{"Foreach with no query - with null in array",
"SELECT * FROM foreach(row=[NULL, NULL, dict(X=1)])"},
{"Query plugin with dots", "Select * from Artifact.Linux.Sys()"},
{"Order by", "select * from test() order by foo"},
{"Order by desc", "select * from test() order by foo DESC"},
{"Limit", "select * from test() limit 1"},
{"Limit and order", "select * from test() order by foo desc limit 1"},
{"Comments Simple", `// This is a single line comment
select * from test() limit 1`},
{"Comments SQL Style", `-- This is a single line comment in sql style
select * from test() limit 1`},
{"Comments Multiline", `/* This is a multiline comment
this is the rest of the comment */
select * from test() limit 1`},
{"Not combined with AND",
"select * from test() WHERE 1 and not foo = 2"},
{"Not combined with AND 2",
"select * from test() WHERE 0 and not foo = 2"},
{"Not combined with OR",
"select * from test() WHERE 1 or not foo = 20"},
{"Not combined with OR 2",
"select * from test() WHERE 0 or not foo = 20"},
{"Group by 1",
"select foo, bar from groupbytest() GROUP BY bar"},
{"Group by *",
"select * from groupbytest() GROUP BY bar"},
{"Group by count",
"select foo, bar, count(items=bar) from groupbytest() GROUP BY bar"},
// Should be exactly the same as above
{"Group by count with *",
"select *, count(items=bar) from groupbytest() GROUP BY bar"},
{"Group by count with where",
"select foo, bar, count(items=bar) from groupbytest() WHERE foo < 4 GROUP BY bar"},
{"Group by min",
"select foo, bar, min(item=foo) from groupbytest() GROUP BY bar"},
{"Group by max",
"select foo, bar, max(item=foo) from groupbytest() GROUP BY bar"},
{"Group by enumrate of string",
"select baz, bar, enumerate(items=baz) from groupbytest() GROUP BY bar"},
{"Groupby evaluates each row twice",
`SELECT * FROM chain(
a={ SELECT count() FROM scope()},
b={
SELECT count(), count(items=bar), bar FROM groupbytest() GROUP BY bar
})`},
{"Lazy row evaluation (Shoud panic if foo=2",
"select foo, panic(column=foo, value=2) from test() where foo = 4"},
{"Quotes strings",
"select 'foo\\'s quote' from scope()"},
{"Hex quotes",
`SELECT format(format='%x', args="\x01\x02\xf0\xf1") FROM scope()`},
{"Test get()",
"select get(item=[dict(foo=3), 2, 3, 4], member='0.foo') AS Foo from scope()"},
{"Array concatenation",
"SELECT (1,2) + (3,4) FROM scope()"},
{"Array concatenation to any",
"SELECT (1,2) + 4 FROM scope()"},
{"Array concatenation with if",
"SELECT (1,2) + if(condition=1, then=(3,4)) AS Field FROM scope()"},
{"Array empty with if",
"SELECT if(condition=1, then=[]) AS Field FROM scope()"},
{"Array concatenation with Null",
"SELECT (1,2) + if(condition=0, then=(3,4)) AS Field FROM scope()"},
{"Spurious line feeds and tabs",
"SELECT \n1\n+\n2\tAS\nFooBar\t\n FROM\n scope(\n)\nWHERE\n FooBar >\n1\nAND\nTRUE\n"},
{"If function and comparison expression",
"SELECT if(condition=1 + 1 = 2, then=2, else=3), if(condition=1 + 2 = 2, then=2, else=3) FROM scope()"},
{"If function and subselects",
"SELECT if(condition=1, then={ SELECT * FROM test() }) FROM scope()"},
{"If function should be lazy",
"SELECT if(condition=FALSE, then=panic(column=3, value=3)) from scope()"},
{"If function should be lazy",
"SELECT if(condition=TRUE, else=panic(column=7, value=7)) from scope()"},
{"If function should be lazy with sub query",
"SELECT if(condition=TRUE, then={ SELECT * FROM test() LIMIT 1}) from scope()"},
{"If function should be lazy with sub query",
"SELECT if(condition=FALSE, then={ SELECT panic(column=8, value=8) FROM test()}) from scope()"},
{"If function should be lazy",
"SELECT if(condition=TRUE, else={ SELECT panic(column=9, value=9) FROM test()}) from scope()"},
{"If function should be lazy WRT stored query 1/2",
"LET bomb = SELECT panic(column=1, value=1) FROM scope()"},
{"If function should be lazy WRT stored query 2/2",
"SELECT if(condition=FALSE, then=bomb) FROM scope()"},
{"If plugin and arrays",
"SELECT * FROM if(condition=1, then=[dict(Foo=1), dict(Foo=2)])"},
{"If plugin and dict",
"SELECT * FROM if(condition=1, then=dict(Foo=2))"},
{"Columns with space in them",
"SELECT foo as `column with space` FROM dict(foo='hello world')"},
{"Alternatives with the OR shortcut operator",
"SELECT get(member='Foo') || get(member='Bar') || 'Hello' FROM scope()"},
{"Alternatives with the OR shortcut operator false",
"SELECT NULL || '', NULL || FALSE, NULL || 'X', 'A' || 'B', 'A' || FALSE, 'A' || '' || 'B' FROM scope()"},
{"Alternatives with AND shortcut operator",
"SELECT NULL && '', TRUE && 'XX', 'A' && 'B', 'A' && FALSE, ((FALSE && 1) || 2), TRUE && 1 || 2 FROM scope()"},
{"Whitespace in the query",
"SELECT * FROM\ntest()"},
}
var multiVQLTest = []vqlTest{
{"Query with LET", "LET X = SELECT * FROM test() SELECT * FROM X"},
{"MultiSelect", "SELECT 'Bar' AS Foo FROM scope() SELECT 'Foo' AS Foo FROM scope()"},
{"LET with index", "LET X = SELECT * FROM test() SELECT X[0], X[1].bar FROM scope()"},
{"LET with extra columns", "LET X = SELECT * FROM test() SELECT *, 1 FROM X"},
{"LET with extra columns before *", "LET X = SELECT * FROM test() SELECT 1, *, 2 FROM X"},
{"LET with extra columns before * and override", "LET X = SELECT * FROM test() SELECT 1000 + foo as foo, *, 2 FROM X"},
{"LET materialized with extra columns", "LET X <= SELECT * FROM test() SELECT *, 1 FROM X"},
{"Column name with space", "LET X <= SELECT 2 AS `Hello World` FROM scope() " +
"SELECT `Hello World`, `Hello World` + 4 AS Foo, X.`Hello World` FROM X"},
{"Group by with columns with spaces",
"LET X = SELECT foo, bar AS `Foo Bar` FROM groupbytest() SELECT * FROM X GROUP BY `Foo Bar`"},
{"Order by with columns with spaces",
"LET X = SELECT foo AS `Foo Bar` FROM groupbytest() SELECT * FROM X ORDER BY `Foo Bar` DESC"},
{"LET with expression",
"LET X = 'Hello world' SELECT X FROM scope()"},
{"LET with expression lazy",
"LET X = panic() SELECT 1 + 1 FROM scope()"},
{"LET materialize with expression",
"LET X <= 'Hello world' SELECT X FROM scope()"},
{"Serialization (Unexpected arg aborts parsing)",
"SELECT panic(value=1, column=1, colume='X'), func_foo() FROM scope()"},
{"LET with expression lazy - string concat",
"LET X = 'hello' SELECT X + 'world', 'world' + X, 'hello world' =~ X FROM scope()"},
// count() increments every time it is called proving X is lazy
// and will be re-evaluated each time. NOTE: Referencing variables
// without calling them **does not** create an isolated scope. In
// this way LET X is different than LET X(Y)
{"Lazy expression in arrays",
"LET X = count() SELECT (1, X), dict(foo=X, bar=[1,X]) FROM scope()"},
{"Calling stored queries as plugins",
"LET X = SELECT Foo FROM scope() SELECT * FROM X(Foo=1)"},
{"Defining functions with args",
"LET X(Foo, Bar) = Foo + Bar SELECT X(Foo=5, Bar=2) FROM scope()"},
{"Defining stored queries with args",
"LET X(Foo, Bar) = SELECT Foo + Bar FROM scope() SELECT * FROM X(Foo=5, Bar=2)"},
{"Defining functions masking variable name",
"LET X(foo) = foo + 2 SELECT X(foo=foo), foo FROM test()"},
{"Defining stored queries masking variable name",
"LET X(foo) = SELECT *, foo FROM range(start=foo, end=foo + 2) LET foo=2 SELECT * FROM X(foo=foo)"},
{"Calling stored query in function context",
// Calling a parameterized stored query in function
// context materialized it in place.
"LET X(foo) = SELECT *, foo FROM range(start=foo, end=foo + 2) SELECT X(foo=5).value, X(foo=10) FROM scope()"},
{"Calling stored query with args",
// Referring to a parameterized stored query in an arg
// without calling it passes the stored query itself
// as an arg.
"LET X(foo) = SELECT *, foo FROM range(start=foo, end=foo + 2) LET foo = 8 SELECT * FROM foreach(row=X, query={ SELECT *, value FROM X(foo=value) })"},
{"Lazy expression evaluates in caller's scope",
"LET X(foo) = 1 + foo SELECT X(foo= foo + 1 ), foo FROM test()"},
// Calling a symbol will reset aggregator context, but simply
// referencing it will not. Therefore Y1 = 6, Y2 = 7 but Y3 = 6 again.
{"Calling lazy expressions as functions allows access to global scope", `
LET Xk = 5
LET Y = Xk + count()
SELECT Y AS Y1, Y AS Y2, Y() AS Y3 FROM scope()
`},
{"Overflow condition - should not get stuck",
"LET X = 1 + X SELECT X(X=1), X FROM test()"},
{"Overflow condition - https://github.com/Velocidex/velociraptor/issues/2845",
"LET X = X.ID SELECT * FROM X"},
{"Overflow condition - should not get stuck",
"LET X = 1 + X LET Y = 1 + Y SELECT X, Y FROM scope()"},
{"Overflow condition materialized - should not get stuck",
"LET X <= 1 + X LET Y = 1 + Y SELECT X, Y FROM scope()"},
{"Overflow with plugins",
"LET foo_plugin(X) = SELECT * FROM chain(a={SELECT * FROM foo_plugin(X=1)}) SELECT * FROM foo_plugin(X=1)"},
{"Escaped identifiers for arg parameters",
"SELECT dict(`arg-with-special chars`=TRUE) FROM scope()"},
// The following two queries should be the same.
{"Group by hidden column",
"select bar, baz from groupbytest() GROUP BY bar select baz from groupbytest() GROUP BY bar "},
// A group by can refer to an expression, in which case the
// expression is calculated for each row.
{"Group by expression",
"select *, bar + bar from groupbytest() GROUP BY bar + bar"},
{"Variable can not mask a function.",
"LET dict(x) = 1 SELECT 1 AS dict, dict(foo=1) FROM scope() WHERE dict"},
{"Foreach evals query in row scope (both queries should be same)", `
LET row_query = SELECT 1 AS ColumnName123 FROM scope()
LET foreach_query = SELECT ColumnName123 FROM scope()
SELECT * FROM foreach(row=row_query, query=foreach_query)
SELECT * FROM foreach(row=row_query, query={SELECT ColumnName123 FROM scope()})
`},
{"Aggregate functions with multiple evaluations", `
SELECT count() AS Count FROM foreach(row=[0, 1, 2])
WHERE Count <= 2 AND Count AND Count AND Count
AND count() and count() -- Each count() instance is unique and has unique state
`},
{"Aggregate functions: min max", `
SELECT min(item=_value) AS Min,
max(item=_value) AS Max,
count() AS Count
FROM foreach(row=[0, 1, 2])
GROUP BY 1
`},
{"Aggregate functions: min max on strings", `
SELECT min(item=_value) AS Min,
max(item=_value) AS Max,
count() AS Count
FROM foreach(row=["AAA", "BBBB", "CCC"])
GROUP BY 1
`},
{"Aggregate functions keep state per unique instance", `
SELECT count() AS A, count() AS B FROM foreach(row=[0, 1, 2])
`},
{"Aggregate functions within a VQL function have their own state", `
LET Adder(X) = SELECT *, count() AS Count FROM range(start=10, end=10 + X, step=1)
SELECT Adder(X=4), Adder(X=2) FROM scope()
`},
{"Aggregate functions within a VQL function have their own state", `
LET Adder(X) = SELECT *, count() AS Count FROM range(start=10, end=10 + X, step=1)
SELECT * FROM foreach(row={ SELECT value FROM range(start=0, end=2, step=1)},
query={
SELECT * FROM Adder(X=value)
})
`},
// A foreach query is not an isolated scope which mean it can
// refer to values outside its definition.
{"Aggregate functions: Sum and Count together", `
LET MyValue <= "Hello"
SELECT * FROM foreach(row=[2, 3, 4],
query={
SELECT count() AS Count,
sum(item=_value) AS Sum,
MyValue
FROM scope()
})`},
// When the subquery is defined as a function it is evaluated in a
// new scope with a new context - so count() and sum() start fresh
// each time. We can still refer to global items inside the
// function definition.
{"Aggregate functions: Sum and Count in stored query definition", `
LET MyValue <= "Hello"
LET CountMe(Value) = SELECT count() AS Count,
Value,
sum(item=Value) AS Sum,
MyValue
FROM scope()
LET _value = 10
SELECT * FROM foreach(row=[2, 3, 4],
query={
SELECT * FROM CountMe(Value=_value)
})`},
// Calling a stored query as a parameter will evaluate it before
// passing to the foreach plugin. It will have access to any scope
// variables available in the foreach but **not** those provided
// by the row variables.
// In the below you can think of CountMe(Value=_value) to be
// expanded first with _value = 10 into an array of rows. That
// array is then passed as the query parameter to foreach.
{"Aggregate functions: Sum and Count in stored query definition", `
LET MyValue <= "Hello"
LET CountMe(Value) = SELECT count() AS Count,
Value,
sum(item=Value) AS Sum,
MyValue
FROM scope()
LET _value = 10
-- CountMe is evaluated at point of definition to return a stored query.
SELECT * FROM foreach(row=[2, 3, 4],
query=CountMe(Value=_value))`},
{"Aggregate functions: Sum all rows", `
SELECT sum(item=_value) AS Total,
sum(item=_value * 2) AS TotalDouble
FROM foreach(row=[2, 3, 4])
GROUP BY 1
`},
// Test if function
{"If function with stored query", `
-- Prove that stored query was evaluated
LET Foo = SELECT 2 FROM scope() WHERE set_env(column="Eval", value=TRUE)
-- Materialize an expression
LET result <= if(condition=TRUE, then=Foo) -- should materialize
SELECT RootEnv.Eval AS Pass FROM scope() -- should be set
`},
{"If function with subqueries", `
LET abc(a) = if(
condition=a,
then={SELECT a AS Pass FROM scope()},
else={SELECT false AS Pass from scope()})
SELECT abc(a=TRUE) AS Pass FROM scope()
`},
{"If function with subqueries should return a lazy query", `
LET _ <= SELECT * FROM reset_objectwithmethods()
LET MyCounter(Length) =
SELECT * FROM foreach(row={
SELECT value
FROM range(start=0, end=Length, step=1)
}, query={
SELECT Value2 FROM objectwithmethods()
WHERE Value2
})
-- The if plugin calls the if function directly here.
-- In previous versions this would cause it to materialize
-- the stored query. In current version the if() function
-- returns the stored query directly so it is not materialized.
SELECT * FROM if(condition=TRUE,
then=if(condition=TRUE,
then=MyCounter(Length=1000)
))
LIMIT 3
SELECT * FROM if(condition=TRUE,
then=if(condition=TRUE,
then={
SELECT VarIsObjectWithMethods.Counter < 20,
Value2 =~ "called" FROM MyCounter(Length=100) }
))
LIMIT 3
// Just prove we did not materialize the MyCounter() query