Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: fix sequence cache negative problem (#18071) #18103

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}