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 admin check table error after rename index with scalar function #56060

Merged
merged 5 commits into from
Sep 13, 2024
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
4 changes: 2 additions & 2 deletions pkg/ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -1304,8 +1304,8 @@ func getChangingColumnOriginName(changingColumn *model.ColumnInfo) string {
return columnName[:pos]
}

func getExpressionIndexOriginName(expressionIdx *model.ColumnInfo) string {
columnName := strings.TrimPrefix(expressionIdx.Name.O, expressionIndexPrefix+"_")
func getExpressionIndexOriginName(originalName pmodel.CIStr) string {
columnName := strings.TrimPrefix(originalName.O, expressionIndexPrefix+"_")
var pos int
if pos = strings.LastIndex(columnName, "_"); pos == -1 {
return columnName
Expand Down
17 changes: 16 additions & 1 deletion pkg/ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,8 @@ func assertAlterWarnExec(tk *testkit.TestKit, t *testing.T, sql string) {
}

func TestAlterAlgorithm(t *testing.T) {
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())
store, dom := testkit.CreateMockStoreAndDomain(t, mockstore.WithDDLChecker())
ddlChecker := dom.DDL().(*schematracker.Checker)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand Down Expand Up @@ -1376,6 +1377,20 @@ func TestAlterAlgorithm(t *testing.T) {
tk.MustExec("alter table t rename index idx_c2 to idx_c, ALGORITHM=INSTANT")
tk.MustExec("alter table t rename index idx_c to idx_c1, ALGORITHM=DEFAULT")

// Test corner case for renameIndexes
tk.MustExec(`create table tscalar(c1 int, col_1_1 int, key col_1(col_1_1))`)
tk.MustExec("alter table tscalar rename index col_1 to col_2")
tk.MustExec("admin check table tscalar")
tk.MustExec("drop table tscalar")

// Test rename index with scalar function
ddlChecker.Disable()
tk.MustExec(`create table tscalar(id int, col_1 json, KEY idx_1 ((cast(col_1 as char(64) array))))`)
tk.MustExec("alter table tscalar rename index idx_1 to idx_1_1")
tk.MustExec("admin check table tscalar")
tk.MustExec("drop table tscalar")
ddlChecker.Enable()

// partition.
assertAlterWarnExec(tk, t, "alter table t ALGORITHM=COPY, truncate partition p1")
assertAlterWarnExec(tk, t, "alter table t ALGORITHM=INPLACE, truncate partition p2")
Expand Down
9 changes: 8 additions & 1 deletion pkg/ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2611,12 +2611,19 @@ func renameIndexes(tblInfo *model.TableInfo, from, to pmodel.CIStr) {
idx.Name.L = strings.Replace(idx.Name.L, from.L, to.L, 1)
idx.Name.O = strings.Replace(idx.Name.O, from.O, to.O, 1)
}
for _, col := range idx.Columns {
originalCol := tblInfo.Columns[col.Offset]
if originalCol.Hidden && getExpressionIndexOriginName(col.Name) == from.O {
col.Name.L = strings.Replace(col.Name.L, from.L, to.L, 1)
col.Name.O = strings.Replace(col.Name.O, from.O, to.O, 1)
}
}
}
}

func renameHiddenColumns(tblInfo *model.TableInfo, from, to pmodel.CIStr) {
for _, col := range tblInfo.Columns {
if col.Hidden && getExpressionIndexOriginName(col) == from.O {
if col.Hidden && getExpressionIndexOriginName(col.Name) == from.O {
col.Name.L = strings.Replace(col.Name.L, from.L, to.L, 1)
col.Name.O = strings.Replace(col.Name.O, from.O, to.O, 1)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/schematracker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ go_test(
],
embed = [":schematracker"],
flaky = True,
shard_count = 15,
shard_count = 16,
deps = [
"//pkg/executor",
"//pkg/infoschema",
Expand Down
4 changes: 2 additions & 2 deletions pkg/ddl/schematracker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (*Checker) RecoverSchema(_ sessionctx.Context, _ *ddl.RecoverSchemaInfo) (e
// CreateTable implements the DDL interface.
func (d *Checker) CreateTable(ctx sessionctx.Context, stmt *ast.CreateTableStmt) error {
err := d.realExecutor.CreateTable(ctx, stmt)
if err != nil {
if err != nil || d.closed.Load() {
return err
}

Expand Down Expand Up @@ -332,7 +332,7 @@ func (d *Checker) DropIndex(ctx sessionctx.Context, stmt *ast.DropIndexStmt) err
// AlterTable implements the DDL interface.
func (d *Checker) AlterTable(ctx context.Context, sctx sessionctx.Context, stmt *ast.AlterTableStmt) error {
err := d.realExecutor.AlterTable(ctx, sctx, stmt)
if err != nil {
if err != nil || d.closed.Load() {
return err
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/ddl/schematracker/dm_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ func TestIndexLength(t *testing.T) {
checkShowCreateTable(t, tblInfo, expected)
}

func TestCreateTableWithIndex(t *testing.T) {
// See issue 56045
sql := "create table test.t(col_1 json, KEY idx_1 ((cast(col_1 as char(64) array))))"
tracker := schematracker.NewSchemaTracker(2)
tracker.CreateTestDB(nil)
execCreate(t, tracker, sql)

sql = "alter table test.t rename index idx_1 to idx_1_1"
execAlter(t, tracker, sql)

tblInfo := mustTableByName(t, tracker, "test", "t")
expected := "CREATE TABLE `t` (\n" +
" `col_1` json DEFAULT NULL,\n" +
" KEY `idx_1_1` ((cast(`col_1` as char(64) array)))\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
checkShowCreateTable(t, tblInfo, expected)
}

func TestIssue5092(t *testing.T) {
// copy TestIssue5092 in db_integration_test.go
sql := "create table test.t (a int)"
Expand Down