Skip to content

Commit

Permalink
This is an automated cherry-pick of #48048
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
Defined2014 authored and ti-chi-bot committed Oct 31, 2023
1 parent 06c954c commit 7866a04
Show file tree
Hide file tree
Showing 4 changed files with 2,854 additions and 5 deletions.
20 changes: 19 additions & 1 deletion executor/admintest/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func TestClusteredIndexAdminRecoverIndex(t *testing.T) {
tblName := model.NewCIStr("t")

// Test no corruption case.
tk.MustExec("create table t (a varchar(255), b int, c char(10), primary key(a, c), index idx(b));")
tk.MustExec("create table t (a varchar(255), b int, c char(10), primary key(a, c), index idx(b), index idx1(c));")
tk.MustExec("insert into t values ('1', 2, '3'), ('1', 2, '4'), ('1', 2, '5');")
tk.MustQuery("admin recover index t `primary`;").Check(testkit.Rows("0 0"))
tk.MustQuery("admin recover index t `idx`;").Check(testkit.Rows("0 3"))
Expand All @@ -452,6 +452,7 @@ func TestClusteredIndexAdminRecoverIndex(t *testing.T) {
sc := ctx.GetSessionVars().StmtCtx

// Some index entries are missed.
// Recover an index don't covered by clustered index.
txn, err := store.Begin()
require.NoError(t, err)
cHandle := testutil.MustNewCommonHandle(t, "1", "3")
Expand All @@ -466,6 +467,23 @@ func TestClusteredIndexAdminRecoverIndex(t *testing.T) {
tk.MustQuery("admin recover index t idx").Check(testkit.Rows("1 3"))
tk.MustQuery("SELECT COUNT(*) FROM t USE INDEX(idx)").Check(testkit.Rows("3"))
tk.MustExec("admin check table t;")

// Recover an index covered by clustered index.
idx1Info := tblInfo.FindIndexByName("idx1")
indexOpr1 := tables.NewIndex(tblInfo.ID, tblInfo, idx1Info)
txn, err = store.Begin()
require.NoError(t, err)
err = indexOpr1.Delete(sc, txn, types.MakeDatums("3"), cHandle)
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
tk.MustGetErrCode("admin check table t", mysql.ErrDataInconsistent)
tk.MustGetErrCode("admin check index t idx1", mysql.ErrDataInconsistent)

tk.MustQuery("SELECT COUNT(*) FROM t USE INDEX(idx1)").Check(testkit.Rows("2"))
tk.MustQuery("admin recover index t idx1").Check(testkit.Rows("1 3"))
tk.MustQuery("SELECT COUNT(*) FROM t USE INDEX(idx1)").Check(testkit.Rows("3"))
tk.MustExec("admin check table t;")
}

func TestAdminRecoverPartitionTableIndex(t *testing.T) {
Expand Down
24 changes: 20 additions & 4 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ func buildIdxColsConcatHandleCols(tblInfo *model.TableInfo, indexInfo *model.Ind

if tblInfo.IsCommonHandle {
for _, c := range pkCols {
columns = append(columns, tblInfo.Columns[c.Offset])
if model.FindColumnInfo(columns, c.Name.L) == nil {
columns = append(columns, tblInfo.Columns[c.Offset])
}
}
return columns
}
Expand Down Expand Up @@ -569,13 +571,18 @@ func (b *executorBuilder) buildRecoverIndex(v *plannercore.RecoverIndex) Executo
table: t,
physicalID: t.Meta().ID,
}
<<<<<<< HEAD:executor/builder.go
sessCtx := e.ctx.GetSessionVars().StmtCtx
e.handleCols = buildHandleColsForExec(sessCtx, tblInfo, index.Meta(), e.columns)
=======
sessCtx := e.Ctx().GetSessionVars().StmtCtx
e.handleCols = buildHandleColsForExec(sessCtx, tblInfo, e.columns)
>>>>>>> 3894bc52321 (admin: fix `admin recover index` with CommonHandle table in TiKV env (#48048)):pkg/executor/builder.go
return e
}

func buildHandleColsForExec(sctx *stmtctx.StatementContext, tblInfo *model.TableInfo,
idxInfo *model.IndexInfo, allColInfo []*model.ColumnInfo) plannercore.HandleCols {
allColInfo []*model.ColumnInfo) plannercore.HandleCols {
if !tblInfo.IsCommonHandle {
extraColPos := len(allColInfo) - 1
intCol := &expression.Column{
Expand All @@ -593,8 +600,12 @@ func buildHandleColsForExec(sctx *stmtctx.StatementContext, tblInfo *model.Table
}
}
pkIdx := tables.FindPrimaryIndex(tblInfo)
for i, c := range pkIdx.Columns {
tblCols[c.Offset].Index = len(idxInfo.Columns) + i
for _, c := range pkIdx.Columns {
for j, colInfo := range allColInfo {
if colInfo.Name.L == c.Name.L {
tblCols[c.Offset].Index = j
}
}
}
return plannercore.NewCommonHandleCols(sctx, tblInfo, pkIdx, tblCols)
}
Expand Down Expand Up @@ -630,8 +641,13 @@ func (b *executorBuilder) buildCleanupIndex(v *plannercore.CleanupIndex) Executo
physicalID: t.Meta().ID,
batchSize: 20000,
}
<<<<<<< HEAD:executor/builder.go
sessCtx := e.ctx.GetSessionVars().StmtCtx
e.handleCols = buildHandleColsForExec(sessCtx, tblInfo, index.Meta(), e.columns)
=======
sessCtx := e.Ctx().GetSessionVars().StmtCtx
e.handleCols = buildHandleColsForExec(sessCtx, tblInfo, e.columns)
>>>>>>> 3894bc52321 (admin: fix `admin recover index` with CommonHandle table in TiKV env (#48048)):pkg/executor/builder.go
return e
}

Expand Down
Loading

0 comments on commit 7866a04

Please sign in to comment.