Skip to content

Commit 245a0ec

Browse files
committed
ddl: fix a bug that Index with empty name can be created unexpectedly
1 parent d216b7b commit 245a0ec

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

pkg/ddl/executor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4916,6 +4916,10 @@ func (e *executor) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast
49164916
if err != nil {
49174917
return errors.Trace(err)
49184918
}
4919+
if len(indexName.L) == 0 {
4920+
// It means that there is already an index exists with same name
4921+
return nil
4922+
}
49194923

49204924
tblInfo := t.Meta()
49214925
finalColumns := make([]*model.ColumnInfo, len(tblInfo.Columns), len(tblInfo.Columns)+len(hiddenCols))

tests/integrationtest/r/table/index.result

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,25 @@ drop table if exists t;
8282
create table t (a int, b int, k varchar(255), primary key (a, b), key k (k));
8383
insert into t values (1, 1, 'abc ');
8484
drop table t;
85+
drop table if exists t;
86+
create table t (
87+
pid char(64) NOT NULL,
88+
another_id int NOT NULL,
89+
field varchar(255),
90+
PRIMARY KEY (`pid`) /*T![clustered_index] CLUSTERED */,
91+
KEY `idx` (`another_id`)
92+
);
93+
CREATE INDEX if not exists idx ON t (`another_id`);
94+
show warnings;
95+
Level Code Message
96+
Note 1061 Duplicate key name 'idx'
97+
show create table t;
98+
Table Create Table
99+
t CREATE TABLE `t` (
100+
`pid` char(64) NOT NULL,
101+
`another_id` int NOT NULL,
102+
`field` varchar(255) DEFAULT NULL,
103+
PRIMARY KEY (`pid`) /*T![clustered_index] CLUSTERED */,
104+
KEY `idx` (`another_id`)
105+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
106+
drop table t;

tests/integrationtest/t/table/index.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,17 @@ drop table if exists t;
5151
create table t (a int, b int, k varchar(255), primary key (a, b), key k (k));
5252
insert into t values (1, 1, 'abc ');
5353
drop table t;
54+
55+
# Test Issue 61265
56+
drop table if exists t;
57+
create table t (
58+
pid char(64) NOT NULL,
59+
another_id int NOT NULL,
60+
field varchar(255),
61+
PRIMARY KEY (`pid`) /*T![clustered_index] CLUSTERED */,
62+
KEY `idx` (`another_id`)
63+
);
64+
CREATE INDEX if not exists idx ON t (`another_id`);
65+
show warnings;
66+
show create table t;
67+
drop table t;

0 commit comments

Comments
 (0)