From e579d1cde98c4971ea4bca966a2eb7ccde75445e Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Fri, 12 Mar 2021 19:36:56 +0800 Subject: [PATCH] planner: fixed a bug that prevented SPM from taking effect (#23197) (#23209) --- bindinfo/bind_test.go | 14 ++++++++++++++ planner/optimize.go | 14 ++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bindinfo/bind_test.go b/bindinfo/bind_test.go index 5daecc922a765..d0df1d6f54591 100644 --- a/bindinfo/bind_test.go +++ b/bindinfo/bind_test.go @@ -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") +} diff --git a/planner/optimize.go b/planner/optimize.go index 0eefceffc2262..bf08b87813124 100644 --- a/planner/optimize.go +++ b/planner/optimize.go @@ -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: @@ -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, "", ""