Skip to content

Commit

Permalink
This is an automated cherry-pick of #48296
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
jiyfhust authored and ti-chi-bot committed Mar 12, 2024
1 parent 1c99441 commit eee3a83
Show file tree
Hide file tree
Showing 4 changed files with 100 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 @@ -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))

0 comments on commit eee3a83

Please sign in to comment.