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) #51707

Closed
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
2 changes: 1 addition & 1 deletion pkg/ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,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 @@ -103,7 +103,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
65 changes: 65 additions & 0 deletions tests/integrationtest/r/table/index.result
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,68 @@ insert into t values (4, 3, 3);
Error 1062 (23000): Duplicate entry '3' for key 't.v2'
set @@tidb_txn_assertion_level=default;
set @@tidb_constraint_check_in_place=default;
<<<<<<< HEAD
=======
drop table if exists t;
create table t(a varchar(20), b varchar(20), unique index idx_a(a(1)));
insert into t values ('qaa', 'abc');
insert into t values ('qbb', 'xyz');
Error 1062 (23000): Duplicate entry 'q' for key 't.idx_a'
insert into t values ('rcc', 'xyz');
select * from t order by a;
a b
qaa abc
rcc xyz
update t set a = 'qcc' where a = 'rcc';
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;
>>>>>>> 5745d3df9de (ddl: make prefix index compatible with mysql 8.0 when prefix length equal to column length (#48296))
28 changes: 28 additions & 0 deletions tests/integrationtest/t/table/index.test
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,31 @@ insert into t values (4, 3, 3);
set @@tidb_txn_assertion_level=default;
set @@tidb_constraint_check_in_place=default;

<<<<<<< HEAD
=======
# TestDuplicateErrorOnPrefixIndex, Issue: #44316.
drop table if exists t;
create table t(a varchar(20), b varchar(20), unique index idx_a(a(1)));
insert into t values ('qaa', 'abc');
-- error 1062
insert into t values ('qbb', 'xyz');
insert into t values ('rcc', 'xyz');
select * from t order by a;
-- error 1062
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;
>>>>>>> 5745d3df9de (ddl: make prefix index compatible with mysql 8.0 when prefix length equal to column length (#48296))