Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: fix adding multi-value index (#51884) #51970

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pkg/ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,7 @@ func writeChunkToLocal(
maxIdxColCnt := maxIndexColumnCount(indexes)
idxDataBuf := make([]types.Datum, maxIdxColCnt)
handleDataBuf := make([]types.Datum, len(c.HandleOutputOffsets))
var restoreDataBuf []types.Datum
count := 0
var lastHandle kv.Handle

Expand All @@ -1795,8 +1796,24 @@ func writeChunkToLocal(
unlock()
}
}()
needRestoreForIndexes := make([]bool, len(indexes))
restore := false
for i, index := range indexes {
needRestore := tables.NeedRestoredData(index.Meta().Columns, c.TableInfo.Columns)
needRestoreForIndexes[i] = needRestore
restore = restore || needRestore
}
if restore {
restoreDataBuf = make([]types.Datum, len(c.HandleOutputOffsets))
}
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
handleDataBuf := extractDatumByOffsets(row, c.HandleOutputOffsets, c.ExprColumnInfos, handleDataBuf)
if restore {
// restoreDataBuf should not truncate index values.
for i, datum := range handleDataBuf {
restoreDataBuf[i] = *datum.Clone()
}
}
h, err := buildHandle(handleDataBuf, c.TableInfo, c.PrimaryKeyInfo, sCtx)
if err != nil {
return 0, nil, errors.Trace(err)
Expand All @@ -1806,7 +1823,10 @@ func writeChunkToLocal(
idxDataBuf = extractDatumByOffsets(
row, copCtx.IndexColumnOutputOffsets(idxID), c.ExprColumnInfos, idxDataBuf)
idxData := idxDataBuf[:len(index.Meta().Columns)]
rsData := getRestoreData(c.TableInfo, copCtx.IndexInfo(idxID), c.PrimaryKeyInfo, handleDataBuf)
var rsData []types.Datum
if needRestoreForIndexes[i] {
rsData = getRestoreData(c.TableInfo, copCtx.IndexInfo(idxID), c.PrimaryKeyInfo, restoreDataBuf)
}
err = writeOneKVToLocal(ctx, writers[i], index, sCtx, writeBufs, idxData, rsData, h)
if err != nil {
return 0, nil, errors.Trace(err)
Expand Down
23 changes: 23 additions & 0 deletions tests/realtikvtest/addindextest/add_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,26 @@ func TestAddForeignKeyWithAutoCreateIndex(t *testing.T) {
tk.MustExec("update employee set pid=id-1 where id>1 and pid is null")
tk.MustExec("alter table employee add foreign key fk_1(pid) references employee(id)")
}

func TestIssue51162(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set global tidb_enable_fast_table_check=0")
tk.MustExec(`CREATE TABLE tl (
col_42 json NOT NULL,
col_43 tinyint(1) DEFAULT NULL,
col_44 char(168) CHARACTER SET gbk COLLATE gbk_bin DEFAULT NULL,
col_45 json DEFAULT NULL,
col_46 text COLLATE utf8mb4_unicode_ci NOT NULL,
col_47 char(43) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'xW2YNb99pse4)',
col_48 time NOT NULL DEFAULT '12:31:25',
PRIMARY KEY (col_47,col_46(2)) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`)

tk.MustExec(`INSERT INTO tl VALUES
('[\"1\"]',0,'1','[1]','Wxup81','1','10:14:20');`)

tk.MustExec("alter table tl add index idx_16(`col_48`,(cast(`col_45` as signed array)),`col_46`(5));")
tk.MustExec("admin check table tl")
}