Skip to content

Commit

Permalink
executor: fix bug when auto_increment meets unsigned. (pingcap#4824)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Oct 19, 2017
1 parent 1a175b5 commit f6eafec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
18 changes: 15 additions & 3 deletions executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,11 @@ func (e *InsertValues) adjustAutoIncrementDatum(row []types.Datum, i int, c *tab
if err != nil {
return errors.Trace(err)
}
row[i].SetInt64(id)
if mysql.HasUnsignedFlag(c.Flag) {
row[i].SetUint64(uint64(id))
} else {
row[i].SetInt64(id)
}
return nil
}

Expand All @@ -1107,7 +1111,11 @@ func (e *InsertValues) adjustAutoIncrementDatum(row []types.Datum, i int, c *tab
return errors.Trace(err)
}
e.ctx.GetSessionVars().InsertID = uint64(recordID)
row[i].SetInt64(recordID)
if mysql.HasUnsignedFlag(c.Flag) {
row[i].SetUint64(uint64(recordID))
} else {
row[i].SetInt64(recordID)
}
retryInfo.AddAutoIncrementID(recordID)
return nil
}
Expand All @@ -1125,7 +1133,11 @@ func (e *InsertValues) adjustAutoIncrementDatum(row []types.Datum, i int, c *tab
}
}

row[i].SetInt64(recordID)
if mysql.HasUnsignedFlag(c.Flag) {
row[i].SetUint64(uint64(recordID))
} else {
row[i].SetInt64(recordID)
}
retryInfo.AddAutoIncrementID(recordID)
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ func (s *testSuite) TestInsert(c *C) {
tk.MustExec("set sql_mode = 'strict_all_tables';")
r = tk.MustQuery("SELECT * FROM t;")
r.Check(testkit.Rows("2017-00-00 00:00:00"))

// test auto_increment with unsigned.
tk.MustExec("drop table if exists test")
tk.MustExec("CREATE TABLE test(id int(10) UNSIGNED NOT NULL AUTO_INCREMENT, p int(10) UNSIGNED NOT NULL, PRIMARY KEY(p), KEY(id))")
tk.MustExec("insert into test(p) value(1)")
tk.MustQuery("select * from test").Check(testkit.Rows("1 1"))
tk.MustQuery("select * from test use index (id) where id = 1").Check(testkit.Rows("1 1"))
tk.MustExec("insert into test values(NULL, 2)")
tk.MustQuery("select * from test use index (id) where id = 2").Check(testkit.Rows("2 2"))
tk.MustExec("insert into test values(2, 3)")
tk.MustQuery("select * from test use index (id) where id = 2").Check(testkit.Rows("2 2", "2 3"))
}

func (s *testSuite) TestInsertAutoInc(c *C) {
Expand Down

0 comments on commit f6eafec

Please sign in to comment.