-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathjoin_node.go
More file actions
889 lines (802 loc) · 23.1 KB
/
join_node.go
File metadata and controls
889 lines (802 loc) · 23.1 KB
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package builder
import (
"router"
"xcontext"
"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/xlog"
)
// JoinStrategy is Join Strategy.
type JoinStrategy int
const (
// Cartesian product.
Cartesian JoinStrategy = iota
// SortMerge Join.
SortMerge
// NestLoop Join.
NestLoop
)
// JoinKey is the column info in the on conditions.
type JoinKey struct {
// field name.
Field string
// table name.
Table string
// index in the fields.
Index int
}
// Comparison is record the sqlparser.Comparison info.
type Comparison struct {
// index in left and right node's fields.
Left, Right int
Operator string
// left expr may in right node.
Exchange bool
}
// otherJoin record join conditions except the equal conditions in left join.
type otherJoin struct {
// filters without tables.
noTables []sqlparser.Expr
// filters belong to JoinNode.Left.
left []selectTuple
// filters belong to the JoinNode.Right.
right []exprInfo
// filters cross-shard.
others []exprInfo
}
// JoinNode cannot be pushed down.
type JoinNode struct {
log *xlog.Log
// router.
router *router.Router
// Left and Right are the nodes for the join.
Left, Right PlanNode
// join strategy.
Strategy JoinStrategy
// JoinTableExpr in FROM clause.
joinExpr *sqlparser.JoinTableExpr
// referred tables' tableInfo map.
referTables map[string]*tableInfo
// whether has parenthese in FROM clause.
hasParen bool
// parent node in the plan tree.
parent *JoinNode
// children plans in select(such as: orderby, limit..).
children []ChildPlan
// Cols defines which columns from left or right results used to build the return result.
// For results coming from left, the values go as -1, -2, etc. For right, they're 1, 2, etc.
// If Cols is {-1, -2, 1, 2}, it means the returned result is {Left0, Left1, Right0, Right1}.
Cols []int `json:",omitempty"`
// the returned result fields.
fields []selectTuple
// the equal join conditions.
// eg: `t1.a=t2.a` in `t1 join t2 on t1.a=t2.a`.
joinOn []exprInfo
// eg: from t1 join t2 on t1.a=t2.b, 't1.a' put in LeftKeys, 't2.a' in RightKeys.
LeftKeys, RightKeys []JoinKey
// eg: t1 join t2 on t1.a>t2.a, 't1.a>t2.a' parse into CmpFilter.
CmpFilter []Comparison
/*
* eg: 't1 left join t2 on t1.a=t2.a and t1.b=2' where t1.c=t2.c and 1=1 and t2.b>2 and t2.str is null.
* 't1.b=2' will parse into otherLeftJoin. If join type is leftjoin, 't1.c=t2.c' parse into otherFilter,
* otherwise parse into joinOn. '1=1' parse into noTableFilter. 't2.str is null' parse into rightNull.
*/
// The cross-shard filters.
otherFilter []exprInfo
// The filters without tables, always true or false.
noTableFilter []sqlparser.Expr
// The join conditions except for the equal condition in left join.
otherLeftJoin *otherJoin
// The null conditions belong to JoinNode.Right, when join type is left join.
rightNull []selectTuple
// whether is left join.
IsLeftJoin bool
// whether the right node has filters in left join.
HasRightFilter bool
// record the `otherLeftJoin.left`'s index in left.fields.
LeftTmpCols []int
// record the `rightNull`'s index in right.fields.
RightTmpCols []int
order int
// Vars defines the list of joinVars that need to be built
// from the Left result before invoking the Right subqquery.
Vars map[string]int
}
// newJoinNode used to create JoinNode.
func newJoinNode(log *xlog.Log, Left, Right PlanNode, router *router.Router, joinExpr *sqlparser.JoinTableExpr,
joinOn []exprInfo, referTables map[string]*tableInfo) *JoinNode {
isLeftJoin := false
if joinExpr != nil && joinExpr.Join == sqlparser.LeftJoinStr {
isLeftJoin = true
}
return &JoinNode{
log: log,
Left: Left,
Right: Right,
router: router,
joinExpr: joinExpr,
joinOn: joinOn,
Vars: make(map[string]int),
referTables: referTables,
IsLeftJoin: isLeftJoin,
Strategy: SortMerge,
}
}
// getReferTables get the referTables.
func (j *JoinNode) getReferTables() map[string]*tableInfo {
return j.referTables
}
// getFields get the fields.
func (j *JoinNode) getFields() []selectTuple {
return j.fields
}
// pushFilter used to push the filter.
func (j *JoinNode) pushFilter(filter exprInfo) error {
if len(filter.referTables) == 0 {
j.noTableFilter = append(j.noTableFilter, filter.expr)
return nil
}
rightTbs := j.Right.getReferTables()
if j.IsLeftJoin {
// If left join's right node has "is null" condition, the condition
// will record in rightNull instead of push down.
if ok, nullFunc := checkIsWithNull(filter, rightTbs); ok {
j.rightNull = append(j.rightNull, nullFunc)
return nil
}
if !j.HasRightFilter {
for _, tb := range filter.referTables {
if _, ok := rightTbs[tb]; ok {
j.HasRightFilter = true
break
}
}
}
}
if len(filter.referTables) == 1 {
tb := filter.referTables[0]
tbInfo := j.referTables[tb]
if len(filter.cols) != 1 {
addFilter(tbInfo.parent, filter)
} else {
if err := j.pushKeyFilter(filter, filter.cols[0].Qualifier.Name.String(), filter.cols[0].Name.String()); err != nil {
return err
}
}
} else {
parent := findParent(filter.referTables, j)
addFilter(parent, filter)
}
return nil
}
// pushKeyFilter used to build the keyFilter based on the filter and joinOn.
// eg: select t1.a,t2.a from t1 join t2 on t1.a=t2.a where t1.a=1;
// push: select t1.a from t1 where t1.a=1 order by t1.a asc;
// select t2.a from t2 where t2.a=1 order by t2.a asc;
func (j *JoinNode) pushKeyFilter(filter exprInfo, table, field string) error {
var tb, col string
var err error
find := false
if _, ok := j.Left.getReferTables()[table]; ok {
for _, join := range j.joinOn {
lt := join.cols[0].Qualifier.Name.String()
lc := join.cols[0].Name.String()
if lt == table && lc == field {
tb = join.cols[1].Qualifier.Name.String()
col = join.cols[1].Name.String()
find = true
break
}
}
if err = j.Left.pushKeyFilter(filter, table, field); err != nil {
return err
}
if find {
// replace the colname.
origin := *(filter.cols[0])
filter.cols[0].Name = sqlparser.NewColIdent(col)
filter.cols[0].Qualifier = sqlparser.TableName{Name: sqlparser.NewTableIdent(tb)}
if err = j.Right.pushKeyFilter(filter, tb, col); err != nil {
return err
}
// recover the colname in exprisson.
*(filter.cols[0]) = origin
}
} else {
for _, join := range j.joinOn {
rt := join.cols[1].Qualifier.Name.String()
rc := join.cols[1].Name.String()
if rt == table && rc == field {
tb = join.cols[0].Qualifier.Name.String()
col = join.cols[0].Name.String()
find = true
break
}
}
if err = j.Right.pushKeyFilter(filter, table, field); err != nil {
return err
}
if find {
origin := *(filter.cols[0])
filter.cols[0].Name = sqlparser.NewColIdent(col)
filter.cols[0].Qualifier = sqlparser.TableName{Name: sqlparser.NewTableIdent(tb)}
if err = j.Left.pushKeyFilter(filter, tb, col); err != nil {
return err
}
*(filter.cols[0]) = origin
}
}
return nil
}
// setParent set the parent node.
func (j *JoinNode) setParent(p *JoinNode) {
j.parent = p
}
// addNoTableFilter used to push the no table filters.
func (j *JoinNode) addNoTableFilter(exprs []sqlparser.Expr) {
j.noTableFilter = append(j.noTableFilter, exprs...)
}
// setOtherJoin separate filters into j.otherLeftJoin.
func (j *JoinNode) setOtherJoin(filters []exprInfo) {
j.otherLeftJoin = &otherJoin{}
for _, filter := range filters {
if len(filter.referTables) == 0 {
j.otherLeftJoin.noTables = append(j.otherLeftJoin.noTables, filter.expr)
continue
}
if checkTbInNode(filter.referTables, j.Left.getReferTables()) {
buf := sqlparser.NewTrackedBuffer(nil)
filter.expr.Format(buf)
field := buf.String()
alias := "tmpc"
tuple := selectTuple{
expr: &sqlparser.AliasedExpr{Expr: filter.expr, As: sqlparser.NewColIdent(alias)},
info: filter,
field: field,
alias: alias,
}
j.otherLeftJoin.left = append(j.otherLeftJoin.left, tuple)
} else if checkTbInNode(filter.referTables, j.Right.getReferTables()) {
j.otherLeftJoin.right = append(j.otherLeftJoin.right, filter)
} else {
j.otherLeftJoin.others = append(j.otherLeftJoin.others, filter)
}
}
}
// pushOthers use to push otherLeftJoin and otherFilter.
// eg: select A.a from A left join B on A.id=B.id and 1=1 and A.c=1 and B.b='a';
// push: select A.c=1 as tmpc_0,A.a,A.id from A order by A.id asc;
// select B.id from B where 1=1 and B.b='a' order by B.id asc;
func (j *JoinNode) pushOthers() error {
if j.otherLeftJoin != nil {
if len(j.otherLeftJoin.noTables) > 0 {
j.Right.addNoTableFilter(j.otherLeftJoin.noTables)
}
for _, field := range j.otherLeftJoin.left {
index, err := j.Left.pushSelectExpr(field)
if err != nil {
return err
}
j.LeftTmpCols = append(j.LeftTmpCols, index)
}
// If its parent is JoinNode, add it to parent.otherFilter.
for _, filter := range j.otherLeftJoin.right {
parent := findParent(filter.referTables, j.Right)
addFilter(parent, filter)
}
if len(j.otherLeftJoin.others) > 0 {
j.judgeStrategy(j.otherLeftJoin.others)
if err := j.pushOtherFilters(j.otherLeftJoin.others, true); err != nil {
return err
}
}
}
j.judgeStrategy(j.otherFilter)
return j.pushOtherFilters(j.otherFilter, false)
}
// judgeStrategy judge the join strategy by analyze `otherLeftJoin.others` and `otherFilter`.
// SortMerge request the cross-shard expression must be a `ComparisonExpr` and its `left`
// and `right`'s parent must be `MergeNode`. Otherwise set the join strategy to NestLoop.
func (j *JoinNode) judgeStrategy(filters []exprInfo) {
for _, filter := range filters {
if j.Strategy == NestLoop {
return
}
if exp, ok := filter.expr.(*sqlparser.ComparisonExpr); ok {
left := findParent(getTbsInExpr(exp.Left), j)
if _, ok := left.(*JoinNode); ok {
j.setNestLoop()
return
}
right := findParent(getTbsInExpr(exp.Right), j)
if _, ok := right.(*JoinNode); ok {
j.setNestLoop()
}
} else {
j.setNestLoop()
}
}
}
// setNestLoop set the strategy to Nest Loop.
func (j *JoinNode) setNestLoop() {
if left, ok := j.Left.(*JoinNode); ok {
left.setNestLoop()
}
if right, ok := j.Right.(*JoinNode); ok {
right.setNestLoop()
}
j.Strategy = NestLoop
}
// pushEqualCmpr used to push the equal Comparison type filters.
// eg: 'select * from t1, t2 where t1.a=t2.a and t1.b=2'.
// 't1.a=t2.a' is the 'join' type filters.
func (j *JoinNode) pushEqualCmprs(joins []exprInfo) PlanNode {
for i, joinCond := range joins {
var parent PlanNode
ltb := j.referTables[joinCond.referTables[0]]
rtb := j.referTables[joinCond.referTables[1]]
parent = findLCA(j, ltb.parent, rtb.parent)
switch node := parent.(type) {
case *MergeNode:
node.addWhere(joinCond.expr)
case *JoinNode:
join, _ := checkJoinOn(node.Left, node.Right, joinCond)
if lmn, ok := node.Left.(*MergeNode); ok {
if rmn, ok := node.Right.(*MergeNode); ok {
if isSameShard(lmn.referTables, rmn.referTables, join.cols[0], join.cols[1]) {
mn, _ := mergeRoutes(lmn, rmn, node.joinExpr, nil)
mn.setParent(node.parent)
setParenthese(mn, node.hasParen)
for _, filter := range node.otherFilter {
mn.addWhere(filter.expr)
}
for _, expr := range node.noTableFilter {
mn.addWhere(expr)
}
if node.joinExpr == nil {
for _, joins := range node.joinOn {
mn.addWhere(joins.expr)
}
}
mn.addWhere(join.expr)
if node.parent == nil {
for _, joinCond := range joins[i+1:] {
mn.addWhere(joinCond.expr)
}
return mn
}
if node.parent.Left == node {
node.parent.Left = mn
} else {
node.parent.Right = mn
}
continue
}
}
}
if node.IsLeftJoin {
node.otherFilter = append(node.otherFilter, join)
} else {
node.joinOn = append(node.joinOn, join)
if node.joinExpr != nil {
node.joinExpr.On = &sqlparser.AndExpr{
Left: node.joinExpr.On,
Right: join.expr,
}
}
}
}
}
return j
}
// calcRoute used to calc the route.
func (j *JoinNode) calcRoute() (PlanNode, error) {
var err error
if j.Left, err = j.Left.calcRoute(); err != nil {
return j, err
}
if j.Right, err = j.Right.calcRoute(); err != nil {
return j, err
}
// left and right node have same routes.
if lmn, ok := j.Left.(*MergeNode); ok {
if rmn, ok := j.Right.(*MergeNode); ok {
if (lmn.backend != "" && lmn.backend == rmn.backend) || rmn.nonGlobalCnt == 0 || lmn.nonGlobalCnt == 0 {
if lmn.nonGlobalCnt == 0 {
lmn.backend = rmn.backend
lmn.routeLen = rmn.routeLen
lmn.indexes = rmn.indexes
}
mn, _ := mergeRoutes(lmn, rmn, j.joinExpr, nil)
mn.setParent(j.parent)
setParenthese(mn, j.hasParen)
for _, filter := range j.otherFilter {
mn.addWhere(filter.expr)
}
mn.addNoTableFilter(j.noTableFilter)
if j.joinExpr == nil && len(j.joinOn) > 0 {
for _, joinCond := range j.joinOn {
mn.addWhere(joinCond.expr)
}
}
return mn, nil
}
}
}
return j, nil
}
// pushSelectExprs used to push the select fields.
func (j *JoinNode) pushSelectExprs(fields, groups []selectTuple, sel *sqlparser.Select, aggTyp aggrType) error {
j.reOrder(0)
if len(groups) > 0 || aggTyp != nullAgg {
aggrPlan := NewAggregatePlan(j.log, sel.SelectExprs, fields, groups, false)
if err := aggrPlan.Build(); err != nil {
return err
}
j.children = append(j.children, aggrPlan)
fields = aggrPlan.tuples
}
for _, tuple := range fields {
if _, err := j.pushSelectExpr(tuple); err != nil {
return err
}
}
if err := j.handleOthers(); err != nil {
return err
}
j.handleJoinOn()
return nil
}
// handleOthers used to handle otherLeftJoin|rightNull|otherFilter.
func (j *JoinNode) handleOthers() error {
var err error
if err = j.pushNullExprs(); err != nil {
return err
}
if err = j.pushOthers(); err != nil {
return err
}
if lp, ok := j.Left.(*JoinNode); ok {
if err = lp.handleOthers(); err != nil {
return err
}
}
if rp, ok := j.Right.(*JoinNode); ok {
if err = rp.handleOthers(); err != nil {
return err
}
}
return nil
}
// pushNullExprs used to push rightNull.
func (j *JoinNode) pushNullExprs() error {
for _, tuple := range j.rightNull {
index, err := j.pushOtherFilter(j.Right, tuple)
if err != nil {
return err
}
j.RightTmpCols = append(j.RightTmpCols, index)
}
return nil
}
// pushOtherFilters used to push otherFilter.
func (j *JoinNode) pushOtherFilters(filters []exprInfo, isOtherJoin bool) error {
for _, filter := range filters {
switch j.Strategy {
case NestLoop:
var m *MergeNode
for _, tb := range filter.referTables {
tbInfo := j.referTables[tb]
if m == nil {
m = tbInfo.parent
continue
}
if m.Order() < tbInfo.parent.Order() {
m = tbInfo.parent
}
}
m.addWhere(filter.expr)
case SortMerge:
var err error
var lidx, ridx int
var exchange bool
exp, _ := filter.expr.(*sqlparser.ComparisonExpr)
left := parseExpr(exp.Left)
right := parseExpr(exp.Right)
ltb := j.Left.getReferTables()
isLeft := checkTbInNode(left.info.referTables, ltb)
// exp: t1.a + 1 = t2.a.
if exp.Operator == sqlparser.EqualStr && (isOtherJoin || !j.IsLeftJoin) &&
len(left.info.referTables) == 1 && len(right.info.referTables) == 1 {
if !isLeft {
exp.Left, exp.Right = exp.Right, exp.Left
left, right = right, left
}
leftKey := j.buildOrderBy(j.Left, left)
rightKey := j.buildOrderBy(j.Right, right)
j.LeftKeys = append(j.LeftKeys, leftKey)
j.RightKeys = append(j.RightKeys, rightKey)
continue
}
if isLeft {
if lidx, err = j.pushOtherFilter(j.Left, left); err != nil {
return err
}
if ridx, err = j.pushOtherFilter(j.Right, right); err != nil {
return err
}
} else {
if lidx, err = j.pushOtherFilter(j.Left, right); err != nil {
return err
}
if ridx, err = j.pushOtherFilter(j.Right, left); err != nil {
return err
}
exchange = true
}
j.CmpFilter = append(j.CmpFilter, Comparison{lidx, ridx, exp.Operator, exchange})
}
}
return nil
}
// pushOtherFilter used to push otherFilter.
func (j *JoinNode) pushOtherFilter(node PlanNode, tuple selectTuple) (int, error) {
var err error
index := -1
table := tuple.info.referTables[0]
if tuple.isCol {
fields := node.getFields()
for i, field := range fields {
if field.isCol {
if table == field.info.referTables[0] && tuple.field == field.field {
index = i
break
}
}
}
}
// key not in the select fields.
if index == -1 {
if !tuple.isCol {
tuple.alias = "tmpc"
}
index, err = node.pushSelectExpr(tuple)
if err != nil {
return index, err
}
}
return index, nil
}
// pushSelectExpr used to push the select field.
func (j *JoinNode) pushSelectExpr(field selectTuple) (int, error) {
if checkTbInNode(field.info.referTables, j.Left.getReferTables()) {
index, err := j.Left.pushSelectExpr(field)
if err != nil {
return -1, err
}
j.Cols = append(j.Cols, -index-1)
} else {
if exp, ok := field.expr.(*sqlparser.AliasedExpr); ok && j.IsLeftJoin {
if _, ok := exp.Expr.(*sqlparser.FuncExpr); ok {
return -1, errors.Errorf("unsupported: expr.'%s'.in.cross-shard.left.join", field.field)
}
}
if !checkTbInNode(field.info.referTables, j.Right.getReferTables()) {
if j.Strategy != NestLoop {
j.setNestLoop()
}
// store the origin field name.
if field.alias == "" {
field.expr.(*sqlparser.AliasedExpr).As = sqlparser.NewColIdent(field.field)
field.alias = field.field
}
}
index, err := j.Right.pushSelectExpr(field)
if err != nil {
return -1, err
}
j.Cols = append(j.Cols, index+1)
}
j.fields = append(j.fields, field)
return len(j.fields) - 1, nil
}
// handleJoinOn used to build order by based on On conditions.
func (j *JoinNode) handleJoinOn() {
// eg: select t1.a,t2.a from t1 join t2 on t1.a=t2.a;
// push: select t1.a from t1 order by t1.a asc;
// select t2.a from t2 order by t2.a asc;
if left, ok := j.Left.(*JoinNode); ok {
left.handleJoinOn()
}
if right, ok := j.Right.(*JoinNode); ok {
right.handleJoinOn()
}
for _, join := range j.joinOn {
var leftKey, rightKey JoinKey
switch j.Strategy {
case NestLoop:
lt := join.cols[0].Qualifier.Name.String()
rt := join.cols[1].Qualifier.Name.String()
ltb := j.referTables[lt]
rtb := j.referTables[rt]
origin := ltb.parent
if origin.Order() < rtb.parent.Order() {
origin = rtb.parent
}
origin.addWhere(join.expr)
leftKey = JoinKey{Field: join.cols[0].Name.String(),
Table: lt,
}
rightKey = JoinKey{Field: join.cols[1].Name.String(),
Table: rt,
}
case SortMerge:
leftKey = j.buildOrderBy(j.Left, parseExpr(join.cols[0]))
rightKey = j.buildOrderBy(j.Right, parseExpr(join.cols[1]))
}
j.LeftKeys = append(j.LeftKeys, leftKey)
j.RightKeys = append(j.RightKeys, rightKey)
}
}
func (j *JoinNode) buildOrderBy(node PlanNode, tuple selectTuple) JoinKey {
var col *sqlparser.ColName
index := -1
table := tuple.info.referTables[0]
if tuple.isCol {
fields := node.getFields()
for i, field := range fields {
if field.isCol {
if table == field.info.referTables[0] && tuple.field == field.field {
index = i
break
}
}
}
col = tuple.info.cols[0]
}
// key not in the select fields.
if index == -1 {
if !tuple.isCol {
tuple.alias = "tmpc"
}
index, _ = node.pushSelectExpr(tuple)
}
if !tuple.isCol {
col = &sqlparser.ColName{Name: tuple.expr.(*sqlparser.AliasedExpr).As}
}
if m, ok := node.(*MergeNode); ok {
m.Sel.(*sqlparser.Select).OrderBy = append(m.Sel.(*sqlparser.Select).OrderBy, &sqlparser.Order{
Expr: col,
Direction: sqlparser.AscScr,
})
}
return JoinKey{tuple.field, table, index}
}
// pushHaving used to push having expr.
func (j *JoinNode) pushHaving(having exprInfo) error {
var parent PlanNode
if len(having.referTables) == 0 {
j.Left.pushHaving(having)
j.Right.pushHaving(having)
return nil
} else if len(having.referTables) == 1 {
tbInfo := j.referTables[having.referTables[0]]
parent = tbInfo.parent
} else {
for _, tb := range having.referTables {
tbInfo := j.referTables[tb]
if parent == nil {
parent = tbInfo.parent
continue
}
if parent != tbInfo.parent {
switch j.Strategy {
case NestLoop:
if parent.Order() < tbInfo.parent.Order() {
parent = tbInfo.parent
}
case SortMerge:
parent = findLCA(j, parent, tbInfo.parent)
}
}
}
}
if mn, ok := parent.(*MergeNode); ok {
mn.addHaving(having.expr)
} else {
buf := sqlparser.NewTrackedBuffer(nil)
having.expr.Format(buf)
return errors.Errorf("unsupported: havings.'%s'.in.cross-shard.join", buf.String())
}
return nil
}
// pushOrderBy used to push the order by exprs.
func (j *JoinNode) pushOrderBy(orderBy sqlparser.OrderBy) error {
orderPlan := NewOrderByPlan(j.log, orderBy, j)
j.children = append(j.children, orderPlan)
return orderPlan.Build()
}
// pushLimit used to push limit.
func (j *JoinNode) pushLimit(limit *sqlparser.Limit) error {
limitPlan := NewLimitPlan(j.log, limit)
j.children = append(j.children, limitPlan)
return limitPlan.Build()
}
// pushMisc used tp push miscelleaneous constructs.
func (j *JoinNode) pushMisc(sel *sqlparser.Select) {
j.Left.pushMisc(sel)
j.Right.pushMisc(sel)
}
// Children returns the children of the plan.
func (j *JoinNode) Children() []ChildPlan {
return j.children
}
// reOrder satisfies the plannode interface.
func (j *JoinNode) reOrder(order int) {
j.Left.reOrder(order)
j.Right.reOrder(j.Left.Order())
j.order = j.Right.Order() + 1
}
// Order satisfies the plannode interface.
func (j *JoinNode) Order() int {
return j.order
}
// buildQuery used to build the QueryTuple.
func (j *JoinNode) buildQuery(root PlanNode) {
if j.Strategy == SortMerge {
if len(j.LeftKeys) == 0 && len(j.CmpFilter) == 0 && !j.IsLeftJoin {
j.Strategy = Cartesian
}
}
j.Right.addNoTableFilter(j.noTableFilter)
j.Right.buildQuery(root)
j.Left.addNoTableFilter(j.noTableFilter)
j.Left.buildQuery(root)
}
// GetQuery used to get the Querys.
func (j *JoinNode) GetQuery() []xcontext.QueryTuple {
querys := j.Left.GetQuery()
querys = append(querys, j.Right.GetQuery()...)
return querys
}
// procure requests for the specified column from the plan
// and returns the join var name for it.
func (j *JoinNode) procure(col *sqlparser.ColName) string {
var joinVar string
field := col.Name.String()
table := col.Qualifier.Name.String()
joinVar = col.Qualifier.Name.CompliantName() + "_" + col.Name.CompliantName()
if _, ok := j.Vars[joinVar]; ok {
return joinVar
}
// `col` must be in `j.Left`.
tuples := j.Left.getFields()
index := -1
for i, tuple := range tuples {
if tuple.isCol {
if field == tuple.field && table == tuple.info.referTables[0] {
index = i
break
}
}
}
// key not in the select fields.
if index == -1 {
tuple := selectTuple{
expr: &sqlparser.AliasedExpr{Expr: col},
info: exprInfo{col, []string{table}, []*sqlparser.ColName{col}, nil},
field: field,
isCol: true,
}
index, _ = j.Left.pushSelectExpr(tuple)
}
j.Vars[joinVar] = index
return joinVar
}