forked from vitessio/vitess
/
route.go
539 lines (498 loc) · 15.9 KB
/
route.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
// Copyright 2016, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package planbuilder
import (
"errors"
"github.com/youtube/vitess/go/vt/sqlparser"
"github.com/youtube/vitess/go/vt/vtgate/engine"
"github.com/youtube/vitess/go/vt/vtgate/vindexes"
)
// route is used to build a Route primitive.
// It's used to build one of the Select routes like
// SelectScatter, etc. Portions of the original Select AST
// are moved into this node, which will be used to build
// the final SQL for this route.
type route struct {
// Redirect may point to another route if this route
// was merged with it. The Resolve function chases
// this pointer till the last un-redirected route.
Redirect *route
// IsRHS is true if the route is the RHS of a
// LEFT JOIN. If so, many restrictions come into play.
IsRHS bool
// Select is the AST for the query fragment that will be
// executed by this route.
Select sqlparser.Select
order int
symtab *symtab
// Colsyms represent the columns returned by this route.
Colsyms []*colsym
// ERoute is the primitive being built.
ERoute *engine.Route
}
func newRoute(from sqlparser.TableExprs, eroute *engine.Route, table *vindexes.Table, vschema VSchema, alias, astName sqlparser.TableIdent) *route {
// We have some circular pointer references here:
// The route points to the symtab idicating
// the symtab that should be used to resolve symbols
// for it. This is same as the SELECT statement's symtab.
// This pointer is needed because each subquery will have
// its own symtab. Multiple routes can point to the same
// symtab.
// The tabelAlias, which is inside the symtab, points back
// to the route to indidcate that the symbol is produced
// by this route. A symbol referenced in a route can actually
// be pointing to a different route. This information is used
// to determine if symbol references are local or not.
rb := &route{
Select: sqlparser.Select{From: from},
symtab: newSymtab(vschema),
order: 1,
ERoute: eroute,
}
_ = rb.symtab.AddAlias(alias, astName, table, rb)
return rb
}
// Resolve resolves redirects, and returns the last
// un-redirected route.
func (rb *route) Resolve() *route {
for rb.Redirect != nil {
rb = rb.Redirect
}
return rb
}
// Symtab returns the associated symtab.
func (rb *route) Symtab() *symtab {
return rb.symtab
}
// SetSymtab sets the symtab.
func (rb *route) SetSymtab(symtab *symtab) {
rb.symtab = symtab
}
// Order returns the order of the node.
func (rb *route) Order() int {
return rb.order
}
// SetOrder sets the order to one above the specified number.
func (rb *route) SetOrder(order int) {
rb.order = order + 1
}
// Primitve returns the built primitive.
func (rb *route) Primitive() engine.Primitive {
return rb.ERoute
}
// Leftmost returns the current route.
func (rb *route) Leftmost() *route {
return rb
}
// Join joins with the RHS. This could produce a merged route
// or a new join node.
func (rb *route) Join(rhs builder, ajoin *sqlparser.JoinTableExpr) (builder, error) {
rRoute, ok := rhs.(*route)
if !ok {
return newJoin(rb, rhs, ajoin)
}
if rb.ERoute.Keyspace.Name != rRoute.ERoute.Keyspace.Name {
return newJoin(rb, rRoute, ajoin)
}
if rb.ERoute.Opcode == engine.SelectUnsharded {
// Two Routes from the same unsharded keyspace can be merged.
return rb.merge(rRoute, ajoin)
}
// Both route are sharded routes. Analyze join condition for merging.
for _, filter := range splitAndExpression(nil, ajoin.On) {
if rb.isSameRoute(rRoute, filter) {
return rb.merge(rRoute, ajoin)
}
}
// Both l & r routes point to the same shard.
if rb.ERoute.Opcode == engine.SelectEqualUnique && rRoute.ERoute.Opcode == engine.SelectEqualUnique {
if valEqual(rb.ERoute.Values, rRoute.ERoute.Values) {
return rb.merge(rRoute, ajoin)
}
}
return newJoin(rb, rRoute, ajoin)
}
// SetRHS marks the route as RHS.
func (rb *route) SetRHS() {
rb.IsRHS = true
}
// merge merges the two routes. The ON clause is also analyzed to
// see if the primitive can be improved. The operation can fail if
// the expression contains a non-pushable subquery.
func (rb *route) merge(rhs *route, ajoin *sqlparser.JoinTableExpr) (builder, error) {
rb.Select.From = sqlparser.TableExprs{ajoin}
if ajoin.Join == sqlparser.LeftJoinStr {
rhs.Symtab().SetRHS()
}
err := rb.Symtab().Merge(rhs.Symtab())
rhs.Redirect = rb
if err != nil {
return nil, err
}
for _, filter := range splitAndExpression(nil, ajoin.On) {
// If VTGate evolves, this section should be rewritten
// to use processBoolExpr.
_, err = findRoute(filter, rb)
if err != nil {
return nil, err
}
rb.UpdatePlan(filter)
}
return rb, nil
}
// isSameRoute returns true if the join constraint makes the routes
// mergeable by unique vindex. The constraint has to be an equality
// like a.id = b.id where both columns have the same unique vindex.
func (rb *route) isSameRoute(rhs *route, filter sqlparser.BoolExpr) bool {
comparison, ok := filter.(*sqlparser.ComparisonExpr)
if !ok {
return false
}
if comparison.Operator != sqlparser.EqualStr {
return false
}
left := comparison.Left
right := comparison.Right
lVindex := rb.Symtab().Vindex(left, rb, false)
if lVindex == nil {
left, right = right, left
lVindex = rb.Symtab().Vindex(left, rb, false)
}
if lVindex == nil || !vindexes.IsUnique(lVindex) {
return false
}
rVindex := rhs.Symtab().Vindex(right, rhs, false)
if rVindex == nil {
return false
}
if rVindex != lVindex {
return false
}
return true
}
// PushFilter pushes the filter into the route. The primitive will
// be updated if the new filter improves it.
func (rb *route) PushFilter(filter sqlparser.BoolExpr, whereType string) error {
if rb.IsRHS {
return errors.New("unsupported: complex left join and where claused")
}
switch whereType {
case sqlparser.WhereStr:
rb.Select.AddWhere(filter)
case sqlparser.HavingStr:
rb.Select.AddHaving(filter)
}
rb.UpdatePlan(filter)
return nil
}
// UpdatePlan evaluates the primitive against the specified
// filter. If it's an improvement, the primitive is updated.
// We assume that the filter has already been pushed into
// the route. This function should only be used when merging
// routes, where the ON clause gets implicitly pushed into
// the merged route.
func (rb *route) UpdatePlan(filter sqlparser.BoolExpr) {
opcode, vindex, values := rb.computePlan(filter)
if opcode == engine.SelectScatter {
return
}
switch rb.ERoute.Opcode {
case engine.SelectEqualUnique:
if opcode == engine.SelectEqualUnique && vindex.Cost() < rb.ERoute.Vindex.Cost() {
rb.updateRoute(opcode, vindex, values)
}
case engine.SelectEqual:
switch opcode {
case engine.SelectEqualUnique:
rb.updateRoute(opcode, vindex, values)
case engine.SelectEqual:
if vindex.Cost() < rb.ERoute.Vindex.Cost() {
rb.updateRoute(opcode, vindex, values)
}
}
case engine.SelectIN:
switch opcode {
case engine.SelectEqualUnique, engine.SelectEqual:
rb.updateRoute(opcode, vindex, values)
case engine.SelectIN:
if vindex.Cost() < rb.ERoute.Vindex.Cost() {
rb.updateRoute(opcode, vindex, values)
}
}
case engine.SelectScatter:
switch opcode {
case engine.SelectEqualUnique, engine.SelectEqual, engine.SelectIN:
rb.updateRoute(opcode, vindex, values)
}
}
}
func (rb *route) updateRoute(opcode engine.RouteOpcode, vindex vindexes.Vindex, values interface{}) {
rb.ERoute.Opcode = opcode
rb.ERoute.Vindex = vindex
rb.ERoute.Values = values
}
// ComputePlan computes the plan for the specified filter.
func (rb *route) computePlan(filter sqlparser.BoolExpr) (opcode engine.RouteOpcode, vindex vindexes.Vindex, values interface{}) {
switch node := filter.(type) {
case *sqlparser.ComparisonExpr:
switch node.Operator {
case sqlparser.EqualStr:
return rb.computeEqualPlan(node)
case sqlparser.InStr:
return rb.computeINPlan(node)
}
}
return engine.SelectScatter, nil, nil
}
// computeEqualPlan computes the plan for an equality constraint.
func (rb *route) computeEqualPlan(comparison *sqlparser.ComparisonExpr) (opcode engine.RouteOpcode, vindex vindexes.Vindex, values interface{}) {
left := comparison.Left
right := comparison.Right
vindex = rb.Symtab().Vindex(left, rb, true)
if vindex == nil {
left, right = right, left
vindex = rb.Symtab().Vindex(left, rb, true)
if vindex == nil {
return engine.SelectScatter, nil, nil
}
}
if !exprIsValue(right, rb) {
return engine.SelectScatter, nil, nil
}
if vindexes.IsUnique(vindex) {
return engine.SelectEqualUnique, vindex, right
}
return engine.SelectEqual, vindex, right
}
// computeINPlan computes the plan for an IN constraint.
func (rb *route) computeINPlan(comparison *sqlparser.ComparisonExpr) (opcode engine.RouteOpcode, vindex vindexes.Vindex, values interface{}) {
vindex = rb.Symtab().Vindex(comparison.Left, rb, true)
if vindex == nil {
return engine.SelectScatter, nil, nil
}
switch node := comparison.Right.(type) {
case sqlparser.ValTuple:
for _, n := range node {
if !exprIsValue(n, rb) {
return engine.SelectScatter, nil, nil
}
}
return engine.SelectIN, vindex, comparison
case sqlparser.ListArg:
return engine.SelectIN, vindex, comparison
}
return engine.SelectScatter, nil, nil
}
// PushSelect pushes the select expression into the route.
func (rb *route) PushSelect(expr *sqlparser.NonStarExpr, _ *route) (colsym *colsym, colnum int, err error) {
colsym = newColsym(rb, rb.Symtab())
if expr.As.Original() != "" {
colsym.Alias = expr.As
}
if col, ok := expr.Expr.(*sqlparser.ColName); ok {
if colsym.Alias.Original() == "" {
colsym.Alias = sqlparser.NewColIdent(sqlparser.String(col))
}
colsym.Vindex = rb.Symtab().Vindex(col, rb, true)
colsym.Underlying = newColref(col)
} else {
if rb.IsRHS {
return nil, 0, errors.New("unsupported: complex left join and column expressions")
}
}
rb.Select.SelectExprs = append(rb.Select.SelectExprs, expr)
rb.Colsyms = append(rb.Colsyms, colsym)
return colsym, len(rb.Colsyms) - 1, nil
}
// PushStar pushes the '*' expression into the route.
func (rb *route) PushStar(expr *sqlparser.StarExpr) *colsym {
colsym := newColsym(rb, rb.Symtab())
colsym.Alias = sqlparser.NewColIdent(sqlparser.String(expr))
rb.Select.SelectExprs = append(rb.Select.SelectExprs, expr)
rb.Colsyms = append(rb.Colsyms, colsym)
return colsym
}
// MakeDistinct sets the DISTINCT property to the select.
func (rb *route) MakeDistinct() {
rb.Select.Distinct = sqlparser.DistinctStr
}
// SetGroupBy sets the GROUP BY clause for the route.
func (rb *route) SetGroupBy(groupBy sqlparser.GroupBy) {
rb.Select.GroupBy = groupBy
}
// AddOrder adds an ORDER BY expression to the route.
func (rb *route) AddOrder(order *sqlparser.Order) error {
if rb.IsRHS {
return errors.New("unsupported: complex left join and order by")
}
rb.Select.OrderBy = append(rb.Select.OrderBy, order)
return nil
}
// SetLimit adds a LIMIT clause to the route.
func (rb *route) SetLimit(limit *sqlparser.Limit) {
rb.Select.Limit = limit
}
// PushMisc updates the comments & 'for update' sections of the route.
func (rb *route) PushMisc(sel *sqlparser.Select) {
rb.Select.Comments = sel.Comments
rb.Select.Lock = sel.Lock
}
// Wireup performs the wire-up tasks.
func (rb *route) Wireup(bldr builder, jt *jointab) error {
// Resolve values stored in the builder.
var err error
switch vals := rb.ERoute.Values.(type) {
case *sqlparser.ComparisonExpr:
// A comparison expression is stored only if it was an IN clause.
// We have to convert it to use a list argutment and resolve values.
rb.ERoute.Values, err = rb.procureValues(bldr, jt, vals.Right)
if err != nil {
return err
}
vals.Right = sqlparser.ListArg("::" + engine.ListVarName)
default:
rb.ERoute.Values, err = rb.procureValues(bldr, jt, vals)
if err != nil {
return err
}
}
// Fix up the AST.
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (bool, error) {
switch node := node.(type) {
case *sqlparser.Select:
if len(node.SelectExprs) == 0 {
node.SelectExprs = sqlparser.SelectExprs([]sqlparser.SelectExpr{
&sqlparser.NonStarExpr{
Expr: sqlparser.NumVal([]byte{'1'}),
},
})
}
case *sqlparser.ComparisonExpr:
if node.Operator == sqlparser.EqualStr {
if exprIsValue(node.Left, rb) && !exprIsValue(node.Right, rb) {
node.Left, node.Right = node.Right, node.Left
}
}
}
return true, nil
}, &rb.Select)
// Generate query while simultaneously resolving values.
varFormatter := func(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) {
switch node := node.(type) {
case *sqlparser.ColName:
if !rb.isLocal(node) {
joinVar := jt.Procure(bldr, node, rb.Order())
rb.ERoute.JoinVars[joinVar] = struct{}{}
buf.Myprintf("%a", ":"+joinVar)
return
}
case *sqlparser.TableName:
node.Name.Format(buf)
return
}
node.Format(buf)
}
buf := sqlparser.NewTrackedBuffer(varFormatter)
varFormatter(buf, &rb.Select)
rb.ERoute.Query = buf.ParsedQuery().Query
rb.ERoute.FieldQuery = rb.generateFieldQuery(&rb.Select, jt)
return nil
}
// procureValues procures and converts the input into
// the expected types for rb.Values.
func (rb *route) procureValues(bldr builder, jt *jointab, val interface{}) (interface{}, error) {
switch val := val.(type) {
case nil:
return nil, nil
case sqlparser.ValTuple:
vals := make([]interface{}, 0, len(val))
for _, val := range val {
v, err := rb.procureValues(bldr, jt, val)
if err != nil {
return nil, err
}
vals = append(vals, v)
}
return vals, nil
case *sqlparser.ColName:
joinVar := jt.Procure(bldr, val, rb.Order())
rb.ERoute.JoinVars[joinVar] = struct{}{}
return ":" + joinVar, nil
case sqlparser.ListArg:
return string(val), nil
case sqlparser.ValExpr:
return valConvert(val)
}
panic("unrecognized symbol")
}
func (rb *route) isLocal(col *sqlparser.ColName) bool {
return col.Metadata.(sym).Route() == rb
}
// generateFieldQuery generates a query with an impossible where.
// This will be used on the RHS node to fetch field info if the LHS
// returns no result.
func (rb *route) generateFieldQuery(sel *sqlparser.Select, jt *jointab) string {
formatter := func(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) {
switch node := node.(type) {
case *sqlparser.Select:
buf.Myprintf("select %v from %v where 1 != 1", node.SelectExprs, node.From)
return
case *sqlparser.JoinTableExpr:
if node.Join == sqlparser.LeftJoinStr || node.Join == sqlparser.RightJoinStr {
// ON clause is requried
buf.Myprintf("%v %s %v on 1 != 1", node.LeftExpr, node.Join, node.RightExpr)
} else {
buf.Myprintf("%v %s %v", node.LeftExpr, node.Join, node.RightExpr)
}
return
case *sqlparser.ColName:
if !rb.isLocal(node) {
_, joinVar := jt.Lookup(node)
buf.Myprintf("%a", ":"+joinVar)
return
}
case *sqlparser.TableName:
node.Name.Format(buf)
return
}
node.Format(buf)
}
buf := sqlparser.NewTrackedBuffer(formatter)
formatter(buf, sel)
return buf.ParsedQuery().Query
}
// SupplyVar should be unreachable.
func (rb *route) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
panic("unreachable")
}
// SupplyCol changes the router to supply the requested column
// name, and returns the result column number. If the column
// is already in the list, it's reused.
func (rb *route) SupplyCol(ref colref) int {
for i, colsym := range rb.Colsyms {
if colsym.Underlying == ref {
return i
}
}
ts := ref.Meta.(*tabsym)
rb.Colsyms = append(rb.Colsyms, &colsym{
Alias: sqlparser.NewColIdent(string(ts.Alias) + "." + ref.Name),
Underlying: ref,
})
rb.Select.SelectExprs = append(
rb.Select.SelectExprs,
&sqlparser.NonStarExpr{
Expr: &sqlparser.ColName{
Metadata: ref.Meta,
Qualifier: &sqlparser.TableName{Name: ts.ASTName},
Name: sqlparser.NewColIdent(ref.Name),
},
},
)
return len(rb.Colsyms) - 1
}
// IsSingle returns true if the route targets only one database.
func (rb *route) IsSingle() bool {
return rb.ERoute.Opcode == engine.SelectUnsharded || rb.ERoute.Opcode == engine.SelectEqualUnique
}