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: fixed a bug that prevented SPM from taking effect #23197

Merged
merged 7 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
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)
eurekaka marked this conversation as resolved.
Show resolved Hide resolved
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