Skip to content

Commit 431a740

Browse files
committed
MDEV-21889 IF EXISTS clause does not work for RENAME COLUMN and RENAME INDEX
1 parent 6fb3e83 commit 431a740

7 files changed

Lines changed: 54 additions & 12 deletions

File tree

‎mysql-test/main/alter_table.result‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,11 @@ t1 CREATE TABLE `t1` (
25942594
`c` float DEFAULT NULL
25952595
) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1
25962596
ALTER TABLE t1 RENAME COLUMN a TO m;
2597+
ALTER TABLE t1 RENAME COLUMN a TO m;
2598+
ERROR 42S22: Unknown column 'a' in 't1'
2599+
ALTER TABLE t1 RENAME COLUMN IF EXISTS a TO m;
2600+
Warnings:
2601+
Note 1054 Unknown column 'a' in 't1'
25972602
SHOW CREATE TABLE t1;
25982603
Table Create Table
25992604
t1 CREATE TABLE `t1` (
@@ -2882,6 +2887,9 @@ t1 CREATE TABLE `t1` (
28822887
# exists, dropped or added within the same ALTER TABLE.
28832888
alter table t1 rename key d to e;
28842889
ERROR 42000: Key 'd' doesn't exist in table 't1'
2890+
alter table t1 rename key if exists d to e;
2891+
Warnings:
2892+
Note 1176 Key 'd' doesn't exist in table 't1'
28852893
show create table t1;
28862894
Table Create Table
28872895
t1 CREATE TABLE `t1` (

‎mysql-test/main/alter_table.test‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,9 @@ ALTER TABLE t1 RENAME COLUMN a TO a;
21102110
--replace_result $default_engine DEFAULT_ENGINE " PAGE_CHECKSUM=1" ""
21112111
SHOW CREATE TABLE t1;
21122112
ALTER TABLE t1 RENAME COLUMN a TO m;
2113+
--error ER_BAD_FIELD_ERROR
2114+
ALTER TABLE t1 RENAME COLUMN a TO m;
2115+
ALTER TABLE t1 RENAME COLUMN IF EXISTS a TO m;
21132116
--replace_result $default_engine DEFAULT_ENGINE " PAGE_CHECKSUM=1" ""
21142117
SHOW CREATE TABLE t1;
21152118
SELECT * FROM t1;
@@ -2293,6 +2296,7 @@ show create table t1;
22932296
--echo # exists, dropped or added within the same ALTER TABLE.
22942297
--error ER_KEY_DOES_NOT_EXISTS
22952298
alter table t1 rename key d to e;
2299+
alter table t1 rename key if exists d to e;
22962300
show create table t1;
22972301
--error ER_KEY_DOES_NOT_EXISTS
22982302
alter table t1 drop key c, rename key c to d;

‎sql/sql_class.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ class Alter_column :public Sql_alloc {
341341
bool alter_if_exists;
342342
Alter_column(LEX_CSTRING par_name, Virtual_column_info *expr, bool par_exists)
343343
:name(par_name), new_name{NULL, 0}, default_value(expr), alter_if_exists(par_exists) {}
344-
Alter_column(LEX_CSTRING par_name, LEX_CSTRING _new_name)
345-
:name(par_name), new_name(_new_name), default_value(NULL), alter_if_exists(false) {}
344+
Alter_column(LEX_CSTRING par_name, LEX_CSTRING _new_name, bool exists)
345+
:name(par_name), new_name(_new_name), default_value(NULL), alter_if_exists(exists) {}
346346
/**
347347
Used to make a clone of this object for ALTER/CREATE TABLE
348348
@sa comment for Key_part_spec::clone
@@ -362,9 +362,10 @@ class Alter_rename_key : public Sql_alloc
362362
public:
363363
LEX_CSTRING old_name;
364364
LEX_CSTRING new_name;
365+
bool alter_if_exists;
365366

366-
Alter_rename_key(LEX_CSTRING old_name_arg, LEX_CSTRING new_name_arg)
367-
: old_name(old_name_arg), new_name(new_name_arg) {}
367+
Alter_rename_key(LEX_CSTRING old_name_arg, LEX_CSTRING new_name_arg, bool exists)
368+
: old_name(old_name_arg), new_name(new_name_arg), alter_if_exists(exists) {}
368369

369370
Alter_rename_key *clone(MEM_ROOT *mem_root) const
370371
{ return new (mem_root) Alter_rename_key(*this); }

‎sql/sql_lex.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ bool LEX::add_alter_list(LEX_CSTRING name, Virtual_column_info *expr,
524524
}
525525

526526

527-
bool LEX::add_alter_list(LEX_CSTRING name, LEX_CSTRING new_name)
527+
bool LEX::add_alter_list(LEX_CSTRING name, LEX_CSTRING new_name, bool exists)
528528
{
529-
Alter_column *ac= new (thd->mem_root) Alter_column(name, new_name);
529+
Alter_column *ac= new (thd->mem_root) Alter_column(name, new_name, exists);
530530
if (unlikely(ac == NULL))
531531
return true;
532532
alter_info.alter_list.push_back(ac, thd->mem_root);

‎sql/sql_lex.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4323,7 +4323,7 @@ struct LEX: public Query_tables_list
43234323
}
43244324
bool add_alter_list(LEX_CSTRING par_name, Virtual_column_info *expr,
43254325
bool par_exists);
4326-
bool add_alter_list(LEX_CSTRING name, LEX_CSTRING new_name);
4326+
bool add_alter_list(LEX_CSTRING name, LEX_CSTRING new_name, bool exists);
43274327
void set_command(enum_sql_command command,
43284328
DDL_options_st options)
43294329
{

‎sql/sql_table.cc‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6256,7 +6256,7 @@ handle_if_exists_options(THD *thd, TABLE *table, Alter_info *alter_info,
62566256
}
62576257
}
62586258

6259-
/* Handle ALTER COLUMN IF EXISTS SET/DROP DEFAULT. */
6259+
/* Handle ALTER/RENAME COLUMN IF EXISTS. */
62606260
{
62616261
List_iterator<Alter_column> it(alter_info->alter_list);
62626262
Alter_column *acol;
@@ -6424,6 +6424,35 @@ handle_if_exists_options(THD *thd, TABLE *table, Alter_info *alter_info,
64246424
alter_info->flags|= left_flags;
64256425
}
64266426

6427+
/* Handle RENAME KEY IF EXISTS. */
6428+
{
6429+
List_iterator<Alter_rename_key> rename_key_it(alter_info->alter_rename_key_list);
6430+
Alter_rename_key *rename_key;
6431+
while ((rename_key= rename_key_it++))
6432+
{
6433+
if (!rename_key->alter_if_exists)
6434+
continue;
6435+
bool exists= false;
6436+
for (uint n_key= 0; n_key < table->s->keys; n_key++)
6437+
{
6438+
if (my_strcasecmp(system_charset_info,
6439+
rename_key->old_name.str,
6440+
table->key_info[n_key].name.str) == 0)
6441+
{
6442+
exists= true;
6443+
break;
6444+
}
6445+
}
6446+
if (exists)
6447+
continue;
6448+
push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
6449+
ER_KEY_DOES_NOT_EXISTS,
6450+
ER_THD(thd, ER_KEY_DOES_NOT_EXISTS),
6451+
rename_key->old_name.str, table->s->table_name.str);
6452+
rename_key_it.remove();
6453+
}
6454+
}
6455+
64276456
/* ALTER TABLE ADD KEY IF NOT EXISTS */
64286457
/* ALTER TABLE ADD FOREIGN KEY IF NOT EXISTS */
64296458
{

‎sql/sql_yacc.yy‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7757,16 +7757,16 @@ alter_list_item:
77577757
lex->name= $3->table;
77587758
lex->alter_info.flags|= ALTER_RENAME;
77597759
}
7760-
| RENAME COLUMN_SYM ident TO_SYM ident
7760+
| RENAME COLUMN_SYM opt_if_exists_table_element ident TO_SYM ident
77617761
{
7762-
if (unlikely(Lex->add_alter_list($3, $5)))
7762+
if (unlikely(Lex->add_alter_list($4, $6, $3)))
77637763
MYSQL_YYABORT;
77647764
}
7765-
| RENAME key_or_index field_ident TO_SYM field_ident
7765+
| RENAME key_or_index opt_if_exists_table_element field_ident TO_SYM field_ident
77667766
{
77677767
LEX *lex=Lex;
77687768
Alter_rename_key *ak= new (thd->mem_root)
7769-
Alter_rename_key($3, $5);
7769+
Alter_rename_key($4, $6, $3);
77707770
if (ak == NULL)
77717771
MYSQL_YYABORT;
77727772
lex->alter_info.alter_rename_key_list.push_back(ak);

0 commit comments

Comments
 (0)