Skip to content

Commit

Permalink
plan: fix insert onduplicate key update (#7406)
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason authored Aug 27, 2018
1 parent a481f2a commit 2d1fae8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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

0 comments on commit 2d1fae8

Please sign in to comment.