Skip to content

Commit 7bf293a

Browse files
committed
cleanup index
Signed-off-by: xiongjiwei <xiongjiwei1996@outlook.com>
1 parent b477b1c commit 7bf293a

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

executor/admin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func (e *CleanupIndexExec) getIdxColTypes() []*types.FieldType {
551551
}
552552
e.idxColFieldTypes = make([]*types.FieldType, 0, len(e.columns))
553553
for _, col := range e.columns {
554-
e.idxColFieldTypes = append(e.idxColFieldTypes, &col.FieldType)
554+
e.idxColFieldTypes = append(e.idxColFieldTypes, col.FieldType.ArrayType())
555555
}
556556
return e.idxColFieldTypes
557557
}

executor/admin_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,51 @@ func TestAdminRecoverIndex(t *testing.T) {
304304
tk.MustExec("admin check table admin_test")
305305
}
306306

307+
func TestAdminCleanupMVIndex(t *testing.T) {
308+
store, domain := testkit.CreateMockStoreAndDomain(t)
309+
310+
tk := testkit.NewTestKit(t, store)
311+
tk.MustExec("use test")
312+
tk.MustExec("drop table if exists t")
313+
tk.MustExec("create table t(pk int primary key, a json, index idx((cast(a as signed array))))")
314+
tk.MustExec("insert into t values (0, '[0,1,2]')")
315+
tk.MustExec("insert into t values (1, '[1,2,3]')")
316+
tk.MustExec("insert into t values (2, '[2,3,4]')")
317+
tk.MustExec("insert into t values (3, '[3,4,5]')")
318+
tk.MustExec("insert into t values (4, '[4,5,6]')")
319+
tk.MustExec("admin check table t")
320+
321+
// Make some corrupted index. Build the index information.
322+
ctx := mock.NewContext()
323+
ctx.Store = store
324+
is := domain.InfoSchema()
325+
dbName := model.NewCIStr("test")
326+
tblName := model.NewCIStr("t")
327+
tbl, err := is.TableByName(dbName, tblName)
328+
require.NoError(t, err)
329+
tblInfo := tbl.Meta()
330+
idxInfo := tblInfo.Indices[0]
331+
tk.Session().GetSessionVars().IndexLookupSize = 3
332+
tk.Session().GetSessionVars().MaxChunkSize = 3
333+
334+
cpIdx := idxInfo.Clone()
335+
cpIdx.MVIndex = false
336+
indexOpr := tables.NewIndex(tblInfo.ID, tblInfo, cpIdx)
337+
338+
txn, err := store.Begin()
339+
require.NoError(t, err)
340+
_, err = indexOpr.Create(ctx, txn, types.MakeDatums(9), kv.IntHandle(9), nil)
341+
require.NoError(t, err)
342+
err = txn.Commit(context.Background())
343+
require.NoError(t, err)
344+
err = tk.ExecToErr("admin check table t")
345+
require.Error(t, err)
346+
347+
r := tk.MustQuery("admin cleanup index t idx")
348+
r.Check(testkit.Rows("1"))
349+
tk.MustExec("admin check table t")
350+
}
351+
307352
func TestClusteredIndexAdminRecoverIndex(t *testing.T) {
308353
store, domain := testkit.CreateMockStoreAndDomain(t)
309354

table/tables/index.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ func (c *index) getIndexedValue(indexedValues []types.Datum) [][]types.Datum {
124124
if !c.tblInfo.Columns[c.idxInfo.Columns[i].Offset].FieldType.IsArray() {
125125
val = append(val, v)
126126
} else {
127-
if v.IsNull() {
127+
// if the datum type is not JSON, it must come from cleanup index.
128+
if v.IsNull() || v.Kind() != types.KindMysqlJSON {
128129
val = append(val, v)
129130
jsonIsNull = true
130131
continue

0 commit comments

Comments
 (0)