Skip to content

Commit 813e449

Browse files
committed
Bug#20146455: FIND_KEY_CI RETURNS NULL, CAUSES CRASH IN FILL_ALTER_INPLACE_INFO
The problem was that the server could terminate if a generated foreign key index was renamed by the same ALTER TABLE statement that added a new foreign key with the same name. The reason this happened is the server figured out that the two foreign keys would result in two generated keys so similar that one of them was redundant. If the redundant key was the same key that was renamed, the code which handles index renames in fill_alter_inplace_info() could not find the key to rename. This patch fixes the problem by treating a renamed key as if it is no longer generated.
1 parent ffdd2c3 commit 813e449

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

mysql-test/r/alter_table.result

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,3 +3382,33 @@ info: Records: 0 Duplicates: 0 Warnings: 0
33823382
ALTER TABLE t1 ALGORITHM=INPLACE, DROP INDEX fld1,
33833383
ADD FULLTEXT INDEX fld1(fld1);
33843384
DROP TABLE t1;
3385+
#
3386+
# Bug#20146455: FIND_KEY_CI RETURNS NULL, CAUSES CRASH IN
3387+
# FILL_ALTER_INPLACE_INFO
3388+
#
3389+
CREATE TABLE t1 (a INT PRIMARY KEY, b INT,
3390+
FOREIGN KEY (b) REFERENCES t1(a)) ENGINE= MyISAM;
3391+
ALTER TABLE t1 RENAME INDEX b TO w, ADD FOREIGN KEY (b) REFERENCES t1(a);
3392+
SHOW CREATE TABLE t1;
3393+
Table Create Table
3394+
t1 CREATE TABLE `t1` (
3395+
`a` int(11) NOT NULL,
3396+
`b` int(11) DEFAULT NULL,
3397+
PRIMARY KEY (`a`),
3398+
KEY `w` (`b`)
3399+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3400+
DROP TABLE t1;
3401+
CREATE TABLE t1 (a INT PRIMARY KEY, b INT,
3402+
FOREIGN KEY (b) REFERENCES t1(a)) ENGINE= InnoDB;
3403+
ALTER TABLE t1 RENAME INDEX b TO w, ADD FOREIGN KEY (b) REFERENCES t1(a);
3404+
SHOW CREATE TABLE t1;
3405+
Table Create Table
3406+
t1 CREATE TABLE `t1` (
3407+
`a` int(11) NOT NULL,
3408+
`b` int(11) DEFAULT NULL,
3409+
PRIMARY KEY (`a`),
3410+
KEY `w` (`b`),
3411+
CONSTRAINT `t1_ibfk_1` FOREIGN KEY (`b`) REFERENCES `t1` (`a`),
3412+
CONSTRAINT `t1_ibfk_2` FOREIGN KEY (`b`) REFERENCES `t1` (`a`)
3413+
) ENGINE=InnoDB DEFAULT CHARSET=latin1
3414+
DROP TABLE t1;

mysql-test/t/alter_table.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,3 +2684,21 @@ ALTER TABLE t1 DROP INDEX fld1, ADD FULLTEXT INDEX fld1(fld1);
26842684
ALTER TABLE t1 ALGORITHM=INPLACE, DROP INDEX fld1,
26852685
ADD FULLTEXT INDEX fld1(fld1);
26862686
DROP TABLE t1;
2687+
2688+
2689+
--echo #
2690+
--echo # Bug#20146455: FIND_KEY_CI RETURNS NULL, CAUSES CRASH IN
2691+
--echo # FILL_ALTER_INPLACE_INFO
2692+
--echo #
2693+
2694+
CREATE TABLE t1 (a INT PRIMARY KEY, b INT,
2695+
FOREIGN KEY (b) REFERENCES t1(a)) ENGINE= MyISAM;
2696+
ALTER TABLE t1 RENAME INDEX b TO w, ADD FOREIGN KEY (b) REFERENCES t1(a);
2697+
SHOW CREATE TABLE t1;
2698+
DROP TABLE t1;
2699+
2700+
CREATE TABLE t1 (a INT PRIMARY KEY, b INT,
2701+
FOREIGN KEY (b) REFERENCES t1(a)) ENGINE= InnoDB;
2702+
ALTER TABLE t1 RENAME INDEX b TO w, ADD FOREIGN KEY (b) REFERENCES t1(a);
2703+
SHOW CREATE TABLE t1;
2704+
DROP TABLE t1;

sql/sql_table.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7888,6 +7888,13 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
78887888

78897889
key_name= rename_key->new_name;
78907890
rename_key_it.remove();
7891+
/*
7892+
If the user has explicitly renamed the key, we should no longer
7893+
treat it as generated. Otherwise this key might be automatically
7894+
dropped by mysql_prepare_create_table() and this will confuse
7895+
code in fill_alter_inplace_info().
7896+
*/
7897+
key_info->flags &= ~HA_GENERATED_KEY;
78917898
break;
78927899
}
78937900
}

0 commit comments

Comments
 (0)