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

plan: fix insert onduplicate key update #7406

Merged
merged 6 commits into from
Aug 27, 2018
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
14 changes: 14 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ func (s *testSuite) TestInsertOnDuplicateKey(c *C) {
tk.MustQuery(`select * from t1`).Check(testkit.Rows("1 400"))
tk.MustExec(`insert into t1 select * from t2 on duplicate key update b1 = values(b1) + b2;`)
tk.MustQuery(`select * from t1`).Check(testkit.Rows("1 400"))

tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(k1 bigint, k2 bigint, val bigint, primary key(k1, k2));`)
tk.MustExec(`insert into t (val, k1, k2) values (3, 1, 2);`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 2 3`))
tk.MustExec(`insert into t (val, k1, k2) select c, a, b from (select 1 as a, 2 as b, 4 as c) tmp on duplicate key update val = tmp.c;`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 2 4`))

tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(k1 double, k2 double, v double, primary key(k1, k2));`)
tk.MustExec(`insert into t (v, k1, k2) select c, a, b from (select "3" c, "1" a, "2" b) tmp on duplicate key update v=c;`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 2 3`))
tk.MustExec(`insert into t (v, k1, k2) select c, a, b from (select "3" c, "1" a, "2" b) tmp on duplicate key update v=c;`)
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 2 3`))
}

func (s *testSuite) TestInsertWrongValueForField(c *C) {
Expand Down
18 changes: 17 additions & 1 deletion plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,23 @@ func (b *planBuilder) buildSelectPlanOfInsert(insert *ast.InsertStmt, insertPlan
return errors.Trace(err)
}

insertPlan.Schema4OnDuplicate = expression.MergeSchema(insertPlan.tableSchema, insertPlan.SelectPlan.Schema())
// schema4NewRow is the schema for the newly created data record based on
// the result of the select statement.
schema4NewRow := expression.NewSchema(make([]*expression.Column, len(insertPlan.Table.Cols()))...)
for i, selCol := range insertPlan.SelectPlan.Schema().Columns {
ordinal := affectedValuesCols[i].Offset
schema4NewRow.Columns[ordinal] = &expression.Column{}
*schema4NewRow.Columns[ordinal] = *selCol

schema4NewRow.Columns[ordinal].RetType = &types.FieldType{}
*schema4NewRow.Columns[ordinal].RetType = affectedValuesCols[i].FieldType
}
for i := range schema4NewRow.Columns {
if schema4NewRow.Columns[i] == nil {
schema4NewRow.Columns[i] = &expression.Column{UniqueID: insertPlan.ctx.GetSessionVars().AllocPlanColumnID()}
}
}
insertPlan.Schema4OnDuplicate = expression.MergeSchema(insertPlan.tableSchema, schema4NewRow)
return nil
}

Expand Down