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: make prefix index compatible with mysql 8.0 when prefix length equal to column length | tidb-test=pr/2308 #48296

Merged
merged 9 commits into from
Mar 12, 2024
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;