Skip to content

Commit

Permalink
table: check duplicate row_id in insert stmt (#27455)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored Sep 23, 2021
1 parent ad9ec4e commit f2cf4cc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
7 changes: 4 additions & 3 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2649,16 +2649,17 @@ func (s *testIntegrationSuite3) TestAutoIncrementForce(c *C) {
}
// Rebase _tidb_row_id.
tk.MustExec("create table t (a int);")
tk.MustExec("alter table t force auto_increment = 2;")
tk.MustExec("insert into t values (1),(2);")
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 1", "2 2"))
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 2", "2 3"))
// Cannot set next global ID to 0.
tk.MustGetErrCode("alter table t force auto_increment = 0;", errno.ErrAutoincReadFailed)
tk.MustExec("alter table t force auto_increment = 1;")
c.Assert(getNextGlobalID(), Equals, uint64(1))
// inserting new rows can overwrite the existing data.
tk.MustExec("insert into t values (3);")
tk.MustExec("insert into t values (3);")
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("3 1", "3 2"))
c.Assert(tk.ExecToErr("insert into t values (3);").Error(), Equals, "[kv:1062]Duplicate entry '2' for key 'PRIMARY'")
tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("3 1", "1 2", "2 3"))

// Rebase auto_increment.
tk.MustExec("drop table if exists t;")
Expand Down
10 changes: 2 additions & 8 deletions table/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,15 @@ type IndexIterator interface {

// CreateIdxOpt contains the options will be used when creating an index.
type CreateIdxOpt struct {
Ctx context.Context
SkipHandleCheck bool // If true, skip the handle constraint check.
Untouched bool // If true, the index key/value is no need to commit.
Ctx context.Context
Untouched bool // If true, the index key/value is no need to commit.
}

// CreateIdxOptFunc is defined for the Create() method of Index interface.
// Here is a blog post about how to use this pattern:
// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
type CreateIdxOptFunc func(*CreateIdxOpt)

// SkipHandleCheck is a defined value of CreateIdxFunc.
var SkipHandleCheck CreateIdxOptFunc = func(opt *CreateIdxOpt) {
opt.SkipHandleCheck = true
}

// IndexIsUntouched uses to indicate the index kv is untouched.
var IndexIsUntouched CreateIdxOptFunc = func(opt *CreateIdxOpt) {
opt.Untouched = true
Expand Down
3 changes: 1 addition & 2 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,7 @@ func (t *TableCommon) AddRecord(sctx sessionctx.Context, r []types.Datum, opts .
value := writeBufs.RowValBuf

var setPresume bool
skipCheck := sctx.GetSessionVars().StmtCtx.BatchCheck
if (t.meta.IsCommonHandle || t.meta.PKIsHandle) && !skipCheck && !opt.SkipHandleCheck {
if !sctx.GetSessionVars().StmtCtx.BatchCheck {
if t.meta.TempTableType != model.TempTableNone {
// Always check key for temporary table because it does not write to TiKV
_, err = txn.Get(ctx, key)
Expand Down

0 comments on commit f2cf4cc

Please sign in to comment.