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: check readEngines when building plan for index hint (#15723) #15773

Merged
merged 3 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 39 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,45 @@ func (s *testIntegrationSuite) TestReadFromStorageHintAndIsolationRead(c *C) {
}
}

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

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, index idx(a));")

// Create virtual tiflash replica info.
dom := domain.GetDomain(tk.Se)
is := dom.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test"))
c.Assert(exists, IsTrue)
for _, tblInfo := range db.Tables {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}

tk.MustExec("set @@session.tidb_isolation_read_engines=\"tiflash\"")
var input []string
var output []struct {
SQL string
Plan []string
Warn []string
}
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery(tt).Rows())
output[i].Warn = s.testData.ConvertSQLWarnToStrings(tk.Se.GetSessionVars().StmtCtx.GetWarnings())
})
res := tk.MustQuery(tt)
res.Check(testkit.Rows(output[i].Plan...))
c.Assert(s.testData.ConvertSQLWarnToStrings(tk.Se.GetSessionVars().StmtCtx.GetWarnings()), DeepEquals, output[i].Warn)
}
}

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

Expand Down
12 changes: 12 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,25 @@ func (b *PlanBuilder) getPossibleAccessPaths(indexHints []*ast.IndexHint, tblInf
}
}

_, isolationReadEnginesHasTiKV := b.ctx.GetSessionVars().GetIsolationReadEngines()[kv.TiKV]
for i, hint := range indexHints {
if hint.HintScope != ast.HintForScan {
continue
}

hasScanHint = true

if !isolationReadEnginesHasTiKV {
if hint.IndexNames != nil {
engineVals, _ := b.ctx.GetSessionVars().GetSystemVar(variable.TiDBIsolationReadEngines)
err := errors.New(fmt.Sprintf("TiDB doesn't support index in the isolation read engines(value: '%v')", engineVals))
if i < indexHintsLen {
return nil, err
}
b.ctx.GetSessionVars().StmtCtx.AppendWarning(err)
}
continue
}
// It is syntactically valid to omit index_list for USE INDEX, which means “use no indexes”.
// Omitting index_list for FORCE INDEX or IGNORE INDEX is a syntax error.
// See https://dev.mysql.com/doc/refman/8.0/en/index-hints.html.
Expand Down
9 changes: 9 additions & 0 deletions planner/core/testdata/integration_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,14 @@
"desc select /*+ read_from_storage(tikv[t]) */ avg(a) from t",
"desc select /*+ read_from_storage(tiflash[t]) */ avg(a) from t"
]
},
{
"name": "TestIsolationReadTiFlashUseIndexHint",
"cases": [
"explain select * from t",
"explain select * from t use index();",
"explain select /*+ use_index(t, idx)*/ * from t",
"explain select /*+ use_index(t)*/ * from t"
]
}
]
39 changes: 39 additions & 0 deletions planner/core/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,44 @@
]
}
]
},
{
"Name": "TestIsolationReadTiFlashUseIndexHint",
"Cases": [
{
"SQL": "explain select * from t",
"Plan": [
"TableReader_5 10000.00 root data:TableScan_4",
"└─TableScan_4 10000.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo"
],
"Warn": null
},
{
"SQL": "explain select * from t use index();",
"Plan": [
"TableReader_5 10000.00 root data:TableScan_4",
"└─TableScan_4 10000.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo"
],
"Warn": null
},
{
"SQL": "explain select /*+ use_index(t, idx)*/ * from t",
"Plan": [
"TableReader_5 10000.00 root data:TableScan_4",
"└─TableScan_4 10000.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo"
],
"Warn": [
"TiDB doesn't support index in the isolation read engines(value: 'tiflash')"
]
},
{
"SQL": "explain select /*+ use_index(t)*/ * from t",
"Plan": [
"TableReader_5 10000.00 root data:TableScan_4",
"└─TableScan_4 10000.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo"
],
"Warn": null
}
]
}
]