Skip to content

Commit

Permalink
planner: fix tiflash path will be identified as table dual (#49338) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Dec 22, 2023
1 parent 8299b27 commit af392b9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
7 changes: 7 additions & 0 deletions planner/core/enforce_mpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestEnforceMPP(t *testing.T) {
tk.MustExec("create table t(a int, b int)")
tk.MustExec("create index idx on t(a)")
tk.MustExec("CREATE TABLE `s` (\n `a` int(11) DEFAULT NULL,\n `b` int(11) DEFAULT NULL,\n `c` int(11) DEFAULT NULL,\n `d` int(11) DEFAULT NULL,\n UNIQUE KEY `a` (`a`),\n KEY `ii` (`a`,`b`)\n)")
tk.MustExec("create table t3(id int, sala char(10), name char(100), primary key(id, sala)) partition by list columns (sala)(partition p1 values in('a'));")

// Default RPC encoding may cause statistics explain result differ and then the test unstable.
tk.MustExec("set @@tidb_enable_chunk_rpc = on")
Expand All @@ -118,6 +119,12 @@ func TestEnforceMPP(t *testing.T) {
Available: true,
}
}
if tblInfo.Name.L == "t3" {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
}

var input []string
Expand Down
24 changes: 23 additions & 1 deletion planner/core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,22 @@ func (ds *DataSource) derivePathStatsAndTryHeuristics() error {
selected, uniqueBest, refinedBest *util.AccessPath
isRefinedPath bool
)
// step1: if user prefer tiFlash store type, tiFlash path should always be built anyway ahead.
var tiflashPath *util.AccessPath
if ds.preferStoreType&preferTiFlash != 0 {
for _, path := range ds.possibleAccessPaths {
if path.StoreType == kv.TiFlash {
err := ds.deriveTablePathStats(path, ds.pushedDownConds, false)
if err != nil {
return err
}
path.IsSingleScan = true
tiflashPath = path
break
}
}
}
// step2: kv path should follow the heuristic rules.
for _, path := range ds.possibleAccessPaths {
if path.IsTablePath() {
err := ds.deriveTablePathStats(path, ds.pushedDownConds, false)
Expand All @@ -303,7 +319,9 @@ func (ds *DataSource) derivePathStatsAndTryHeuristics() error {
ds.deriveIndexPathStats(path, ds.pushedDownConds, false)
path.IsSingleScan = ds.isSingleScan(path.FullIdxCols, path.FullIdxColLens)
}
// step: 3
// Try some heuristic rules to select access path.
// tiFlash path also have table-range-scan (range point like here) to be heuristic treated.
if len(path.Ranges) == 0 {
selected = path
break
Expand Down Expand Up @@ -366,9 +384,13 @@ func (ds *DataSource) derivePathStatsAndTryHeuristics() error {
// heuristic rule pruning other path should consider hint prefer.
// If no hints and some path matches a heuristic rule, just remove other possible paths.
if selected != nil {
// if user wanna tiFlash read, while current heuristic choose a TiKV path. so we shouldn't prune other paths.
ds.possibleAccessPaths[0] = selected
ds.possibleAccessPaths = ds.possibleAccessPaths[:1]
// if user wanna tiFlash read, while current heuristic choose a TiKV path. so we shouldn't prune tiFlash path.
keep := ds.preferStoreType&preferTiFlash != 0 && selected.StoreType != kv.TiFlash
if keep {
// also keep tiflash path as well.
ds.possibleAccessPaths = append(ds.possibleAccessPaths, tiflashPath)
return nil
}
ds.possibleAccessPaths[0] = selected
Expand Down
4 changes: 3 additions & 1 deletion planner/core/testdata/enforce_mpp_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"explain format='verbose' select count(*) from t where a=1",
"explain format='verbose' select /*+ read_from_storage(tikv[t]) */ count(*) from t where a=1",
"explain format='verbose' select /*+ read_from_storage(tiflash[t]) */ count(*) from t where a=1",
"explain select /*+ READ_FROM_STORAGE(TIFLASH[s]) */ a from s where a = 10 and b is null; -- index path huristic rule will prune tiflash path"
"explain select /*+ READ_FROM_STORAGE(TIFLASH[s]) */ a from s where a = 10 and b is null; -- index path huristic rule will prune tiflash path",
"explain select /*+ read_from_storage(tiflash[t3]) */ * from t3 where sala='a' and id =1; -- once hinted, walk with tiflash range scan",
"explain select * from t3 where sala='a' and id =1; -- once not hinted, walk with tikv point get"
]
},
{
Expand Down
28 changes: 24 additions & 4 deletions planner/core/testdata/enforce_mpp_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,33 @@
{
"SQL": "explain select /*+ READ_FROM_STORAGE(TIFLASH[s]) */ a from s where a = 10 and b is null; -- index path huristic rule will prune tiflash path",
"Plan": [
"TableReader_12 0.10 root data:ExchangeSender_11",
"└─ExchangeSender_11 0.10 mpp[tiflash] ExchangeType: PassThrough",
"TableReader_9 0.10 root data:ExchangeSender_8",
"└─ExchangeSender_8 0.10 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection_5 0.10 mpp[tiflash] test.s.a",
" └─Selection_10 0.10 mpp[tiflash] eq(test.s.a, 10), isnull(test.s.b)",
" └─TableFullScan_9 10000.00 mpp[tiflash] table:s keep order:false, stats:pseudo"
" └─Selection_7 0.10 mpp[tiflash] eq(test.s.a, 10), isnull(test.s.b)",
" └─TableFullScan_6 10000.00 mpp[tiflash] table:s keep order:false, stats:pseudo"
],
"Warn": null
},
{
"SQL": "explain select /*+ read_from_storage(tiflash[t3]) */ * from t3 where sala='a' and id =1; -- once hinted, walk with tiflash range scan",
"Plan": [
"TableReader_9 1.00 root data:TableRangeScan_8",
"└─TableRangeScan_8 1.00 cop[tiflash] table:t3, partition:p1 range:[1 \"a\",1 \"a\"], keep order:false, stats:pseudo"
],
"Warn": [
"disable dynamic pruning due to t3 has no global stats",
"MPP mode may be blocked because table `t3`is a partition table which is not supported when `@@tidb_partition_prune_mode=static`."
]
},
{
"SQL": "explain select * from t3 where sala='a' and id =1; -- once not hinted, walk with tikv point get",
"Plan": [
"Point_Get_6 1.00 root table:t3, partition:p1, clustered index:PRIMARY(id, sala) "
],
"Warn": [
"disable dynamic pruning due to t3 has no global stats"
]
}
]
},
Expand Down

0 comments on commit af392b9

Please sign in to comment.