From d901863643afb1a40ae96b1ee800e8a8c6d5808a Mon Sep 17 00:00:00 2001 From: tangenta Date: Wed, 15 Apr 2020 20:56:37 +0800 Subject: [PATCH] executor: insert negative primary key into auto_random table won't trigger rebase (#15397) --- executor/ddl_test.go | 19 +++++++++++++++++++ executor/insert_common.go | 3 +++ util/testutil/testutil.go | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/executor/ddl_test.go b/executor/ddl_test.go index 4196b9e046721..a40ad59b748a2 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -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) { diff --git a/executor/insert_common.go b/executor/insert_common.go index aa0116e2f6c4d..f36347d2a5761 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -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() diff --git a/util/testutil/testutil.go b/util/testutil/testutil.go index 3ee8439248172..b36578e9dee19 100644 --- a/util/testutil/testutil.go +++ b/util/testutil/testutil.go @@ -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