Skip to content

Commit

Permalink
executor: truncate column values from index KV during admin check (pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta authored Mar 10, 2021
1 parent f6a61bc commit c22cbc0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ func (s *testSuite3) TestAdminCheckPartitionTableFailed(c *C) {
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
err = tk.ExecToErr("admin check table admin_test_p")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, fmt.Sprintf("[executor:8003]admin_test_p err:[admin:8223]index:<nil> != record:&admin.RecordData{Handle:%d, Values:[]types.Datum{types.Datum{k:0x1, decimal:0x0, length:0x0, i:%d, collation:\"\", b:[]uint8(nil), x:interface {}(nil)}}}", i, i))
c.Assert(executor.ErrAdminCheckTable.Equal(err), IsTrue)
// TODO: fix admin recover for partition table.
Expand Down
15 changes: 11 additions & 4 deletions executor/distsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,17 +1051,24 @@ func (w *tableWorker) compareData(ctx context.Context, task *lookupTableTask, ta
vals = append(vals, row.GetDatum(i, &col.FieldType))
}
tablecodec.TruncateIndexValues(tblInfo, w.idxLookup.index, vals)
sctx := w.idxLookup.ctx.GetSessionVars().StmtCtx
for i, val := range vals {
col := w.idxTblCols[i]
tp := &col.FieldType
ret := chunk.Compare(idxRow, i, &val)
if ret != 0 {
return errors.Errorf("col %s, handle %#v, index:%#v != record:%#v", col.Name, handle, idxRow.GetDatum(i, tp), val)
idxVal := idxRow.GetDatum(i, tp)
tablecodec.TruncateIndexValue(&idxVal, w.idxLookup.index.Columns[i], col.ColumnInfo)
cmpRes, err := idxVal.CompareDatum(sctx, &val)
if err != nil {
return errors.Errorf("col %s, handle %#v, index:%#v != record:%#v, compare err:%#v", col.Name,
handle, idxRow.GetDatum(i, tp), val, err)
}
if cmpRes != 0 {
return errors.Errorf("col %s, handle %#v, index:%#v != record:%#v", col.Name,
handle, idxRow.GetDatum(i, tp), val)
}
}
}
}

return nil
}

Expand Down
21 changes: 21 additions & 0 deletions session/clustered_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,24 @@ func (s *testClusteredSerialSuite) TestClusteredIndexDecodeRestoredDataV5(c *C)
tk.MustExec("admin check table t;")
tk.MustExec("drop table t;")
}

// https://github.com/pingcap/tidb/issues/23178
func (s *testClusteredSerialSuite) TestPrefixedClusteredIndexUniqueKeyWithNewCollation(c *C) {
defer collate.SetNewCollationEnabledForTest(false)
collate.SetNewCollationEnabledForTest(true)
tk := testkit.NewTestKitWithInit(c, s.store)
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.AlterPrimaryKey = false
})
tk.MustExec("use test;")
tk.Se.GetSessionVars().EnableClusteredIndex = true
tk.MustExec("create table t (a text collate utf8mb4_general_ci not null, b int(11) not null, " +
"primary key (a(10), b) clustered, key idx(a(2)) ) default charset=utf8mb4 collate=utf8mb4_bin;")
tk.MustExec("insert into t values ('aaa', 2);")
// Key-value content: sk = sortKey, p = prefixed
// row record: sk(aaa), 2 -> aaa
// index record: sk(p(aa)), {sk(aaa), 2} -> restore data(aaa)
tk.MustExec("admin check table t;")
tk.MustExec("drop table t;")
}

0 comments on commit c22cbc0

Please sign in to comment.