/
route.go
859 lines (779 loc) · 25.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
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
/*
Copyright 2019 The Vitess Authors.
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 (
"strconv"
"vitess.io/vitess/go/mysql/collations"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
var _ logicalPlan = (*route)(nil)
// 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 {
v3Plan
order int
// 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
// Select is the AST for the query fragment that will be
// executed by this route.
Select sqlparser.SelectStatement
// resultColumns represent the columns returned by this route.
resultColumns []*resultColumn
// weight_string keeps track of the weight_string expressions
// that were added additionally for each column. These expressions
// are added to be used for collation of text columns.
weightStrings map[*resultColumn]int
// substitutions contain the list of table expressions that
// have to be substituted in the route's query.
substitutions []*tableSubstitution
// condition stores the AST condition that will be used
// to resolve the ERoute Values field.
condition sqlparser.Expr
// eroute is the primitive being built.
eroute *engine.Route
}
type tableSubstitution struct {
newExpr, oldExpr *sqlparser.AliasedTableExpr
}
func newRoute(stmt sqlparser.SelectStatement) (*route, *symtab) {
rb := &route{
Select: stmt,
order: 1,
weightStrings: make(map[*resultColumn]int),
}
return rb, newSymtabWithRoute(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
}
// Order implements the logicalPlan interface
func (rb *route) Order() int {
return rb.order
}
// Reorder implements the logicalPlan interface
func (rb *route) Reorder(order int) {
rb.order = order + 1
}
// Primitive implements the logicalPlan interface
func (rb *route) Primitive() engine.Primitive {
return rb.eroute
}
// ResultColumns implements the logicalPlan interface
func (rb *route) ResultColumns() []*resultColumn {
return rb.resultColumns
}
// PushAnonymous pushes an anonymous expression like '*' or NEXT VALUES
// into the select expression list of the route. This function is
// similar to PushSelect.
func (rb *route) PushAnonymous(expr sqlparser.SelectExpr) *resultColumn {
// TODO: we should not assume that the query is a SELECT
sel := rb.Select.(*sqlparser.Select)
sel.SelectExprs = append(sel.SelectExprs, expr)
// We just create a place-holder resultColumn. It won't
// match anything.
rc := &resultColumn{column: &column{origin: rb}}
rb.resultColumns = append(rb.resultColumns, rc)
return rc
}
// SetLimit adds a LIMIT clause to the route.
func (rb *route) SetLimit(limit *sqlparser.Limit) {
rb.Select.SetLimit(limit)
}
// Wireup implements the logicalPlan interface
func (rb *route) Wireup(plan logicalPlan, jt *jointab) error {
// Precaution: update ERoute.Values only if it's not set already.
if rb.eroute.Values == nil {
// Resolve values stored in the logical plan.
switch vals := rb.condition.(type) {
case *sqlparser.ComparisonExpr:
pv, err := rb.procureValues(plan, jt, vals.Right)
if err != nil {
return err
}
rb.eroute.Values = []evalengine.Expr{pv}
vals.Right = sqlparser.ListArg(engine.ListVarName)
case nil:
// no-op.
default:
pv, err := rb.procureValues(plan, jt, vals)
if err != nil {
return err
}
rb.eroute.Values = []evalengine.Expr{pv}
}
}
// 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.SelectExpr{
&sqlparser.AliasedExpr{
Expr: sqlparser.NewIntLiteral("1"),
},
}
}
case *sqlparser.ComparisonExpr:
if node.Operator == sqlparser.EqualOp {
if rb.exprIsValue(node.Left) && !rb.exprIsValue(node.Right) {
node.Left, node.Right = node.Right, node.Left
}
}
}
return true, nil
}, rb.Select)
// Substitute table names
for _, sub := range rb.substitutions {
*sub.oldExpr = *sub.newExpr
}
// 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(plan, node, rb.Order())
buf.WriteArg(":", joinVar)
return
}
case sqlparser.TableName:
if !sqlparser.SystemSchema(node.Qualifier.String()) {
node.Name.Format(buf)
return
}
node.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
}
// prepareTheAST does minor fixups of the SELECT struct before producing the query string
func (rb *route) prepareTheAST() {
_ = sqlparser.Walk(func(node sqlparser.SQLNode) (bool, error) {
switch node := node.(type) {
case *sqlparser.Select:
if len(node.SelectExprs) == 0 {
node.SelectExprs = []sqlparser.SelectExpr{
&sqlparser.AliasedExpr{
Expr: sqlparser.NewIntLiteral("1"),
},
}
}
case *sqlparser.ComparisonExpr:
// 42 = colName -> colName = 42
b := node.Operator == sqlparser.EqualOp
value := sqlparser.IsValue(node.Left)
name := sqlparser.IsColName(node.Right)
if b &&
value &&
name {
node.Left, node.Right = node.Right, node.Left
}
}
return true, nil
}, rb.Select)
}
// procureValues procures and converts the input into
// the expected types for rb.Values.
func (rb *route) procureValues(plan logicalPlan, jt *jointab, val sqlparser.Expr) (evalengine.Expr, error) {
switch typedVal := val.(type) {
case sqlparser.ValTuple:
exprs := make([]evalengine.Expr, 0, len(typedVal))
for _, item := range typedVal {
v, err := rb.procureValues(plan, jt, item)
if err != nil {
return nil, err
}
exprs = append(exprs, v)
}
return evalengine.NewTupleExpr(exprs...), nil
case *sqlparser.ColName:
joinVar := jt.Procure(plan, typedVal, rb.Order())
return evalengine.NewBindVar(joinVar, collations.TypedCollation{}), nil
default:
return evalengine.Translate(typedVal, semantics.EmptySemTable())
}
}
func (rb *route) isLocal(col *sqlparser.ColName) bool {
return col.Metadata.(*column).Origin() == 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.SelectStatement, jt *jointab) string {
formatter := func(buf *sqlparser.TrackedBuffer, node sqlparser.SQLNode) {
switch node := node.(type) {
case *sqlparser.ColName:
if !rb.isLocal(node) {
_, joinVar := jt.Lookup(node)
buf.WriteArg(":", joinVar)
return
}
case sqlparser.TableName:
if !sqlparser.SystemSchema(node.Qualifier.String()) {
node.Name.Format(buf)
return
}
node.Format(buf)
return
}
sqlparser.FormatImpossibleQuery(buf, node)
}
buffer := sqlparser.NewTrackedBuffer(formatter)
node := buffer.WriteNode(sel)
query := node.ParsedQuery()
return query.Query
}
// SupplyVar implements the logicalPlan interface
func (rb *route) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
// route is an atomic primitive. So, SupplyVar cannot be
// called on it.
panic("BUG: route is an atomic node.")
}
// SupplyCol implements the logicalPlan interface
func (rb *route) SupplyCol(col *sqlparser.ColName) (rc *resultColumn, colNumber int) {
c := col.Metadata.(*column)
for i, rc := range rb.resultColumns {
if rc.column == c {
return rc, i
}
}
// A new result has to be returned.
rc = &resultColumn{column: c}
rb.resultColumns = append(rb.resultColumns, rc)
// TODO: we should not assume that the query is a SELECT query
sel := rb.Select.(*sqlparser.Select)
sel.SelectExprs = append(sel.SelectExprs, &sqlparser.AliasedExpr{Expr: col})
return rc, len(rb.resultColumns) - 1
}
// SupplyWeightString implements the logicalPlan interface
func (rb *route) SupplyWeightString(colNumber int, alsoAddToGroupBy bool) (weightcolNumber int, err error) {
rc := rb.resultColumns[colNumber]
s, ok := rb.Select.(*sqlparser.Select)
if !ok {
return 0, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected AST struct for query")
}
aliasExpr, ok := s.SelectExprs[colNumber].(*sqlparser.AliasedExpr)
if !ok {
return 0, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected AST struct for query %T", s.SelectExprs[colNumber])
}
weightStringExpr := &sqlparser.FuncExpr{
Name: sqlparser.NewIdentifierCI("weight_string"),
Exprs: []sqlparser.SelectExpr{
&sqlparser.AliasedExpr{
Expr: aliasExpr.Expr,
},
},
}
expr := &sqlparser.AliasedExpr{
Expr: weightStringExpr,
}
if alsoAddToGroupBy {
sel, isSelect := rb.Select.(*sqlparser.Select)
if !isSelect {
return 0, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "cannot add weight string in %T", rb.Select)
}
sel.AddGroupBy(weightStringExpr)
}
if weightcolNumber, ok := rb.weightStrings[rc]; ok {
return weightcolNumber, nil
}
// It's ok to pass nil for pb and logicalPlan because PushSelect doesn't use them.
// TODO: we are ignoring a potential error here. need to clean this up
_, _, weightcolNumber, err = planProjection(nil, rb, expr, nil)
if err != nil {
return 0, err
}
rb.weightStrings[rc] = weightcolNumber
return weightcolNumber, nil
}
// Rewrite implements the logicalPlan interface
func (rb *route) Rewrite(inputs ...logicalPlan) error {
if len(inputs) != 0 {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "route: wrong number of inputs")
}
return nil
}
// Inputs implements the logicalPlan interface
func (rb *route) Inputs() []logicalPlan {
return []logicalPlan{}
}
// MergeSubquery returns true if the subquery route could successfully be merged
// with the outer route.
func (rb *route) MergeSubquery(pb *primitiveBuilder, inner *route) bool {
if rb.SubqueryCanMerge(pb, inner) {
if inner.eroute.Opcode == engine.DBA && (len(inner.eroute.SysTableTableName) > 0 || len(inner.eroute.SysTableTableSchema) > 0) {
switch rb.eroute.Opcode {
case engine.DBA, engine.Reference:
rb.eroute.SysTableTableSchema = append(rb.eroute.SysTableTableSchema, inner.eroute.SysTableTableSchema...)
for k, v := range inner.eroute.SysTableTableName {
if rb.eroute.SysTableTableName == nil {
rb.eroute.SysTableTableName = map[string]evalengine.Expr{}
}
rb.eroute.SysTableTableName[k] = v
}
rb.eroute.Opcode = engine.DBA
default:
return false
}
} else {
if rb.eroute.Opcode == engine.Reference {
rb.eroute.RoutingParameters = inner.eroute.RoutingParameters
rb.condition = inner.condition
}
}
rb.substitutions = append(rb.substitutions, inner.substitutions...)
inner.Redirect = rb
return true
}
return false
}
// MergeUnion returns true if the rhs route could successfully be merged
// with the rb route.
func (rb *route) MergeUnion(right *route, isDistinct bool) bool {
if rb.unionCanMerge(right, isDistinct) {
rb.substitutions = append(rb.substitutions, right.substitutions...)
right.Redirect = rb
return true
}
return false
}
func (rb *route) isSingleShard() bool {
return rb.eroute.Opcode.IsSingleShard()
}
// JoinCanMerge, SubqueryCanMerge and unionCanMerge have subtly different behaviors.
// The difference in behavior is around SelectReference.
// It's not worth trying to reuse the code between them.
func (rb *route) JoinCanMerge(pb *primitiveBuilder, rrb *route, ajoin *sqlparser.JoinTableExpr, where sqlparser.Expr) bool {
if rb.eroute.Keyspace.Name != rrb.eroute.Keyspace.Name {
return false
}
if rrb.eroute.Opcode == engine.Reference {
// Any opcode can join with a reference table.
return true
}
switch rb.eroute.Opcode {
case engine.Unsharded:
return rb.eroute.Opcode == rrb.eroute.Opcode
case engine.EqualUnique:
// Check if they target the same shard.
if rrb.eroute.Opcode == engine.EqualUnique && rb.eroute.Vindex == rrb.eroute.Vindex && valEqual(rb.condition, rrb.condition) {
return true
}
case engine.Reference:
return true
case engine.Next:
return false
case engine.DBA:
if rrb.eroute.Opcode != engine.DBA {
return false
}
if where == nil {
return true
}
return ajoin != nil
}
if ajoin == nil {
return false
}
for _, filter := range sqlparser.SplitAndExpression(nil, ajoin.Condition.On) {
if rb.canMergeOnFilter(pb, rrb, filter) {
return true
}
}
return false
}
func (rb *route) SubqueryCanMerge(pb *primitiveBuilder, inner *route) bool {
if rb.eroute.Keyspace.Name != inner.eroute.Keyspace.Name {
return false
}
// if either side is a reference table, and we know the other side will only run once,
// we can just merge them and use the opcode of the other side
if rb.eroute.Opcode == engine.Reference || inner.eroute.Opcode == engine.Reference {
return rb.isSingleShard() && inner.isSingleShard()
}
switch rb.eroute.Opcode {
case engine.Unsharded, engine.DBA:
return rb.eroute.Opcode == inner.eroute.Opcode
case engine.EqualUnique:
// Check if they target the same shard.
if inner.eroute.Opcode == engine.EqualUnique && rb.eroute.Vindex == inner.eroute.Vindex && valEqual(rb.condition, inner.condition) {
return true
}
case engine.Next:
return false
}
switch vals := inner.condition.(type) {
case *sqlparser.ColName:
if pb.st.Vindex(vals, rb) == inner.eroute.Vindex {
return true
}
}
return false
}
func (rb *route) unionCanMerge(other *route, distinct bool) bool {
if rb.eroute.Keyspace.Name != other.eroute.Keyspace.Name {
return false
}
switch rb.eroute.Opcode {
case engine.Unsharded, engine.Reference:
return rb.eroute.Opcode == other.eroute.Opcode
case engine.DBA:
return other.eroute.Opcode == engine.DBA &&
len(rb.eroute.SysTableTableSchema) == 0 &&
len(rb.eroute.SysTableTableName) == 0 &&
len(other.eroute.SysTableTableSchema) == 0 &&
len(other.eroute.SysTableTableName) == 0
case engine.EqualUnique:
// Check if they target the same shard.
if other.eroute.Opcode == engine.EqualUnique && rb.eroute.Vindex == other.eroute.Vindex && valEqual(rb.condition, other.condition) {
return true
}
case engine.Scatter:
return other.eroute.Opcode == engine.Scatter && !distinct
case engine.Next:
return false
}
return false
}
// canMergeOnFilter 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) canMergeOnFilter(pb *primitiveBuilder, rrb *route, filter sqlparser.Expr) bool {
comparison, ok := filter.(*sqlparser.ComparisonExpr)
if !ok {
return false
}
if comparison.Operator != sqlparser.EqualOp {
return false
}
left := comparison.Left
right := comparison.Right
lVindex := pb.st.Vindex(left, rb)
if lVindex == nil {
left, right = right, left
lVindex = pb.st.Vindex(left, rb)
}
if lVindex == nil || !lVindex.IsUnique() {
return false
}
rVindex := pb.st.Vindex(right, rrb)
if rVindex == nil {
return false
}
return rVindex == lVindex
}
// 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.
func (rb *route) UpdatePlan(pb *primitiveBuilder, filter sqlparser.Expr) {
switch rb.eroute.Opcode {
// For these opcodes, a new filter will not make any difference, so we can just exit early
case engine.Unsharded, engine.Next, engine.DBA, engine.Reference, engine.None:
return
}
opcode, vindex, values := rb.computePlan(pb, filter)
if opcode == engine.Scatter {
return
}
// If we get SelectNone in next filters, override the previous route plan.
if opcode == engine.None {
rb.updateRoute(opcode, vindex, values)
return
}
switch rb.eroute.Opcode {
case engine.EqualUnique:
if opcode == engine.EqualUnique && vindex.Cost() < rb.eroute.Vindex.Cost() {
rb.updateRoute(opcode, vindex, values)
}
case engine.Equal:
switch opcode {
case engine.EqualUnique:
rb.updateRoute(opcode, vindex, values)
case engine.Equal:
if vindex.Cost() < rb.eroute.Vindex.Cost() {
rb.updateRoute(opcode, vindex, values)
}
}
case engine.IN:
switch opcode {
case engine.EqualUnique, engine.Equal:
rb.updateRoute(opcode, vindex, values)
case engine.IN:
if vindex.Cost() < rb.eroute.Vindex.Cost() {
rb.updateRoute(opcode, vindex, values)
}
}
case engine.MultiEqual:
switch opcode {
case engine.EqualUnique, engine.Equal, engine.IN:
rb.updateRoute(opcode, vindex, values)
case engine.MultiEqual:
if vindex.Cost() < rb.eroute.Vindex.Cost() {
rb.updateRoute(opcode, vindex, values)
}
}
case engine.Scatter:
switch opcode {
case engine.EqualUnique, engine.Equal, engine.IN, engine.MultiEqual, engine.None:
rb.updateRoute(opcode, vindex, values)
}
}
}
func (rb *route) updateRoute(opcode engine.Opcode, vindex vindexes.SingleColumn, condition sqlparser.Expr) {
rb.eroute.Opcode = opcode
rb.eroute.Vindex = vindex
rb.condition = condition
}
// computePlan computes the plan for the specified filter.
func (rb *route) computePlan(pb *primitiveBuilder, filter sqlparser.Expr) (opcode engine.Opcode, vindex vindexes.SingleColumn, condition sqlparser.Expr) {
switch node := filter.(type) {
case *sqlparser.ComparisonExpr:
switch node.Operator {
case sqlparser.EqualOp:
return rb.computeEqualPlan(pb, node)
case sqlparser.InOp:
return rb.computeINPlan(pb, node)
case sqlparser.NotInOp:
return rb.computeNotInPlan(node.Right), nil, nil
case sqlparser.LikeOp:
return rb.computeLikePlan(pb, node)
}
case *sqlparser.IsExpr:
return rb.computeISPlan(pb, node)
}
return engine.Scatter, nil, nil
}
// computeLikePlan computes the plan for 'LIKE' constraint
func (rb *route) computeLikePlan(pb *primitiveBuilder, comparison *sqlparser.ComparisonExpr) (opcode engine.Opcode, vindex vindexes.SingleColumn, condition sqlparser.Expr) {
left := comparison.Left
right := comparison.Right
if sqlparser.IsNull(right) {
return engine.None, nil, nil
}
if !rb.exprIsValue(right) {
return engine.Scatter, nil, nil
}
vindex = pb.st.Vindex(left, rb)
if vindex == nil {
// if there is no vindex defined, scatter
return engine.Scatter, nil, nil
}
if subsharding, ok := vindex.(vindexes.Prefixable); ok {
return engine.Equal, subsharding.PrefixVindex(), right
}
return engine.Scatter, nil, nil
}
// computeEqualPlan computes the plan for an equality constraint.
func (rb *route) computeEqualPlan(pb *primitiveBuilder, comparison *sqlparser.ComparisonExpr) (opcode engine.Opcode, vindex vindexes.SingleColumn, condition sqlparser.Expr) {
left := comparison.Left
right := comparison.Right
if sqlparser.IsNull(right) {
return engine.None, nil, nil
}
vindex = pb.st.Vindex(left, rb)
if vindex == nil {
left, right = right, left
vindex = pb.st.Vindex(left, rb)
if vindex == nil {
return engine.Scatter, nil, nil
}
}
if !rb.exprIsValue(right) {
return engine.Scatter, nil, nil
}
if vindex.IsUnique() {
return engine.EqualUnique, vindex, right
}
return engine.Equal, vindex, right
}
// computeIS computes the plan for an equality constraint.
func (rb *route) computeISPlan(pb *primitiveBuilder, comparison *sqlparser.IsExpr) (opcode engine.Opcode, vindex vindexes.SingleColumn, expr sqlparser.Expr) {
// we only handle IS NULL correct. IsExpr can contain other expressions as well
if comparison.Right != sqlparser.IsNullOp {
return engine.Scatter, nil, nil
}
vindex = pb.st.Vindex(comparison.Left, rb)
// fallback to scatter gather if there is no vindex
if vindex == nil {
return engine.Scatter, nil, nil
}
if _, isLookup := vindex.(vindexes.Lookup); isLookup {
// the lookup table is keyed by the lookup value, so it does not support nulls
return engine.Scatter, nil, nil
}
if vindex.IsUnique() {
return engine.EqualUnique, vindex, &sqlparser.NullVal{}
}
return engine.Equal, vindex, &sqlparser.NullVal{}
}
// computeINPlan computes the plan for an IN constraint.
func (rb *route) computeINPlan(pb *primitiveBuilder, comparison *sqlparser.ComparisonExpr) (opcode engine.Opcode, vindex vindexes.SingleColumn, expr sqlparser.Expr) {
switch comparison.Left.(type) {
case *sqlparser.ColName:
return rb.computeSimpleINPlan(pb, comparison)
case sqlparser.ValTuple:
return rb.computeCompositeINPlan(pb, comparison)
}
return engine.Scatter, nil, nil
}
// computeSimpleINPlan computes the plan for a simple IN constraint.
func (rb *route) computeSimpleINPlan(pb *primitiveBuilder, comparison *sqlparser.ComparisonExpr) (opcode engine.Opcode, vindex vindexes.SingleColumn, expr sqlparser.Expr) {
vindex = pb.st.Vindex(comparison.Left, rb)
if vindex == nil {
return engine.Scatter, nil, nil
}
switch node := comparison.Right.(type) {
case sqlparser.ValTuple:
if len(node) == 1 && sqlparser.IsNull(node[0]) {
return engine.None, nil, nil
}
for _, n := range node {
if !rb.exprIsValue(n) {
return engine.Scatter, nil, nil
}
}
return engine.IN, vindex, comparison
case sqlparser.ListArg:
return engine.IN, vindex, comparison
}
return engine.Scatter, nil, nil
}
// computeCompositeINPlan computes the plan for a composite IN constraint.
func (rb *route) computeCompositeINPlan(pb *primitiveBuilder, comparison *sqlparser.ComparisonExpr) (opcode engine.Opcode, vindex vindexes.SingleColumn, values sqlparser.Expr) {
leftTuple := comparison.Left.(sqlparser.ValTuple)
return rb.iterateCompositeIN(pb, comparison, nil, leftTuple)
}
// iterateCompositeIN recursively walks the LHS tuple of the IN clause looking
// for column names. For those that match a vindex, it builds a multi-value plan
// using the corresponding values in the RHS. It returns the best of the plans built.
func (rb *route) iterateCompositeIN(
pb *primitiveBuilder,
comparison *sqlparser.ComparisonExpr,
coordinates []int,
tuple sqlparser.ValTuple,
) (opcode engine.Opcode, vindex vindexes.SingleColumn, values sqlparser.Expr) {
opcode = engine.Scatter
cindex := len(coordinates)
coordinates = append(coordinates, 0)
for idx, expr := range tuple {
coordinates[cindex] = idx
switch expr := expr.(type) {
case sqlparser.ValTuple:
newOpcode, newVindex, newValues := rb.iterateCompositeIN(pb, comparison, coordinates, expr)
opcode, vindex, values = bestOfComposite(opcode, newOpcode, vindex, newVindex, values, newValues)
case *sqlparser.ColName:
newVindex := pb.st.Vindex(expr, rb)
if newVindex != nil {
newOpcode, newValues := rb.compositePlanForCol(pb, comparison, coordinates)
opcode, vindex, values = bestOfComposite(opcode, newOpcode, vindex, newVindex, values, newValues)
}
}
}
return opcode, vindex, values
}
// compositePlanForCol builds a plan for a matched column in the LHS
// of a composite IN clause.
func (rb *route) compositePlanForCol(pb *primitiveBuilder, comparison *sqlparser.ComparisonExpr, coordinates []int) (opcode engine.Opcode, values sqlparser.Expr) {
rightTuple, ok := comparison.Right.(sqlparser.ValTuple)
if !ok {
return engine.Scatter, nil
}
retVal := make(sqlparser.ValTuple, len(rightTuple))
for i, rval := range rightTuple {
val := tupleAccess(rval, coordinates)
if val == nil {
return engine.Scatter, nil
}
if !rb.exprIsValue(val) {
return engine.Scatter, nil
}
retVal[i] = val
}
return engine.MultiEqual, retVal
}
// tupleAccess returns the value of the expression that corresponds
// to the specified coordinates.
func tupleAccess(expr sqlparser.Expr, coordinates []int) sqlparser.Expr {
tuple, _ := expr.(sqlparser.ValTuple)
for _, idx := range coordinates {
if idx >= len(tuple) {
return nil
}
expr = tuple[idx]
tuple, _ = expr.(sqlparser.ValTuple)
}
return expr
}
// bestOfComposite returns the best of two composite IN clause plans.
func bestOfComposite(opcode1, opcode2 engine.Opcode, vindex1, vindex2 vindexes.SingleColumn, values1, values2 sqlparser.Expr) (opcode engine.Opcode, vindex vindexes.SingleColumn, values sqlparser.Expr) {
if opcode1 == engine.Scatter {
return opcode2, vindex2, values2
}
if opcode2 == engine.Scatter {
return opcode1, vindex1, values1
}
if vindex1.Cost() < vindex2.Cost() {
return opcode1, vindex1, values1
}
return opcode2, vindex2, values2
}
// computeNotInPlan looks for null values to produce a SelectNone if found
func (rb *route) computeNotInPlan(right sqlparser.Expr) engine.Opcode {
switch node := right.(type) {
case sqlparser.ValTuple:
for _, n := range node {
if sqlparser.IsNull(n) {
return engine.None
}
}
}
return engine.Scatter
}
// exprIsValue returns true if the expression can be treated as a value
// for the routeOption. External references are treated as value.
func (rb *route) exprIsValue(expr sqlparser.Expr) bool {
if node, ok := expr.(*sqlparser.ColName); ok {
return node.Metadata.(*column).Origin() != rb
}
return sqlparser.IsValue(expr)
}
// queryTimeout returns DirectiveQueryTimeout value if set, otherwise returns 0.
func queryTimeout(d *sqlparser.CommentDirectives) int {
val, _ := d.GetString(sqlparser.DirectiveQueryTimeout, "0")
if intVal, err := strconv.Atoi(val); err == nil {
return intVal
}
return 0
}