Skip to content

Commit

Permalink
fix sequence cache can be negative (pingcap#18071)
Browse files Browse the repository at this point in the history
  • Loading branch information
AilinKid authored Jun 17, 2020
1 parent a142759 commit 096f2e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ddl/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func validateSequenceOptions(seqInfo *model.SequenceInfo) bool {
// Increment shouldn't be set as 0.
return false
}
if seqInfo.CacheValue <= 0 {
// Cache value should be bigger than 0.
return false
}
maxIncrement = math2.Abs(seqInfo.Increment)

return seqInfo.MaxValue >= seqInfo.Start &&
Expand Down
27 changes: 27 additions & 0 deletions ddl/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,3 +972,30 @@ func (s *testSequenceSuite) TestSequenceDefaultLogic(c *C) {
s.tk.MustGetErrMsg("alter table t add column b int default next value for seq", "[ddl:8230]Unsupported using sequence as default value in add column 'b'")
s.tk.MustQuery("select * from t").Check(testkit.Rows("-1", "-1", "-1"))
}

// Close issue #17945, sequence cache shouldn't be negative.
func (s *testSequenceSuite) TestSequenceCacheShouldNotBeNegative(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")

s.tk.MustExec("drop sequence if exists seq")
_, err := s.tk.Exec("create sequence seq cache -1")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:4136]Sequence 'test.seq' values are conflicting")

_, err = s.tk.Exec("create sequence seq cache 0")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:4136]Sequence 'test.seq' values are conflicting")

// This will error because
// 1: maxvalue = -1 by default
// 2: minvalue = -9223372036854775807 by default
// 3: increment = -9223372036854775807 by user
// `seqInfo.CacheValue < (math.MaxInt64-absIncrement)/absIncrement` will
// ensure there is enough value for one cache allocation at least.
_, err = s.tk.Exec("create sequence seq INCREMENT -9223372036854775807 cache 1")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:4136]Sequence 'test.seq' values are conflicting")

s.tk.MustExec("create sequence seq cache 1")
}

0 comments on commit 096f2e8

Please sign in to comment.