From ec1870711c6413b62759ee5590629dda067a6a90 Mon Sep 17 00:00:00 2001 From: Chengpeng Yan <41809508+Reminiscent@users.noreply.github.com> Date: Thu, 22 Sep 2022 18:11:03 +0800 Subject: [PATCH 1/4] cherry pick #37924 to release-5.4 Signed-off-by: ti-srebot --- executor/insert_test.go | 19 +++++++++++++++++++ planner/core/planbuilder.go | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/executor/insert_test.go b/executor/insert_test.go index 906777ac05e05..a02e1d1baedd7 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -356,8 +356,27 @@ func (s *testSuite3) TestUpdateDuplicateKey(c *C) { c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1-2-4' for key 'PRIMARY'") } +<<<<<<< HEAD func (s *testSuite3) TestInsertWrongValueForField(c *C) { tk := testkit.NewTestKit(c, s.store) +======= +func TestIssue37187(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + tk.MustExec("drop table if exists a, b") + tk.MustExec("create table t1 (a int(11) ,b varchar(100) ,primary key (a));") + tk.MustExec("create table t2 (c int(11) ,d varchar(100) ,primary key (c));") + tk.MustExec("prepare in1 from 'insert into t1 (a,b) select c,null from t2 t on duplicate key update b=t.d';") + err := tk.ExecToErr("execute in1;") + require.NoError(t, err) +} + +func TestInsertWrongValueForField(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) +>>>>>>> d5b2c9b17... planner: fix panic when prepare and execute the insert on duplicate (#37924) tk.MustExec("use test") tk.MustExec(`drop table if exists t1;`) tk.MustExec(`create table t1(a bigint);`) diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index c7addcaf222d3..8ec0f34e68710 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -3798,6 +3798,11 @@ func (b *PlanBuilder) buildSelectPlanOfInsert(ctx context.Context, insert *ast.I } sel.Fields.Fields = append(sel.Fields.Fields, &ast.SelectField{Expr: colName, Offset: len(sel.Fields.Fields)}) } + defer func(originSelFieldLen int) { + // Revert the change for ast. Because when we use the 'prepare' and 'execute' statement it will both build plan which will cause problem. + // You can see the issue #37187 for more details. + sel.Fields.Fields = sel.Fields.Fields[:originSelFieldLen] + }(actualColLen) } } } From 4bcfe8a3d418a9496ec1ba7bc7c914bccb5ab1e1 Mon Sep 17 00:00:00 2001 From: Reminiscent Date: Thu, 22 Sep 2022 20:27:24 +0800 Subject: [PATCH 2/4] resolve conflicts --- executor/insert_test.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/executor/insert_test.go b/executor/insert_test.go index a02e1d1baedd7..ec54dfdf1b698 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -356,13 +356,8 @@ func (s *testSuite3) TestUpdateDuplicateKey(c *C) { c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1-2-4' for key 'PRIMARY'") } -<<<<<<< HEAD -func (s *testSuite3) TestInsertWrongValueForField(c *C) { +func (s *testSuite3) TestIssue37187(c *C) { tk := testkit.NewTestKit(c, s.store) -======= -func TestIssue37187(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("drop table if exists a, b") @@ -370,13 +365,11 @@ func TestIssue37187(t *testing.T) { tk.MustExec("create table t2 (c int(11) ,d varchar(100) ,primary key (c));") tk.MustExec("prepare in1 from 'insert into t1 (a,b) select c,null from t2 t on duplicate key update b=t.d';") err := tk.ExecToErr("execute in1;") - require.NoError(t, err) + c.Assert(err, NotNil) } -func TestInsertWrongValueForField(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) ->>>>>>> d5b2c9b17... planner: fix panic when prepare and execute the insert on duplicate (#37924) +func (s *testSuite3) TestInsertWrongValueForField(c *C) { + tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec(`drop table if exists t1;`) tk.MustExec(`create table t1(a bigint);`) From 2ab2f247fb524208e81c435251e9542a58e7cfe5 Mon Sep 17 00:00:00 2001 From: Reminiscent Date: Fri, 23 Sep 2022 15:13:11 +0800 Subject: [PATCH 3/4] fixut --- executor/insert_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/insert_test.go b/executor/insert_test.go index ec54dfdf1b698..8e83f03e0ffbf 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -365,7 +365,7 @@ func (s *testSuite3) TestIssue37187(c *C) { tk.MustExec("create table t2 (c int(11) ,d varchar(100) ,primary key (c));") tk.MustExec("prepare in1 from 'insert into t1 (a,b) select c,null from t2 t on duplicate key update b=t.d';") err := tk.ExecToErr("execute in1;") - c.Assert(err, NotNil) + c.Assert(err, nil) } func (s *testSuite3) TestInsertWrongValueForField(c *C) { From 5cbcf148a1b341a4441736358ec78ebfed892cf4 Mon Sep 17 00:00:00 2001 From: Reminiscent Date: Fri, 23 Sep 2022 15:42:10 +0800 Subject: [PATCH 4/4] fixut --- executor/insert_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/executor/insert_test.go b/executor/insert_test.go index 8e83f03e0ffbf..a0294f48b03a5 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -365,7 +365,7 @@ func (s *testSuite3) TestIssue37187(c *C) { tk.MustExec("create table t2 (c int(11) ,d varchar(100) ,primary key (c));") tk.MustExec("prepare in1 from 'insert into t1 (a,b) select c,null from t2 t on duplicate key update b=t.d';") err := tk.ExecToErr("execute in1;") - c.Assert(err, nil) + c.Assert(err, IsNil) } func (s *testSuite3) TestInsertWrongValueForField(c *C) {