diff --git a/executor/insert_test.go b/executor/insert_test.go index 5696f201f7a49..8497b7ac88f43 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -363,6 +363,20 @@ func TestUpdateDuplicateKey(t *testing.T) { require.EqualError(t, err, "[kv:1062]Duplicate entry '1-2-4' for key 'PRIMARY'") } +func TestIssue37187(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + 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, clean := testkit.CreateMockStore(t) defer clean() diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index aab670ad79612..a5d8e2230b61d 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -3824,6 +3824,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) } } }