-
Notifications
You must be signed in to change notification settings - Fork 3
/
optimizer_test.go
83 lines (76 loc) · 1.8 KB
/
optimizer_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
package kvql
import "testing"
type builderTest struct {
query string
needError bool
}
func TestPlanBuilder(t *testing.T) {
tdata := []builderTest{
builderTest{
"select key, sum(int(value)) where true", true,
},
builderTest{
"select key, value", true,
},
builderTest{
"select key, value where true;", false,
},
}
txn := &fuzzQueryStorage{}
for i, item := range tdata {
opt := NewOptimizer(item.query)
_, err := opt.buildPlan(txn)
if berr, ok := err.(QueryBinder); ok {
berr.BindQuery(item.query)
berr.SetPadding(0)
}
if err == nil && item.needError {
t.Errorf("[%d] query: `%s` need error, but got nil", i, item.query)
} else if err != nil && !item.needError {
t.Errorf("[%d] query: `%s` should not return error but got:\n%s", i, item.query, err.Error())
}
}
}
func buildPlan(query string) (FinalPlan, error) {
txn := &fuzzQueryStorage{}
opt := NewOptimizer(query)
return opt.buildPlan(txn)
}
func TestOptimizeDelete(t *testing.T) {
queries := []string{
"delete where key in ('k1', 'k2')",
"delete where key = 'k1' | key = 'k2'",
}
for _, query := range queries {
plan, err := buildPlan(query)
if err != nil {
t.Fatal(err)
}
if p, ok := plan.(*RemovePlan); ok {
if len(p.Keys) != 2 {
t.Fatal("Remove plan should contains 2 keys")
}
} else {
t.Fatal("Should optimize as remove plan")
}
}
}
func TestOptimizeDelete2(t *testing.T) {
queries := []string{
"delete where key in ('k1', 'k2') and upper(key) = 'K1'",
}
for _, query := range queries {
plan, err := buildPlan(query)
if err != nil {
t.Fatal(err)
}
if p, ok := plan.(*DeletePlan); ok {
cp := p.ChildPlan
if _, ok := cp.(*MultiGetPlan); !ok {
t.Fatal("Should optimize as multi get plan")
}
} else {
t.Fatal("Should optimize as delete plan")
}
}
}