@@ -22,6 +22,7 @@ import (
22
22
"context"
23
23
"time"
24
24
25
+ << << << < HEAD :table / table .go
25
26
mysql "github.com/pingcap/tidb/errno"
26
27
"github.com/pingcap/tidb/kv"
27
28
"github.com/pingcap/tidb/meta/autoid"
@@ -31,6 +32,20 @@ import (
31
32
"github.com/pingcap/tidb/util/dbterror"
32
33
"github.com/pingcap/tidb/util/sqlexec"
33
34
"github.com/pingcap/tidb/util/tracing"
35
+ == == == =
36
+ mysql "github.com/pingcap/tidb/pkg/errno"
37
+ "github.com/pingcap/tidb/pkg/expression"
38
+ "github.com/pingcap/tidb/pkg/kv"
39
+ "github.com/pingcap/tidb/pkg/meta/autoid"
40
+ "github.com/pingcap/tidb/pkg/parser/model"
41
+ "github.com/pingcap/tidb/pkg/sessionctx"
42
+ "github.com/pingcap/tidb/pkg/sessionctx/variable"
43
+ tbctx "github.com/pingcap/tidb/pkg/table/context"
44
+ "github.com/pingcap/tidb/pkg/types"
45
+ "github.com/pingcap/tidb/pkg/util/dbterror"
46
+ "github.com/pingcap/tidb/pkg/util/sqlexec"
47
+ "github.com/pingcap/tidb/pkg/util/tracing"
48
+ >> >> >> > f5e591d107b (* : fix 'Duplicate entry ' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626 )):pkg / table / table .go
34
49
)
35
50
36
51
// Type is used to distinguish between different tables that store data in different ways.
@@ -204,13 +219,30 @@ type Table interface {
204
219
GetPartitionedTable () PartitionedTable
205
220
}
206
221
222
+ func getIncrementAndOffset (vars * variable.SessionVars ) (int , int ) {
223
+ increment := vars .AutoIncrementIncrement
224
+ offset := vars .AutoIncrementOffset
225
+ // When the value of auto_increment_offset is greater than that of auto_increment_increment,
226
+ // the value of auto_increment_offset is ignored.
227
+ // Ref https://dev.mysql.com/doc/refman/8.0/en/replication-options-source.html
228
+ if offset > increment {
229
+ offset = 1
230
+ }
231
+ return increment , offset
232
+ }
233
+
207
234
// AllocAutoIncrementValue allocates an auto_increment value for a new row.
208
235
func AllocAutoIncrementValue (ctx context.Context , t Table , sctx sessionctx.Context ) (int64 , error ) {
209
236
r , ctx := tracing .StartRegionEx (ctx , "table.AllocAutoIncrementValue" )
210
237
defer r .End ()
238
+ << << << < HEAD:table / table .go
211
239
increment := sctx .GetSessionVars ().AutoIncrementIncrement
212
240
offset := sctx .GetSessionVars ().AutoIncrementOffset
213
241
alloc := t .Allocators (sctx ).Get (autoid .AutoIncrementType )
242
+ == == == =
243
+ increment , offset := getIncrementAndOffset (sctx .GetSessionVars ())
244
+ alloc := t .Allocators (sctx .GetTableCtx ()).Get (autoid .AutoIncrementType )
245
+ >> >> >> > f5e591d107b (* : fix 'Duplicate entry ' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626 )):pkg / table / table .go
214
246
_ , max , err := alloc .Alloc (ctx , uint64 (1 ), int64 (increment ), int64 (offset ))
215
247
if err != nil {
216
248
return 0 , err
@@ -220,18 +252,25 @@ func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Conte
220
252
221
253
// AllocBatchAutoIncrementValue allocates batch auto_increment value for rows, returning firstID, increment and err.
222
254
// The caller can derive the autoID by adding increment to firstID for N-1 times.
255
+ << << << < HEAD:table / table .go
223
256
func AllocBatchAutoIncrementValue (ctx context.Context , t Table , sctx sessionctx.Context , N int ) (firstID int64 , increment int64 , err error ) {
224
257
increment = int64 (sctx .GetSessionVars ().AutoIncrementIncrement )
225
258
offset := int64 (sctx .GetSessionVars ().AutoIncrementOffset )
226
259
alloc := t .Allocators (sctx ).Get (autoid .AutoIncrementType )
227
260
min , max , err := alloc .Alloc (ctx , uint64 (N ), increment , offset )
261
+ == == == =
262
+ func AllocBatchAutoIncrementValue (ctx context .Context , t Table , sctx sessionctx .Context , N int ) ( /* firstID */ int64 /* increment */ , int64 /* err */ , error ) {
263
+ increment1 , offset := getIncrementAndOffset (sctx .GetSessionVars ())
264
+ alloc := t .Allocators (sctx .GetTableCtx ()).Get (autoid .AutoIncrementType )
265
+ min , max , err := alloc .Alloc (ctx , uint64 (N ), int64 (increment1 ), int64 (offset ))
266
+ >> >> >> > f5e591d107b (* : fix 'Duplicate entry ' error when @@auto_increment_increment and @@auto_increment_offset is set (#52626 )):pkg / table / table .go
228
267
if err != nil {
229
268
return min , max , err
230
269
}
231
270
// SeekToFirstAutoIDUnSigned seeks to first autoID. Because AutoIncrement always allocate from 1,
232
271
// signed and unsigned value can be unified as the unsigned handle.
233
- nr := int64 (autoid .SeekToFirstAutoIDUnSigned (uint64 (min ), uint64 (increment ), uint64 (offset )))
234
- return nr , increment , nil
272
+ nr := int64 (autoid .SeekToFirstAutoIDUnSigned (uint64 (min ), uint64 (increment1 ), uint64 (offset )))
273
+ return nr , int64 ( increment1 ) , nil
235
274
}
236
275
237
276
// PhysicalTable is an abstraction for two kinds of table representation: partition or non-partitioned table.
0 commit comments