From ee7b18fa3f7866b95b13577f9843b6ef6e5b7cb3 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 13 Jan 2023 20:23:47 +0800 Subject: [PATCH] executor: reset the related session vars for both INSERT and REPLACE (#40354) (#40385) close pingcap/tidb#40351 --- executor/executor.go | 2 ++ executor/insert.go | 2 -- executor/write_test.go | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 2996bc65a6635..a45d33b280653 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1980,6 +1980,8 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { vars.ClearStmtVars() vars.PrevFoundInBinding = vars.FoundInBinding vars.FoundInBinding = false + vars.CurrInsertBatchExtraCols = nil + vars.CurrInsertValues = chunk.Row{} return } diff --git a/executor/insert.go b/executor/insert.go index a391d6412aae8..3c9689e1749fc 100644 --- a/executor/insert.go +++ b/executor/insert.go @@ -315,8 +315,6 @@ func (e *InsertExec) Next(ctx context.Context, req *chunk.Chunk) error { // Close implements the Executor Close interface. func (e *InsertExec) Close() error { - e.ctx.GetSessionVars().CurrInsertValues = chunk.Row{} - e.ctx.GetSessionVars().CurrInsertBatchExtraCols = e.ctx.GetSessionVars().CurrInsertBatchExtraCols[0:0:0] e.setMessage() if e.SelectExec != nil { return e.SelectExec.Close() diff --git a/executor/write_test.go b/executor/write_test.go index ae61dba931d2d..27cbe84a71b6f 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -4376,3 +4376,24 @@ func TestIssueInsertPrefixIndexForNonUTF8Collation(t *testing.T) { tk.MustExec("insert into t3 select 'abc '") tk.MustGetErrCode("insert into t3 select 'abc d'", 1062) } + +func TestMutipleReplaceAndInsertInOneSession(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t_securities(id bigint not null auto_increment primary key, security_id varchar(8), market_id smallint, security_type int, unique key uu(security_id, market_id))") + tk.MustExec(`insert into t_securities (security_id, market_id, security_type) values ("1", 2, 7), ("7", 1, 7) ON DUPLICATE KEY UPDATE security_type = VALUES(security_type)`) + tk.MustExec(`replace into t_securities (security_id, market_id, security_type) select security_id+1, 1, security_type from t_securities where security_id="7";`) + tk.MustExec(`INSERT INTO t_securities (security_id, market_id, security_type) values ("1", 2, 7), ("7", 1, 7) ON DUPLICATE KEY UPDATE security_type = VALUES(security_type)`) + + tk.MustQuery("select * from t_securities").Sort().Check(testkit.Rows("1 1 2 7", "2 7 1 7", "3 8 1 7")) + + tk2 := testkit.NewTestKit(t, store) + tk2.MustExec("use test") + tk2.MustExec(`insert into t_securities (security_id, market_id, security_type) values ("1", 2, 7), ("7", 1, 7) ON DUPLICATE KEY UPDATE security_type = VALUES(security_type)`) + tk2.MustExec(`insert into t_securities (security_id, market_id, security_type) select security_id+2, 1, security_type from t_securities where security_id="7";`) + tk2.MustExec(`INSERT INTO t_securities (security_id, market_id, security_type) values ("1", 2, 7), ("7", 1, 7) ON DUPLICATE KEY UPDATE security_type = VALUES(security_type)`) + + tk2.MustQuery("select * from t_securities").Sort().Check(testkit.Rows("1 1 2 7", "2 7 1 7", "3 8 1 7", "8 9 1 7")) +}