Skip to content

Commit

Permalink
planner: correct the dbName for hint (#15319)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu authored Mar 17, 2020
1 parent 544e45e commit 5268094
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
48 changes: 48 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,3 +1277,51 @@ func (s *testPlanSuite) TestNominalSort(c *C) {
tk.MustQuery(ts).Check(testkit.Rows(output[i].Result...))
}
}

func (s *testPlanSuite) TestHintFromDiffDatabase(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer func() {
dom.Close()
store.Close()
}()
se, err := session.CreateSession4Test(store)
c.Assert(err, IsNil)
ctx := context.Background()
_, err = se.Execute(ctx, "use test")
c.Assert(err, IsNil)
_, err = se.Execute(ctx, `drop table if exists test.t1`)
c.Assert(err, IsNil)
_, err = se.Execute(ctx, `create table test.t1(a bigint, index idx_a(a));`)
c.Assert(err, IsNil)
_, err = se.Execute(ctx, `create table test.t2(a bigint, index idx_a(a));`)
c.Assert(err, IsNil)

_, err = se.Execute(ctx, "drop database if exists test2")
c.Assert(err, IsNil)
_, err = se.Execute(ctx, "create database test2")
c.Assert(err, IsNil)
_, err = se.Execute(ctx, "use test2")
c.Assert(err, IsNil)

var input []string
var output []struct {
SQL string
Plan string
}
is := domain.GetDomain(se).InfoSchema()
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
comment := Commentf("case:%v sql: %s", i, tt)
stmt, err := s.ParseOneStmt(tt, "", "")
c.Assert(err, IsNil, comment)
p, _, err := planner.Optimize(ctx, se, stmt, is)
c.Assert(err, IsNil, comment)
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = core.ToString(p)
})
c.Assert(core.ToString(p), Equals, output[i].Plan, comment)
}
}
2 changes: 1 addition & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func tableNames2HintTableInfo(ctx sessionctx.Context, hintTables []ast.HintTable
hintTableInfos := make([]hintTableInfo, len(hintTables))
defaultDBName := model.NewCIStr(ctx.GetSessionVars().CurrentDB)
for i, hintTable := range hintTables {
tableInfo := hintTableInfo{tblName: hintTable.TableName, selectOffset: p.getHintOffset(hintTable.QBName, nodeType, currentOffset)}
tableInfo := hintTableInfo{dbName: hintTable.DBName, tblName: hintTable.TableName, selectOffset: p.getHintOffset(hintTable.QBName, nodeType, currentOffset)}
if tableInfo.dbName.L == "" {
tableInfo.dbName = defaultDBName
}
Expand Down
6 changes: 6 additions & 0 deletions planner/core/testdata/plan_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -569,5 +569,11 @@
"select /*+ INL_HASH_JOIN(t1) */ t1.b, t2.b from t1 inner join t2 on t1.a = t2.a;",
"select /*+ INL_MERGE_JOIN(t1) */ t1.b, t2.b from t1 inner join t2 on t1.a = t2.a;"
]
},
{
"name": "TestHintFromDiffDatabase",
"cases": [
"select /*+ inl_hash_join(test.t1) */ * from test.t2 join test.t1 on test.t2.a = test.t1.a"
]
}
]
9 changes: 9 additions & 0 deletions planner/core/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -1661,5 +1661,14 @@
"Hints": "USE_INDEX(@`sel_1` `test`.`t1` `idx_a`), USE_INDEX(@`sel_1` `test`.`t2` )"
}
]
},
{
"Name": "TestHintFromDiffDatabase",
"Cases": [
{
"SQL": "select /*+ inl_hash_join(test.t1) */ * from test.t2 join test.t1 on test.t2.a = test.t1.a",
"Plan": "IndexHashJoin{IndexReader(Index(t2.idx_a)[[-inf,+inf]])->IndexReader(Index(t1.idx_a)[[NULL,+inf]]->Sel([not(isnull(test.t1.a))]))}(test.t2.a,test.t1.a)"
}
]
}
]

0 comments on commit 5268094

Please sign in to comment.