Skip to content

Commit

Permalink
ddl: reject alter auto_random_base on a non auto_random table (#17714) (
Browse files Browse the repository at this point in the history
#17827)

* cherry pick #17714 to release-3.1

Signed-off-by: sre-bot <sre-bot@pingcap.com>

* resolve conflicts

Co-authored-by: tangenta <tangenta@126.com>
Co-authored-by: Arenatlx <ailinsilence4@gmail.com>
Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
  • Loading branch information
4 people authored Jun 29, 2020
1 parent d8ea508 commit c1f3909
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
21 changes: 14 additions & 7 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2150,20 +2150,27 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6
if err != nil {
return errors.Trace(err)
}
autoIncID, err := t.Allocator(ctx, tp).NextGlobalAutoID(t.Meta().ID)
var actionType model.ActionType
switch tp {
case autoid.AutoRandomType:
if t.Meta().AutoRandomBits == 0 {
return errors.Trace(ErrInvalidAutoRandom.GenWithStackByArgs(autoid.AutoRandomRebaseNotApplicable))
}
actionType = model.ActionRebaseAutoRandomBase
case autoid.RowIDAllocType:
actionType = model.ActionRebaseAutoID
}

autoID, err := t.Allocator(ctx, tp).NextGlobalAutoID(t.Meta().ID)
if err != nil {
return errors.Trace(err)
}
// If newBase < autoIncID, we need to do a rebase before returning.
// If newBase < autoID, we need to do a rebase before returning.
// Assume there are 2 TiDB servers: TiDB-A with allocator range of 0 ~ 30000; TiDB-B with allocator range of 30001 ~ 60000.
// If the user sends SQL `alter table t1 auto_increment = 100` to TiDB-B,
// and TiDB-B finds 100 < 30001 but returns without any handling,
// then TiDB-A may still allocate 99 for auto_increment column. This doesn't make sense for the user.
newBase = mathutil.MaxInt64(newBase, autoIncID)
actionType := model.ActionRebaseAutoID
if tp == autoid.AutoRandomType {
actionType = model.ActionRebaseAutoRandomBase
}
newBase = mathutil.MaxInt64(newBase, autoID)
job := &model.Job{
SchemaID: schema.ID,
TableID: t.Meta().ID,
Expand Down
6 changes: 6 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,12 @@ func (s *testAutoRandomSuite) TestAutoRandomTableOption(c *C) {
c.Assert(orderedHandles[i], Equals, i+3000000)
}
tk.MustExec("drop table alter_table_auto_random_option")

// Alter auto_random_base on non auto_random table.
tk.MustExec("create table alter_auto_random_normal (a int)")
_, err = tk.Exec("alter table alter_auto_random_normal auto_random_base = 100")
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), autoid.AutoRandomRebaseNotApplicable), IsTrue, Commentf(err.Error()))
}

// Test filter different kind of allocators.
Expand Down
2 changes: 2 additions & 0 deletions meta/autoid/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ const (
AutoRandomExplicitInsertDisabledErrMsg = "Explicit insertion on auto_random column is disabled. Try to set @@allow_auto_random_explicit_insert = true."
// AutoRandomOnNonBigIntColumn is reported when define auto random to non bigint column
AutoRandomOnNonBigIntColumn = "auto_random option must be defined on `bigint` column, but not on `%s` column"
// AutoRandomRebaseNotApplicable is reported when alter auto_random base on a non auto_random table.
AutoRandomRebaseNotApplicable = "alter auto_random_base of a non auto_random table"
)

0 comments on commit c1f3909

Please sign in to comment.