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

*: fix lost index bug of insert on duplicate key update (#16672) #16690

Merged
merged 1 commit into from
Apr 22, 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
38 changes: 38 additions & 0 deletions executor/union_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,41 @@ func (s *testSuite4) TestUnionScanForMemBufferReader(c *C) {
tk.MustQuery("select * from t1 use index(idx2);").Check(testkit.Rows("1 2 1"))
tk.MustExec("admin check table t1;")
}

func (s *testSuite4) TestForUpdateUntouchedIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")

checkFunc := func() {
tk.MustExec("begin")
tk.MustExec("insert into t values ('a', 1), ('b', 3), ('a', 2) on duplicate key update b = b + 1;")
tk.MustExec("commit")
tk.MustExec("admin check table t")

// Test for autocommit
tk.MustExec("set autocommit=0")
tk.MustExec("insert into t values ('a', 1), ('b', 3), ('a', 2) on duplicate key update b = b + 1;")
tk.MustExec("set autocommit=1")
tk.MustExec("admin check table t")
}

// Test for primary key.
tk.MustExec("create table t (a varchar(10) primary key,b int)")
checkFunc()

// Test for unique key.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a varchar(10),b int, unique index(a))")
checkFunc()

// Test for on duplicate update also conflict too.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int,b int, unique index(a))")
tk.MustExec("begin")
_, err := tk.Exec("insert into t values (1, 1), (2, 2), (1, 3) on duplicate key update a = a + 1;")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '2' for key 'a'")
tk.MustExec("commit")
tk.MustExec("admin check table t")
}
8 changes: 8 additions & 0 deletions kv/buffer_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func NewBufferStore(r Retriever, cap int) *BufferStore {
}
}

// NewBufferStoreFrom creates a BufferStore from retriever and mem-buffer.
func NewBufferStoreFrom(r Retriever, buf MemBuffer) *BufferStore {
return &BufferStore{
r: r,
MemBuffer: buf,
}
}

// Reset resets s.MemBuffer.
func (s *BufferStore) Reset() {
s.MemBuffer.Reset()
Expand Down
8 changes: 8 additions & 0 deletions session/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ func (st *TxnState) Get(k kv.Key) ([]byte, error) {
return val, nil
}

// GetMemBuffer overrides the Transaction interface.
func (st *TxnState) GetMemBuffer() kv.MemBuffer {
if st.buf == nil || st.buf.Size() == 0 {
return st.Transaction.GetMemBuffer()
}
return kv.NewBufferStoreFrom(st.Transaction.GetMemBuffer(), st.buf)
}

// BatchGet overrides the Transaction interface.
func (st *TxnState) BatchGet(keys []kv.Key) (map[string][]byte, error) {
bufferValues := make([][]byte, len(keys))
Expand Down