forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexed_joins.go
1059 lines (964 loc) · 28.1 KB
/
indexed_joins.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
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020-2021 Dolthub, 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 analyzer
import (
"fmt"
"regexp"
"strings"
"github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/plan"
"github.com/gabereiser/go-mysql-server/sql/transform"
)
// constructJoinPlan finds an optimal table ordering and access plan
// for the tables in the query.
func constructJoinPlan(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
span, ctx := ctx.Span("construct_join_plan")
defer span.End()
if !n.Resolved() {
return n, transform.SameTree, nil
}
if plan.IsNoRowNode(n) {
return n, transform.SameTree, nil
}
_, isUpdate := n.(*plan.Update)
reorder := true
transform.NodeWithCtx(n, nil, func(c transform.Context) (sql.Node, transform.TreeIdentity, error) {
switch n := c.Node.(type) {
case *plan.JSONTable:
// TODO make a JoinTypeJSONTable[Cross], and use have its TES
// treated the same way as a left join for reordering.
reorder = false
case *plan.Project:
// TODO: fix natural joins, their project nodes should apply
// to the top-level scope not the middle of a join tree.
switch c.Parent.(type) {
case *plan.JoinNode:
reorder = false
}
case *plan.JoinNode:
if n.JoinType().IsPhysical() {
// TODO: nested subqueries attempt to replan joins, which
// is not ideal but not the end of the world.
reorder = false
}
default:
}
return n, transform.SameTree, nil
})
return inOrderReplanJoin(ctx, a, scope, nil, n, reorder, isUpdate)
}
// inOrderReplanJoin either fixes field indexes for join nodes or
// replans a join.
// TODO: fixing JSONTable and natural joins makes this unnecessary
func inOrderReplanJoin(
ctx *sql.Context,
a *Analyzer,
scope *Scope,
sch sql.Schema,
n sql.Node,
reorder, isUpdate bool,
) (sql.Node, transform.TreeIdentity, error) {
if _, ok := n.(sql.OpaqueNode); ok {
return n, transform.SameTree, nil
}
children := n.Children()
var newChildren []sql.Node
allSame := transform.SameTree
j, ok := n.(*plan.JoinNode)
if !ok {
for i := range children {
newChild, same, err := inOrderReplanJoin(ctx, a, scope, sch, children[i], reorder, isUpdate)
if err != nil {
return n, transform.SameTree, err
}
if !same {
if len(newChildren) == 0 {
newChildren = make([]sql.Node, len(children))
copy(newChildren, children)
}
newChildren[i] = newChild
allSame = transform.NewTree
}
}
if allSame {
return n, transform.SameTree, nil
}
ret, err := n.WithChildren(newChildren...)
if err != nil {
return nil, transform.SameTree, nil
}
return ret, transform.NewTree, err
}
// two different base cases, depending on whether we reorder or not
if reorder {
ret, err := replanJoin(ctx, j, a, scope)
if err != nil {
return nil, transform.SameTree, fmt.Errorf("failed to replan join: %w", err)
}
if isUpdate {
ret = plan.NewProject(expression.SchemaToGetFields(n.Schema()), ret)
}
return ret, transform.NewTree, nil
}
l, lSame, err := inOrderReplanJoin(ctx, a, scope, sch, j.Left(), reorder, isUpdate)
if err != nil {
return nil, transform.SameTree, err
}
rView := append(sch, j.Left().Schema()...)
r, rSame, err := inOrderReplanJoin(ctx, a, scope, rView, j.Right(), reorder, isUpdate)
if err != nil {
return nil, transform.SameTree, err
}
ret, err := j.WithChildren(l, r)
if err != nil {
return n, transform.SameTree, nil
}
if j.JoinCond() != nil {
selfView := append(sch, j.Schema()...)
f, fSame, err := FixFieldIndexes(scope, a, selfView, j.JoinCond())
if lSame && rSame && fSame {
return n, transform.SameTree, nil
}
ret, err = j.WithExpressions(f)
if err != nil {
return n, transform.SameTree, nil
}
}
return ret, transform.NewTree, nil
}
func replanJoin(ctx *sql.Context, n *plan.JoinNode, a *Analyzer, scope *Scope) (sql.Node, error) {
stats, err := a.Catalog.Statistics(ctx)
if err != nil {
return nil, err
}
m := NewMemo(ctx, stats, scope, a.Coster, a.Carder)
j := newJoinOrderBuilder(m)
j.reorderJoin(n)
err = convertSemiToInnerJoin(a, m)
if err != nil {
return nil, err
}
err = addRightSemiJoins(m)
if err != nil {
return nil, err
}
err = addLookupJoins(m)
if err != nil {
return nil, err
}
err = addHashJoins(m)
if err != nil {
return nil, err
}
err = addMergeJoins(m)
if err != nil {
return nil, err
}
if hint := extractJoinHint(n); !hint.IsEmpty() {
// this should probably happen earlier, but the root is not
// populated before reordering
m.WithJoinOrder(hint)
}
err = m.optimizeRoot()
if err != nil {
return nil, err
}
if a.Verbose && a.Debug {
a.Log(m.String())
}
return m.bestRootPlan()
}
// addLookupJoins prefixes memo join group expressions with indexed join
// alternatives to join plans added by joinOrderBuilder. We can assume that a
// join with a non-nil join filter is not degenerate, and we can apply indexed
// joins for any join plan where the right child is i) an indexable relation,
// ii) with an index that matches a prefix of the indexable relation's free
// attributes in the join filter. Costing is responsible for choosing the most
// appropriate execution plan among options added to an expression group.
func addLookupJoins(m *Memo) error {
var aliases = make(TableAliases)
seen := make(map[GroupId]struct{})
return dfsExprGroup(m.root, m, seen, func(e relExpr) error {
var right *exprGroup
var join *joinBase
switch e := e.(type) {
case *innerJoin:
right = e.right
join = e.joinBase
case *leftJoin:
right = e.right
join = e.joinBase
//TODO fullouterjoin
case *semiJoin:
right = e.right
join = e.joinBase
case *antiJoin:
right = e.right
join = e.joinBase
default:
return nil
}
if len(join.filter) == 0 {
return nil
}
attrSource, indexes, err := lookupCandidates(m.ctx, right.first, aliases)
if err != nil {
return err
}
if or, ok := join.filter[0].(*expression.Or); ok && len(join.filter) == 1 {
// Special case disjoint filter. The execution plan will perform an index
// lookup for each predicate leaf in the OR tree.
// TODO: memoize equality expressions, index lookup, concat so that we
// can consider multiple index options. Otherwise the search space blows
// up.
conds := splitDisjunction(or)
concat := splitIndexableOr(conds, indexes, attrSource, aliases)
if len(concat) != len(conds) {
return nil
}
rel := &concatJoin{
joinBase: join.copy(),
concat: concat,
}
for _, l := range concat {
l.parent = rel.joinBase
}
e.group().prepend(rel)
return nil
}
conds := collectJoinConds(attrSource, join.filter...)
for _, idx := range indexes {
keyExprs, nullmask := indexMatchesKeyExprs(idx, conds, aliases)
if len(keyExprs) == 0 {
continue
}
rel := &lookupJoin{
joinBase: join.copy(),
lookup: &lookup{
source: attrSource,
index: idx,
keyExprs: keyExprs,
nullmask: nullmask,
},
}
rel.lookup.parent = rel.joinBase
e.group().prepend(rel)
}
return nil
})
}
// convertSemiToInnerJoin adds inner join alternatives for semi joins.
// The inner join plans can be explored (optimized) further.
// Example: semiJoin(xy ab) => project(ab) -> innerJoin(xy, distinct(ab))
// Ref sction 2.1.1 of:
// https://www.researchgate.net/publication/221311318_Cost-Based_Query_Transformation_in_Oracle
// TODO: need more elegant way to extend the number of groups, interner
func convertSemiToInnerJoin(a *Analyzer, m *Memo) error {
seen := make(map[GroupId]struct{})
return dfsExprGroup(m.root, m, seen, func(e relExpr) error {
semi, ok := e.(*semiJoin)
if !ok {
return nil
}
rightOutTables := m.tableProps.getTableNames(semi.right.relProps.OutputTables())
projectExpressions := []sql.Expression{}
onlyEquality := true
for _, f := range semi.filter {
transform.InspectExpr(f, func(e sql.Expression) bool {
switch e := e.(type) {
case *expression.GetField:
// getField expressions tell us which columns are used, so we can create the correct project
tableName := strings.ToLower(e.Table())
isRightOutTable := stringContains(rightOutTables, tableName)
if isRightOutTable {
projectExpressions = append(projectExpressions, e)
}
case *expression.Literal, *expression.BindVar,
*expression.And, *expression.Or, *expression.Equals, *expression.Arithmetic:
// these expressions are equality expressions
default:
onlyEquality = false
}
return !onlyEquality
})
if !onlyEquality {
return nil
}
}
// project is a new group
newRight := &project{
relBase: &relBase{},
child: semi.right,
projections: projectExpressions,
}
rightGrp := m.memoize(newRight)
// distinct is a new group
rightDistinct := &distinct{
relBase: &relBase{},
child: rightGrp,
}
rightDistinctGrp := m.memoize(rightDistinct)
// join and its commute are a new group
newJoin := &innerJoin{
joinBase: &joinBase{
relBase: &relBase{},
left: semi.left,
right: rightDistinctGrp,
op: plan.JoinTypeInner,
filter: semi.filter,
},
}
joinGrp := m.memoize(newJoin)
newJoinCommuted := &innerJoin{
joinBase: &joinBase{
relBase: &relBase{g: joinGrp},
left: rightDistinctGrp,
right: semi.left,
op: plan.JoinTypeInner,
filter: semi.filter,
},
}
joinGrp.prepend(newJoinCommuted)
// project belongs to the original group
rel := &project{
relBase: &relBase{g: e.group()},
child: joinGrp,
projections: expression.SchemaToGetFields(semi.left.relProps.outputColsForRel(semi.left.first)),
}
e.group().prepend(rel)
return nil
})
}
// addRightSemiJoins allows for a reversed semiJoin operator when
// the join attributes of the left side are provably unique.
func addRightSemiJoins(m *Memo) error {
var aliases = make(TableAliases)
seen := make(map[GroupId]struct{})
return dfsExprGroup(m.root, m, seen, func(e relExpr) error {
semi, ok := e.(*semiJoin)
if !ok {
return nil
}
if len(semi.filter) == 0 {
return nil
}
attrSource, indexes, err := lookupCandidates(m.ctx, semi.left.first, aliases)
if err != nil {
return err
}
// check that the right side is unique on the join keys
conds := collectJoinConds(attrSource, semi.filter...)
for _, idx := range indexes {
if !idx.IsUnique() {
continue
}
keyExprs, nullmask := indexMatchesKeyExprs(idx, conds, aliases)
if len(keyExprs) == 0 {
continue
}
if len(keyExprs) != len(idx.Expressions()) {
continue
}
rel := &lookupJoin{
joinBase: semi.joinPrivate().copy(),
lookup: &lookup{
source: attrSource,
index: idx,
keyExprs: keyExprs,
nullmask: nullmask,
},
}
rel.op = plan.JoinTypeRightSemi
rel.left, rel.right = rel.right, rel.left
rel.lookup.parent = rel.joinBase
e.group().prepend(rel)
}
return nil
})
}
// lookupCandidates returns a normalized table name and a list of available
// candidate indexes as replacements for the given relExpr, or empty values
// if there are no suitable indexes.
func lookupCandidates(ctx *sql.Context, rel relExpr, aliases TableAliases) (string, []sql.Index, error) {
switch n := rel.(type) {
case *tableAlias:
return tableAliasLookupCand(ctx, n.table, aliases)
case *tableScan:
return tableScanLookupCand(ctx, n.table)
case *selectSingleRel:
switch t := n.table.Rel.(type) {
case *plan.TableAlias:
return tableAliasLookupCand(ctx, t, aliases)
case *plan.ResolvedTable:
return tableScanLookupCand(ctx, t)
default:
}
case *distinct:
if s, ok := n.child.first.(sourceRel); ok {
switch n := s.(type) {
case *tableAlias:
return tableAliasLookupCand(ctx, n.table, aliases)
case *tableScan:
return tableScanLookupCand(ctx, n.table)
default:
}
}
default:
}
return "", nil, nil
}
func tableScanLookupCand(ctx *sql.Context, n *plan.ResolvedTable) (string, []sql.Index, error) {
attributeSource := strings.ToLower(n.Name())
table := n.Table
if w, ok := table.(sql.TableWrapper); ok {
table = w.Underlying()
}
indexableTable, ok := table.(sql.IndexAddressableTable)
if !ok {
return "", nil, nil
}
indexes, err := indexableTable.GetIndexes(ctx)
if err != nil {
return "", nil, err
}
return attributeSource, indexes, nil
}
func tableAliasLookupCand(ctx *sql.Context, n *plan.TableAlias, aliases TableAliases) (string, []sql.Index, error) {
attributeSource := strings.ToLower(n.Name())
rt, ok := n.Child.(*plan.ResolvedTable)
if !ok {
return "", nil, nil
}
table := rt.Table
if w, ok := table.(sql.TableWrapper); ok {
table = w.Underlying()
}
indexableTable, ok := table.(sql.IndexAddressableTable)
if !ok {
return "", nil, nil
}
aliases.add(n, indexableTable)
indexes, err := indexableTable.GetIndexes(ctx)
if err != nil {
return "", nil, nil
}
return attributeSource, indexes, nil
}
// dfsExprGroup runs a callback |cb| on all execution plans in the memo expression
// group. An expression group is defined by 1) a set of child expression
// groups that serve as logical inputs to this operator, and 2) a set of logically
// equivalent plans for executing this expression group's operator. We recursively
// walk to expression group leaves, and then traverse every execution plan in leaf
// groups before working upwards back to the root group.
func dfsExprGroup(grp *exprGroup, m *Memo, seen map[GroupId]struct{}, cb func(rel relExpr) error) error {
if _, ok := seen[grp.id]; ok {
return nil
} else {
seen[grp.id] = struct{}{}
}
n := grp.first
for n != nil {
for _, c := range n.children() {
err := dfsExprGroup(c, m, seen, cb)
if err != nil {
return err
}
}
err := cb(n)
if err != nil {
return err
}
n = n.next()
}
return nil
}
func collectJoinConds(attributeSource string, filters ...sql.Expression) []*joinColExpr {
var conds []*joinColExpr
var outer []sql.Expression
for i := range filters {
l, r := extractJoinColumnExpr(filters[i])
if l == nil || r == nil {
// unusable as lookup
outer = append(outer, filters[i])
continue
}
// TODO(max): expression algebra to isolate arithmetic
// ex: (b.i = c.i + 1) cannot use a c.i lookup without converting the
// expression to (b.i - 1 = c.i), so that (b.i - 1) is a proper lookup
// key
if _, ok := l.colExpr.(*expression.GetField); ok && strings.ToLower(l.col.Table()) == attributeSource {
conds = append(conds, l)
} else if _, ok := r.colExpr.(*expression.GetField); ok && strings.ToLower(r.col.Table()) == attributeSource {
conds = append(conds, r)
} else {
outer = append(outer, filters[i])
}
}
return conds
}
// indexMatchesKeyExprs returns keyExprs and nullmask for a parametrized
// lookup from the outer scope (row) into the given index for a join condition.
// For example, the filters: [(ab.a + 1 = xy.y), (ab.b <=> xy.x)] will cover
// the the index xy(x,y). The second filter is not null rejecting, so the nullmask
// will be [0,1]. The keyExprs will be [(ab.a + 1), (ab.b)], which project into
// the table lookup (xy.x, xy.y).
func indexMatchesKeyExprs(i sql.Index, joinColExprs []*joinColExpr, tableAliases TableAliases) ([]sql.Expression, []bool) {
idxExprs := i.Expressions()
count := len(idxExprs)
if count > len(joinColExprs) {
count = len(joinColExprs)
}
keyExprs := make([]sql.Expression, count)
nullmask := make([]bool, count)
IndexExpressions:
for i := 0; i < count; i++ {
for j, col := range joinColExprs {
// check same column name
if strings.ToLower(idxExprs[i]) == strings.ToLower(normalizeExpression(tableAliases, col.col).String()) {
// get field into left table
keyExprs[i] = joinColExprs[j].comparand
nullmask[i] = joinColExprs[j].matchnull
continue IndexExpressions
}
}
return nil, nil
}
// TODO: better way of validating that we can apply an index lookup
lb := plan.NewLookupBuilder(i, keyExprs, nullmask)
look, err := lb.GetLookup(lb.GetZeroKey())
if err != nil {
return nil, nil
}
if !i.CanSupport(look.Ranges...) {
return nil, nil
}
return keyExprs, nullmask
}
// splitIndexableOr attempts to build a list of index lookups for a disjoint
// filter expression. The prototypical pattern will be a tree of OR and equality
// expressions: [eq] OR [eq] OR [eq] ...
func splitIndexableOr(filters []sql.Expression, indexes []sql.Index, attributeSource string, aliases TableAliases) []*lookup {
var concat []*lookup
for _, f := range filters {
if eq, ok := f.(*expression.Equals); ok {
i := firstMatchingIndex(eq, indexes, attributeSource, aliases)
if i == nil {
return nil
}
concat = append(concat, i)
}
}
return concat
}
// firstMatchingIndex returns first index that |e| can use as a lookup.
// This simplifies index selection for concatJoin to avoid building
// memo objects for equality expressions and indexes.
func firstMatchingIndex(e *expression.Equals, indexes []sql.Index, attributeSource string, aliases TableAliases) *lookup {
for _, lIdx := range indexes {
lConds := collectJoinConds(attributeSource, e)
lKeyExprs, lNullmask := indexMatchesKeyExprs(lIdx, lConds, aliases)
if len(lKeyExprs) == 0 {
continue
}
return &lookup{
index: lIdx,
keyExprs: lKeyExprs,
nullmask: lNullmask,
}
}
return nil
}
func addHashJoins(m *Memo) error {
seen := make(map[GroupId]struct{})
return dfsExprGroup(m.root, m, seen, func(e relExpr) error {
switch e.(type) {
case *innerJoin, *leftJoin:
default:
return nil
}
join := e.(joinRel).joinPrivate()
if len(join.filter) == 0 {
return nil
}
var innerExpr, outerExpr []sql.Expression
for _, f := range join.filter {
switch f := f.(type) {
case *expression.Equals:
if exprMapsToSource(f.Left(), join.left, m.tableProps) &&
exprMapsToSource(f.Right(), join.right, m.tableProps) {
innerExpr = append(innerExpr, f.Left())
outerExpr = append(outerExpr, f.Right())
} else if exprMapsToSource(f.Right(), join.left, m.tableProps) &&
exprMapsToSource(f.Left(), join.right, m.tableProps) {
innerExpr = append(innerExpr, f.Right())
outerExpr = append(outerExpr, f.Left())
} else {
return nil
}
default:
return nil
}
}
rel := &hashJoin{
joinBase: join.copy(),
innerAttrs: innerExpr,
outerAttrs: outerExpr,
}
e.group().prepend(rel)
return nil
})
}
// exprMapsToSource returns true if all GetFields in the expression
// source outputs from |grp|
func exprMapsToSource(e sql.Expression, grp *exprGroup, tProps *tableProps) bool {
outerOnly := true
transform.InspectExpr(e, func(e sql.Expression) bool {
switch e := e.(type) {
case *expression.GetField:
if id, ok := tProps.getId(strings.ToLower(e.Table())); ok {
exprTable := sql.NewFastIntSet(int(tableIdForSource(id)))
outerOnly = outerOnly && exprTable.Intersects(grp.relProps.OutputTables())
}
default:
}
return !outerOnly
})
return outerOnly
}
// addMergeJoins will add merge join operators to join relations
// with native indexes providing sort enforcement on an equality
// filter.
// TODO: sort-merge joins
func addMergeJoins(m *Memo) error {
var aliases = make(TableAliases)
seen := make(map[GroupId]struct{})
return dfsExprGroup(m.root, m, seen, func(e relExpr) error {
var join *joinBase
switch e := e.(type) {
case *innerJoin:
join = e.joinBase
case *leftJoin:
join = e.joinBase
//TODO semijoin, antijoin, fullouterjoin
default:
return nil
}
if len(join.filter) == 0 {
return nil
}
lAttrSource, lIndexes, err := lookupCandidates(m.ctx, join.left.first, aliases)
if err != nil {
return err
} else if lAttrSource == "" {
return nil
}
rAttrSource, rIndexes, err := lookupCandidates(m.ctx, join.right.first, aliases)
if err != nil {
return err
} else if rAttrSource == "" {
return nil
}
for i, f := range join.filter {
var l, r sql.Expression
switch f := f.(type) {
case *expression.Equals:
l = f.Left()
r = f.Right()
default:
continue
}
ltc, ok := attrsRefSingleTableCol(l)
if !ok {
continue
}
rtc, ok := attrsRefSingleTableCol(r)
if !ok {
continue
}
switch {
case ltc.table == lAttrSource && rtc.table == rAttrSource:
case rtc.table == lAttrSource && ltc.table == rAttrSource:
l, r = r, l
ltc, rtc = rtc, ltc
default:
continue
}
// check that comparer is not non-decreasing
if !isWeaklyMonotonic(l) || !isWeaklyMonotonic(r) {
continue
}
if tab, ok := aliases[ltc.table]; ok {
ltc = tableCol{table: strings.ToLower(tab.Name()), col: ltc.col}
}
if tab, ok := aliases[rtc.table]; ok {
rtc = tableCol{table: strings.ToLower(tab.Name()), col: rtc.col}
}
lIdx := sortedIndexScanForTableCol(lIndexes, ltc, l)
if lIdx == nil {
continue
}
rIdx := sortedIndexScanForTableCol(rIndexes, rtc, r)
if rIdx == nil {
continue
}
newFilters := make([]sql.Expression, len(join.filter))
copy(newFilters, join.filter)
newFilters[i], _ = f.WithChildren(l, r)
// merge cond first
newFilters[0], newFilters[i] = newFilters[i], newFilters[0]
jb := join.copy()
if d, ok := jb.left.first.(*distinct); ok && lIdx.idx.IsUnique() {
jb.left = d.child
}
if d, ok := jb.right.first.(*distinct); ok && rIdx.idx.IsUnique() {
jb.right = d.child
}
jb.filter = newFilters
rel := &mergeJoin{
joinBase: jb,
innerScan: lIdx,
outerScan: rIdx,
}
rel.innerScan.parent = rel.joinBase
rel.outerScan.parent = rel.joinBase
e.group().prepend(rel)
}
return nil
})
}
// sortedIndexScanForTableCol returns the first indexScan found for a relation
// that provide a prefix for the joinFilters rel free attribute. I.e. the
// indexScan will return the same rows as the rel, but sorted by |col|.
func sortedIndexScanForTableCol(is []sql.Index, tc tableCol, e sql.Expression) *indexScan {
for _, idx := range is {
if strings.ToLower(idx.Expressions()[0]) != tc.String() {
continue
}
rang := sql.Range{sql.AllRangeColumnExpr(e.Type())}
if !idx.CanSupport(rang) {
return nil
}
return &indexScan{
source: tc.table,
idx: idx,
}
}
return nil
}
// isWeaklyMonotonic is a weak test of whether an expression
// will be strictly increasing as the value of column attribute
// inputs increases.
//
// The simplest example is `x`, which will increase
// as `x` increases, and decrease as `x` decreases.
//
// An example of a non-monotonic expression is `mod(x, 4)`,
// which is strictly non-increasing from x=3 -> x=4.
//
// A non-obvious non-monotonic function is `x+y`. The index `(x,y)`
// will be non-increasing on (y), and so `x+y` can decrease.
// TODO: stricter monotonic check
func isWeaklyMonotonic(e sql.Expression) bool {
switch e := e.(type) {
case *expression.Arithmetic:
if e.Op == sqlparser.MinusStr {
// TODO minus can be OK if it's not on the GetField
return false
}
case *expression.Equals, *expression.NullSafeEquals, *expression.Literal, *expression.GetField,
*expression.Tuple, *expression.IsNull, *expression.BindVar:
default:
return false
}
for _, c := range e.Children() {
if !isWeaklyMonotonic(c) {
return false
}
}
return true
}
// attrsRefSingleTableCol returns false if there are
// getFields sourced from zero or more than one table.
func attrsRefSingleTableCol(e sql.Expression) (tableCol, bool) {
var tc tableCol
var invalid bool
transform.InspectExpr(e, func(e sql.Expression) bool {
switch e := e.(type) {
case *expression.GetField:
newTc := tableCol{col: strings.ToLower(e.Name()), table: strings.ToLower(e.Table())}
if tc.table == "" && !invalid {
tc = newTc
} else if tc != newTc {
invalid = true
}
default:
}
return invalid
})
return tc, !invalid && tc.table != ""
}
func extractJoinHint(n *plan.JoinNode) JoinOrderHint {
if n.Comment() != "" {
return parseJoinHint(n.Comment())
}
return EmptyJoinOrder
}
var hintRegex = regexp.MustCompile("(\\s*[a-z_]+\\([^\\(]+\\)\\s*)+")
// TODO: this is pretty nasty. Should be done in the parser instead.
func parseJoinHint(comment string) JoinOrderHint {
comment = strings.TrimPrefix(comment, "/*+")
comment = strings.TrimSuffix(comment, "*/")
comment = strings.ToLower(strings.TrimSpace(comment))
hints := hintRegex.FindAll([]byte(comment), -1)
for _, hint := range hints {
hintStr := strings.TrimSpace(string(hint))
if strings.HasPrefix(string(hintStr), "join_order(") {
var tables []string
var table strings.Builder
for _, b := range hintStr[len("join_order("):] {
switch b {
case ',', ')':
tables = append(tables, strings.TrimSpace(table.String()))
table = strings.Builder{}
default:
table.WriteRune(b)
}
}
return JoinOrderHint{
tables: tables,
}
}
}
return EmptyJoinOrder
}
type QueryHint interface {
fmt.Stringer
HintType() string
}
type JoinOrderHint struct {
tables []string
}
var EmptyJoinOrder = JoinOrderHint{}
func (j JoinOrderHint) String() string {
return "JOIN_ORDER(" + strings.Join(j.tables, ",") + ")"
}
func (j JoinOrderHint) HintType() string {
return "JOIN_ORDER"
}
func (j JoinOrderHint) IsEmpty() bool {
return len(j.tables) == 0
}
// joinOrderDeps encodes a groups relational dependencies in a bitset.
// This is equivalent to an expression group's base table inputs but
// reordered by the join hint table order.
type joinOrderDeps struct {
groups map[GroupId]vertexSet
cache map[uint64]bool
order map[GroupId]uint64
}
func newJoinOrderDeps(order map[GroupId]uint64) *joinOrderDeps {
return &joinOrderDeps{
groups: make(map[GroupId]vertexSet),
cache: make(map[uint64]bool),
order: order,
}
}
func (o joinOrderDeps) build(grp *exprGroup) {
s := vertexSet(0)
// convert global table order to hint order
inputs := grp.relProps.InputTables()
for idx, ok := inputs.Next(0); ok; idx, ok = inputs.Next(idx + 1) {
if i, ok := o.order[GroupId(idx+1)]; ok {
// If group |idx+1| is a dependency of this table, record the
// ordinal position of that group given by the hint order.
s = s.add(i)
}
}
o.groups[grp.id] = s
for _, g := range grp.children() {
if _, ok := o.groups[g.id]; !ok {
// avoid duplicate work
o.build(g)
}
}
}
func (o joinOrderDeps) isValid() bool {
for _, v := range o.groups {
if v == vertexSet(0) {
// invalid hint table name, fallback
return false
}
}
return true
}
func (o joinOrderDeps) obeysOrder(n relExpr) bool {
key := relKey(n)
if v, ok := o.cache[key]; ok {