Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix index merge plan when expr cannot be pushed to tikv #30341

Merged
merged 32 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
56bf325
planner: fix index merge plan when expr has not been pushed to tikv.
guo-shaoge Dec 1, 2021
c5d2ddb
Merge branch 'master' of https://github.com/pingcap/tidb into fix_ind…
guo-shaoge Dec 1, 2021
c4dc91f
Merge branch 'master' into fix_index_merge_sel_plan
qw4990 Dec 3, 2021
b60786e
Merge branch 'master' into fix_index_merge_sel_plan
guo-shaoge Dec 7, 2021
4800457
fix comment
guo-shaoge Dec 8, 2021
7de60f4
Merge branch 'fix_index_merge_sel_plan' of github.com:guo-shaoge/tidb…
guo-shaoge Dec 8, 2021
b4654ea
fix comment
guo-shaoge Dec 11, 2021
cc74a3e
fix cannot be pushed expr in partial_path.IndexFilters
guo-shaoge Dec 12, 2021
594b734
Merge branch 'master' into fix_index_merge_sel_plan
guo-shaoge Dec 12, 2021
bc26b95
Merge branch 'fix_index_merge_sel_plan' of github.com:guo-shaoge/tidb…
guo-shaoge Dec 12, 2021
5d98c2c
fix cost
guo-shaoge Dec 12, 2021
7f00244
Update planner/core/find_best_task.go
guo-shaoge Dec 13, 2021
eba2cbd
Update planner/core/find_best_task.go
guo-shaoge Dec 13, 2021
9b4ec81
Update planner/core/find_best_task.go
guo-shaoge Dec 13, 2021
b59caf3
check partial path's TableFilters can be pushed to TiKV or not.
guo-shaoge Dec 14, 2021
3630fdb
Merge branch 'master' of https://github.com/pingcap/tidb into fix_ind…
guo-shaoge Dec 14, 2021
bc8c836
fix
guo-shaoge Dec 14, 2021
9187957
fix didn't push not expr
guo-shaoge Dec 14, 2021
b89e269
fix est row
guo-shaoge Dec 19, 2021
fb21ec4
check expr can be pushed in ds.pushedDownConds
guo-shaoge Dec 19, 2021
19ffe55
add more comment
guo-shaoge Dec 19, 2021
e3f3e2e
fix index on virtual column for index merge
guo-shaoge Dec 19, 2021
e249201
fix typo
guo-shaoge Dec 19, 2021
cfe8309
Merge branch 'master' into fix_index_merge_sel_plan
guo-shaoge Dec 21, 2021
f47ebc9
fix case in index_merge_reader_test.go
guo-shaoge Dec 21, 2021
11e5179
fix case
guo-shaoge Dec 21, 2021
887bccd
a simple way to do same thing
guo-shaoge Dec 21, 2021
1d6abd4
fix warnings and case
guo-shaoge Dec 21, 2021
b75d087
fix comment
guo-shaoge Dec 22, 2021
ff0fb07
add index merge warnings
guo-shaoge Dec 22, 2021
8b50eec
fix warning case
guo-shaoge Dec 23, 2021
a78dc67
Merge branch 'master' into fix_index_merge_sel_plan
ti-chi-bot Dec 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package core

