Skip to content

Commit

Permalink
planner: make hint read_from_storage take effect for tikv (pingcap#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lzmhhh123 committed Jan 19, 2020
1 parent 67b66c7 commit 12a9adf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions planner/core/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,10 @@ func (s *testAnalyzeSuite) TestTiFlashCostModel(c *C) {
"TableReader_7 10000.00 root data:TableScan_6",
"└─TableScan_6 10000.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo",
))
tk.MustQuery("desc select /*+ read_from_storage(tikv[t]) */ * from t").Check(testkit.Rows(
"TableReader_5 10000.00 root data:TableScan_4",
"└─TableScan_4 10000.00 cop[tikv] table:t, range:[-inf,+inf], keep order:false, stats:pseudo",
))
tk.MustQuery("desc select * from t where t.a = 1 or t.a = 2").Check(testkit.Rows(
"TableReader_6 2.00 root data:TableScan_5",
"└─TableScan_5 2.00 cop[tikv] table:t, range:[1,1], [2,2], keep order:false, stats:pseudo",
Expand Down
21 changes: 18 additions & 3 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,16 @@ func (ds *DataSource) setPreferredStoreType(hintInfo *tableHintInfo) {
if hintInfo.ifPreferTiFlash(alias) {
ds.preferStoreType |= preferTiFlash
}
if hintInfo.ifPreferTiKV(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
}
}

func resetNotNullFlag(schema *expression.Schema, start, end int) {
Expand Down Expand Up @@ -1991,7 +2001,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, nodeType n
var (
sortMergeTables, INLJTables, hashJoinTables []hintTableInfo
indexHintList []indexHintInfo
tiflashTables []hintTableInfo
tiflashTables, tikvTables []hintTableInfo
aggHints aggHintInfo
)
for _, hint := range hints {
Expand Down Expand Up @@ -2034,18 +2044,23 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, nodeType n
if hint.StoreType.L == HintTiFlash {
tiflashTables = tableNames2HintTableInfo(hint.Tables)
}
if hint.StoreType.L == HintTiKV {
tikvTables = tableNames2HintTableInfo(hint.Tables)
}
default:
// ignore hints that not implemented
}
}
if len(sortMergeTables)+len(INLJTables)+len(hashJoinTables)+len(indexHintList)+len(tiflashTables) > 0 || aggHints.preferAggType != 0 || aggHints.preferAggToCop {
if len(sortMergeTables)+len(INLJTables)+len(hashJoinTables)+len(indexHintList)+len(tiflashTables)+len(tikvTables) > 0 ||
aggHints.preferAggType != 0 || aggHints.preferAggToCop {
b.tableHintInfo = append(b.tableHintInfo, tableHintInfo{
sortMergeJoinTables: sortMergeTables,
indexNestedLoopJoinTables: INLJTables,
hashJoinTables: hashJoinTables,
indexHintList: indexHintList,
aggHints: aggHints,
flashTables: tiflashTables,
tiflashTables: tiflashTables,
tikvTables: tikvTables,
})
return true
}
Expand Down
9 changes: 7 additions & 2 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ type tableHintInfo struct {
sortMergeJoinTables []hintTableInfo
hashJoinTables []hintTableInfo
indexHintList []indexHintInfo
flashTables []hintTableInfo
tiflashTables []hintTableInfo
tikvTables []hintTableInfo
aggHints aggHintInfo
}

Expand Down Expand Up @@ -103,7 +104,11 @@ func (info *tableHintInfo) ifPreferINLJ(tableNames ...*model.CIStr) bool {
}

func (info *tableHintInfo) ifPreferTiFlash(tableNames ...*model.CIStr) bool {
return info.matchTableName(tableNames, info.flashTables)
return info.matchTableName(tableNames, info.tiflashTables)
}

func (info *tableHintInfo) ifPreferTiKV(tableNames ...*model.CIStr) bool {
return info.matchTableName(tableNames, info.tikvTables)
}

// matchTableName checks whether the hint hit the need.
Expand Down

0 comments on commit 12a9adf

Please sign in to comment.