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
ti-srebot authored Mar 12, 2021
1 parent 8b9ed63 commit e579d1c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 14 additions & 0 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1865,3 +1865,17 @@ func (s *testSuite) TestCaptureWithZeroSlowLogThreshold(c *C) {
c.Assert(len(rows), Equals, 1)
c.Assert(rows[0][0], Equals, "select * from test . 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")
}
14 changes: 2 additions & 12 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,7 @@ func extractSelectAndNormalizeDigest(stmtNode ast.StmtNode, specifiledDB string)
switch x.Stmt.(type) {
case *ast.SelectStmt, *ast.DeleteStmt, *ast.UpdateStmt, *ast.InsertStmt:
plannercore.EraseLastSemicolon(x)
var normalizeExplainSQL string
if specifiledDB != "" {
normalizeExplainSQL = parser.Normalize(utilparser.RestoreWithDefaultDB(x, specifiledDB))
} else {
normalizeExplainSQL = parser.Normalize(x.Text())
}
normalizeExplainSQL := parser.Normalize(utilparser.RestoreWithDefaultDB(x, specifiledDB))
idx := int(0)
switch n := x.Stmt.(type) {
case *ast.SelectStmt:
Expand Down Expand Up @@ -308,12 +303,7 @@ func extractSelectAndNormalizeDigest(stmtNode ast.StmtNode, specifiledDB string)
if len(x.Text()) == 0 {
return x, "", ""
}
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
}
return nil, "", ""
Expand Down

0 comments on commit e579d1c

Please sign in to comment.