diff --git a/executor/admin.go b/executor/admin.go index 21378b21b1677..7fc5ad37fb31e 100644 --- a/executor/admin.go +++ b/executor/admin.go @@ -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 } diff --git a/executor/admin_test.go b/executor/admin_test.go index cd5c0664d031a..c91cd343718db 100644 --- a/executor/admin_test.go +++ b/executor/admin_test.go @@ -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) diff --git a/table/tables/index.go b/table/tables/index.go index 29e3964959aa9..2636a82bffee4 100644 --- a/table/tables/index.go +++ b/table/tables/index.go @@ -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