-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter_optimizer_test.go
223 lines (213 loc) · 5.27 KB
/
filter_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
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
package kvql
import (
"fmt"
"sort"
"testing"
)
func optimizeQuery(query string) (*ScanType, error) {
p := NewParser(query)
gstmt, err := p.Parse()
if err != nil {
return nil, err
}
stmt := gstmt.(*SelectStmt)
o := NewFilterOptimizer(stmt.Where, nil, nil)
ret := o.optimizeExpr(stmt.Where.Expr)
return ret, nil
}
func assertScanType(t *testing.T, st *ScanType, tp byte, keys []string) {
assertScanTypeWithID(0, t, st, tp, keys)
}
func assertScanTypeWithID(id int, t *testing.T, st *ScanType, tp byte, keys []string) {
if tp != st.scanTp {
t.Errorf("[%d] Scan Type expect %v but got %v", id, ScanTypeToString(tp), ScanTypeToString(st.scanTp))
return
}
kstrs := make([]string, len(st.keys))
for i, k := range st.keys {
kstrs[i] = string(k)
}
if len(keys) != len(kstrs) {
t.Errorf("[%d] Scan Type keys expect %v but got %v", id, keys, kstrs)
return
}
if st.scanTp == MGET {
sort.Strings(kstrs)
}
for i, k := range keys {
if k != kstrs[i] {
t.Errorf("[%d] Scan Type keys expect %v but got %v", id, keys, kstrs)
}
}
}
type optTData struct {
query string
scanTp byte
keys []string
}
func TestOptimizers(t *testing.T) {
tdata := []optTData{
// PREFIX & MGET
optTData{
"select * where key ^= 'k' & (key = 'k1' | key = 'm1')",
MGET, []string{"k1"},
},
optTData{
"select * where key ^= 'k' & (key = 'm1' | key = 'm2')",
EMPTY, nil,
},
// PREFIX | MGET
optTData{
"select * where key ^= 'k' | (key = 'k1' | key = 'k2')",
PREFIX, []string{"k"},
},
optTData{
"select * where key ^= 'k' | (key in ('k1', 'k2'))",
PREFIX, []string{"k"},
},
optTData{
"select * where key ^= 'k' | (key = 'k1' | key = 'm2')",
FULL, nil,
},
// PREFIX & RANGE
optTData{
"select * where key ^= 'k' & (key > 'k1' & key < 'k8')",
RANGE, []string{"k1", "k8"},
},
optTData{
"select * where key ^= 'k' & (key > 'j1' & key < 'l8')",
PREFIX, []string{"k"},
},
optTData{
"select * where key ^= 'k' & (key > 'k1' & key < 'l8')",
RANGE, []string{"k1", "l8"},
},
optTData{
"select * where key ^= 'l' & (key > 'k1' & key < 'l8')",
RANGE, []string{"l", "l8"},
},
optTData{
"select * where key ^= 'j' & (key > 'k1' & key < 'l8')",
EMPTY, nil,
},
optTData{
"select * where key ^= 'm' & (key > 'k1' & key < 'l8')",
EMPTY, nil,
},
optTData{
"select * where key ^= 'm' & (key > 'k1' & key < 'm')",
MGET, []string{"m"},
},
// PREFIX | RANGE
optTData{
"select * where key ^= 'k' | (key > 'k1' & key < 'k8')",
PREFIX, []string{"k"},
},
optTData{
"select * where key ^= 'k' | (key > 'j1' & key < 'l8')",
RANGE, []string{"j1", "l8"},
},
optTData{
"select * where key ^= 'k' | (key > 'k1' & key < 'l8')",
RANGE, []string{"k", "l8"},
},
optTData{
"select * where key ^= 'l' | (key > 'k1' & key < 'l8')",
RANGE, []string{"k1", ""},
},
optTData{
"select * where key ^= 'j' | (key > 'k1' & key < 'l8')",
RANGE, []string{"j", "l8"},
},
optTData{
"select * where key ^= 'm' | (key > 'k1' & key < 'l8')",
RANGE, []string{"k1", ""},
},
optTData{
"select * where key ^= 'm' | (key > 'k1' & key < 'm')",
RANGE, []string{"k1", ""},
},
// RANGE & RANGE
optTData{
"select * where (key > 'k01' & key < 'k10') & (key > 'k05' & key < 'k12')",
RANGE, []string{"k05", "k10"},
},
// RANGE | RANGE
optTData{
"select * where (key > 'k1' & key < 'k5') | (key > 'k4' & key < 'k9')",
RANGE, []string{"k1", "k9"},
},
optTData{
"select * where (key > 'k1' & key < 'k5') | (key > 'k8' & key < 'k9')",
RANGE, []string{"k1", "k9"},
},
// RANGE & MGET
optTData{
"select * where (key > 'i' & key <= 'k') & (key = 'j1' | key = 'k' | key = 'k1')",
MGET, []string{"j1", "k"},
},
optTData{
"select * where (key > 'i' & key <= 'k') & (key = 'l1' | key = 'l2' | key = 'l3')",
EMPTY, nil,
},
// RANGE | MGET
optTData{
"select * where (key > 'k1' & key < 'k9') | (key = 'k5')",
RANGE, []string{"k1", "k9"},
},
optTData{
"select * where (key > 'k1' & key < 'k9') | (key = 'l1')",
RANGE, []string{"k1", "l1"},
},
optTData{
"select * where (key > 'k1' & key < 'k9') | (key = 'j1')",
RANGE, []string{"j1", "k9"},
},
optTData{
"select * where (key > 'k1' & key < 'k9') | (key = 'j1' | key = 'm1')",
FULL, nil,
},
// NOT RANGE
optTData{
"select * where !(key > 'k1' & key < 'k9')",
FULL, nil,
},
// Just MGET
optTData{
"select * where key in ('k1', 'k2')",
MGET, []string{"k1", "k2"},
},
// Just RANGE use between and
optTData{
"select * where key between 'k1' and 'k2'",
RANGE, []string{"k1", "k2"},
},
// and operator
optTData{
"select * where key = 'k1' and key = 'k2'",
EMPTY, nil,
},
// or operator
optTData{
"select * where key = 'k1' or key between 'k2' and 'k3'",
RANGE, []string{"k1", "k3"},
},
optTData{
"select * where key = 'k1' or key ='k2'",
MGET, []string{"k1", "k2"},
},
// Mix and, or keywords
optTData{
"select * where (key > 'k1' and key < 'k9') or (key = 'j1' or key = 'm1')",
FULL, nil,
},
}
for i, item := range tdata {
st, err := optimizeQuery(item.query)
if err != nil {
t.Errorf("[%d] %v", i, err)
}
fmt.Printf("[%d] %s\n `- %s\n\n", i, item.query, st.String())
assertScanTypeWithID(i, t, st, item.scanTp, item.keys)
}
}