Skip to content

Commit

Permalink
cleanup index
Browse files Browse the repository at this point in the history
Signed-off-by: xiongjiwei <xiongjiwei1996@outlook.com>
  • Loading branch information
xiongjiwei committed Jan 9, 2023
1 parent b477b1c commit 7bf293a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion executor/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func (e *CleanupIndexExec) getIdxColTypes() []*types.FieldType {
}
e.idxColFieldTypes = make([]*types.FieldType, 0, len(e.columns))
for _, col := range e.columns {
e.idxColFieldTypes = append(e.idxColFieldTypes, &col.FieldType)
e.idxColFieldTypes = append(e.idxColFieldTypes, col.FieldType.ArrayType())
}
return e.idxColFieldTypes
}
Expand Down
45 changes: 45 additions & 0 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,51 @@ func TestAdminRecoverIndex(t *testing.T) {
tk.MustExec("admin check table admin_test")
}

func TestAdminCleanupMVIndex(t *testing.T) {
store, domain := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(pk int primary key, a json, index idx((cast(a as signed array))))")
tk.MustExec("insert into t values (0, '[0,1,2]')")
tk.MustExec("insert into t values (1, '[1,2,3]')")
tk.MustExec("insert into t values (2, '[2,3,4]')")
tk.MustExec("insert into t values (3, '[3,4,5]')")
tk.MustExec("insert into t values (4, '[4,5,6]')")
tk.MustExec("admin check table t")

// Make some corrupted index. Build the index information.
ctx := mock.NewContext()
ctx.Store = store
is := domain.InfoSchema()
dbName := model.NewCIStr("test")
tblName := model.NewCIStr("t")
tbl, err := is.TableByName(dbName, tblName)
require.NoError(t, err)
tblInfo := tbl.Meta()
idxInfo := tblInfo.Indices[0]
tk.Session().GetSessionVars().IndexLookupSize = 3
tk.Session().GetSessionVars().MaxChunkSize = 3

cpIdx := idxInfo.Clone()
cpIdx.MVIndex = false
indexOpr := tables.NewIndex(tblInfo.ID, tblInfo, cpIdx)

txn, err := store.Begin()
require.NoError(t, err)
_, err = indexOpr.Create(ctx, txn, types.MakeDatums(9), kv.IntHandle(9), nil)
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
err = tk.ExecToErr("admin check table t")
require.Error(t, err)

r := tk.MustQuery("admin cleanup index t idx")
r.Check(testkit.Rows("1"))
tk.MustExec("admin check table t")
}

func TestClusteredIndexAdminRecoverIndex(t *testing.T) {
store, domain := testkit.CreateMockStoreAndDomain(t)

Expand Down
3 changes: 2 additions & 1 deletion table/tables/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func (c *index) getIndexedValue(indexedValues []types.Datum) [][]types.Datum {
if !c.tblInfo.Columns[c.idxInfo.Columns[i].Offset].FieldType.IsArray() {
val = append(val, v)
} else {
if v.IsNull() {
// if the datum type is not JSON, it must come from cleanup index.
if v.IsNull() || v.Kind() != types.KindMysqlJSON {
val = append(val, v)
jsonIsNull = true
continue
Expand Down

0 comments on commit 7bf293a

Please sign in to comment.