-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
indexmerge_path.go
905 lines (846 loc) · 31.9 KB
/
indexmerge_path.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
// Copyright 2022 PingCAP, 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 core
import (
"fmt"
"math"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/planner/util"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/ranger"
"go.uber.org/zap"
)
// generateIndexMergePath generates IndexMerge AccessPaths on this DataSource.
func (ds *DataSource) generateIndexMergePath() error {
// Consider the IndexMergePath. Now, we just generate `IndexMergePath` in DNF case.
// Use allConds instread of pushedDownConds,
// because we want to use IndexMerge even if some expr cannot be pushed to TiKV.
// We will create new Selection for exprs that cannot be pushed in convertToIndexMergeScan.
indexMergeConds := make([]expression.Expression, 0, len(ds.allConds))
for _, expr := range ds.allConds {
indexMergeConds = append(indexMergeConds, expression.PushDownNot(ds.ctx, expr))
}
stmtCtx := ds.ctx.GetSessionVars().StmtCtx
isPossibleIdxMerge := len(indexMergeConds) > 0 && // have corresponding access conditions, and
(len(ds.possibleAccessPaths) > 1 || // (have multiple index paths, or
(len(ds.possibleAccessPaths) == 1 && isMVIndexPath(ds.possibleAccessPaths[0]))) // have a MVIndex)
sessionAndStmtPermission := (ds.ctx.GetSessionVars().GetEnableIndexMerge() || len(ds.indexMergeHints) > 0) && !stmtCtx.NoIndexMergeHint
// We current do not consider `IndexMergePath`:
// 1. If there is an index path.
// 2. TODO: If there exists exprs that cannot be pushed down. This is to avoid wrongly estRow of Selection added by rule_predicate_push_down.
needConsiderIndexMerge := true
if len(ds.indexMergeHints) == 0 {
for i := 1; i < len(ds.possibleAccessPaths); i++ {
if len(ds.possibleAccessPaths[i].AccessConds) != 0 {
needConsiderIndexMerge = false
break
}
}
if needConsiderIndexMerge {
// PushDownExprs() will append extra warnings, which is annoying. So we reset warnings here.
warnings := stmtCtx.GetWarnings()
extraWarnings := stmtCtx.GetExtraWarnings()
_, remaining := expression.PushDownExprs(stmtCtx, indexMergeConds, ds.ctx.GetClient(), kv.UnSpecified)
stmtCtx.SetWarnings(warnings)
stmtCtx.SetExtraWarnings(extraWarnings)
remainingExpr := 0
for _, expr := range remaining {
// Handle these 3 functions specially since they can be used to access MVIndex.
if sf, ok := expr.(*expression.ScalarFunction); ok {
if sf.FuncName.L == ast.JSONMemberOf || sf.FuncName.L == ast.JSONOverlaps ||
sf.FuncName.L == ast.JSONContains {
continue
}
}
remainingExpr++
}
if remainingExpr > 0 {
needConsiderIndexMerge = false
}
}
}
if isPossibleIdxMerge && sessionAndStmtPermission && needConsiderIndexMerge && ds.tableInfo.TempTableType != model.TempTableLocal {
err := ds.generateAndPruneIndexMergePath(indexMergeConds, ds.indexMergeHints != nil)
if err != nil {
return err
}
} else if len(ds.indexMergeHints) > 0 {
ds.indexMergeHints = nil
var msg string
if !isPossibleIdxMerge {
msg = "No available filter or available index."
} else if !sessionAndStmtPermission {
msg = "Got no_index_merge hint or tidb_enable_index_merge is off."
} else if ds.tableInfo.TempTableType == model.TempTableLocal {
msg = "Cannot use IndexMerge on temporary table."
}
msg = fmt.Sprintf("IndexMerge is inapplicable or disabled. %s", msg)
stmtCtx.AppendWarning(errors.Errorf(msg))
logutil.BgLogger().Debug(msg)
}
return nil
}
// getIndexMergeOrPath generates all possible IndexMergeOrPaths.
func (ds *DataSource) generateIndexMergeOrPaths(filters []expression.Expression) error {
usedIndexCount := len(ds.possibleAccessPaths)
for i, cond := range filters {
sf, ok := cond.(*expression.ScalarFunction)
if !ok || sf.FuncName.L != ast.LogicOr {
continue
}
var partialPaths = make([]*util.AccessPath, 0, usedIndexCount)
dnfItems := expression.FlattenDNFConditions(sf)
for _, item := range dnfItems {
cnfItems := expression.SplitCNFItems(item)
itemPaths := ds.accessPathsForConds(cnfItems, usedIndexCount)
if len(itemPaths) == 0 {
partialPaths = nil
break
}
partialPath, err := ds.buildIndexMergePartialPath(itemPaths)
if err != nil {
return err
}
if partialPath == nil {
partialPaths = nil
break
}
partialPaths = append(partialPaths, partialPath)
}
// If all of the partialPaths use the same index, we will not use the indexMerge.
singlePath := true
for i := len(partialPaths) - 1; i >= 1; i-- {
if partialPaths[i].Index != partialPaths[i-1].Index {
singlePath = false
break
}
}
if singlePath {
continue
}
if len(partialPaths) > 1 {
possiblePath := ds.buildIndexMergeOrPath(filters, partialPaths, i)
if possiblePath == nil {
return nil
}
accessConds := make([]expression.Expression, 0, len(partialPaths))
for _, p := range partialPaths {
indexCondsForP := p.AccessConds[:]
indexCondsForP = append(indexCondsForP, p.IndexFilters...)
if len(indexCondsForP) > 0 {
accessConds = append(accessConds, expression.ComposeCNFCondition(ds.ctx, indexCondsForP...))
}
}
accessDNF := expression.ComposeDNFCondition(ds.ctx, accessConds...)
sel, _, err := ds.tableStats.HistColl.Selectivity(ds.ctx, []expression.Expression{accessDNF}, nil)
if err != nil {
logutil.BgLogger().Debug("something wrong happened, use the default selectivity", zap.Error(err))
sel = SelectionFactor
}
possiblePath.CountAfterAccess = sel * ds.tableStats.RowCount
ds.possibleAccessPaths = append(ds.possibleAccessPaths, possiblePath)
}
}
return nil
}
// isInIndexMergeHints returns true if the input index name is not excluded by the IndexMerge hints, which means either
// (1) there's no IndexMerge hint, (2) there's IndexMerge hint but no specified index names, or (3) the input index
// name is specified in the IndexMerge hints.
func (ds *DataSource) isInIndexMergeHints(name string) bool {
if len(ds.indexMergeHints) == 0 {
return true
}
for _, hint := range ds.indexMergeHints {
if hint.indexHint == nil || len(hint.indexHint.IndexNames) == 0 {
return true
}
for _, hintName := range hint.indexHint.IndexNames {
if strings.EqualFold(strings.ToLower(name), strings.ToLower(hintName.String())) {
return true
}
}
}
return false
}
// indexMergeHintsHasSpecifiedIdx returns true if there's IndexMerge hint, and it has specified index names.
func (ds *DataSource) indexMergeHintsHasSpecifiedIdx() bool {
for _, hint := range ds.indexMergeHints {
if hint.indexHint == nil || len(hint.indexHint.IndexNames) == 0 {
continue
}
if len(hint.indexHint.IndexNames) > 0 {
return true
}
}
return false
}
// indexMergeHintsHasSpecifiedIdx return true if the input index name is specified in the IndexMerge hint.
func (ds *DataSource) isSpecifiedInIndexMergeHints(name string) bool {
for _, hint := range ds.indexMergeHints {
if hint.indexHint == nil || len(hint.indexHint.IndexNames) == 0 {
continue
}
for _, hintName := range hint.indexHint.IndexNames {
if strings.EqualFold(strings.ToLower(name), strings.ToLower(hintName.String())) {
return true
}
}
}
return false
}
// accessPathsForConds generates all possible index paths for conditions.
func (ds *DataSource) accessPathsForConds(conditions []expression.Expression, usedIndexCount int) []*util.AccessPath {
var results = make([]*util.AccessPath, 0, usedIndexCount)
for i := 0; i < usedIndexCount; i++ {
path := &util.AccessPath{}
if ds.possibleAccessPaths[i].IsTablePath() {
if !ds.isInIndexMergeHints("primary") {
continue
}
if ds.tableInfo.IsCommonHandle {
path.IsCommonHandlePath = true
path.Index = ds.possibleAccessPaths[i].Index
} else {
path.IsIntHandlePath = true
}
err := ds.deriveTablePathStats(path, conditions, true)
if err != nil {
logutil.BgLogger().Debug("can not derive statistics of a path", zap.Error(err))
continue
}
var unsignedIntHandle bool
if path.IsIntHandlePath && ds.tableInfo.PKIsHandle {
if pkColInfo := ds.tableInfo.GetPkColInfo(); pkColInfo != nil {
unsignedIntHandle = mysql.HasUnsignedFlag(pkColInfo.GetFlag())
}
}
// If the path contains a full range, ignore it.
if ranger.HasFullRange(path.Ranges, unsignedIntHandle) {
continue
}
// If we have point or empty range, just remove other possible paths.
if len(path.Ranges) == 0 || path.OnlyPointRange(ds.SCtx()) {
if len(results) == 0 {
results = append(results, path)
} else {
results[0] = path
results = results[:1]
}
break
}
} else {
path.Index = ds.possibleAccessPaths[i].Index
if !ds.isInIndexMergeHints(path.Index.Name.L) {
continue
}
err := ds.fillIndexPath(path, conditions)
if err != nil {
logutil.BgLogger().Debug("can not derive statistics of a path", zap.Error(err))
continue
}
ds.deriveIndexPathStats(path, conditions, true)
// If the path contains a full range, ignore it.
if ranger.HasFullRange(path.Ranges, false) {
continue
}
// If we have empty range, or point range on unique index, just remove other possible paths.
if len(path.Ranges) == 0 || (path.OnlyPointRange(ds.SCtx()) && path.Index.Unique) {
if len(results) == 0 {
results = append(results, path)
} else {
results[0] = path
results = results[:1]
}
break
}
}
results = append(results, path)
}
return results
}
// buildIndexMergePartialPath chooses the best index path from all possible paths.
// Now we choose the index with minimal estimate row count.
func (ds *DataSource) buildIndexMergePartialPath(indexAccessPaths []*util.AccessPath) (*util.AccessPath, error) {
if len(indexAccessPaths) == 1 {
return indexAccessPaths[0], nil
}
minEstRowIndex := 0
minEstRow := math.MaxFloat64
for i := 0; i < len(indexAccessPaths); i++ {
rc := indexAccessPaths[i].CountAfterAccess
if len(indexAccessPaths[i].IndexFilters) > 0 {
rc = indexAccessPaths[i].CountAfterIndex
}
if rc < minEstRow {
minEstRowIndex = i
minEstRow = rc
}
}
return indexAccessPaths[minEstRowIndex], nil
}
// buildIndexMergeOrPath generates one possible IndexMergePath.
func (ds *DataSource) buildIndexMergeOrPath(filters []expression.Expression, partialPaths []*util.AccessPath, current int) *util.AccessPath {
indexMergePath := &util.AccessPath{PartialIndexPaths: partialPaths}
indexMergePath.TableFilters = append(indexMergePath.TableFilters, filters[:current]...)
indexMergePath.TableFilters = append(indexMergePath.TableFilters, filters[current+1:]...)
var addCurrentFilter bool
for _, path := range partialPaths {
// If any partial path contains table filters, we need to keep the whole DNF filter in the Selection.
if len(path.TableFilters) > 0 {
addCurrentFilter = true
}
// If any partial path's index filter cannot be pushed to TiKV, we should keep the whole DNF filter.
if len(path.IndexFilters) != 0 && !expression.CanExprsPushDown(ds.ctx.GetSessionVars().StmtCtx, path.IndexFilters, ds.ctx.GetClient(), kv.TiKV) {
addCurrentFilter = true
// Clear IndexFilter, the whole filter will be put in indexMergePath.TableFilters.
path.IndexFilters = nil
}
if len(path.TableFilters) != 0 && !expression.CanExprsPushDown(ds.ctx.GetSessionVars().StmtCtx, path.TableFilters, ds.ctx.GetClient(), kv.TiKV) {
addCurrentFilter = true
path.TableFilters = nil
}
}
if addCurrentFilter {
indexMergePath.TableFilters = append(indexMergePath.TableFilters, filters[current])
}
return indexMergePath
}
// generateIndexMergeAndPaths generates IndexMerge paths for `AND` (a.k.a. intersection type IndexMerge)
func (ds *DataSource) generateIndexMergeAndPaths(normalPathCnt int) *util.AccessPath {
// For now, we only consider intersection type IndexMerge when the index names are specified in the hints.
if !ds.indexMergeHintsHasSpecifiedIdx() {
return nil
}
// 1. Collect partial paths from normal paths.
var partialPaths []*util.AccessPath
for i := 0; i < normalPathCnt; i++ {
originalPath := ds.possibleAccessPaths[i]
// No need to consider table path as a partial path.
if ds.possibleAccessPaths[i].IsTablePath() {
continue
}
if !ds.isSpecifiedInIndexMergeHints(originalPath.Index.Name.L) {
continue
}
// If the path contains a full range, ignore it.
if ranger.HasFullRange(originalPath.Ranges, false) {
continue
}
newPath := originalPath.Clone()
partialPaths = append(partialPaths, newPath)
}
if len(partialPaths) < 2 {
return nil
}
// 2. Collect filters that can't be covered by the partial paths and deduplicate them.
finalFilters := make([]expression.Expression, 0)
partialFilters := make([]expression.Expression, 0, len(partialPaths))
hashCodeSet := make(map[string]struct{})
for _, path := range partialPaths {
// Classify filters into coveredConds and notCoveredConds.
coveredConds := make([]expression.Expression, 0, len(path.AccessConds)+len(path.IndexFilters))
notCoveredConds := make([]expression.Expression, 0, len(path.IndexFilters)+len(path.TableFilters))
// AccessConds can be covered by partial path.
coveredConds = append(coveredConds, path.AccessConds...)
for i, cond := range path.IndexFilters {
// IndexFilters can be covered by partial path if it can be pushed down to TiKV.
if !expression.CanExprsPushDown(ds.ctx.GetSessionVars().StmtCtx, []expression.Expression{cond}, ds.ctx.GetClient(), kv.TiKV) {
path.IndexFilters = append(path.IndexFilters[:i], path.IndexFilters[i+1:]...)
notCoveredConds = append(notCoveredConds, cond)
} else {
coveredConds = append(coveredConds, cond)
}
}
// TableFilters can't be covered by partial path.
notCoveredConds = append(notCoveredConds, path.TableFilters...)
// Record covered filters in hashCodeSet.
// Note that we only record filters that not appear in the notCoveredConds. It's possible that a filter appear
// in both coveredConds and notCoveredConds (e.g. because of prefix index). So we need this extra check to
// avoid wrong deduplication.
notCoveredHashCodeSet := make(map[string]struct{})
for _, cond := range notCoveredConds {
hashCode := string(cond.HashCode(ds.ctx.GetSessionVars().StmtCtx))
notCoveredHashCodeSet[hashCode] = struct{}{}
}
for _, cond := range coveredConds {
hashCode := string(cond.HashCode(ds.ctx.GetSessionVars().StmtCtx))
if _, ok := notCoveredHashCodeSet[hashCode]; !ok {
hashCodeSet[hashCode] = struct{}{}
}
}
finalFilters = append(finalFilters, notCoveredConds...)
partialFilters = append(partialFilters, coveredConds...)
}
// Remove covered filters from finalFilters and deduplicate finalFilters.
dedupedFinalFilters := make([]expression.Expression, 0, len(finalFilters))
for _, cond := range finalFilters {
hashCode := string(cond.HashCode(ds.ctx.GetSessionVars().StmtCtx))
if _, ok := hashCodeSet[hashCode]; !ok {
dedupedFinalFilters = append(dedupedFinalFilters, cond)
hashCodeSet[hashCode] = struct{}{}
}
}
// 3. Estimate the row count after partial paths.
sel, _, err := ds.tableStats.HistColl.Selectivity(ds.ctx, partialFilters, nil)
if err != nil {
logutil.BgLogger().Debug("something wrong happened, use the default selectivity", zap.Error(err))
sel = SelectionFactor
}
indexMergePath := &util.AccessPath{
PartialIndexPaths: partialPaths,
IndexMergeIsIntersection: true,
TableFilters: dedupedFinalFilters,
CountAfterAccess: sel * ds.tableStats.RowCount,
}
return indexMergePath
}
func (ds *DataSource) generateAndPruneIndexMergePath(indexMergeConds []expression.Expression, needPrune bool) error {
regularPathCount := len(ds.possibleAccessPaths)
// 1. Generate possible IndexMerge paths for `OR`.
err := ds.generateIndexMergeOrPaths(indexMergeConds)
if err != nil {
return err
}
// 2. Generate possible IndexMerge paths for `AND`.
indexMergeAndPath := ds.generateIndexMergeAndPaths(regularPathCount)
if indexMergeAndPath != nil {
ds.possibleAccessPaths = append(ds.possibleAccessPaths, indexMergeAndPath)
}
// 3. Generate possible IndexMerge paths for MVIndex.
mvIndexMergePath, err := ds.generateIndexMerge4MVIndex(regularPathCount, indexMergeConds)
if err != nil {
return err
}
if mvIndexMergePath != nil {
ds.possibleAccessPaths = append(ds.possibleAccessPaths, mvIndexMergePath...)
}
// 4. If needed, append a warning if no IndexMerge is generated.
// If without hints, it means that `enableIndexMerge` is true
if len(ds.indexMergeHints) == 0 {
return nil
}
// With hints and without generated IndexMerge paths
if regularPathCount == len(ds.possibleAccessPaths) {
ds.indexMergeHints = nil
ds.ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("IndexMerge is inapplicable"))
return nil
}
// 4. If needPrune is true, prune non-IndexMerge paths.
// Do not need to consider the regular paths in find_best_task().
// So we can use index merge's row count as DataSource's row count.
if needPrune {
ds.possibleAccessPaths = ds.possibleAccessPaths[regularPathCount:]
minRowCount := ds.possibleAccessPaths[0].CountAfterAccess
for _, path := range ds.possibleAccessPaths {
if minRowCount < path.CountAfterAccess {
minRowCount = path.CountAfterAccess
}
}
if ds.stats.RowCount > minRowCount {
ds.stats = ds.tableStats.ScaleByExpectCnt(minRowCount)
}
}
return nil
}
// generateIndexMergeOnDNF4MVIndex generates IndexMerge paths for MVIndex upon DNF filters.
/*
select * from t where ((1 member of (a) and b=1) or (2 member of (a) and b=2)) and (c > 10)
IndexMerge(OR)
IndexRangeScan(a, b, [1 1, 1 1])
IndexRangeScan(a, b, [2 2, 2 2])
Selection(c > 10)
TableRowIdScan(t)
Two limitations now:
1). all filters in the DNF have to be used as access-filters: ((1 member of (a)) or (2 member of (a)) or b > 10) cannot be used to access the MVIndex.
2). cannot support json_contains: (json_contains(a, '[1, 2]') or json_contains(a, '[3, 4]')) is not supported since a single IndexMerge cannot represent this SQL.
*/
func (ds *DataSource) generateIndexMergeOnDNF4MVIndex(normalPathCnt int, filters []expression.Expression) (mvIndexPaths []*util.AccessPath, err error) {
for idx := 0; idx < normalPathCnt; idx++ {
if !isMVIndexPath(ds.possibleAccessPaths[idx]) {
continue // not a MVIndex path
}
idxCols, ok := ds.prepareCols4MVIndex(ds.possibleAccessPaths[idx].Index)
if !ok {
continue
}
for current, filter := range filters {
sf, ok := filter.(*expression.ScalarFunction)
if !ok || sf.FuncName.L != ast.LogicOr {
continue
}
dnfFilters := expression.FlattenDNFConditions(sf) // [(1 member of (a) and b=1), (2 member of (a) and b=2)]
// build partial paths for each dnf filter
cannotFit := false
var partialPaths []*util.AccessPath
for _, dnfFilter := range dnfFilters {
mvIndexFilters := []expression.Expression{dnfFilter}
if sf, ok := dnfFilter.(*expression.ScalarFunction); ok && sf.FuncName.L == ast.LogicAnd {
mvIndexFilters = expression.FlattenCNFConditions(sf) // (1 member of (a) and b=1) --> [(1 member of (a)), b=1]
}
accessFilters, remainingFilters := ds.collectFilters4MVIndex(mvIndexFilters, idxCols)
if len(accessFilters) == 0 || len(remainingFilters) > 0 { // limitation 1
cannotFit = true
break
}
paths, isIntersection, ok, err := ds.buildPartialPaths4MVIndex(accessFilters, idxCols, ds.possibleAccessPaths[idx].Index)
if err != nil {
return nil, err
}
if isIntersection || !ok { // limitation 2
cannotFit = true
break
}
partialPaths = append(partialPaths, paths...)
}
if cannotFit {
continue
}
var remainingFilters []expression.Expression
remainingFilters = append(remainingFilters, filters[:current]...)
remainingFilters = append(remainingFilters, filters[current+1:]...)
indexMergePath := ds.buildPartialPathUp4MVIndex(partialPaths, false, remainingFilters)
mvIndexPaths = append(mvIndexPaths, indexMergePath)
}
}
return
}
// generateIndexMergeJSONMVIndexPath generates paths for (json_member_of / json_overlaps / json_contains) on multi-valued index.
/*
1. select * from t where 1 member of (a)
IndexMerge(AND)
IndexRangeScan(a, [1,1])
TableRowIdScan(t)
2. select * from t where json_contains(a, '[1, 2, 3]')
IndexMerge(AND)
IndexRangeScan(a, [1,1])
IndexRangeScan(a, [2,2])
IndexRangeScan(a, [3,3])
TableRowIdScan(t)
3. select * from t where json_overlap(a, '[1, 2, 3]')
IndexMerge(OR)
IndexRangeScan(a, [1,1])
IndexRangeScan(a, [2,2])
IndexRangeScan(a, [3,3])
TableRowIdScan(t)
*/
func (ds *DataSource) generateIndexMerge4MVIndex(normalPathCnt int, filters []expression.Expression) (mvIndexPaths []*util.AccessPath, err error) {
dnfMVIndexPaths, err := ds.generateIndexMergeOnDNF4MVIndex(normalPathCnt, filters)
if err != nil {
return nil, err
}
mvIndexPaths = append(mvIndexPaths, dnfMVIndexPaths...)
for idx := 0; idx < normalPathCnt; idx++ {
if !isMVIndexPath(ds.possibleAccessPaths[idx]) {
continue // not a MVIndex path
}
idxCols, ok := ds.prepareCols4MVIndex(ds.possibleAccessPaths[idx].Index)
if !ok {
continue
}
accessFilters, remainingFilters := ds.collectFilters4MVIndex(filters, idxCols)
if len(accessFilters) == 0 { // cannot use any filter on this MVIndex
continue
}
partialPaths, isIntersection, ok, err := ds.buildPartialPaths4MVIndex(accessFilters, idxCols, ds.possibleAccessPaths[idx].Index)
if err != nil {
return nil, err
}
if !ok {
continue
}
mvIndexPaths = append(mvIndexPaths, ds.buildPartialPathUp4MVIndex(partialPaths, isIntersection, remainingFilters))
}
return
}
// buildPartialPathUp4MVIndex builds these partial paths up to a complete index merge path.
func (ds *DataSource) buildPartialPathUp4MVIndex(partialPaths []*util.AccessPath, isIntersection bool, remainingFilters []expression.Expression) *util.AccessPath {
indexMergePath := &util.AccessPath{PartialIndexPaths: partialPaths, IndexMergeAccessMVIndex: true}
indexMergePath.IndexMergeIsIntersection = isIntersection
indexMergePath.TableFilters = remainingFilters
// TODO: use a naive estimation strategy here now for simplicity, make it more accurate.
minEstRows, maxEstRows := math.MaxFloat64, -1.0
for _, p := range indexMergePath.PartialIndexPaths {
minEstRows = math.Min(minEstRows, p.CountAfterAccess)
maxEstRows = math.Max(maxEstRows, p.CountAfterAccess)
}
if indexMergePath.IndexMergeIsIntersection {
indexMergePath.CountAfterAccess = minEstRows
} else {
indexMergePath.CountAfterAccess = maxEstRows
}
return indexMergePath
}
// buildPartialPaths4MVIndex builds partial paths by using these accessFilters upon this MVIndex.
// The accessFilters must be corresponding to these idxCols.
// OK indicates whether it builds successfully. These partial paths should be ignored if ok==false.
func (ds *DataSource) buildPartialPaths4MVIndex(accessFilters []expression.Expression,
idxCols []*expression.Column, mvIndex *model.IndexInfo) (
partialPaths []*util.AccessPath, isIntersection bool, ok bool, err error) {
var virColID = -1
for i := range idxCols {
if idxCols[i].VirtualExpr != nil {
virColID = i
break
}
}
if virColID == -1 { // unexpected, no vir-col on this MVIndex
return nil, false, false, nil
}
if len(accessFilters) <= virColID { // no filter related to the vir-col, build a partial path directly.
partialPath, ok, err := ds.buildPartialPath4MVIndex(accessFilters, idxCols, mvIndex)
return []*util.AccessPath{partialPath}, false, ok, err
}
virCol := idxCols[virColID]
jsonType := virCol.GetType().ArrayType()
targetJSONPath, ok := unwrapJSONCast(virCol.VirtualExpr)
if !ok {
return nil, false, false, nil
}
// extract values related to this vir-col, for example, extract [1, 2] from `json_contains(j, '[1, 2]')`
var virColVals []expression.Expression
sf, ok := accessFilters[virColID].(*expression.ScalarFunction)
if !ok {
return nil, false, false, nil
}
switch sf.FuncName.L {
case ast.JSONMemberOf: // (1 member of a->'$.zip')
v, ok := unwrapJSONCast(sf.GetArgs()[0]) // cast(1 as json) --> 1
if !ok {
return nil, false, false, nil
}
virColVals = append(virColVals, v)
case ast.JSONContains: // (json_contains(a->'$.zip', '[1, 2, 3]')
isIntersection = true
virColVals, ok = jsonArrayExpr2Exprs(ds.ctx, sf.GetArgs()[1], jsonType)
if !ok {
return nil, false, false, nil
}
case ast.JSONOverlaps: // (json_overlaps(a->'$.zip', '[1, 2, 3]')
var jsonPathIdx int
if sf.GetArgs()[0].Equal(ds.ctx, targetJSONPath) {
jsonPathIdx = 0 // (json_overlaps(a->'$.zip', '[1, 2, 3]')
} else if sf.GetArgs()[1].Equal(ds.ctx, targetJSONPath) {
jsonPathIdx = 1 // (json_overlaps('[1, 2, 3]', a->'$.zip')
} else {
return nil, false, false, nil
}
var ok bool
virColVals, ok = jsonArrayExpr2Exprs(ds.ctx, sf.GetArgs()[1-jsonPathIdx], jsonType)
if !ok {
return nil, false, false, nil
}
default:
return nil, false, false, nil
}
for _, v := range virColVals {
// rewrite json functions to EQ to calculate range, `(1 member of j)` -> `j=1`.
eq, err := expression.NewFunction(ds.ctx, ast.EQ, types.NewFieldType(mysql.TypeTiny), virCol, v)
if err != nil {
return nil, false, false, err
}
accessFilters[virColID] = eq
partialPath, ok, err := ds.buildPartialPath4MVIndex(accessFilters, idxCols, mvIndex)
if !ok || err != nil {
return nil, false, ok, err
}
partialPaths = append(partialPaths, partialPath)
}
return partialPaths, isIntersection, true, nil
}
// buildPartialPath4MVIndex builds a partial path on this MVIndex with these accessFilters.
func (ds *DataSource) buildPartialPath4MVIndex(accessFilters []expression.Expression, idxCols []*expression.Column, mvIndex *model.IndexInfo) (*util.AccessPath, bool, error) {
partialPath := &util.AccessPath{Index: mvIndex}
partialPath.Ranges = ranger.FullRange()
for i := 0; i < len(idxCols); i++ {
partialPath.IdxCols = append(partialPath.IdxCols, idxCols[i])
partialPath.IdxColLens = append(partialPath.IdxColLens, mvIndex.Columns[i].Length)
partialPath.FullIdxCols = append(partialPath.FullIdxCols, idxCols[i])
partialPath.FullIdxColLens = append(partialPath.FullIdxColLens, mvIndex.Columns[i].Length)
}
if err := ds.detachCondAndBuildRangeForPath(partialPath, accessFilters); err != nil {
return nil, false, err
}
if len(partialPath.AccessConds) != len(accessFilters) || len(partialPath.TableFilters) > 0 {
// not all filters are used in this case.
return nil, false, nil
}
return partialPath, true, nil
}
func (ds *DataSource) prepareCols4MVIndex(mvIndex *model.IndexInfo) (idxCols []*expression.Column, ok bool) {
var virColNum = 0
for i := range mvIndex.Columns {
colOffset := mvIndex.Columns[i].Offset
colMeta := ds.table.Meta().Cols()[colOffset]
var col *expression.Column
for _, c := range ds.TblCols {
if c.ID == colMeta.ID {
col = c
break
}
}
if col == nil { // unexpected, no vir-col on this MVIndex
return nil, false
}
if col.GetType().IsArray() {
virColNum++
col = col.Clone().(*expression.Column)
col.RetType = col.GetType().ArrayType() // use the underlying type directly: JSON-ARRAY(INT) --> INT
col.RetType.SetCharset(charset.CharsetBin)
col.RetType.SetCollate(charset.CollationBin)
}
idxCols = append(idxCols, col)
}
if virColNum != 1 { // assume only one vir-col in the MVIndex
return nil, false
}
return idxCols, true
}
// collectFilters4MVIndex splits these filters into 2 parts where accessFilters can be used to access this index directly.
// For idx(x, cast(a as array), z), `x=1 and (2 member of a) and z=1 and x+z>0` is splitted to:
// accessFilters: `x=1 and (2 member of a) and z=1`, remaining: `x+z>0`.
func (ds *DataSource) collectFilters4MVIndex(filters []expression.Expression, idxCols []*expression.Column) (accessFilters, remainingFilters []expression.Expression) {
usedAsAccess := make([]bool, len(filters))
for _, col := range idxCols {
found := false
for i, f := range filters {
if usedAsAccess[i] {
continue
}
if ds.checkFilter4MVIndexColumn(f, col) {
accessFilters = append(accessFilters, f)
usedAsAccess[i] = true
found = true
break
}
}
if !found {
break
}
}
for i := range usedAsAccess {
if !usedAsAccess[i] {
remainingFilters = append(remainingFilters, filters[i])
}
}
return accessFilters, remainingFilters
}
// checkFilter4MVIndexColumn checks whether this filter can be used as an accessFilter to access the MVIndex column.
func (ds *DataSource) checkFilter4MVIndexColumn(filter expression.Expression, idxCol *expression.Column) bool {
sf, ok := filter.(*expression.ScalarFunction)
if !ok {
return false
}
if idxCol.VirtualExpr != nil { // the virtual column on the MVIndex
targetJSONPath, ok := unwrapJSONCast(idxCol.VirtualExpr)
if !ok {
return false
}
switch sf.FuncName.L {
case ast.JSONMemberOf: // (1 member of a)
return targetJSONPath.Equal(ds.ctx, sf.GetArgs()[1])
case ast.JSONContains: // json_contains(a, '1')
return targetJSONPath.Equal(ds.ctx, sf.GetArgs()[0])
case ast.JSONOverlaps: // json_overlaps(a, '1') or json_overlaps('1', a)
return targetJSONPath.Equal(ds.ctx, sf.GetArgs()[0]) ||
targetJSONPath.Equal(ds.ctx, sf.GetArgs()[1])
default:
return false
}
} else {
if sf.FuncName.L != ast.EQ { // only support EQ now
return false
}
args := sf.GetArgs()
var argCol *expression.Column
var argConst *expression.Constant
if c, isCol := args[0].(*expression.Column); isCol {
if con, isCon := args[1].(*expression.Constant); isCon {
argCol, argConst = c, con
}
} else if c, isCol := args[1].(*expression.Column); isCol {
if con, isCon := args[0].(*expression.Constant); isCon {
argCol, argConst = c, con
}
}
if argCol == nil || argConst == nil {
return false
}
if argCol.Equal(ds.ctx, idxCol) {
return true
}
}
return false
}
// jsonArrayExpr2Exprs converts a JsonArray expression to expression list: cast('[1, 2, 3]' as JSON) --> []expr{1, 2, 3}
func jsonArrayExpr2Exprs(sctx sessionctx.Context, jsonArrayExpr expression.Expression, targetType *types.FieldType) ([]expression.Expression, bool) {
if !expression.IsInmutableExpr(jsonArrayExpr) || jsonArrayExpr.GetType().EvalType() != types.ETJson {
return nil, false
}
jsonArray, isNull, err := jsonArrayExpr.EvalJSON(sctx, chunk.Row{})
if isNull || err != nil {
return nil, false
}
if jsonArray.TypeCode != types.JSONTypeCodeArray {
single, ok := jsonValue2Expr(jsonArray, targetType) // '1' -> []expr{1}
if ok {
return []expression.Expression{single}, true
}
return nil, false
}
var exprs []expression.Expression
for i := 0; i < jsonArray.GetElemCount(); i++ { // '[1, 2, 3]' -> []expr{1, 2, 3}
expr, ok := jsonValue2Expr(jsonArray.ArrayGetElem(i), targetType)
if !ok {
return nil, false
}
exprs = append(exprs, expr)
}
return exprs, true
}
func jsonValue2Expr(v types.BinaryJSON, targetType *types.FieldType) (expression.Expression, bool) {
datum, err := expression.ConvertJSON2Tp(v, targetType)
if err != nil {
return nil, false
}
return &expression.Constant{
Value: types.NewDatum(datum),
RetType: targetType,
}, true
}
func unwrapJSONCast(expr expression.Expression) (expression.Expression, bool) {
if expr == nil {
return nil, false
}
sf, ok := expr.(*expression.ScalarFunction)
if !ok {
return nil, false
}
if sf == nil || sf.FuncName.L != ast.Cast || sf.GetType().EvalType() != types.ETJson {
return nil, false
}
return sf.GetArgs()[0], true
}
func isMVIndexPath(path *util.AccessPath) bool {
return !path.IsTablePath() && path.Index != nil && path.Index.MVIndex
}