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 rename index for expression indexes (#51984) #52580

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
9 changes: 9 additions & 0 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -2041,3 +2041,12 @@
}
return columnName[:pos]
}

func getExpressionIndexOriginName(expressionIdx *model.ColumnInfo) string {
columnName := strings.TrimPrefix(expressionIdx.Name.O, expressionIndexPrefix+"_")
var pos int
if pos = strings.LastIndex(columnName, "_"); pos == -1 {
return columnName
}
return columnName[:pos]

Check warning on line 2051 in ddl/column.go

View check run for this annotation

Codecov / codecov/patch

ddl/column.go#L2045-L2051

Added lines #L2045 - L2051 were not covered by tests
}
11 changes: 11 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@
}

renameIndexes(tblInfo, from, to)
renameHiddenColumns(tblInfo, from, to)

if ver, err = updateVersionAndTableInfo(d, t, job, tblInfo, true); err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
Expand Down Expand Up @@ -2284,3 +2286,12 @@
}
}
}

func renameHiddenColumns(tblInfo *model.TableInfo, from, to model.CIStr) {
for _, col := range tblInfo.Columns {
if col.Hidden && getExpressionIndexOriginName(col) == 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)
}

Check warning on line 2295 in ddl/index.go

View check run for this annotation

Codecov / codecov/patch

ddl/index.go#L2293-L2295

Added lines #L2293 - L2295 were not covered by tests
}
}
27 changes: 27 additions & 0 deletions tests/integrationtest/r/ddl/db_rename.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
drop table if exists t;
create table t (pk int primary key, c int default 1, c1 int default 1, unique key k1(c), key k2(c1));
alter table t rename index k1 to k3;
admin check index t k3;
alter table t rename index k3 to k3;
admin check index t k3;
alter table t rename index x to x;
Error 1176 (42000): Key 'x' doesn't exist in table 't'
alter table t rename index k3 to k2;
Error 1061 (42000): Duplicate key name 'k2'
alter table t rename index k2 to K2;
alter table t rename key k3 to K2;
Error 1061 (42000): Duplicate key name 'K2'
drop table t;
create table t(j json);
alter table t add index idx1((cast(j as char(10) array)));
alter table t rename index idx1 to idx2;
alter table t add index idx1((cast(j as char(10) array)));
insert into t values ('["1"]');
alter table t add index IDX3((cast(j as char(10) array)));
alter table t rename index IDX3 to IDX4;
alter table t add index IDX3((cast(j as char(10) array)));
insert into t values ('["2"]');
select * from t;
j
["1"]
["2"]
27 changes: 27 additions & 0 deletions tests/integrationtest/t/ddl/db_rename.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# TestRenameIndex
drop table if exists t;
create table t (pk int primary key, c int default 1, c1 int default 1, unique key k1(c), key k2(c1));
alter table t rename index k1 to k3;
admin check index t k3;
alter table t rename index k3 to k3;
admin check index t k3;
-- error 1176
alter table t rename index x to x;
-- error 1061
alter table t rename index k3 to k2;
alter table t rename index k2 to K2;
-- error 1061
alter table t rename key k3 to K2;

# TestIssue51431
drop table t;
create table t(j json);
alter table t add index idx1((cast(j as char(10) array)));
alter table t rename index idx1 to idx2;
alter table t add index idx1((cast(j as char(10) array)));
insert into t values ('["1"]');
alter table t add index IDX3((cast(j as char(10) array)));
alter table t rename index IDX3 to IDX4;
alter table t add index IDX3((cast(j as char(10) array)));
insert into t values ('["2"]');
select * from t;