forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexes.go
1148 lines (1025 loc) · 32.3 KB
/
indexes.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"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/expression/function/spatial"
"github.com/gabereiser/go-mysql-server/sql/plan"
"github.com/gabereiser/go-mysql-server/sql/types"
)
// indexLookup contains an sql.IndexLookup and all sql.Index that are involved
// in it.
type indexLookup struct {
fields []sql.Expression
fields2 []sql.Expression2
lookup sql.IndexLookup
indexes []sql.Index
expr sql.Expression
expr2 sql.Expression2
}
type indexLookupsByTable map[string]*indexLookup
// getIndexes returns indexes applicable to all tables in the node given for the expression given, keyed by the name of
// the table (aliased as appropriate). If more than one index per table is usable for the expression given, chooses a
// best match (typically the longest prefix by column count).
func getIndexes(
ctx *sql.Context,
ia *indexAnalyzer,
e sql.Expression,
tableAliases TableAliases,
) (indexLookupsByTable, error) {
var result = make(indexLookupsByTable)
switch e := e.(type) {
case *expression.Or:
// If more than one table is involved in a disjunction, we can't use indexed lookups. This is because we will
// inappropriately restrict the iterated values of the indexed table to matching index values, when during a cross
// join we must consider every row from each table.
if len(findTables(e)) > 1 {
return nil, nil
}
leftIndexes, err := getIndexes(ctx, ia, e.Left, tableAliases)
if err != nil {
return nil, err
}
rightIndexes, err := getIndexes(ctx, ia, e.Right, tableAliases)
if err != nil {
return nil, err
}
for table, leftIdx := range leftIndexes {
result[table] = leftIdx
}
// Merge any indexes for the same table on the left and right sides.
for table, leftIdx := range leftIndexes {
foundRightIdx := false
if rightIdx, ok := rightIndexes[table]; ok {
if canMergeIndexes(leftIdx.lookup, rightIdx.lookup) {
var allRanges sql.RangeCollection
allRanges = append(sql.RangeCollection{}, leftIdx.lookup.Ranges...)
allRanges = append(allRanges, rightIdx.lookup.Ranges...)
newRanges, err := sql.RemoveOverlappingRanges(allRanges...)
if err != nil {
return nil, nil
}
newLookup := sql.IndexLookup{Index: leftIdx.lookup.Index, Ranges: newRanges}
if err != nil {
return nil, err
}
leftIdx.lookup = newLookup
if leftIdx.expr == nil {
leftIdx.expr = rightIdx.expr
} else if rightIdx.expr != nil {
leftIdx.expr = expression.NewOr(leftIdx.expr, rightIdx.expr)
}
leftIdx.indexes = append(leftIdx.indexes, rightIdx.indexes...)
result[table] = leftIdx
foundRightIdx = true
delete(rightIndexes, table)
} else {
// Since we can return one index per table, if we can't merge the right-hand index from this table with the
// left-hand index, return no indexes. Returning a single one will lead to incorrect results from e.g.
// pushdown operations when only one side of the OR expression is used to index the table.
return nil, nil
}
}
// By the same token, if we cannot match an index on the right side for each index on the left, we can't use the
// left index either. Doing so would produce incorrect results, since both expressions must be considered for a
// row's inclusion in the result set.
if !foundRightIdx {
return nil, nil
}
}
// If there are any left-over indexes, we can't consider them because they don't have matching left-hand indexes.
if len(rightIndexes) > 0 {
return nil, nil
}
case *expression.InTuple, *expression.HashInTuple:
cmp := e.(expression.Comparer)
if !isEvaluable(cmp.Left()) && isEvaluable(cmp.Right()) {
gf := expression.ExtractGetField(cmp.Left())
if gf == nil {
return nil, nil
}
colExprs := normalizeExpressions(tableAliases, cmp.Left())
idx := ia.MatchingIndex(ctx, ctx.GetCurrentDatabase(), gf.Table(), colExprs...)
if idx != nil {
value, err := cmp.Right().Eval(ctx, nil)
if err != nil {
return nil, err
}
var lookup sql.IndexLookup
values, ok := value.([]interface{})
if ok {
lookup, err = sql.NewIndexBuilder(idx).Equals(ctx, colExprs[0].String(), values...).Build(ctx)
if err != nil {
return nil, err
}
} else {
// For single length tuples, we don't return []interface{}, just the first element
lookup, err = sql.NewIndexBuilder(idx).Equals(ctx, colExprs[0].String(), value).Build(ctx)
if err != nil {
return nil, err
}
}
if lookup.IsEmpty() {
return nil, nil
}
getField := expression.ExtractGetField(cmp.Left())
if getField == nil {
return result, nil
}
result[getField.Table()] = &indexLookup{
fields: []sql.Expression{e},
indexes: []sql.Index{idx},
lookup: lookup,
expr: e,
}
}
}
case *expression.Equals,
*expression.NullSafeEquals,
*expression.LessThan,
*expression.GreaterThan,
*expression.LessThanOrEqual,
*expression.GreaterThanOrEqual:
lookup, err := getComparisonIndexLookup(ctx, ia, e.(expression.Comparer), tableAliases)
if err != nil || lookup == nil {
return result, err
}
getField := expression.ExtractGetField(e)
if getField == nil {
return result, nil
}
result[getField.Table()] = lookup
case *expression.IsNull:
return getIndexes(ctx, ia, expression.NewNullSafeEquals(e.Child, expression.NewLiteral(nil, types.Null)), tableAliases)
case *expression.Not:
r, err := getNegatedIndexes(ctx, ia, e, tableAliases)
if err != nil {
return nil, err
}
for table, indexLookup := range r {
result[table] = indexLookup
}
case *expression.Between:
if !isEvaluable(e.Val) && isEvaluable(e.Upper) && isEvaluable(e.Lower) {
gf := expression.ExtractGetField(e)
if gf == nil {
return nil, nil
}
normalizedExpressions := normalizeExpressions(tableAliases, e.Val)
idx := ia.MatchingIndex(ctx, ctx.GetCurrentDatabase(), gf.Table(), normalizedExpressions...)
if idx != nil {
upper, err := e.Upper.Eval(ctx, nil)
if err != nil {
return nil, err
}
lower, err := e.Lower.Eval(ctx, nil)
if err != nil {
return nil, err
}
lookup, err := sql.NewIndexBuilder(idx).GreaterOrEqual(ctx, normalizedExpressions[0].String(), lower).
LessOrEqual(ctx, normalizedExpressions[0].String(), upper).Build(ctx)
if err != nil || lookup.IsEmpty() {
return nil, err
}
getField := expression.ExtractGetField(e)
if getField == nil {
return result, nil
}
result[getField.Table()] = &indexLookup{
fields: []sql.Expression{getField},
indexes: []sql.Index{idx},
lookup: lookup,
expr: e,
}
}
}
case *expression.And:
exprs := splitConjunction(e)
// First treat the AND expression as a match on >= 2 columns (for keys that span multiple columns)
multiColumnIndexes, unusedExprs, err := getMultiColumnIndexes(ctx, exprs, ia, tableAliases)
if err != nil {
return nil, err
}
result := multiColumnIndexes
// Next try to match the remaining expressions individually
for _, e := range unusedExprs {
// TODO: eventually support merging spatial lookups
if _, ok := e.(*spatial.Intersects); ok {
continue
}
indexes, err := getIndexes(ctx, ia, e, tableAliases)
if err != nil {
return nil, err
}
// Merge this index if possible. If at any time we cannot merge the result, then we simply return nil. Returning
// an indexed lookup for only part of an expression leads to incorrect results, e.g. (col = 1 AND col = 2) can
// either return a merged index lookup for both values, or for neither. Returning either one leads to incorrect
// results.
if !canMergeIndexLookups(result, indexes) {
continue
}
result, err = indexesIntersection(ctx, result, indexes)
if err != nil {
return nil, err
}
}
return result, nil
}
// TODO (james): add all other spatial index supported functions here
// TODO: make generalizable to all functions?
switch e := e.(type) {
case *spatial.Intersects, *spatial.Within:
// don't pushdown functions with bindvars
if exprHasBindVar(e) {
return nil, nil
}
// Will be non-nil only when there is exactly one *expression.GetField
getField := expression.ExtractGetField(e)
if getField == nil {
return nil, nil
}
// Assume these are all BinaryExpression with exactly two children
children := e.Children()
if len(children) != 2 {
return nil, fmt.Errorf("st function is not a binary expression")
}
// Put GetField on the left
left, right := children[0], children[1]
if _, ok := right.(*expression.GetField); ok {
left, right = right, left
}
value, err := right.Eval(ctx, nil)
if err != nil {
return nil, err
}
if value == nil {
return nil, nil
}
g, ok := value.(types.GeometryValue)
if !ok {
return nil, sql.ErrInvalidGISData.New()
}
minX, minY, maxX, maxY := g.BBox()
lower := types.Point{X: minX, Y: minY}
upper := types.Point{X: maxX, Y: maxY}
normalizedExpressions := normalizeExpressions(tableAliases, left)
idx := ia.MatchingIndex(ctx, ctx.GetCurrentDatabase(), getField.Table(), normalizedExpressions...)
if idx == nil || !idx.IsSpatial() {
return nil, nil
}
bld := sql.NewSpatialIndexBuilder(idx)
bld.AddRange(lower, upper)
lookup, err := bld.Build()
if err != nil || lookup.IsEmpty() {
return nil, err
}
result[getField.Table()] = &indexLookup{
fields: []sql.Expression{getField},
indexes: []sql.Index{idx},
lookup: lookup,
expr: e,
}
}
return result, nil
}
// getComparisonIndexLookup returns the index and index lookup for the given
// comparison if any index can be found.
// It works for the following comparisons: eq, lt, gt, gte and lte.
// TODO: add support for BETWEEN
func getComparisonIndexLookup(
ctx *sql.Context,
ia *indexAnalyzer,
e expression.Comparer,
tableAliases TableAliases,
) (*indexLookup, error) {
left, right := e.Left(), e.Right()
// if the form is SOMETHING OP {INDEXABLE EXPR}, swap it, so it's {INDEXABLE EXPR} OP SOMETHING
if !isEvaluable(right) {
left, right, e = swapTermsOfExpression(e)
}
if isEvaluable(left) || !isEvaluable(right) {
return nil, nil
}
gf := expression.ExtractGetField(left)
if gf == nil {
return nil, nil
}
normalizedExpressions := normalizeExpressions(tableAliases, left)
idx := ia.MatchingIndex(ctx, ctx.GetCurrentDatabase(), gf.Table(), normalizedExpressions...)
if idx == nil {
return nil, nil
}
var value interface{}
var err error
value, err = right.Eval(ctx, nil)
if err != nil {
return nil, err
}
var lookup sql.IndexLookup
switch e.(type) {
case *expression.NullSafeEquals:
if value == nil {
lookup, err = sql.NewIndexBuilder(idx).IsNull(ctx, normalizedExpressions[0].String()).Build(ctx)
} else {
lookup, err = sql.NewIndexBuilder(idx).Equals(ctx, normalizedExpressions[0].String(), value).Build(ctx)
}
case *expression.Equals:
lookup, err = sql.NewIndexBuilder(idx).Equals(ctx, normalizedExpressions[0].String(), value).Build(ctx)
case *expression.GreaterThan:
lookup, err = sql.NewIndexBuilder(idx).GreaterThan(ctx, normalizedExpressions[0].String(), value).Build(ctx)
case *expression.GreaterThanOrEqual:
lookup, err = sql.NewIndexBuilder(idx).GreaterOrEqual(ctx, normalizedExpressions[0].String(), value).Build(ctx)
case *expression.LessThan:
lookup, err = sql.NewIndexBuilder(idx).LessThan(ctx, normalizedExpressions[0].String(), value).Build(ctx)
case *expression.LessThanOrEqual:
lookup, err = sql.NewIndexBuilder(idx).LessOrEqual(ctx, normalizedExpressions[0].String(), value).Build(ctx)
default:
return nil, nil
}
if err != nil || lookup.IsEmpty() {
return nil, err
}
var fields2 []sql.Expression2
if field2, ok := left.(sql.Expression2); ok {
fields2 = []sql.Expression2{field2}
}
expr2, _ := e.(sql.Expression2)
return &indexLookup{
fields: []sql.Expression{left},
fields2: fields2,
lookup: lookup,
indexes: []sql.Index{idx},
expr: e,
expr2: expr2,
}, nil
}
// Returns an equivalent expression to the one given with the left and right terms reversed. The new left and right side
// of the expression are returned as well.
func swapTermsOfExpression(e expression.Comparer) (left sql.Expression, right sql.Expression, newExpr expression.Comparer) {
left, right = e.Left(), e.Right()
left, right = right, left
switch e.(type) {
case *expression.GreaterThanOrEqual:
e = expression.NewLessThanOrEqual(left, right)
case *expression.GreaterThan:
e = expression.NewLessThan(left, right)
case *expression.LessThan:
e = expression.NewGreaterThan(left, right)
case *expression.LessThanOrEqual:
e = expression.NewGreaterThanOrEqual(left, right)
}
return left, right, e
}
func getNegatedIndexes(
ctx *sql.Context,
ia *indexAnalyzer,
not *expression.Not,
tableAliases TableAliases,
) (indexLookupsByTable, error) {
switch e := not.Child.(type) {
case *expression.Not:
return getIndexes(ctx, ia, e.Child, tableAliases)
case *expression.Equals, *expression.NullSafeEquals:
cmp := e.(expression.Comparer)
left, right := cmp.Left(), cmp.Right()
// if the form is SOMETHING OP {INDEXABLE EXPR}, swap it, so it's {INDEXABLE EXPR} OP SOMETHING
if !isEvaluable(right) {
left, right, _ = swapTermsOfExpression(cmp)
}
if isEvaluable(left) || !isEvaluable(right) {
return nil, nil
}
gf := expression.ExtractGetField(left)
if gf == nil {
return nil, nil
}
normalizedExpressions := normalizeExpressions(tableAliases, left)
idx := ia.MatchingIndex(ctx, ctx.GetCurrentDatabase(), gf.Table(), normalizedExpressions...)
if idx == nil {
return nil, nil
}
value, err := right.Eval(ctx, nil)
if err != nil {
return nil, err
}
_, nullsafe := not.Child.(*expression.NullSafeEquals)
var lookup sql.IndexLookup
if nullsafe && value == nil {
lookup, err = sql.NewIndexBuilder(idx).IsNotNull(ctx, normalizedExpressions[0].String()).Build(ctx)
} else {
lookup, err = sql.NewIndexBuilder(idx).NotEquals(ctx, normalizedExpressions[0].String(), value).Build(ctx)
}
if err != nil || lookup.IsEmpty() {
return nil, err
}
getField := expression.ExtractGetField(left)
if getField == nil {
return nil, nil
}
result := indexLookupsByTable{
getField.Table(): {
fields: []sql.Expression{left},
indexes: []sql.Index{idx},
lookup: lookup,
},
}
return result, nil
case *expression.InTuple, *expression.HashInTuple:
cmp := e.(expression.Comparer)
// Take the index of a SOMETHING IN SOMETHING expression only if:
// the right branch is evaluable and the indexlookup supports set
// operations.
if !isEvaluable(cmp.Left()) && isEvaluable(cmp.Right()) {
gf := expression.ExtractGetField(cmp.Left())
if gf == nil {
return nil, nil
}
normalizedExpressions := normalizeExpressions(tableAliases, cmp.Left())
idx := ia.MatchingIndex(ctx, ctx.GetCurrentDatabase(), gf.Table(), normalizedExpressions...)
if idx != nil {
value, err := cmp.Right().Eval(ctx, nil)
if err != nil {
return nil, err
}
var values []interface{}
switch v := value.(type) {
case []interface{}:
values = v
default:
values = []interface{}{v}
}
idxBuilder := sql.NewIndexBuilder(idx)
for _, val := range values {
idxBuilder = idxBuilder.NotEquals(ctx, normalizedExpressions[0].String(), val)
}
lookup, err := idxBuilder.Build(ctx)
if err != nil {
return nil, err
}
getField := expression.ExtractGetField(cmp.Left())
if getField == nil {
return nil, nil
}
return indexLookupsByTable{
getField.Table(): {
fields: []sql.Expression{cmp.Left()},
indexes: []sql.Index{idx},
lookup: lookup,
},
}, nil
}
}
return nil, nil
case *expression.IsNull:
return getNegatedIndexes(ctx, ia,
expression.NewNot(
expression.NewNullSafeEquals(
e.Child,
expression.NewLiteral(nil, types.Null),
),
),
tableAliases)
case *expression.GreaterThan:
lte := expression.NewLessThanOrEqual(e.Left(), e.Right())
return getIndexes(ctx, ia, lte, tableAliases)
case *expression.GreaterThanOrEqual:
lt := expression.NewLessThan(e.Left(), e.Right())
return getIndexes(ctx, ia, lt, tableAliases)
case *expression.LessThan:
gte := expression.NewGreaterThanOrEqual(e.Left(), e.Right())
return getIndexes(ctx, ia, gte, tableAliases)
case *expression.LessThanOrEqual:
gt := expression.NewGreaterThan(e.Left(), e.Right())
return getIndexes(ctx, ia, gt, tableAliases)
case *expression.Between:
or := expression.NewOr(
expression.NewLessThan(e.Val, e.Lower),
expression.NewGreaterThan(e.Val, e.Upper),
)
return getIndexes(ctx, ia, or, tableAliases)
case *expression.Or:
and := expression.NewAnd(
expression.NewNot(e.Left),
expression.NewNot(e.Right),
)
return getIndexes(ctx, ia, and, tableAliases)
case *expression.And:
or := expression.NewOr(
expression.NewNot(e.Left),
expression.NewNot(e.Right),
)
return getIndexes(ctx, ia, or, tableAliases)
default:
return nil, nil
}
}
func indexesIntersection(ctx *sql.Context, left, right indexLookupsByTable) (indexLookupsByTable, error) {
var result = make(indexLookupsByTable)
for table, idx := range left {
if idx2, ok := right[table]; ok && canMergeIndexes(idx.lookup, idx2.lookup) {
newRangeCollections, err := idx.lookup.Ranges.Intersect(idx2.lookup.Ranges)
if err != nil || newRangeCollections == nil {
continue
}
idx.lookup = sql.IndexLookup{Index: idx.lookup.Index, Ranges: newRangeCollections}
if err != nil {
return nil, err
}
idx.indexes = append(idx.indexes, idx2.indexes...)
if idx.expr == nil {
idx.expr = idx2.expr
} else if idx2.expr != nil {
idx.expr = expression.NewAnd(idx.expr, idx2.expr)
}
}
result[table] = idx
}
// Put in the result map the indexes for tables we don't have indexes yet.
// The others were already handled by the previous loop.
for table, lookup := range right {
if _, ok := result[table]; !ok {
result[table] = lookup
}
}
return result, nil
}
func getMultiColumnIndexes(
ctx *sql.Context,
exprs []sql.Expression,
ia *indexAnalyzer,
tableAliases TableAliases,
) (indexLookupsByTable, []sql.Expression, error) {
result := make(indexLookupsByTable)
var unusedExprs []sql.Expression
usedExprs := make(map[sql.Expression]struct{})
columnExprs := columnExprsByTable(exprs)
for table, exps := range columnExprs {
colExprs := make([]sql.Expression, len(exps))
nilColExpr := false
for i, e := range exps {
if e.colExpr == nil {
nilColExpr = true
}
colExprs[i] = e.colExpr
}
// Further analysis requires that we have a col expr for every expression, and it's possible we don't
if nilColExpr {
continue
}
exprList := ia.ExpressionsWithIndexes(ctx.GetCurrentDatabase(), colExprs...)
if len(exprList) == 0 {
continue
}
lookup, err := getMultiColumnIndexForExpressions(ctx, ia, table, exprList[0], exps, tableAliases)
if err != nil {
return nil, nil, err
}
if lookup == nil {
continue
}
exprMap := make(map[string]struct{})
for _, exprListItem := range exprList[0] {
exprMap[exprListItem.String()] = struct{}{}
}
if _, ok := result[table]; ok {
newResult := indexLookupsByTable{
table: lookup,
}
if !canMergeIndexLookups(result, newResult) {
return nil, nil, nil
}
result, err = indexesIntersection(ctx, result, newResult)
if err != nil {
return nil, nil, err
}
} else {
result[table] = lookup
}
for _, e := range exps {
if _, ok := exprMap[e.col.String()]; ok {
usedExprs[e.comparison] = struct{}{}
}
}
}
for _, expr := range exprs {
if _, ok := usedExprs[expr]; !ok {
unusedExprs = append(unusedExprs, expr)
}
}
return result, unusedExprs, nil
}
func getMultiColumnIndexForExpressions(
ctx *sql.Context,
ia *indexAnalyzer,
table string,
selected []sql.Expression,
exprs []joinColExpr,
tableAliases TableAliases,
) (*indexLookup, error) {
normalizedExpressions := normalizeExpressions(tableAliases, selected...)
index := ia.MatchingIndex(ctx, ctx.GetCurrentDatabase(), table, normalizedExpressions...)
if index == nil {
return nil, nil
}
indexBuilder := sql.NewIndexBuilder(index)
var expressions []sql.Expression
var allMatches joinColExprs
for _, selectedExpr := range normalizedExpressions {
matchedExprs := findColumns(exprs, selectedExpr.String())
allMatches = append(allMatches, matchedExprs...)
for _, expr := range matchedExprs {
switch expr.comparison.(type) {
case *expression.Equals,
*expression.NullSafeEquals,
*expression.LessThan,
*expression.GreaterThan,
*expression.LessThanOrEqual,
*expression.GreaterThanOrEqual:
if !isEvaluable(expr.comparand) {
return nil, nil
}
val, err := expr.comparand.Eval(ctx, nil)
if err != nil {
return nil, err
}
expressions = append(expressions, expr.colExpr)
switch expr.comparison.(type) {
case *expression.NullSafeEquals:
if val == nil {
indexBuilder = indexBuilder.IsNull(ctx, expr.col.String())
} else {
indexBuilder = indexBuilder.Equals(ctx, expr.col.String(), val)
}
case *expression.Equals:
indexBuilder = indexBuilder.Equals(ctx, expr.col.String(), val)
case *expression.GreaterThan:
indexBuilder = indexBuilder.GreaterThan(ctx, expr.col.String(), val)
case *expression.GreaterThanOrEqual:
indexBuilder = indexBuilder.GreaterOrEqual(ctx, expr.col.String(), val)
case *expression.LessThan:
indexBuilder = indexBuilder.LessThan(ctx, expr.col.String(), val)
case *expression.LessThanOrEqual:
indexBuilder = indexBuilder.LessOrEqual(ctx, expr.col.String(), val)
default:
return nil, nil
}
case *expression.Between:
between, ok := expr.comparison.(*expression.Between)
if !ok {
return nil, nil
}
lower, err := between.Lower.Eval(ctx, nil)
if err != nil {
return nil, err
}
upper, err := between.Upper.Eval(ctx, nil)
if err != nil {
return nil, err
}
expressions = append(expressions, expression.ExtractGetField(between))
indexBuilder = indexBuilder.GreaterOrEqual(ctx, expr.col.String(), lower)
indexBuilder = indexBuilder.LessOrEqual(ctx, expr.col.String(), upper)
case *expression.InTuple:
cmp := expr.comparison.(expression.Comparer)
if !isEvaluable(cmp.Left()) && isEvaluable(cmp.Right()) {
value, err := cmp.Right().Eval(ctx, nil)
if err != nil {
return nil, err
}
values, ok := value.([]interface{})
if ok {
indexBuilder = indexBuilder.Equals(ctx, expr.col.String(), values...)
} else {
// For single length tuples, we don't return []interface{}, just the first element
indexBuilder = indexBuilder.Equals(ctx, expr.col.String(), value)
}
} else {
return nil, nil
}
case *expression.Not:
switch expr.comparison.(*expression.Not).Child.(type) {
//TODO: We should transform NOT nodes for comparisons at some other analyzer step, e.g. (NOT <) becomes (>=)
case *expression.NullSafeEquals, *expression.Equals:
val, err := expr.comparand.Eval(ctx, nil)
if err != nil {
return nil, err
}
_, nullsafe := expr.comparison.(*expression.Not).Child.(*expression.NullSafeEquals)
expressions = append(expressions, selectedExpr)
if val == nil && nullsafe {
indexBuilder = indexBuilder.IsNotNull(ctx, expr.col.String())
} else {
indexBuilder = indexBuilder.NotEquals(ctx, expr.col.String(), val)
}
default:
return nil, nil
}
default:
return nil, nil
}
}
}
lookup, err := indexBuilder.Build(ctx)
if err != nil {
return nil, err
}
if lookup.IsEmpty() {
return nil, nil
}
var lookupExpr sql.Expression
for _, m := range allMatches {
if lookupExpr == nil {
lookupExpr = m.comparison
} else {
lookupExpr = expression.NewAnd(lookupExpr, m.comparison)
}
}
return &indexLookup{
fields: expressions,
lookup: lookup,
indexes: []sql.Index{index},
expr: lookupExpr,
}, nil
}
// A joinColExpr captures a GetField expression used in a comparison, as well as some additional contextual
// information. Example, for the base expression col1 + 1 > col2 - 1:
// col refers to `col1`
// colExpr refers to `col1 + 1`
// comparand refers to `col2 - 1`
// comparandCol refers to `col2`
// comparison refers to `col1 + 1 > col2 - 1`
// indexes contains any indexes onto col1's table that can be used during the join
// TODO: rename
type joinColExpr struct {
// The field (column) being evaluated, which may not be the entire term in the comparison
col *expression.GetField
// The entire expression on this side of the comparison
colExpr sql.Expression
// The expression this field is being compared to (the other term in the comparison)
comparand sql.Expression
// The other field (column) this field is being compared to (the other term in the comparison)
comparandCol *expression.GetField
// The comparison expression in which this joinColExpr is one term
comparison sql.Expression
// Whether the comparison expression will match null or not.
matchnull bool
}
type joinColExprs []*joinColExpr
type joinExpressionsByTable map[string]joinColExprs
// extractExpressions returns the Expressions in the slice of joinColExpr given.
func extractExpressions(colExprs []*joinColExpr) []sql.Expression {
result := make([]sql.Expression, len(colExprs))
for i, expr := range colExprs {
result[i] = expr.colExpr
}
return result
}
// extractComparands returns the comparand Expressions in the slice of joinColExpr given.
func extractComparands(colExprs []*joinColExpr) []sql.Expression {
result := make([]sql.Expression, len(colExprs))
for i, expr := range colExprs {
result[i] = expr.comparand
}
return result
}
func findColumns(cols []joinColExpr, column string) []*joinColExpr {
var returnedCols []*joinColExpr
for _, col := range cols {
if col.col.String() == column {
jce := col
returnedCols = append(returnedCols, &jce)
}
}
return returnedCols
}
func columnExprsByTable(exprs []sql.Expression) map[string][]joinColExpr {
var result = make(map[string][]joinColExpr)
for _, expr := range exprs {
table, colExpr := extractColumnExpr(expr)
if colExpr == nil {
continue
}
result[table] = append(result[table], *colExpr)
}
return result
}
func extractColumnExpr(e sql.Expression) (string, *joinColExpr) {
switch e := e.(type) {
case *expression.Not:
table, colExpr := extractColumnExpr(e.Child)
if colExpr != nil {
colExpr = &joinColExpr{
col: colExpr.col,
colExpr: colExpr.colExpr,
comparand: colExpr.comparand,
comparison: expression.NewNot(colExpr.comparison),
matchnull: colExpr.matchnull,
}
}
return table, colExpr
case *expression.Equals,
*expression.NullSafeEquals,
*expression.GreaterThan,
*expression.LessThan,
*expression.GreaterThanOrEqual,
*expression.LessThanOrEqual,
*expression.IsNull:
cmp := e.(expression.Comparer)
left, right := cmp.Left(), cmp.Right()
if !isEvaluable(right) {
left, right, e = swapTermsOfExpression(cmp)
}
if !isEvaluable(right) {
return "", nil
}
_, matchnull := e.(*expression.NullSafeEquals)
leftCol, rightCol := expression.ExtractGetField(left), expression.ExtractGetField(right)
if leftCol == nil {
return "", nil
}
return leftCol.Table(), &joinColExpr{
col: leftCol,
colExpr: left,
comparand: right,
comparandCol: rightCol,
comparison: e,
matchnull: matchnull,
}
case *expression.Between:
if !isEvaluable(e.Upper) || !isEvaluable(e.Lower) || isEvaluable(e.Val) {
return "", nil
}
col := expression.ExtractGetField(e)
if col == nil {
return "", nil
}
return col.Table(), &joinColExpr{
col: col,
colExpr: e.Val,
comparand: nil,
comparandCol: nil,
comparison: e,
matchnull: false,
}
case *expression.InTuple:
col := expression.ExtractGetField(e.Left())
if col == nil {
return "", nil
}
return col.Table(), &joinColExpr{
col: col,
colExpr: e.Left(),
comparand: e.Right(),
comparandCol: nil,
comparison: e,
matchnull: false,
}
default:
return "", nil
}