diff --git a/mysql-test/r/alter_table.result b/mysql-test/r/alter_table.result index d1df0aced63..5a2b48219af 100644 --- a/mysql-test/r/alter_table.result +++ b/mysql-test/r/alter_table.result @@ -1893,3 +1893,9 @@ DROP TABLE ti3; CREATE TABLE tm1(i INT DEFAULT 1) engine=MyISAM; ALTER TABLE tm1 ADD INDEX ii1(i), ALTER COLUMN i DROP DEFAULT; DROP TABLE tm1; +create table if not exists t1 (i int); +alter table t1 add key (i); +alter table t1 add key if not exists (i); +Warnings: +Note 1061 Duplicate key name 'i' +DROP TABLE t1; diff --git a/mysql-test/t/alter_table.test b/mysql-test/t/alter_table.test index f40f8c11fac..b0b017f2b70 100644 --- a/mysql-test/t/alter_table.test +++ b/mysql-test/t/alter_table.test @@ -1634,3 +1634,12 @@ DROP TABLE ti3; CREATE TABLE tm1(i INT DEFAULT 1) engine=MyISAM; ALTER TABLE tm1 ADD INDEX ii1(i), ALTER COLUMN i DROP DEFAULT; DROP TABLE tm1; + +# +# MDEV-4435 Server crashes in my_strcasecmp_utf8 on ADD KEY IF NOT EXISTS with implicit name when the key exists. +# +create table if not exists t1 (i int); +alter table t1 add key (i); +alter table t1 add key if not exists (i); +DROP TABLE t1; + diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 887aef643d2..97d33055455 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -5389,17 +5389,29 @@ handle_if_exists_options(THD *thd, TABLE *table, Alter_info *alter_info) Key *key; List_iterator key_it(alter_info->key_list); uint n_key; + const char *keyname; while ((key=key_it++)) { if (!key->create_if_not_exists) continue; + /* If the name of the key is not specified, */ + /* let us check the name of the first key part. */ + if ((keyname= key->name.str) == NULL) + { + List_iterator part_it(key->columns); + Key_part_spec *kp; + if ((kp= part_it++)) + keyname= kp->field_name.str; + if (keyname == NULL) + continue; + } for (n_key=0; n_key < table->s->keys; n_key++) { if (my_strcasecmp(system_charset_info, - key->name.str, table->key_info[n_key].name) == 0) + keyname, table->key_info[n_key].name) == 0) { push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE, - ER_DUP_KEYNAME, ER(ER_DUP_KEYNAME), key->name.str); + ER_DUP_KEYNAME, ER(ER_DUP_KEYNAME), keyname); key_it.remove(); if (key->type == Key::FOREIGN_KEY) { @@ -7924,7 +7936,8 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, if (alter_info->flags == 0) { my_snprintf(alter_ctx.tmp_name, sizeof(alter_ctx.tmp_name), - ER(ER_INSERT_INFO), 0L, 0L, 0L); + ER(ER_INSERT_INFO), 0L, 0L, + thd->get_stmt_da()->current_statement_warn_count()); my_ok(thd, 0L, 0L, alter_ctx.tmp_name); DBUG_RETURN(false); }