Skip to content

Commit

Permalink
planner: do not push keep descending order to tiflash (pingcap#13572)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzmhhh123 committed Jan 19, 2020
1 parent 12a9adf commit 35fe5a3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
15 changes: 15 additions & 0 deletions cmd/explaintest/r/access_tiflash.result
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ id count task operator info
TableReader_7 44.00 root data:Selection_6
└─Selection_6 44.00 cop[tiflash] or(and(gt(Column#1, 1), lt(Column#1, 20)), and(ge(Column#1, 30), lt(Column#1, 55)))
└─TableScan_5 44.00 cop[tiflash] table:tt, range:[-inf,+inf], keep order:false, stats:pseudo
drop table if exists ttt;
create table ttt (a int, primary key (a desc));
desc select * from ttt order by ttt.a desc;
id count task operator info
TableReader_11 10000.00 root data:TableScan_10
└─TableScan_10 10000.00 cop[tikv] table:ttt, range:[-inf,+inf], keep order:true, desc, stats:pseudo
desc select /*+ read_from_storage(tiflash[ttt]) */ * from ttt order by ttt.a desc;
id count task operator info
Sort_4 10000.00 root test.ttt.a:desc
└─TableReader_8 10000.00 root data:TableScan_7
└─TableScan_7 10000.00 cop[tiflash] table:ttt, range:[-inf,+inf], keep order:false, stats:pseudo
desc select /*+ read_from_storage(tiflash[ttt]) */ * from ttt order by ttt.a;
id count task operator info
TableReader_11 10000.00 root data:TableScan_10
└─TableScan_10 10000.00 cop[tiflash] table:ttt, range:[-inf,+inf], keep order:true, stats:pseudo
9 changes: 8 additions & 1 deletion cmd/explaintest/t/access_tiflash.test
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ desc select /*+ read_from_storage(tiflash[t]) */ sum(isnull(a)) from t;
create table tt(a int, b int, primary key(a));

desc select * from tt where (tt.a > 1 and tt.a < 20) or (tt.a >= 30 and tt.a < 55);
desc select /*+ read_from_storage(tiflash[tt]) */ * from tt where (tt.a > 1 and tt.a < 20) or (tt.a >= 30 and tt.a < 55);
desc select /*+ read_from_storage(tiflash[tt]) */ * from tt where (tt.a > 1 and tt.a < 20) or (tt.a >= 30 and tt.a < 55);

drop table if exists ttt;
create table ttt (a int, primary key (a desc));

desc select * from ttt order by ttt.a desc;
desc select /*+ read_from_storage(tiflash[ttt]) */ * from ttt order by ttt.a desc;
desc select /*+ read_from_storage(tiflash[ttt]) */ * from ttt order by ttt.a;
19 changes: 12 additions & 7 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ func compareCandidates(lhs, rhs *candidatePath) int {
func (ds *DataSource) getTableCandidate(path *accessPath, prop *property.PhysicalProperty) *candidatePath {
candidate := &candidatePath{path: path}
pkCol := ds.getPKIsHandleCol()
candidate.isMatchProp = len(prop.Items) == 1 && pkCol != nil && prop.Items[0].Col.Equal(nil, pkCol)
if len(prop.Items) == 1 && pkCol != nil {
candidate.isMatchProp = prop.Items[0].Col.Equal(nil, pkCol)
if path.storeType == kv.TiFlash {
candidate.isMatchProp = candidate.isMatchProp && !prop.Items[0].Desc
}
}
candidate.columnSet = expression.ExtractColumnSet(path.accessConds)
candidate.isSingleScan = true
return candidate
Expand Down Expand Up @@ -415,6 +420,12 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty) (t task, err
}, nil
}
if path.isTablePath {
if ds.preferStoreType&preferTiFlash != 0 && path.storeType == kv.TiKV {
continue
}
if ds.preferStoreType&preferTiKV != 0 && path.storeType == kv.TiFlash {
continue
}
tblTask, err := ds.convertToTableScan(prop, candidate)
if err != nil {
return nil, err
Expand Down Expand Up @@ -837,12 +848,6 @@ func (ds *DataSource) convertToTableScan(prop *property.PhysicalProperty, candid
filterCondition: path.tableFilters,
StoreType: path.storeType,
}.Init(ds.ctx)
if ds.preferStoreType&preferTiFlash != 0 {
ts.StoreType = kv.TiFlash
}
if ds.preferStoreType&preferTiKV != 0 {
ts.StoreType = kv.TiKV
}
if ts.StoreType == kv.TiFlash {
ts.filterCondition = append(ts.filterCondition, ts.AccessCondition...)
ts.AccessCondition = nil
Expand Down
21 changes: 17 additions & 4 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/expression/aggregation"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/privilege"
Expand Down Expand Up @@ -396,18 +397,30 @@ func (ds *DataSource) setPreferredStoreType(hintInfo *tableHintInfo) {
} else {
alias = &ds.tableInfo.Name
}
if hintInfo.ifPreferTiFlash(alias) {
ds.preferStoreType |= preferTiFlash
}
if hintInfo.ifPreferTiKV(alias) {
ds.preferStoreType |= preferTiKV
}
if hintInfo.ifPreferTiFlash(alias) {
if ds.preferStoreType != 0 {
errMsg := fmt.Sprintf("Storage hints are conflict, you can only specify one storage type of table %s", alias.L)
warning := ErrInternal.GenWithStack(errMsg)
ds.ctx.GetSessionVars().StmtCtx.AppendWarning(warning)
ds.preferStoreType = 0
return
}
ds.preferStoreType |= preferTiKV
ds.preferStoreType |= preferTiFlash
hasTiFlashPath := false
for _, path := range ds.possibleAccessPaths {
if path.storeType == kv.TiFlash {
hasTiFlashPath = true
break
}
}
// TODO: For now, if there is a TiFlash hint for a table, we enforce a TiFlash path. But hint is just a suggestion
// for the planner. We can keep it since we need it to debug with PD and TiFlash. In future, this should be removed.
if !hasTiFlashPath {
ds.possibleAccessPaths = append(ds.possibleAccessPaths, &accessPath{isTablePath: true, storeType: kv.TiFlash})
}
}
}

Expand Down

0 comments on commit 35fe5a3

Please sign in to comment.