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

ddl: never write untouched index values to temp index #41879

Merged
merged 1 commit into from
Mar 2, 2023
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
41 changes: 41 additions & 0 deletions ddl/indexmergetest/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,3 +897,44 @@ func TestAddIndexDuplicateAndWriteConflict(t *testing.T) {
tk.MustExec("admin check table t;")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 1", "2 1"))
}

func TestAddIndexUpdateUntouchedValues(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(id int primary key, b int, k int);")
tk.MustExec("insert into t values (1, 1, 1);")

tk1 := testkit.NewTestKit(t, store)
tk1.MustExec("use test")

d := dom.DDL()
originalCallback := d.GetHook()
defer d.SetHook(originalCallback)
callback := &callback.TestDDLCallback{}
var runDML bool
callback.OnJobRunAfterExported = func(job *model.Job) {
if t.Failed() || runDML {
return
}
switch job.SchemaState {
case model.StateWriteReorganization:
_, err := tk1.Exec("begin;")
assert.NoError(t, err)
_, err = tk1.Exec("update t set k=k+1 where id = 1;")
assert.NoError(t, err)
_, err = tk1.Exec("insert into t values (2, 1, 2);")
// Should not report "invalid temp index value".
assert.NoError(t, err)
_, err = tk1.Exec("commit;")
assert.NoError(t, err)
runDML = true
}
}
d.SetHook(callback)

tk.MustGetErrCode("alter table t add unique index idx(b);", errno.ErrDupEntry)
tk.MustExec("admin check table t;")
tk.MustQuery("select * from t;").Check(testkit.Rows("1 1 2", "2 1 2"))
}
13 changes: 8 additions & 5 deletions table/tables/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,12 @@ func (c *index) Create(sctx sessionctx.Context, txn kv.Transaction, indexedValue

if !distinct || skipCheck || opt.Untouched {
val := idxVal
if keyIsTempIdxKey && !opt.Untouched { // Untouched key-values never occur in the storage.
if opt.Untouched && (keyIsTempIdxKey || len(tempKey) > 0) {
// Untouched key-values never occur in the storage and the temp index is not public.
// It is unnecessary to write the untouched temp index key-values.
continue
}
if keyIsTempIdxKey {
tempVal := tablecodec.TempIndexValueElem{Value: idxVal, KeyVer: keyVer, Distinct: distinct}
val = tempVal.Encode(nil)
}
Expand All @@ -248,10 +253,8 @@ func (c *index) Create(sctx sessionctx.Context, txn kv.Transaction, indexedValue
return nil, err
}
if len(tempKey) > 0 {
if !opt.Untouched { // Untouched key-values never occur in the storage.
tempVal := tablecodec.TempIndexValueElem{Value: idxVal, KeyVer: keyVer, Distinct: distinct}
val = tempVal.Encode(nil)
}
tempVal := tablecodec.TempIndexValueElem{Value: idxVal, KeyVer: keyVer, Distinct: distinct}
val = tempVal.Encode(nil)
err = txn.GetMemBuffer().Set(tempKey, val)
if err != nil {
return nil, err
Expand Down