-
Notifications
You must be signed in to change notification settings - Fork 2k
/
expr.go
355 lines (334 loc) · 10.8 KB
/
expr.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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package planbuilder
import (
"bytes"
"errors"
"fmt"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
)
// splitAndExpression breaks up the Expr into AND-separated conditions
// and appends them to filters, which can be shuffled and recombined
// as needed.
func splitAndExpression(filters []sqlparser.Expr, node sqlparser.Expr) []sqlparser.Expr {
if node == nil {
return filters
}
switch node := node.(type) {
case *sqlparser.AndExpr:
filters = splitAndExpression(filters, node.Left)
return splitAndExpression(filters, node.Right)
case *sqlparser.ParenExpr:
// If the inner expression is AndExpr, then we can remove
// the parenthesis because they are unnecessary.
if node, ok := node.Expr.(*sqlparser.AndExpr); ok {
return splitAndExpression(filters, node)
}
}
return append(filters, node)
}
// skipParenthesis skips the parenthesis (if any) of an expression and
// returns the innermost unparenthesized expression.
func skipParenthesis(node sqlparser.Expr) sqlparser.Expr {
if node, ok := node.(*sqlparser.ParenExpr); ok {
return skipParenthesis(node.Expr)
}
return node
}
type subqueryInfo struct {
ast *sqlparser.Subquery
bldr builder
origin builder
}
// findOrigin identifies the right-most origin referenced by expr. In situations where
// the expression references columns from multiple origins, the expression will be
// pushed to the right-most origin, and the executor will use the results of
// the previous origins to feed the necessary values to the primitives on the right.
//
// If the expression contains a subquery, the right-most origin identification
// also follows the same rules of a normal expression. This is achieved by
// looking at the Externs field of its symbol table that contains the list of
// external references.
//
// Once the target origin is identified, we have to verify that the subquery's
// route can be merged with it. If it cannot, we fail the query. This is because
// we don't have the ability to wire up subqueries through expression evaluation
// primitives. Consequently, if the plan for a subquery comes out as a Join,
// we can immediately error out.
//
// Since findOrigin can itself be called from within a subquery, it has to assume
// that some of the external references may actually be pointing to an outer
// query. The isLocal response from the symtab is used to make sure that we
// only analyze symbols that point to the current symtab.
//
// If an expression has no references to the current query, then the left-most
// origin is chosen as the default.
func (pb *primitiveBuilder) findOrigin(expr sqlparser.Expr) (pullouts []*pulloutSubquery, origin builder, pushExpr sqlparser.Expr, err error) {
// highestOrigin tracks the highest origin referenced by the expression.
// Default is the First.
highestOrigin := pb.bldr.First()
// subqueries tracks the list of subqueries encountered.
var subqueries []subqueryInfo
// constructsMap tracks the sub-construct in which a subquery
// occurred. The construct type decides on how the query gets
// pulled out.
constructsMap := make(map[*sqlparser.Subquery]sqlparser.Expr)
err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *sqlparser.ColName:
newOrigin, isLocal, err := pb.st.Find(node)
if err != nil {
return false, err
}
if isLocal && newOrigin.Order() > highestOrigin.Order() {
highestOrigin = newOrigin
}
case *sqlparser.ComparisonExpr:
if node.Operator == sqlparser.InStr || node.Operator == sqlparser.NotInStr {
if sq, ok := node.Right.(*sqlparser.Subquery); ok {
constructsMap[sq] = node
}
}
case *sqlparser.ExistsExpr:
constructsMap[node.Subquery] = node
case *sqlparser.Subquery:
spb := newPrimitiveBuilder(pb.vschema, pb.jt)
switch stmt := node.Select.(type) {
case *sqlparser.Select:
if err := spb.processSelect(stmt, pb.st); err != nil {
return false, err
}
case *sqlparser.Union:
if err := spb.processUnion(stmt, pb.st); err != nil {
return false, err
}
default:
panic(fmt.Sprintf("BUG: unexpected SELECT type: %T", node))
}
sqi := subqueryInfo{
ast: node,
bldr: spb.bldr,
}
for _, extern := range spb.st.Externs {
// No error expected. These are resolved externs.
newOrigin, isLocal, _ := pb.st.Find(extern)
if !isLocal {
continue
}
if highestOrigin.Order() < newOrigin.Order() {
highestOrigin = newOrigin
}
if sqi.origin == nil {
sqi.origin = newOrigin
} else if sqi.origin.Order() < newOrigin.Order() {
sqi.origin = newOrigin
}
}
subqueries = append(subqueries, sqi)
return false, nil
case *sqlparser.FuncExpr:
switch {
// If it's last_insert_id, ensure it's a single unsharded route.
case node.Name.EqualString("last_insert_id"):
if rb, isRoute := pb.bldr.(*route); !isRoute || rb.ERoute.Keyspace.Sharded {
return false, errors.New("unsupported: LAST_INSERT_ID is only allowed for unsharded keyspaces")
}
}
return true, nil
}
return true, nil
}, expr)
if err != nil {
return nil, nil, nil, err
}
highestRoute, _ := highestOrigin.(*route)
for _, sqi := range subqueries {
subroute, _ := sqi.bldr.(*route)
if highestRoute != nil && subroute != nil && highestRoute.SubqueryCanMerge(pb, subroute) {
subroute.Redirect = highestRoute
continue
}
if sqi.origin != nil {
return nil, nil, nil, errors.New("unsupported: cross-shard correlated subquery")
}
sqName, hasValues := pb.jt.GenerateSubqueryVars()
construct, ok := constructsMap[sqi.ast]
if !ok {
// (subquery) -> :_sq
expr = sqlparser.ReplaceExpr(expr, sqi.ast, sqlparser.NewValArg([]byte(":"+sqName)))
pullouts = append(pullouts, newPulloutSubquery(engine.PulloutValue, sqName, hasValues, sqi.bldr))
continue
}
switch construct := construct.(type) {
case *sqlparser.ComparisonExpr:
if construct.Operator == sqlparser.InStr {
// a in (subquery) -> (:__sq_has_values = 1 and (a in ::__sq))
newExpr := &sqlparser.ParenExpr{
Expr: &sqlparser.AndExpr{
Left: &sqlparser.ComparisonExpr{
Left: sqlparser.NewValArg([]byte(":" + hasValues)),
Operator: sqlparser.EqualStr,
Right: sqlparser.NewIntVal([]byte("1")),
},
Right: &sqlparser.ParenExpr{
Expr: sqlparser.ReplaceExpr(construct, sqi.ast, sqlparser.ListArg([]byte("::"+sqName))),
},
},
}
expr = sqlparser.ReplaceExpr(expr, construct, newExpr)
pullouts = append(pullouts, newPulloutSubquery(engine.PulloutIn, sqName, hasValues, sqi.bldr))
} else {
// a not in (subquery) -> (:__sq_has_values = 0 or (a not in ::__sq))
newExpr := &sqlparser.ParenExpr{
Expr: &sqlparser.OrExpr{
Left: &sqlparser.ComparisonExpr{
Left: sqlparser.NewValArg([]byte(":" + hasValues)),
Operator: sqlparser.EqualStr,
Right: sqlparser.NewIntVal([]byte("0")),
},
Right: &sqlparser.ParenExpr{
Expr: sqlparser.ReplaceExpr(construct, sqi.ast, sqlparser.ListArg([]byte("::"+sqName))),
},
},
}
expr = sqlparser.ReplaceExpr(expr, construct, newExpr)
pullouts = append(pullouts, newPulloutSubquery(engine.PulloutNotIn, sqName, hasValues, sqi.bldr))
}
case *sqlparser.ExistsExpr:
// exists (subquery) -> :__sq_has_values
expr = sqlparser.ReplaceExpr(expr, construct, sqlparser.NewValArg([]byte(":"+hasValues)))
pullouts = append(pullouts, newPulloutSubquery(engine.PulloutExists, sqName, hasValues, sqi.bldr))
}
}
return pullouts, highestOrigin, expr, nil
}
func hasSubquery(node sqlparser.SQLNode) bool {
has := false
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
if _, ok := node.(*sqlparser.Subquery); ok {
has = true
return false, errors.New("dummy")
}
return true, nil
}, node)
return has
}
func (pb *primitiveBuilder) validateSubquerySamePlan(nodes ...sqlparser.SQLNode) bool {
var keyspace string
if rb, ok := pb.bldr.(*route); ok {
keyspace = rb.ERoute.Keyspace.Name
}
samePlan := true
for _, node := range nodes {
inSubQuery := false
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch nodeType := node.(type) {
case *sqlparser.Subquery, *sqlparser.Insert:
inSubQuery = true
return true, nil
case *sqlparser.Select:
if !inSubQuery {
return true, nil
}
spb := newPrimitiveBuilder(pb.vschema, pb.jt)
if err := spb.processSelect(nodeType, pb.st); err != nil {
samePlan = false
return false, err
}
innerRoute, ok := spb.bldr.(*route)
if !ok {
samePlan = false
return false, errors.New("dummy")
}
if innerRoute.ERoute.Keyspace.Name != keyspace {
samePlan = false
return false, errors.New("dummy")
}
case *sqlparser.Union:
if !inSubQuery {
return true, nil
}
spb := newPrimitiveBuilder(pb.vschema, pb.jt)
if err := spb.processUnion(nodeType, pb.st); err != nil {
samePlan = false
return false, err
}
innerRoute, ok := spb.bldr.(*route)
if !ok {
samePlan = false
return false, errors.New("dummy")
}
if innerRoute.ERoute.Keyspace.Name != keyspace {
samePlan = false
return false, errors.New("dummy")
}
}
return true, nil
}, node)
if !samePlan {
return false
}
}
return true
}
func valEqual(a, b sqlparser.Expr) bool {
switch a := a.(type) {
case *sqlparser.ColName:
if b, ok := b.(*sqlparser.ColName); ok {
return a.Metadata == b.Metadata
}
case *sqlparser.SQLVal:
b, ok := b.(*sqlparser.SQLVal)
if !ok {
return false
}
switch a.Type {
case sqlparser.ValArg:
if b.Type == sqlparser.ValArg {
return bytes.Equal([]byte(a.Val), []byte(b.Val))
}
case sqlparser.StrVal:
switch b.Type {
case sqlparser.StrVal:
return bytes.Equal([]byte(a.Val), []byte(b.Val))
case sqlparser.HexVal:
return hexEqual(b, a)
}
case sqlparser.HexVal:
return hexEqual(a, b)
case sqlparser.IntVal:
if b.Type == (sqlparser.IntVal) {
return bytes.Equal([]byte(a.Val), []byte(b.Val))
}
}
}
return false
}
func hexEqual(a, b *sqlparser.SQLVal) bool {
v, err := a.HexDecode()
if err != nil {
return false
}
switch b.Type {
case sqlparser.StrVal:
return bytes.Equal(v, b.Val)
case sqlparser.HexVal:
v2, err := b.HexDecode()
if err != nil {
return false
}
return bytes.Equal(v, v2)
}
return false
}