Skip to content

Commit

Permalink
executor: insert negative primary key into auto_random table won't tr…
Browse files Browse the repository at this point in the history
…igger rebase (#15397)
  • Loading branch information
tangenta authored Apr 15, 2020
1 parent 3edfffa commit d901863
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
19 changes: 19 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,25 @@ func (s *testAutoRandomSuite) TestAutoRandomBitsData(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, autoid.ErrAutoRandReadFailed.GenWithStackByArgs().Error())
tk.MustExec("drop table t")

// Test insert negative integers explicitly won't trigger rebase.
tk.MustExec("create table t (a bigint primary key auto_random(15), b int)")
for i := 1; i <= 100; i++ {
tk.MustExec("insert into t(b) values (?)", i)
tk.MustExec("insert into t(a, b) values (?, ?)", -i, i)
}
allHandles, err = ddltestutil.ExtractAllTableHandles(tk.Se, "test_auto_random_bits", "t")
c.Assert(err, IsNil)
// orderedHandles should be [-100, -99, ..., -2, -1, 1, 2, ..., 99, 100]
orderedHandles = testutil.ConfigTestUtils.MaskSortHandles(allHandles, 15, mysql.TypeLonglong)
size = int64(len(allHandles))
for i := int64(0); i < 100; i++ {
c.Assert(orderedHandles[i], Equals, i-100)
}
for i := int64(100); i < size; i++ {
c.Assert(orderedHandles[i], Equals, i-99)
}
tk.MustExec("drop table t")
}

func (s *testSuite6) TestMaxHandleAddIndex(c *C) {
Expand Down
3 changes: 3 additions & 0 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,9 @@ func (e *InsertValues) allocAutoRandomID(fieldType *types.FieldType) (int64, err
}

func (e *InsertValues) rebaseAutoRandomID(recordID int64, fieldType *types.FieldType) error {
if recordID < 0 {
return nil
}
alloc := e.Table.Allocators(e.ctx).Get(autoid.AutoRandomType)
tableInfo := e.Table.Meta()

Expand Down
2 changes: 1 addition & 1 deletion util/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (a *autoRandom) RestoreAutoRandomTestConfig() {
globalCfg.AlterPrimaryKey = a.originAlterPrimaryKey
}

// MaskSortHandles masks highest shard_bits numbers of table handles and sort it.
// MaskSortHandles sorts the handles by lowest (fieldTypeBits - 1 - shardBitsCount) bits.
func (a *autoRandom) MaskSortHandles(handles []int64, shardBitsCount int, fieldType byte) []int64 {
typeBitsLength := mysql.DefaultLengthOfMysqlTypes[fieldType] * 8
const signBitCount = 1
Expand Down

0 comments on commit d901863

Please sign in to comment.