Skip to content

Commit

Permalink
ddl: fix adding multi-value index (pingcap#51884) (pingcap#52498)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Apr 17, 2024
1 parent dadf2fa commit d42a2ab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1685,15 +1685,29 @@ func writeChunkToLocal(writer ingest.Writer,
var lastHandle kv.Handle
unlock := writer.LockForWrite()
defer unlock()
var restoreDataBuf []types.Datum
restore := tables.NeedRestoredData(index.Meta().Columns, copCtx.tblInfo.Columns)
if restore {
restoreDataBuf = make([]types.Datum, len(copCtx.handleOutputOffsets))
}
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
idxDataBuf, handleDataBuf = idxDataBuf[:0], handleDataBuf[:0]
idxDataBuf = extractDatumByOffsets(row, copCtx.idxColOutputOffsets, copCtx.expColInfos, idxDataBuf)
handleDataBuf := extractDatumByOffsets(row, copCtx.handleOutputOffsets, copCtx.expColInfos, handleDataBuf)
if restore {
// restoreDataBuf should not truncate index values.
for i, datum := range handleDataBuf {
restoreDataBuf[i] = *datum.Clone()
}
}
handle, err := buildHandle(handleDataBuf, copCtx.tblInfo, copCtx.pkInfo, sCtx)
if err != nil {
return 0, nil, errors.Trace(err)
}
rsData := getRestoreData(copCtx.tblInfo, copCtx.idxInfo, copCtx.pkInfo, handleDataBuf)
var rsData []types.Datum
if restore {
rsData = getRestoreData(copCtx.tblInfo, copCtx.idxInfo, copCtx.pkInfo, restoreDataBuf)
}
err = writeOneKVToLocal(writer, index, sCtx, writeBufs, idxDataBuf, rsData, handle)
if err != nil {
return 0, nil, errors.Trace(err)
Expand Down
22 changes: 22 additions & 0 deletions tests/realtikvtest/addindextest/add_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ func TestAddIndexDistCancel(t *testing.T) {
tk.MustExec(`set global tidb_enable_dist_task=0;`)
}

func TestIssue51162(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
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")
}

func TestAddUKWithSmallIntHandles(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
Expand Down

0 comments on commit d42a2ab

Please sign in to comment.