Skip to content

Commit e1067d1

Browse files
authored
*: fix 'Duplicate entry' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626) (#53114)
close #52622
1 parent 42669ad commit e1067d1

File tree

4 files changed

+72
-19
lines changed

4 files changed

+72
-19
lines changed

autoid_service/autoid.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (alloc *autoIDValue) rebase4Unsigned(ctx context.Context,
182182
return nil
183183
}
184184
// Satisfied by alloc.end, need to update alloc.base.
185-
if requiredBase <= uint64(alloc.end) {
185+
if requiredBase > uint64(alloc.base) && requiredBase <= uint64(alloc.end) {
186186
alloc.base = int64(requiredBase)
187187
return nil
188188
}
@@ -225,7 +225,7 @@ func (alloc *autoIDValue) rebase4Signed(ctx context.Context, store kv.Storage, d
225225
return nil
226226
}
227227
// Satisfied by alloc.end, need to update alloc.base.
228-
if requiredBase <= alloc.end {
228+
if requiredBase > alloc.base && requiredBase <= alloc.end {
229229
alloc.base = requiredBase
230230
return nil
231231
}
@@ -461,6 +461,7 @@ func (s *Service) allocAutoID(ctx context.Context, req *autoid.AutoIDRequest) (*
461461
if err1 != nil {
462462
return err1
463463
}
464+
val.base = currentEnd
464465
val.end = currentEnd
465466
return nil
466467
})

executor/autoidtest/autoid_test.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ func TestAutoIDIncrementAndOffset(t *testing.T) {
606606
for _, str := range []string{"", " AUTO_ID_CACHE 1"} {
607607
tk.MustExec(`create table io (a int key auto_increment)` + str)
608608
tk.MustExec(`insert into io values (null),(null),(null)`)
609-
tk.MustQuery(`select * from io`).Check(testkit.Rows("10", "15", "20"))
609+
tk.MustQuery(`select * from io`).Check(testkit.Rows("1", "6", "11"))
610610
tk.MustExec(`drop table io`)
611611
}
612612

@@ -616,23 +616,23 @@ func TestAutoIDIncrementAndOffset(t *testing.T) {
616616
tk.Session().GetSessionVars().AutoIncrementOffset = 10
617617
tk.Session().GetSessionVars().AutoIncrementIncrement = 2
618618
tk.MustExec(`insert into io values (),(),()`)
619-
tk.MustQuery(`select * from io`).Check(testkit.Rows("10", "12", "14"))
619+
tk.MustQuery(`select * from io`).Check(testkit.Rows("1", "3", "5"))
620620
tk.MustExec(`delete from io`)
621621

622622
// Test reset the increment.
623623
tk.Session().GetSessionVars().AutoIncrementIncrement = 5
624624
tk.MustExec(`insert into io values (),(),()`)
625-
tk.MustQuery(`select * from io`).Check(testkit.Rows("15", "20", "25"))
625+
tk.MustQuery(`select * from io`).Check(testkit.Rows("6", "11", "16"))
626626
tk.MustExec(`delete from io`)
627627

628628
tk.Session().GetSessionVars().AutoIncrementIncrement = 10
629629
tk.MustExec(`insert into io values (),(),()`)
630-
tk.MustQuery(`select * from io`).Check(testkit.Rows("30", "40", "50"))
630+
tk.MustQuery(`select * from io`).Check(testkit.Rows("20", "30", "40"))
631631
tk.MustExec(`delete from io`)
632632

633633
tk.Session().GetSessionVars().AutoIncrementIncrement = 5
634634
tk.MustExec(`insert into io values (),(),()`)
635-
tk.MustQuery(`select * from io`).Check(testkit.Rows("55", "60", "65"))
635+
tk.MustQuery(`select * from io`).Check(testkit.Rows("41", "46", "51"))
636636
tk.MustExec(`drop table io`)
637637
}
638638

@@ -643,10 +643,10 @@ func TestAutoIDIncrementAndOffset(t *testing.T) {
643643
tk.MustExec(`create table io (a int, b int auto_increment, key(b))` + str)
644644
tk.MustExec(`insert into io(b) values (null),(null),(null)`)
645645
// AutoID allocation will take increment and offset into consideration.
646-
tk.MustQuery(`select b from io`).Check(testkit.Rows("10", "12", "14"))
646+
tk.MustQuery(`select b from io`).Check(testkit.Rows("1", "3", "5"))
647647
if str == "" {
648648
// HandleID allocation will ignore the increment and offset.
649-
tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("15", "16", "17"))
649+
tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("6", "7", "8"))
650650
} else {
651651
// Separate row id and auto inc id, increment and offset works on auto inc id
652652
tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("1", "2", "3"))
@@ -655,9 +655,9 @@ func TestAutoIDIncrementAndOffset(t *testing.T) {
655655

656656
tk.Session().GetSessionVars().AutoIncrementIncrement = 10
657657
tk.MustExec(`insert into io(b) values (null),(null),(null)`)
658-
tk.MustQuery(`select b from io`).Check(testkit.Rows("20", "30", "40"))
658+
tk.MustQuery(`select b from io`).Check(testkit.Rows("10", "20", "30"))
659659
if str == "" {
660-
tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("41", "42", "43"))
660+
tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("31", "32", "33"))
661661
} else {
662662
tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("4", "5", "6"))
663663
}
@@ -768,3 +768,43 @@ func TestIssue39528(t *testing.T) {
768768
// Make sure the code does not visit tikv on allocate path.
769769
require.False(t, codeRun)
770770
}
771+
772+
func TestIssue52622(t *testing.T) {
773+
store := testkit.CreateMockStore(t)
774+
775+
tk := testkit.NewTestKit(t, store)
776+
tk.MustExec("use test")
777+
tk.MustExec(`set @@auto_increment_increment = 66;`)
778+
tk.MustExec(`set @@auto_increment_offset = 9527;`)
779+
780+
tk.MustQuery(`select @@auto_increment_increment;`).Check(testkit.Rows("66"))
781+
tk.MustQuery(`select @@auto_increment_offset;`).Check(testkit.Rows("9527"))
782+
783+
for i := 0; i < 2; i++ {
784+
createTableSQL := "create table issue52622 (id int primary key auto_increment, k int)"
785+
if i == 0 {
786+
createTableSQL = createTableSQL + " AUTO_ID_CACHE 1"
787+
}
788+
789+
tk.MustExec(createTableSQL)
790+
tk.MustExec("insert into issue52622 (k) values (1),(2),(3);")
791+
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3"))
792+
if i == 0 {
793+
tk.MustQuery("show create table issue52622").CheckContain("134")
794+
}
795+
tk.MustExec("insert into issue52622 (k) values (4);")
796+
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3", "199 4"))
797+
798+
tk.MustExec("truncate table issue52622;")
799+
tk.MustExec("insert into issue52622 (k) values (1)")
800+
tk.MustExec("insert into issue52622 (k) values (2)")
801+
tk.MustExec("insert into issue52622 (k) values (3)")
802+
if i == 0 {
803+
tk.MustQuery("show create table issue52622").CheckContain("134")
804+
}
805+
tk.MustExec("insert into issue52622 (k) values (4);")
806+
tk.MustQuery("select * from issue52622").Check(testkit.Rows("1 1", "67 2", "133 3", "199 4"))
807+
808+
tk.MustExec("drop table issue52622;")
809+
}
810+
}

