Skip to content

Commit

Permalink
serssion, server: fix the correctness of the query field in the DDL j…
Browse files Browse the repository at this point in the history
…ob (#15435) (#15444)
  • Loading branch information
sre-bot authored Mar 24, 2020
1 parent f3de03d commit 31c7959
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 0 additions & 6 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,12 +1030,6 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu
}

func (s *session) executeStatement(ctx context.Context, connID uint64, stmtNode ast.StmtNode, stmt sqlexec.Statement, recordSets []sqlexec.RecordSet, inMulitQuery bool) ([]sqlexec.RecordSet, error) {
s.SetValue(sessionctx.QueryString, stmt.OriginText())
if _, ok := stmtNode.(ast.DDLNode); ok {
s.SetValue(sessionctx.LastExecuteDDL, true)
} else {
s.ClearValue(sessionctx.LastExecuteDDL)
}
logStmt(stmtNode, s.sessionVars)
startTime := time.Now()
recordSet, err := runStmt(ctx, s, stmt)
Expand Down
14 changes: 14 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ func (s *testSessionSuite) TestQueryString(c *C) {
tk.MustExec("create table mutil1 (a int);create table multi2 (a int)")
queryStr := tk.Se.Value(sessionctx.QueryString)
c.Assert(queryStr, Equals, "create table multi2 (a int)")

// Test execution of DDL through the "ExecutePreparedStmt" interface.
_, err := tk.Se.Execute(context.Background(), "use test;")
c.Assert(err, IsNil)
_, err = tk.Se.Execute(context.Background(), "CREATE TABLE t (id bigint PRIMARY KEY, age int)")
c.Assert(err, IsNil)
_, err = tk.Se.Execute(context.Background(), "show create table t")
c.Assert(err, IsNil)
id, _, _, err := tk.Se.PrepareStmt("CREATE TABLE t2(id bigint PRIMARY KEY, age int)")
c.Assert(err, IsNil)
_, err = tk.Se.ExecutePreparedStmt(context.Background(), id)
c.Assert(err, IsNil)
qs := tk.Se.Value(sessionctx.QueryString)
c.Assert(qs.(string), Equals, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)")
}

func (s *testSessionSuite) TestAffectedRows(c *C) {
Expand Down
7 changes: 7 additions & 0 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement)
span1.LogKV("sql", s.OriginText())
defer span1.Finish()
}
sctx.SetValue(sessionctx.QueryString, s.OriginText())
if _, ok := s.(*executor.ExecStmt).StmtNode.(ast.DDLNode); ok {
sctx.SetValue(sessionctx.LastExecuteDDL, true)
} else {
sctx.ClearValue(sessionctx.LastExecuteDDL)
}

se := sctx.(*session)
sessVars := se.GetSessionVars()
// Save origTxnCtx here to avoid it reset in the transaction retry.
Expand Down

0 comments on commit 31c7959

Please sign in to comment.