From ce531c7f0d3bfb54bd04d2ebf23021b0d4bd552a Mon Sep 17 00:00:00 2001 From: zimulala Date: Thu, 10 Mar 2016 14:27:24 +0800 Subject: [PATCH] executor: fix issue959 and add tests --- executor/executor_test.go | 34 +++++++++++++++++++++++++++++++++- executor/executor_write.go | 3 +++ meta/autoid/autoid.go | 4 ++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index 7dac2ee6ba457..e948ea61368ed 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -351,13 +351,45 @@ func (s *testSuite) TestInsertAutoInc(c *C) { insertSQL := `insert into insert_autoinc_test(c1) values (1), (2)` tk.MustExec(insertSQL) - tk.MustExec("begin") r := tk.MustQuery("select * from insert_autoinc_test;") rowStr1 := fmt.Sprintf("%v %v", "1", "1") rowStr2 := fmt.Sprintf("%v %v", "2", "2") r.Check(testkit.Rows(rowStr1, rowStr2)) tk.MustExec("commit") + + tk.MustExec("begin") + insertSQL = `insert into insert_autoinc_test(id, c1) values (5,5)` + tk.MustExec(insertSQL) + insertSQL = `insert into insert_autoinc_test(c1) values (6)` + tk.MustExec(insertSQL) + tk.MustExec("commit") + tk.MustExec("begin") + r = tk.MustQuery("select * from insert_autoinc_test;") + rowStr3 := fmt.Sprintf("%v %v", "5", "5") + rowStr4 := fmt.Sprintf("%v %v", "6", "6") + r.Check(testkit.Rows(rowStr1, rowStr2, rowStr3, rowStr4)) + tk.MustExec("commit") + + tk.MustExec("begin") + insertSQL = `insert into insert_autoinc_test(id, c1) values (3,3)` + tk.MustExec(insertSQL) + tk.MustExec("commit") + tk.MustExec("begin") + r = tk.MustQuery("select * from insert_autoinc_test;") + rowStr5 := fmt.Sprintf("%v %v", "3", "3") + r.Check(testkit.Rows(rowStr1, rowStr2, rowStr5, rowStr3, rowStr4)) + tk.MustExec("commit") + + tk.MustExec("begin") + insertSQL = `insert into insert_autoinc_test(c1) values (7)` + tk.MustExec(insertSQL) + tk.MustExec("commit") + tk.MustExec("begin") + r = tk.MustQuery("select * from insert_autoinc_test;") + rowStr6 := fmt.Sprintf("%v %v", "7", "7") + r.Check(testkit.Rows(rowStr1, rowStr2, rowStr5, rowStr3, rowStr4, rowStr6)) + tk.MustExec("commit") } func (s *testSuite) TestReplace(c *C) { diff --git a/executor/executor_write.go b/executor/executor_write.go index 784028117bfaa..2d575cf5ae98b 100644 --- a/executor/executor_write.go +++ b/executor/executor_write.go @@ -645,6 +645,9 @@ func (e *InsertValues) initDefaultValues(row []types.Datum, marked map[int]struc var defaultValueCols []*column.Col for i, c := range e.Table.Cols() { if row[i].Kind() != types.KindNull { + if mysql.HasAutoIncrementFlag(c.Flag) { + e.Table.RebaseAutoID(row[i].GetValue().(int64)) + } // Column value is not nil, continue. continue } diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index 5727e90e483ba..8984cb5f007b1 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -41,7 +41,7 @@ type allocator struct { dbID int64 } -// Rebase rebase the autoID base for table with tableID and the new base value. +// Rebase rebases the autoID base for table with tableID and the new base value. func (alloc *allocator) Rebase(tableID, newBase int64) error { if tableID == 0 { return errors.New("Invalid tableID") @@ -119,7 +119,7 @@ type memoryAllocator struct { dbID int64 } -// Rebase rebase the autoID base for table with tableID and the new base value. +// Rebase rebases the autoID base for table with tableID and the new base value. func (alloc *memoryAllocator) Rebase(tableID, newBase int64) error { // TODO: implement it. return nil