Skip to content

Commit

Permalink
executor: set the correct stmtCtx for explain statement (#11186) (#11978
Browse files Browse the repository at this point in the history
)
  • Loading branch information
XuHuaiyu authored and jackysp committed Sep 2, 2019
1 parent 969db3b commit 59edb77
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
7 changes: 6 additions & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,13 +1346,18 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
default:
sc.MemTracker.SetActionOnExceed(&memory.LogOnExceed{})
}

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.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
Expand Down
6 changes: 3 additions & 3 deletions executor/point_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (s *testPointGetSuite) TestIndexLookupCharPK(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 tmp where a = "aa";`).Check(testkit.Rows(`aa bb`))
tk.MustIndexLookup(`select * from t tmp where a = "aab";`).Check(testkit.Rows())
tk.MustTableDual(`select * from t tmp where a = "aab";`).Check(testkit.Rows())

tk.MustExec(`truncate table t;`)
tk.MustExec(`insert into t values("a ", "b ");`)
Expand All @@ -189,9 +189,9 @@ func (s *testPointGetSuite) TestIndexLookupCharPK(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 tmp where a = "a";`).Check(testkit.Rows())
tk.MustTableDual(`select * from t tmp where a = "a";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())
tk.MustTableDual(`select * from t tmp where a = "a ";`).Check(testkit.Rows())

// Test CHAR BINARY.
tk.MustExec(`drop table if exists t;`)
Expand Down
14 changes: 14 additions & 0 deletions util/testkit/testkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,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...)
Expand Down

0 comments on commit 59edb77

Please sign in to comment.