From 2e96fbb0d144ac9bc2f7b7b3a86048ab5a94df1a Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Thu, 5 Jan 2023 15:43:40 +0000 Subject: [PATCH 1/2] executor: reset the related session vars for both INSERT and REPLACE --- executor/executor.go | 2 ++ executor/insert.go | 2 -- executor/writetest/write_test.go | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 1679ed9e57e12..ae7a7383f5983 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -2186,6 +2186,8 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { vars.PrevFoundInBinding = vars.FoundInBinding vars.FoundInBinding = false vars.DurationWaitTS = 0 + vars.CurrInsertBatchExtraCols = nil + vars.CurrInsertValues = chunk.Row{} return } diff --git a/executor/insert.go b/executor/insert.go index d0b09e302860c..1a4eb27d3c6f6 100644 --- a/executor/insert.go +++ b/executor/insert.go @@ -345,8 +345,6 @@ func (e *InsertExec) Close() error { defer e.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.RegisterStats(e.id, e.stats) } defer e.memTracker.ReplaceBytesUsed(0) - 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/writetest/write_test.go b/executor/writetest/write_test.go index 32939a1b16033..6dfe0285433b0 100644 --- a/executor/writetest/write_test.go +++ b/executor/writetest/write_test.go @@ -4318,3 +4318,15 @@ 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 := testkit.CreateMockStore(t) + 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")) +} From aae4a2e82a3547136e9bcd7d57b20a7b67d290a1 Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Thu, 5 Jan 2023 17:05:45 +0000 Subject: [PATCH 2/2] add test according to comment --- executor/writetest/write_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/executor/writetest/write_test.go b/executor/writetest/write_test.go index 6dfe0285433b0..f6846a944a486 100644 --- a/executor/writetest/write_test.go +++ b/executor/writetest/write_test.go @@ -4329,4 +4329,12 @@ func TestMutipleReplaceAndInsertInOneSession(t *testing.T) { 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")) }