Skip to content

Commit

Permalink
planner: unify OR type IndexMerge code paths (#58396)
Browse files Browse the repository at this point in the history
ref #58361
  • Loading branch information
time-and-fate authored Dec 27, 2024
1 parent 3c40731 commit e44c60c
Show file tree
Hide file tree
Showing 13 changed files with 436 additions and 453 deletions.
12 changes: 12 additions & 0 deletions pkg/planner/cardinality/testdata/cardinality_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -1483,12 +1483,24 @@
"expr": "((a < 5)) or ((a > 10 and true))",
"row_count": 6
},
{
"table_name": "t",
"type": "Index Stats-Range",
"expr": "((a < 5))",
"row_count": 6
},
{
"table_name": "t",
"type": "Index Stats-Range",
"expr": "((a < 5)) or ((a > 10 and true))",
"row_count": 6
},
{
"table_name": "t",
"type": "Index Stats-Range",
"expr": "((a > 10 and true))",
"row_count": 1
},
{
"table_name": "t",
"type": "Table Stats-Expression-CNF",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@
"Plan": [
"PartitionUnion 33.00 root ",
"├─IndexMerge 11.00 root type: union",
"│ ├─TableRangeScan(Build) 1.00 cop[tikv] table:t, partition:p0 range:[1,1], keep order:false, stats:pseudo",
"│ ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, partition:p0, index:b(b) range:[2,2], keep order:false, stats:pseudo",
"│ ├─TableRangeScan(Build) 1.00 cop[tikv] table:t, partition:p0 range:[1,1], keep order:false, stats:pseudo",
"│ └─TableRowIDScan(Probe) 11.00 cop[tikv] table:t, partition:p0 keep order:false, stats:pseudo",
"├─IndexMerge 11.00 root type: union",
"│ ├─TableRangeScan(Build) 1.00 cop[tikv] table:t, partition:p1 range:[1,1], keep order:false, stats:pseudo",
"│ ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, partition:p1, index:b(b) range:[2,2], keep order:false, stats:pseudo",
"│ ├─TableRangeScan(Build) 1.00 cop[tikv] table:t, partition:p1 range:[1,1], keep order:false, stats:pseudo",
"│ └─TableRowIDScan(Probe) 11.00 cop[tikv] table:t, partition:p1 keep order:false, stats:pseudo",
"└─TableReader 11.00 root MppVersion: 2, data:ExchangeSender",
" └─ExchangeSender 11.00 mpp[tiflash] ExchangeType: PassThrough",
Expand Down
23 changes: 1 addition & 22 deletions pkg/planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ func matchPropForIndexMergeAlternatives(ds *logicalop.DataSource, path *util.Acc
// if matchIdxes greater than 1, we should sort this match alternative path by its CountAfterAccess.
alternatives := oneORBranch
slices.SortStableFunc(matchIdxes, func(a, b int) int {
res := cmpAlternativesByRowCount(alternatives[a], alternatives[b])
res := cmpAlternatives(ds.SCtx().GetSessionVars())(alternatives[a], alternatives[b])
if res != 0 {
return res
}
Expand Down Expand Up @@ -1033,27 +1033,6 @@ func matchPropForIndexMergeAlternatives(ds *logicalop.DataSource, path *util.Acc
break
}
}
pushDownCtx := util.GetPushDownCtx(ds.SCtx())
for _, path := range determinedIndexPartialPaths {
// If any partial path contains table filters, we need to keep the whole DNF filter in the Selection.
if len(path.TableFilters) > 0 {
if !expression.CanExprsPushDown(pushDownCtx, path.TableFilters, kv.TiKV) {
// if this table filters can't be pushed down, all of them should be kept in the table side, cleaning the lookup side here.
path.TableFilters = nil
}
shouldKeepCurrentFilter = 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(pushDownCtx, path.IndexFilters, kv.TiKV) {
shouldKeepCurrentFilter = true
// Clear IndexFilter, the whole filter will be put in indexMergePath.TableFilters.
path.IndexFilters = nil
}
}
// Keep this filter as a part of table filters for safety if it has any parameter.
if expression.MaybeOverOptimized4PlanCache(ds.SCtx().GetExprCtx(), []expression.Expression{path.IndexMergeORSourceFilter}) {
shouldKeepCurrentFilter = true
}
if shouldKeepCurrentFilter {
// add the cnf expression back as table filer.
indexMergePath.TableFilters = append(indexMergePath.TableFilters, path.IndexMergeORSourceFilter)
Expand Down
Loading

0 comments on commit e44c60c

Please sign in to comment.