import (
"bytes"
"fmt"
"math"
"strings"
Expand Down Expand Up @@ -973,14 +974,17 @@ func (ds *DataSource) convertToIndexMergeScan(prop *property.PhysicalProperty, c
if prop.ExpectedCnt < ds.stats.RowCount {
totalRowCount *= prop.ExpectedCnt / ds.stats.RowCount
}
ts, partialCost, err := ds.buildIndexMergeTableScan(prop, path.TableFilters, totalRowCount)
ts, partialCost, remainedFilters, err := ds.buildIndexMergeTableScan(prop, path.TableFilters, totalRowCount)
guo-shaoge marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
totalCost += partialCost
cop.tablePlan = ts
cop.idxMergePartPlans = scans
cop.cst = totalCost
if remainedFilters != nil {
cop.rootTaskConds = remainedFilters
}
task = cop.convertToRootTask(ds.ctx)
ds.addSelection4PlanCache(task.(*rootTask), ds.tableStats.ScaleByExpectCnt(totalRowCount), prop)
return task, nil
Expand Down Expand Up @@ -1093,7 +1097,7 @@ func setIndexMergeTableScanHandleCols(ds *DataSource, ts *PhysicalTableScan) (er
}

func (ds *DataSource) buildIndexMergeTableScan(prop *property.PhysicalProperty, tableFilters []expression.Expression,
totalRowCount float64) (PhysicalPlan, float64, error) {
totalRowCount float64) (PhysicalPlan, float64, []expression.Expression, error) {
var partialCost float64
sessVars := ds.ctx.GetSessionVars()
ts := PhysicalTableScan{
Expand All @@ -1108,7 +1112,7 @@ func (ds *DataSource) buildIndexMergeTableScan(prop *property.PhysicalProperty,
ts.SetSchema(ds.schema.Clone())
err := setIndexMergeTableScanHandleCols(ds, ts)
if err != nil {
return nil, 0, err
return nil, 0, nil, err
}
if ts.Table.PKIsHandle {
if pkColInfo := ts.Table.GetPkColInfo(); pkColInfo != nil {
Expand All @@ -1124,17 +1128,40 @@ func (ds *DataSource) buildIndexMergeTableScan(prop *property.PhysicalProperty,
ts.stats.StatsVersion = statistics.PseudoVersion
}
if len(tableFilters) > 0 {
// Here we want to add a Selection for exprs that cannot be pushed to TiKV, but some of them has already been put
// in Selection above DataSource, so we need to filter these exprs.
_, filtersInSelection := expression.PushDownExprs(sessVars.StmtCtx, tableFilters, ds.ctx.GetClient(), kv.UnSpecified)
guo-shaoge marked this conversation as resolved.
Show resolved Hide resolved
pushedFilters, remainedFilters := expression.PushDownExprs(sessVars.StmtCtx, tableFilters, ds.ctx.GetClient(), kv.TiKV)
remainedFilters = removeExprsInSelection(sessVars.StmtCtx, remainedFilters, filtersInSelection)
partialCost += totalRowCount * sessVars.CopCPUFactor
selectivity, _, err := ds.tableStats.HistColl.Selectivity(ds.ctx, tableFilters, nil)
if err != nil {
logutil.BgLogger().Debug("calculate selectivity failed, use selection factor", zap.Error(err))
selectivity = SelectionFactor
if len(pushedFilters) != 0 {
selectivity, _, err := ds.tableStats.HistColl.Selectivity(ds.ctx, pushedFilters, nil)
if err != nil {
logutil.BgLogger().Debug("calculate selectivity failed, use selection factor", zap.Error(err))
selectivity = SelectionFactor
}
sel := PhysicalSelection{Conditions: pushedFilters}.Init(ts.ctx, ts.stats.ScaleByExpectCnt(selectivity*totalRowCount), ts.blockOffset)
sel.SetChildren(ts)
return sel, partialCost, remainedFilters, nil
}
return ts, partialCost, remainedFilters, nil
}
return ts, partialCost, nil, nil
}

func removeExprsInSelection(sc *stmtctx.StatementContext, remainedFilters []expression.Expression, filtersInSelection []expression.Expression) (res []expression.Expression) {
for _, e1 := range remainedFilters {
remove := false
for _, e2 := range filtersInSelection {
if bytes.Equal(e1.HashCode(sc), e2.HashCode(sc)) {
remove = true
}
}
if !remove {
res = append(res, e1)
}
sel := PhysicalSelection{Conditions: tableFilters}.Init(ts.ctx, ts.stats.ScaleByExpectCnt(selectivity*totalRowCount), ts.blockOffset)
sel.SetChildren(ts)
return sel, partialCost, nil
}
return ts, partialCost, nil
return res
}

func indexCoveringCol(col *expression.Column, indexCols []*expression.Column, idxColLens []int) bool {
Expand Down
26 changes: 26 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4954,6 +4954,32 @@ func (s *testIntegrationSuite) TestIssue30094(c *C) {
))
}

func (s *testIntegrationSuite) TestIssue30200(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1(c1 varchar(100), c2 varchar(100), key(c1), key(c2), c3 varchar(100));")
tk.MustExec("insert into t1 values('ab', '10', '10');")
// lpad has not been pushed to TiKV or TiFlash.
tk.MustQuery("explain format=brief select /*+ use_index_merge(t1) */ * from t1 where c1 = 'ab' or c2 = '10' and char_length(lpad(c1, 10, 'a')) = 10;").Check(testkit.Rows(
"Selection 8000.00 root or(eq(test.t1.c1, \"ab\"), and(eq(test.t1.c2, \"10\"), eq(char_length(lpad(test.t1.c1, 10, \"a\")), 10)))",
Copy link
Collaborator Author

@guo-shaoge guo-shaoge Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The estRow of Selection is defult row count. It's unexpected because its child's estRow is 19.99.

Why: IndexMerge's stats is calculated using selectivility of c1 = 'ab' or c2 = '10', and it's stored in path.CountAfterAccess. (check #19040). So the ds.stats is not changed. That's why Selection's stat is a defalut value.

"└─IndexMerge 19.99 root ",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t1, index:c1(c1) range:[\"ab\",\"ab\"], keep order:false, stats:pseudo",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t1, index:c2(c2) range:[\"10\",\"10\"], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 19.99 cop[tikv] table:t1 keep order:false, stats:pseudo"))
tk.MustQuery("select /*+ use_index_merge(t1) */ 1 from t1 where c1 = 'de' or c2 = '10' and char_length(lpad(c1, 10, 'a')) = 10;").Check(testkit.Rows("1"))

// left has not been pushed to TiKV, but it has been pushed to TiFlash.
tk.MustQuery("explain format=brief select /*+ use_index_merge(t1) */ * from t1 where c1 = 'ab' or c2 = '10' and char_length(left(c1, 10)) = 10;").Check(testkit.Rows(
"Selection 0.04 root or(eq(test.t1.c1, \"ab\"), and(eq(test.t1.c2, \"10\"), eq(char_length(left(test.t1.c1, 10)), 10)))",
"└─IndexMerge 19.99 root ",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t1, index:c1(c1) range:[\"ab\",\"ab\"], keep order:false, stats:pseudo",
" ├─IndexRangeScan(Build) 10.00 cop[tikv] table:t1, index:c2(c2) range:[\"10\",\"10\"], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 19.99 cop[tikv] table:t1 keep order:false, stats:pseudo"))
tk.MustQuery("select /*+ use_index_merge(t1) */ 1 from t1 where c1 = 'ab' or c2 = '10' and char_length(left(c1, 10)) = 10;").Check(testkit.Rows("1"))
}

func (s *testIntegrationSuite) TestIssue29705(c *C) {
tk := testkit.NewTestKit(c, s.store)
origin := tk.MustQuery("SELECT @@session.tidb_partition_prune_mode")
Expand Down
10 changes: 5 additions & 5 deletions planner/core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (ds *DataSource) DeriveStats(childStats []*property.StatsInfo, selfSchema *
isReadOnlyTxn = false
}
// Consider the IndexMergePath. Now, we just generate `IndexMergePath` in DNF case.
isPossibleIdxMerge := len(ds.pushedDownConds) > 0 && len(ds.possibleAccessPaths) > 1
isPossibleIdxMerge := len(ds.allConds) > 0 && len(ds.possibleAccessPaths) > 1
sessionAndStmtPermission := (ds.ctx.GetSessionVars().GetEnableIndexMerge() || len(ds.indexMergeHints) > 0) && !ds.ctx.GetSessionVars().StmtCtx.NoIndexMergeHint
// If there is an index path, we current do not consider `IndexMergePath`.
needConsiderIndexMerge := true
Expand Down Expand Up @@ -520,7 +520,7 @@ func (is *LogicalIndexScan) DeriveStats(childStats []*property.StatsInfo, selfSc
// getIndexMergeOrPath generates all possible IndexMergeOrPaths.
func (ds *DataSource) generateIndexMergeOrPaths() error {
usedIndexCount := len(ds.possibleAccessPaths)
for i, cond := range ds.pushedDownConds {
for i, cond := range ds.allConds {
sf, ok := cond.(*expression.ScalarFunction)
if !ok || sf.FuncName.L != ast.LogicOr {
continue
Expand Down Expand Up @@ -696,12 +696,12 @@ func (ds *DataSource) buildIndexMergePartialPath(indexAccessPaths []*util.Access
// buildIndexMergeOrPath generates one possible IndexMergePath.
func (ds *DataSource) buildIndexMergeOrPath(partialPaths []*util.AccessPath, current int) *util.AccessPath {
indexMergePath := &util.AccessPath{PartialIndexPaths: partialPaths}
indexMergePath.TableFilters = append(indexMergePath.TableFilters, ds.pushedDownConds[:current]...)
indexMergePath.TableFilters = append(indexMergePath.TableFilters, ds.pushedDownConds[current+1:]...)
indexMergePath.TableFilters = append(indexMergePath.TableFilters, ds.allConds[:current]...)
indexMergePath.TableFilters = append(indexMergePath.TableFilters, ds.allConds[current+1:]...)
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 {
indexMergePath.TableFilters = append(indexMergePath.TableFilters, ds.pushedDownConds[current])
indexMergePath.TableFilters = append(indexMergePath.TableFilters, ds.allConds[current])
break
}
}
Expand Down
8 changes: 6 additions & 2 deletions planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ func (t *copTask) convertToRootTaskImpl(ctx sessionctx.Context) *rootTask {
setTableScanToTableRowIDScan(p.tablePlan)
newTask.p = p
p.cost = newTask.cost()
t.handleRootTaskConds(ctx, newTask)
if t.needExtraProj {
schema := t.originSchema
proj := PhysicalProjection{Exprs: expression.Column2Exprs(schema.Columns)}.Init(ctx, p.stats, t.idxMergePartPlans[0].SelectBlockOffset(), nil)
Expand Down Expand Up @@ -1066,6 +1067,11 @@ func (t *copTask) convertToRootTaskImpl(ctx sessionctx.Context) *rootTask {
}
}

t.handleRootTaskConds(ctx, newTask)
return newTask
}

func (t *copTask) handleRootTaskConds(ctx sessionctx.Context, newTask *rootTask) {
if len(t.rootTaskConds) > 0 {
selectivity, _, err := t.tblColHists.Selectivity(ctx, t.rootTaskConds, nil)
if err != nil {
Expand All @@ -1077,8 +1083,6 @@ func (t *copTask) convertToRootTaskImpl(ctx sessionctx.Context) *rootTask {
newTask.p = sel
sel.cost = newTask.cost()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we add a new Selection operator, guess we should update newTask's cost?
newTask.addCost(t.count() * sessVars.CPUFactor) just like PhysicalSelection.attach2Task did.
@winoros

}

return newTask
}

// setTableScanToTableRowIDScan is to update the isChildOfIndexLookUp attribute of PhysicalTableScan child
Expand Down