Skip to content

Commit

Permalink
executor: handle update case of autoid bug(pingcap#11109), and add mo…
Browse files Browse the repository at this point in the history
…re test
  • Loading branch information
nange authored and lzmhhh123 committed Jul 23, 2019
1 parent 672eec8 commit 7ed8e9e
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 6 deletions.
11 changes: 7 additions & 4 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,10 @@ func (e *InsertValues) adjustAutoIncrementDatum(d types.Datum, hasValue bool, c
d.SetNull()
}
if !d.IsNull() {
recordID = getAutoRecordID(d, &c.FieldType)
recordID, err = getAutoRecordID(d, &c.FieldType)
if err != nil {
return types.Datum{}, err
}
}
// Use the value if it's not null and not 0.
if recordID != 0 {
Expand Down Expand Up @@ -507,7 +510,7 @@ func (e *InsertValues) adjustAutoIncrementDatum(d types.Datum, hasValue bool, c
return casted, nil
}

func getAutoRecordID(d types.Datum, target *types.FieldType) int64 {
func getAutoRecordID(d types.Datum, target *types.FieldType) (int64, error) {
var recordID int64

switch target.Tp {
Expand All @@ -517,10 +520,10 @@ func getAutoRecordID(d types.Datum, target *types.FieldType) int64 {
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
recordID = d.GetInt64()
default:
panic("should never happen")
return 0, errors.Errorf("unexpected field type [%v]", target.Tp)
}

return recordID
return recordID, nil
}

func (e *InsertValues) handleWarning(err error, logInfo string) {
Expand Down
45 changes: 45 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,31 @@ func (s *testSuite3) TestInsertWithAutoidSchema(c *C) {
`select * from t1 where id = 4`,
testkit.Rows(`4 5`),
},
{
`insert into t1(id, n) values('5', 6)`,
`select * from t1 where id = 5`,
testkit.Rows(`5 6`),
},
{
`insert into t1(n) values(7)`,
`select * from t1 where id = 6`,
testkit.Rows(`6 7`),
},
{
`insert into t1(id, n) values(7.4, 8)`,
`select * from t1 where id = 7`,
testkit.Rows(`7 8`),
},
{
`insert into t1(id, n) values(7.5, 9)`,
`select * from t1 where id = 8`,
testkit.Rows(`8 9`),
},
{
`insert into t1(n) values(9)`,
`select * from t1 where id = 8`,
testkit.Rows(`8 9`),
},
{
`insert into t2(id, n) values(1, 1)`,
`select * from t2 where id = 1`,
Expand Down Expand Up @@ -323,6 +348,16 @@ func (s *testSuite3) TestInsertWithAutoidSchema(c *C) {
`select * from t4 where id = 6`,
testkit.Rows(`6 6`),
},
{
`insert into t4(id, n) values(7, '7.7')`,
`select * from t4 where id = 7`,
testkit.Rows(`7 7.7`),
},
{
`insert into t4(id) values(8)`,
`select * from t4 where id = 8`,
testkit.Rows(`8 8`),
},
{
`insert into t5(id, n) values(1, 1)`,
`select * from t5 where id = 1`,
Expand Down Expand Up @@ -368,6 +403,16 @@ func (s *testSuite3) TestInsertWithAutoidSchema(c *C) {
`select * from t6 where id = 6`,
testkit.Rows(`6 6`),
},
{
`insert into t6(id, n) values(7, '7.7')`,
`select * from t4 where id = 7`,
testkit.Rows(`7 7.7`),
},
{
`insert into t6(id) values(8)`,
`select * from t4 where id = 8`,
testkit.Rows(`8 8`),
},
{
`insert into t7(id, n) values(1, 1)`,
`select * from t7 where id = 1`,
Expand Down
95 changes: 95 additions & 0 deletions executor/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,98 @@ func (s *testUpdateSuite) TestUpdateGenColInTxn(c *C) {
tk.MustQuery(`select * from t;`).Check(testkit.Rows(
`1 2`))
}

func (s *testUpdateSuite) TestUpdateWithAutoidSchema(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test`)
tk.MustExec(`create table t1(id int primary key auto_increment, n int);`)
tk.MustExec(`create table t2(id int primary key, n float auto_increment, key I_n(n));`)
tk.MustExec(`create table t3(id int primary key, n double auto_increment, key I_n(n));`)

tests := []struct {
exec string
query string
result [][]interface{}
}{
{
`insert into t1 set n = 1`,
`select * from t1 where id = 1`,
testkit.Rows(`1 1`),
},
{
`update t1 set id = id+1`,
`select * from t1 where id = 2`,
testkit.Rows(`2 1`),
},
{
`insert into t1 set n = 2`,
`select * from t1 where id = 3`,
testkit.Rows(`3 2`),
},
{
`update t1 set id = id + '1.1' where id = 3`,
`select * from t1 where id = 4`,
testkit.Rows(`4 2`),
},
{
`insert into t1 set n = 3`,
`select * from t1 where id = 5`,
testkit.Rows(`5 3`),
},
{
`insert into t2 set id = 1`,
`select * from t2 where id = 1`,
testkit.Rows(`1 1`),
},
{
`update t2 set n = n+1`,
`select * from t2 where id = 1`,
testkit.Rows(`1 2`),
},
{
`insert into t2 set id = 2`,
`select * from t2 where id = 2`,
testkit.Rows(`2 3`),
},
{
`update t2 set n = n + '2.2'`,
`select * from t2 where id = 2`,
testkit.Rows(`2 5.2`),
},
{
`insert into t2 set id = 3`,
`select * from t2 where id = 3`,
testkit.Rows(`3 6`),
},
{
`insert into t3 set id = 1`,
`select * from t3 where id = 1`,
testkit.Rows(`1 1`),
},
{
`update t3 set n = n+1`,
`select * from t3 where id = 1`,
testkit.Rows(`1 2`),
},
{
`insert into t3 set id = 2`,
`select * from t3 where id = 2`,
testkit.Rows(`2 3`),
},
{
`update t3 set n = n + '3.3'`,
`select * from t3 where id = 2`,
testkit.Rows(`2 6.3`),
},
{
`insert into t3 set id = 3`,
`select * from t3 where id = 3`,
testkit.Rows(`3 7`),
},
}

for _, tt := range tests {
tk.MustExec(tt.exec)
tk.MustQuery(tt.query).Check(tt.result)
}
}
8 changes: 6 additions & 2 deletions executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ func updateRecord(ctx sessionctx.Context, h int64, oldData, newData []types.Datu
modified[i] = true
// Rebase auto increment id if the field is changed.
if mysql.HasAutoIncrementFlag(col.Flag) {
if err = t.RebaseAutoID(ctx, newData[i].GetInt64(), true); err != nil {
return false, false, 0, errors.Trace(err)
recordID, err := getAutoRecordID(newData[i], &col.FieldType)
if err != nil {
return false, false, 0, err
}
if err = t.RebaseAutoID(ctx, recordID, true); err != nil {
return false, false, 0, err
}
}
if col.IsPKHandleColumn(t.Meta()) {
Expand Down

0 comments on commit 7ed8e9e

Please sign in to comment.