Skip to content

Commit

Permalink
ddl: make prefix index compatible with mysql 8.0 when prefix length e…
Browse files Browse the repository at this point in the history
…qual to column length (#48296)

close #48295
  • Loading branch information
jiyfhust committed Mar 12, 2024
1 parent 15947c1 commit 5745d3d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func SetIdxColNameOffset(idxCol *model.IndexColumn, changingCol *model.ColumnInf
idxCol.Name = changingCol.Name
idxCol.Offset = changingCol.Offset
canPrefix := types.IsTypePrefixable(changingCol.GetType())
if !canPrefix || (changingCol.GetFlen() < idxCol.Length) {
if !canPrefix || (changingCol.GetFlen() <= idxCol.Length) {
idxCol.Length = types.UnspecifiedLength
}
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ func buildIndexColumns(ctx sessionctx.Context, columns []*model.ColumnInfo, inde
mvIndex = true
}
indexColLen := ip.Length
indexColumnLength, err := getIndexColumnLength(col, ip.Length)
if indexColLen != types.UnspecifiedLength &&
types.IsTypeChar(col.FieldType.GetType()) &&
indexColLen == col.FieldType.GetFlen() {
indexColLen = types.UnspecifiedLength
}
indexColumnLength, err := getIndexColumnLength(col, indexColLen)
if err != nil {
return nil, false, err
}
Expand Down
47 changes: 47 additions & 0 deletions tests/integrationtest/r/table/index.result
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,50 @@ Error 1062 (23000): Duplicate entry 'q' for key 't.idx_a'
update ignore t set a = 'qcc' where a = 'rcc';
Level Code Message
Warning 1062 Duplicate entry 'q' for key 't.idx_a'
drop table if exists t;
create table t (id int, a varchar(64), b varchar(64), c varchar(64), index idx_a(a(64)));
show create table t;
Table Create Table
t CREATE TABLE `t` (
`id` int(11) DEFAULT NULL,
`a` varchar(64) DEFAULT NULL,
`b` varchar(64) DEFAULT NULL,
`c` varchar(64) DEFAULT NULL,
KEY `idx_a` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
alter table t add index idx_b(b(64));
show create table t;
Table Create Table
t CREATE TABLE `t` (
`id` int(11) DEFAULT NULL,
`a` varchar(64) DEFAULT NULL,
`b` varchar(64) DEFAULT NULL,
`c` varchar(64) DEFAULT NULL,
KEY `idx_a` (`a`),
KEY `idx_b` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
alter table t add index idx_c(c(32));
show create table t;
Table Create Table
t CREATE TABLE `t` (
`id` int(11) DEFAULT NULL,
`a` varchar(64) DEFAULT NULL,
`b` varchar(64) DEFAULT NULL,
`c` varchar(64) DEFAULT NULL,
KEY `idx_a` (`a`),
KEY `idx_b` (`b`),
KEY `idx_c` (`c`(32))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
alter table t modify column c varchar(32);
show create table t;
Table Create Table
t CREATE TABLE `t` (
`id` int(11) DEFAULT NULL,
`a` varchar(64) DEFAULT NULL,
`b` varchar(64) DEFAULT NULL,
`c` varchar(32) DEFAULT NULL,
KEY `idx_a` (`a`),
KEY `idx_b` (`b`),
KEY `idx_c` (`c`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table t;
12 changes: 12 additions & 0 deletions tests/integrationtest/t/table/index.test
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ update t set a = 'qcc' where a = 'rcc';
--enable_warnings;
update ignore t set a = 'qcc' where a = 'rcc';
--disable_warnings;

# Test Issue 48295.
drop table if exists t;
create table t (id int, a varchar(64), b varchar(64), c varchar(64), index idx_a(a(64)));
show create table t;
alter table t add index idx_b(b(64));
show create table t;
alter table t add index idx_c(c(32));
show create table t;
alter table t modify column c varchar(32);
show create table t;
drop table t;

0 comments on commit 5745d3d

Please sign in to comment.