Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix panic when prepare and execute the insert on duplicate (#37924) #38102

Merged
merged 4 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ func (s *testSuite3) TestUpdateDuplicateKey(c *C) {
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1-2-4' for key 'PRIMARY'")
}

func (s *testSuite3) TestIssue37187(c *C) {
tk := testkit.NewTestKit(c, s.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;")
c.Assert(err, IsNil)
}

func (s *testSuite3) TestInsertWrongValueForField(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
5 changes: 5 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3428,6 +3428,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