Skip to content

Commit

Permalink
ddl: never write untouched index values to temp index (#41879) (#41884)
Browse files Browse the repository at this point in the history
close #41880
  • Loading branch information
ti-chi-bot committed Mar 3, 2023
1 parent 8fe1379 commit 9a3f362
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
41 changes: 41 additions & 0 deletions ddl/index_merge_tmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,44 @@ func TestAddIndexMultipleDelete(t *testing.T) {
tk.MustQuery("select * from t;").Check(testkit.Rows())
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockDMLExecution"))
}

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 := &ddl.TestDDLCallback{}
var runDML bool
callback.OnJobRunBeforeExported = 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"))
}
2 changes: 1 addition & 1 deletion ddl/ingest/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (ei *engineInfo) newWriterContext(workerID int, unique bool) (*WriterContex

func (ei *engineInfo) closeWriters() error {
var firstErr error
for wid := range ei.writerCache.Keys() {
for _, wid := range ei.writerCache.Keys() {
if w, ok := ei.writerCache.Load(wid); ok {
_, err := w.Close(ei.ctx)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions table/tables/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,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.
return nil, nil
}
if keyIsTempIdxKey {
tempVal := tablecodec.TempIndexValueElem{Value: idxVal, KeyVer: keyVer, Distinct: distinct}
val = tempVal.Encode(nil)
}
Expand All @@ -186,10 +191,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

0 comments on commit 9a3f362

Please sign in to comment.