table/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ go_library(
2222
"//parser/types",
2323
"//sessionctx",
2424
"//sessionctx/stmtctx",
25+
"//sessionctx/variable",
2526
"//types",
2627
"//util/chunk",
2728
"//util/dbterror",

table/table.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/pingcap/tidb/meta/autoid"
2929
"github.com/pingcap/tidb/parser/model"
3030
"github.com/pingcap/tidb/sessionctx"
31+
"github.com/pingcap/tidb/sessionctx/variable"
3132
"github.com/pingcap/tidb/types"
3233
"github.com/pingcap/tidb/util/dbterror"
3334
"github.com/pingcap/tidb/util/sqlexec"
@@ -198,14 +199,25 @@ type Table interface {
198199
Type() Type
199200
}
200201

202+
func getIncrementAndOffset(vars *variable.SessionVars) (int, int) {
203+
increment := vars.AutoIncrementIncrement
204+
offset := vars.AutoIncrementOffset
205+
// When the value of auto_increment_offset is greater than that of auto_increment_increment,
206+
// the value of auto_increment_offset is ignored.
207+
// Ref https://dev.mysql.com/doc/refman/8.0/en/replication-options-source.html
208+
if offset > increment {
209+
offset = 1
210+
}
211+
return increment, offset
212+
}
213+
201214
// AllocAutoIncrementValue allocates an auto_increment value for a new row.
202215
func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context) (int64, error) {
203216
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
204217
span1 := span.Tracer().StartSpan("table.AllocAutoIncrementValue", opentracing.ChildOf(span.Context()))
205218
defer span1.Finish()
206219
}
207-
increment := sctx.GetSessionVars().AutoIncrementIncrement
208-
offset := sctx.GetSessionVars().AutoIncrementOffset
220+
increment, offset := getIncrementAndOffset(sctx.GetSessionVars())
209221
alloc := t.Allocators(sctx).Get(autoid.AutoIncrementType)
210222
_, max, err := alloc.Alloc(ctx, uint64(1), int64(increment), int64(offset))
211223
if err != nil {
@@ -216,18 +228,17 @@ func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Conte
216228

217229
// AllocBatchAutoIncrementValue allocates batch auto_increment value for rows, returning firstID, increment and err.
218230
// The caller can derive the autoID by adding increment to firstID for N-1 times.
219-
func AllocBatchAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context, N int) (firstID int64, increment int64, err error) {
220-
increment = int64(sctx.GetSessionVars().AutoIncrementIncrement)
221-
offset := int64(sctx.GetSessionVars().AutoIncrementOffset)
231+
func AllocBatchAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context, N int) ( /* firstID */ int64 /* increment */, int64 /* err */, error) {
232+
increment1, offset := getIncrementAndOffset(sctx.GetSessionVars())
222233
alloc := t.Allocators(sctx).Get(autoid.AutoIncrementType)
223-
min, max, err := alloc.Alloc(ctx, uint64(N), increment, offset)
234+
min, max, err := alloc.Alloc(ctx, uint64(N), int64(increment1), int64(offset))
224235
if err != nil {
225236
return min, max, err
226237
}
227238
// SeekToFirstAutoIDUnSigned seeks to first autoID. Because AutoIncrement always allocate from 1,
228239
// signed and unsigned value can be unified as the unsigned handle.
229-
nr := int64(autoid.SeekToFirstAutoIDUnSigned(uint64(min), uint64(increment), uint64(offset)))
230-
return nr, increment, nil
240+
nr := int64(autoid.SeekToFirstAutoIDUnSigned(uint64(min), uint64(increment1), uint64(offset)))
241+
return nr, int64(increment1), nil
231242
}
232243

233244
// PhysicalTable is an abstraction for two kinds of table representation: partition or non-partitioned table.

0 commit comments

Comments
 (0)