Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#37924
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
Reminiscent authored and ti-chi-bot committed Dec 12, 2022
1 parent 825f4c8 commit cf370d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,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)
>>>>>>> d5b2c9b17d (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);`)
Expand Down
5 changes: 5 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3155,6 +3155,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)
}
}
}
Expand Down

0 comments on commit cf370d3

Please sign in to comment.