From cc07b110825eac0528b06738a5d6f9a2d970db88 Mon Sep 17 00:00:00 2001 From: HuaiyuXu <391585975@qq.com> Date: Tue, 27 Aug 2019 14:09:35 +0800 Subject: [PATCH] executor: set the correct stmtCtx for explain statement (#11186) --- executor/executor.go | 13 +++++++------ executor/explainfor_test.go | 20 ++++++++++++++++++++ executor/point_get_test.go | 6 +++--- util/testkit/testkit.go | 14 ++++++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 91391ee3df6ac..2b258f5669cba 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1386,13 +1386,19 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { action.SetLogHook(domain.GetDomain(ctx).ExpensiveQueryHandle().LogOnQueryExceedMemQuota) sc.MemTracker.SetActionOnExceed(action) } - if execStmt, ok := s.(*ast.ExecuteStmt); ok { s, err = getPreparedStmt(execStmt, vars) if err != nil { return } } + // execute missed stmtID uses empty sql + sc.OriginalSQL = s.Text() + if explainStmt, ok := s.(*ast.ExplainStmt); ok { + sc.InExplainStmt = true + sc.CastStrToIntStrict = true + s = explainStmt.Stmt + } // TODO: Many same bool variables here. // We should set only two variables ( // IgnoreErr and StrictSQLMode) to avoid setting the same bool variables and @@ -1454,9 +1460,6 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { } sc.PadCharToFullLength = ctx.GetSessionVars().SQLMode.HasPadCharToFullLengthMode() sc.CastStrToIntStrict = true - case *ast.ExplainStmt: - sc.InExplainStmt = true - sc.CastStrToIntStrict = true case *ast.ShowStmt: sc.IgnoreTruncate = true sc.IgnoreZeroInDate = true @@ -1500,8 +1503,6 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { if err != nil { return err } - // execute missed stmtID uses empty sql - sc.OriginalSQL = s.Text() vars.StmtCtx = sc return } diff --git a/executor/explainfor_test.go b/executor/explainfor_test.go index b1ef27ea1bd19..632874180495b 100644 --- a/executor/explainfor_test.go +++ b/executor/explainfor_test.go @@ -79,3 +79,23 @@ func (s *testSuite) TestExplainFor(c *C) { tkRoot.Se.SetSessionManager(&mockSessionManager1{PS: ps}) tkRoot.MustExec(fmt.Sprintf("explain for connection %d", tkRootProcess.ID)) } + +func (s *testSuite) TestIssue11124(c *C) { + tk := testkit.NewTestKitWithInit(c, s.store) + tk2 := testkit.NewTestKitWithInit(c, s.store) + tk.MustExec("create table kankan1(id int, name text);") + tk.MustExec("create table kankan2(id int, h1 text);") + tk.MustExec("insert into kankan1 values(1, 'a'), (2, 'a');") + tk.MustExec("insert into kankan2 values(2, 'z');") + tk.MustQuery("select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1'") + tkRootProcess := tk.Se.ShowProcess() + ps := []*util.ProcessInfo{tkRootProcess} + tk.Se.SetSessionManager(&mockSessionManager1{PS: ps}) + tk2.Se.SetSessionManager(&mockSessionManager1{PS: ps}) + + rs := tk.MustQuery("explain select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1'").Rows() + rs2 := tk2.MustQuery(fmt.Sprintf("explain for connection %d", tkRootProcess.ID)).Rows() + for i := range rs { + c.Assert(rs[i], DeepEquals, rs2[i]) + } +} diff --git a/executor/point_get_test.go b/executor/point_get_test.go index 25fcee26c8ad7..d888c7417513f 100644 --- a/executor/point_get_test.go +++ b/executor/point_get_test.go @@ -272,7 +272,7 @@ func (s *testPointGetSuite) TestIndexLookupChar(c *C) { // Test truncate with sql mode `PAD_CHAR_TO_FULL_LENGTH`. tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`) tk.MustIndexLookup(`select * from t where a = "aa";`).Check(testkit.Rows(`aa bb`)) - tk.MustIndexLookup(`select * from t where a = "aab";`).Check(testkit.Rows()) + tk.MustTableDual(`select * from t where a = "aab";`).Check(testkit.Rows()) tk.MustExec(`truncate table t;`) tk.MustExec(`insert into t values("a ", "b ");`) @@ -285,9 +285,9 @@ func (s *testPointGetSuite) TestIndexLookupChar(c *C) { // Test trailing spaces with sql mode `PAD_CHAR_TO_FULL_LENGTH`. tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`) - tk.MustIndexLookup(`select * from t where a = "a";`).Check(testkit.Rows()) + tk.MustTableDual(`select * from t where a = "a";`).Check(testkit.Rows()) tk.MustIndexLookup(`select * from t where a = "a ";`).Check(testkit.Rows(`a b`)) - tk.MustIndexLookup(`select * from t where a = "a ";`).Check(testkit.Rows()) + tk.MustTableDual(`select * from t where a = "a ";`).Check(testkit.Rows()) // Test CHAR BINARY. tk.MustExec(`drop table if exists t;`) diff --git a/util/testkit/testkit.go b/util/testkit/testkit.go index 947afe70961de..8caa015e81861 100644 --- a/util/testkit/testkit.go +++ b/util/testkit/testkit.go @@ -199,6 +199,20 @@ func (tk *TestKit) MustIndexLookup(sql string, args ...interface{}) *Result { return tk.MustQuery(sql, args...) } +// MustTableDual checks whether the plan for the sql is TableDual. +func (tk *TestKit) MustTableDual(sql string, args ...interface{}) *Result { + rs := tk.MustQuery("explain "+sql, args...) + hasTableDual := false + for i := range rs.rows { + if strings.Contains(rs.rows[i][0], "TableDual") { + hasTableDual = true + break + } + } + tk.c.Assert(hasTableDual, check.IsTrue) + return tk.MustQuery(sql, args...) +} + // MustPointGet checks whether the plan for the sql is Point_Get. func (tk *TestKit) MustPointGet(sql string, args ...interface{}) *Result { rs := tk.MustQuery("explain "+sql, args...)