@@ -28,6 +28,7 @@ import (
28
28
"github.com/pingcap/tidb/pkg/meta/autoid"
29
29
"github.com/pingcap/tidb/pkg/parser/model"
30
30
"github.com/pingcap/tidb/pkg/sessionctx"
31
+ "github.com/pingcap/tidb/pkg/sessionctx/variable"
31
32
tbctx "github.com/pingcap/tidb/pkg/table/context"
32
33
"github.com/pingcap/tidb/pkg/types"
33
34
"github.com/pingcap/tidb/pkg/util/dbterror"
@@ -214,12 +215,23 @@ type Table interface {
214
215
GetPartitionedTable () PartitionedTable
215
216
}
216
217
218
+ func getIncrementAndOffset (vars * variable.SessionVars ) (int , int ) {
219
+ increment := vars .AutoIncrementIncrement
220
+ offset := vars .AutoIncrementOffset
221
+ // When the value of auto_increment_offset is greater than that of auto_increment_increment,
222
+ // the value of auto_increment_offset is ignored.
223
+ // Ref https://dev.mysql.com/doc/refman/8.0/en/replication-options-source.html
224
+ if offset > increment {
225
+ offset = 1
226
+ }
227
+ return increment , offset
228
+ }
229
+
217
230
// AllocAutoIncrementValue allocates an auto_increment value for a new row.
218
231
func AllocAutoIncrementValue (ctx context.Context , t Table , sctx sessionctx.Context ) (int64 , error ) {
219
232
r , ctx := tracing .StartRegionEx (ctx , "table.AllocAutoIncrementValue" )
220
233
defer r .End ()
221
- increment := sctx .GetSessionVars ().AutoIncrementIncrement
222
- offset := sctx .GetSessionVars ().AutoIncrementOffset
234
+ increment , offset := getIncrementAndOffset (sctx .GetSessionVars ())
223
235
alloc := t .Allocators (sctx .GetTableCtx ()).Get (autoid .AutoIncrementType )
224
236
_ , max , err := alloc .Alloc (ctx , uint64 (1 ), int64 (increment ), int64 (offset ))
225
237
if err != nil {
@@ -230,18 +242,17 @@ func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Conte
230
242
231
243
// AllocBatchAutoIncrementValue allocates batch auto_increment value for rows, returning firstID, increment and err.
232
244
// The caller can derive the autoID by adding increment to firstID for N-1 times.
233
- func AllocBatchAutoIncrementValue (ctx context.Context , t Table , sctx sessionctx.Context , N int ) (firstID int64 , increment int64 , err error ) {
234
- increment = int64 (sctx .GetSessionVars ().AutoIncrementIncrement )
235
- offset := int64 (sctx .GetSessionVars ().AutoIncrementOffset )
245
+ func AllocBatchAutoIncrementValue (ctx context.Context , t Table , sctx sessionctx.Context , N int ) ( /* firstID */ int64 /* increment */ , int64 /* err */ , error ) {
246
+ increment1 , offset := getIncrementAndOffset (sctx .GetSessionVars ())
236
247
alloc := t .Allocators (sctx .GetTableCtx ()).Get (autoid .AutoIncrementType )
237
- min , max , err := alloc .Alloc (ctx , uint64 (N ), increment , offset )
248
+ min , max , err := alloc .Alloc (ctx , uint64 (N ), int64 ( increment1 ), int64 ( offset ) )
238
249
if err != nil {
239
250
return min , max , err
240
251
}
241
252
// SeekToFirstAutoIDUnSigned seeks to first autoID. Because AutoIncrement always allocate from 1,
242
253
// signed and unsigned value can be unified as the unsigned handle.
243
- nr := int64 (autoid .SeekToFirstAutoIDUnSigned (uint64 (min ), uint64 (increment ), uint64 (offset )))
244
- return nr , increment , nil
254
+ nr := int64 (autoid .SeekToFirstAutoIDUnSigned (uint64 (min ), uint64 (increment1 ), uint64 (offset )))
255
+ return nr , int64 ( increment1 ) , nil
245
256
}
246
257
247
258
// PhysicalTable is an abstraction for two kinds of table representation: partition or non-partitioned table.
0 commit comments