Commit e0a6cfb3 authored by Sergei Petrunia's avatar Sergei Petrunia

MDEV-25078: ALTER INDEX is inconsistent with ADD/DROP/RENAME index

Support IF EXISTS in the command that alter index visibility:

  ALTER TABLE ALTER (KEY|INDEX) [IF EXISTS] index_name [NOT] IGNORED
parent 4e19539c
......@@ -479,3 +479,52 @@ t1 CREATE TABLE `t1` (
KEY `a` (`a`) IGNORED
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
#
# MDEV-25078, part #2: allow IF EXISTS
#
create table t1 (a int, b int, c int, key(a), key(b), key(c));
alter table t1 alter key if exists no_such_key ignored;
Warnings:
Note 1176 Key 'no_such_key' doesn't exist in table 't1'
alter table t1 alter key if exists a ignored;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
KEY `a` (`a`) IGNORED,
KEY `b` (`b`),
KEY `c` (`c`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
alter table t1
alter key if exists no_such_key ignored,
alter key if exists c ignored ;
Warnings:
Note 1176 Key 'no_such_key' doesn't exist in table 't1'
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
KEY `a` (`a`) IGNORED,
KEY `b` (`b`),
KEY `c` (`c`) IGNORED
) ENGINE=MyISAM DEFAULT CHARSET=latin1
alter table t1
alter key if exists no_such_key not ignored,
alter key if exists c not ignored ;
Warnings:
Note 1176 Key 'no_such_key' doesn't exist in table 't1'
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
KEY `a` (`a`) IGNORED,
KEY `b` (`b`),
KEY `c` (`c`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
......@@ -442,3 +442,23 @@ CREATE TABLE t1 (a INT, KEY (a));
ALTER TABLE t1 ALTER KEY a IGNORED;
SHOW CREATE TABLE t1;
DROP TABLE t1;
--echo #
--echo # MDEV-25078, part #2: allow IF EXISTS
--echo #
create table t1 (a int, b int, c int, key(a), key(b), key(c));
alter table t1 alter key if exists no_such_key ignored;
alter table t1 alter key if exists a ignored;
show create table t1;
alter table t1
alter key if exists no_such_key ignored,
alter key if exists c ignored ;
show create table t1;
alter table t1
alter key if exists no_such_key not ignored,
alter key if exists c not ignored ;
show create table t1;
drop table t1;
......@@ -390,13 +390,14 @@ class Alter_rename_key : public Sql_alloc
class Alter_index_ignorability: public Sql_alloc
{
public:
Alter_index_ignorability(const char *name, bool is_ignored) :
m_name(name), m_is_ignored(is_ignored)
Alter_index_ignorability(const char *name, bool is_ignored, bool if_exists) :
m_name(name), m_is_ignored(is_ignored), m_if_exists(if_exists)
{
assert(name != NULL);
}
const char *name() const { return m_name; }
bool if_exists() const { return m_if_exists; }
/* The ignorability after the operation is performed. */
bool is_ignored() const { return m_is_ignored; }
......@@ -406,6 +407,7 @@ class Alter_index_ignorability: public Sql_alloc
private:
const char *m_name;
bool m_is_ignored;
bool m_if_exists;
};
......
......@@ -5829,7 +5829,33 @@ handle_if_exists_options(THD *thd, TABLE *table, Alter_info *alter_info,
rename_key_it.remove();
}
}
/* Handle ALTER KEY IF EXISTS. */
{
List_iterator<Alter_index_ignorability> ignor_it(alter_info->alter_index_ignorability_list);
Alter_index_ignorability *aii;
while ((aii= ignor_it++))
{
if (!aii->if_exists())
continue;
bool exists= false;
for (uint n_key= 0; n_key < table->s->keys; n_key++)
{
if (my_strcasecmp(system_charset_info, aii->name(),
table->key_info[n_key].name.str) == 0)
{
exists= true;
break;
}
}
if (exists)
continue;
push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
ER_KEY_DOES_NOT_EXISTS,
ER_THD(thd, ER_KEY_DOES_NOT_EXISTS),
aii->name(), table->s->table_name.str);
ignor_it.remove();
}
}
/* ALTER TABLE ADD KEY IF NOT EXISTS */
/* ALTER TABLE ADD FOREIGN KEY IF NOT EXISTS */
{
......
......@@ -7832,11 +7832,11 @@ alter_list_item:
if (unlikely(Lex->add_alter_list($4, $7, $3)))
MYSQL_YYABORT;
}
| ALTER key_or_index ident ignorability
| ALTER key_or_index opt_if_exists_table_element ident ignorability
{
LEX *lex= Lex;
Alter_index_ignorability *ac= new (thd->mem_root)
Alter_index_ignorability($3.str, $4);
Alter_index_ignorability($4.str, $5, $3);
if (ac == NULL)
MYSQL_YYABORT;
lex->alter_info.alter_index_ignorability_list.push_back(ac);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment