Skip to content

Commit

Permalink
planner: fixed a bug that prevented SPM from taking effect (#23197)
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelice authored Mar 9, 2021
1 parent 5720831 commit 1ca62a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
18 changes: 18 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2061,3 +2061,21 @@ func (s *testSuite) TestExplainTableStmts(c *C) {
tk.MustExec("explain table t")
tk.MustExec("desc table t")
}

func (s *testSuite) TestSPMWithoutUseDatabase(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk1 := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
s.cleanBindingEnv(tk1)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, key(a))")
tk.MustExec("create global binding for select * from t using select * from t force index(a)")

err := tk1.ExecToErr("select * from t")
c.Assert(err, ErrorMatches, "*No database selected")
tk1.MustQuery(`select @@last_plan_from_binding;`).Check(testkit.Rows("0"))
c.Assert(tk1.MustUseIndex("select * from test.t", "a"), IsTrue)
tk1.MustExec("select * from test.t")
tk1.MustQuery(`select @@last_plan_from_binding;`).Check(testkit.Rows("1"))
}
14 changes: 2 additions & 12 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,7 @@ func extractSelectAndNormalizeDigest(stmtNode ast.StmtNode, specifiledDB string)
}
switch x.Stmt.(type) {
case *ast.SelectStmt, *ast.DeleteStmt, *ast.UpdateStmt, *ast.InsertStmt:
var normalizeSQL string
if specifiledDB != "" {
normalizeSQL = parser.Normalize(utilparser.RestoreWithDefaultDB(x.Stmt, specifiledDB))
} else {
normalizeSQL = parser.Normalize(x.Text())
}
normalizeSQL := parser.Normalize(utilparser.RestoreWithDefaultDB(x.Stmt, specifiledDB))
normalizeSQL = plannercore.EraseLastSemicolonInSQL(normalizeSQL)
hash := parser.DigestNormalized(normalizeSQL)
return x.Stmt, normalizeSQL, hash, nil
Expand Down Expand Up @@ -326,12 +321,7 @@ func extractSelectAndNormalizeDigest(stmtNode ast.StmtNode, specifiledDB string)
if len(x.Text()) == 0 {
return x, "", "", nil
}
var normalizedSQL, hash string
if specifiledDB != "" {
normalizedSQL, hash = parser.NormalizeDigest(utilparser.RestoreWithDefaultDB(x, specifiledDB))
} else {
normalizedSQL, hash = parser.NormalizeDigest(x.Text())
}
normalizedSQL, hash := parser.NormalizeDigest(utilparser.RestoreWithDefaultDB(x, specifiledDB))
return x, normalizedSQL, hash, nil
}
return nil, "", "", nil
Expand Down

0 comments on commit 1ca62a0

Please sign in to comment.