From e6d8ad5aa1ceeaffa65aff640f05da321cda890a Mon Sep 17 00:00:00 2001 From: Lynn Date: Tue, 17 Mar 2020 20:22:37 +0800 Subject: [PATCH 1/2] serssion, server: fix the correctness of the query field in the DDL job --- session/session.go | 6 ------ session/session_test.go | 15 +++++++++++++++ session/tidb.go | 7 +++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/session/session.go b/session/session.go index a194a7d616ec8..0109d05703fd9 100644 --- a/session/session.go +++ b/session/session.go @@ -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) diff --git a/session/session_test.go b/session/session_test.go index 658304f01f4cd..0683e814e163e 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -299,6 +299,21 @@ 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) + params := []types.Datum{} + _, err = tk.Se.ExecutePreparedStmt(context.Background(), id, params) + 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) { diff --git a/session/tidb.go b/session/tidb.go index 34515fa182d3b..163ff42c7e07d 100644 --- a/session/tidb.go +++ b/session/tidb.go @@ -251,6 +251,13 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement) defer span1.Finish() ctx = opentracing.ContextWithSpan(ctx, span1) } + sctx.SetValue(sessionctx.QueryString, s.OriginText()) + if _, ok := s.(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. From 54187401217df858f074504e1e91cc9b7931b46d Mon Sep 17 00:00:00 2001 From: Lynn Date: Tue, 17 Mar 2020 23:17:31 +0800 Subject: [PATCH 2/2] session: tiny update --- session/tidb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/session/tidb.go b/session/tidb.go index 163ff42c7e07d..36760c0ee96a6 100644 --- a/session/tidb.go +++ b/session/tidb.go @@ -252,7 +252,7 @@ func runStmt(ctx context.Context, sctx sessionctx.Context, s sqlexec.Statement) ctx = opentracing.ContextWithSpan(ctx, span1) } sctx.SetValue(sessionctx.QueryString, s.OriginText()) - if _, ok := s.(ast.DDLNode); ok { + if _, ok := s.(*executor.ExecStmt).StmtNode.(ast.DDLNode); ok { sctx.SetValue(sessionctx.LastExecuteDDL, true) } else { sctx.ClearValue(sessionctx.LastExecuteDDL)