Commit 193725b8 authored by Aleksey Midenkov's avatar Aleksey Midenkov

MDEV-7318 RENAME INDEX

This patch adds support of RENAME INDEX operation to the ALTER TABLE
statement. Code which determines if ALTER TABLE can be done in-place
for "simple" storage engines like MyISAM, Heap and etc. was updated to
handle ALTER TABLE ... RENAME INDEX as an in-place operation. Support
for in-place ALTER TABLE ... RENAME INDEX for InnoDB was covered by
MDEV-13301.

Syntax changes
==============

A new type of <alter_specification> is added:

<rename index clause> ::= RENAME ( INDEX | KEY ) <oldname> TO <newname>

Where <oldname> and <newname> are identifiers for old name and new
name of the index.

Semantic changes
================

The result of "ALTER TABLE t1 RENAME INDEX a TO b" is a table which
contents and structure are identical to the old version of 't1' with
the only exception index 'a' being called 'b'.

Neither <oldname> nor <newname> can be "primary". The index being
renamed should exist and its new name should not be occupied
by another index on the same table.

Related to: WL#6555, MDEV-13301
parent fa8ad754
This diff is collapsed.
...@@ -2273,6 +2273,186 @@ DROP VIEW v1; ...@@ -2273,6 +2273,186 @@ DROP VIEW v1;
DROP TABLE t3,t1,t2; DROP TABLE t3,t1,t2;
SET DEFAULT_STORAGE_ENGINE= @save_default_engine; SET DEFAULT_STORAGE_ENGINE= @save_default_engine;
--echo #
--echo # MDEV-7318 RENAME INDEX
--echo #
--echo #
--echo # 1) Tests for syntax and semantics of ALTER TABLE RENAME
--echo # KEY/INDEX result.
--echo #
--echo # 1.a) Both RENAME KEY and RENAME INDEX variants should be
--echo # allowed and produce expected results.
create table t1 (pk int primary key, i int, j int, key a(i));
alter table t1 rename key a to b;
show create table t1;
alter table t1 rename index b to c;
show create table t1;
--echo # 1.b) It should be impossible to rename index that doesn't
--echo # exists, dropped or added within the same ALTER TABLE.
--error ER_KEY_DOES_NOT_EXITS
alter table t1 rename key d to e;
show create table t1;
--error ER_KEY_DOES_NOT_EXITS
alter table t1 drop key c, rename key c to d;
show create table t1;
--error ER_KEY_DOES_NOT_EXITS
alter table t1 add key d(j), rename key d to e;
show create table t1;
--echo # 1.c) It should be impossible to rename index to a name
--echo # which is already used by another index, or is used
--echo # by index which is added within the same ALTER TABLE.
alter table t1 add key d(j);
--error ER_DUP_KEYNAME
alter table t1 rename key c to d;
show create table t1;
alter table t1 drop key d;
--error ER_DUP_KEYNAME
alter table t1 add key d(j), rename key c to d;
show create table t1;
--echo # 1.d) It should be possible to rename index to a name
--echo # which belongs to index which is dropped within the
--echo # same ALTER TABLE.
alter table t1 add key d(j);
alter table t1 drop key c, rename key d to c;
show create table t1;
--echo # 1.e) We disallow renaming from/to PRIMARY as it might
--echo # lead to some other key becoming "primary" internally,
--echo # which will be interpreted as dropping/addition of
--echo # primary key.
--error ER_PARSE_ERROR
alter table t1 rename key primary to d;
show create table t1;
--echo # Even using 'funny' syntax.
--error ER_WRONG_NAME_FOR_INDEX
alter table t1 rename key `primary` to d;
show create table t1;
--error ER_PARSE_ERROR
alter table t1 rename key c to primary;
show create table t1;
--error ER_WRONG_NAME_FOR_INDEX
alter table t1 rename key c to `primary`;
show create table t1;
drop table t1;
--echo #
--echo # 2) More complex tests for semantics of ALTER TABLE.
--echo #
--echo # 2.a) Check that standalone RENAME KEY works as expected
--echo # for unique and non-unique indexes.
create table t1 (a int, unique u(a), b int, key k(b));
alter table t1 rename key u to uu;
show create table t1;
alter table t1 rename key k to kk;
show create table t1;
--echo # 2.b) Check how that this clause can be mixed with other
--echo # clauses which don't affect key or its columns.
alter table t1 rename key kk to kkk, add column c int;
show create table t1;
alter table t1 rename key uu to uuu, add key c(c);
show create table t1;
alter table t1 rename key kkk to k, drop key uuu;
show create table t1;
alter table t1 rename key k to kk, rename to t2;
show create table t2;
drop table t2;
--echo #
--echo # 3) Test coverage for handling of RENAME INDEX clause in
--echo # various storage engines and using different ALTER
--echo # algorithm.
--echo #
--echo # 3.a) Test coverage for simple storage engines (MyISAM/Heap).
create table t1 (i int, key k(i)) engine=myisam;
insert into t1 values (1);
create table t2 (i int, key k(i)) engine=memory;
insert into t2 values (1);
--echo # MyISAM and Heap should be able to handle key renaming in-place.
alter table t1 algorithm=inplace, rename key k to kk;
alter table t2 algorithm=inplace, rename key k to kk;
show create table t1;
show create table t2;
--echo # So by default in-place algorithm should be chosen.
--echo # (ALTER TABLE should report 0 rows affected).
--enable_info
alter table t1 rename key kk to kkk;
alter table t2 rename key kk to kkk;
--disable_info
show create table t1;
show create table t2;
--echo # Copy algorithm should work as well.
alter table t1 algorithm=copy, rename key kkk to kkkk;
alter table t2 algorithm=copy, rename key kkk to kkkk;
show create table t1;
show create table t2;
--echo # When renaming is combined with other in-place operation
--echo # it still works as expected (i.e. works in-place).
alter table t1 algorithm=inplace, rename key kkkk to k, alter column i set default 100;
alter table t2 algorithm=inplace, rename key kkkk to k, alter column i set default 100;
show create table t1;
show create table t2;
--echo # Combining with non-inplace operation results in the whole ALTER
--echo # becoming non-inplace.
--error ER_ALTER_OPERATION_NOT_SUPPORTED
alter table t1 algorithm=inplace, rename key k to kk, add column j int;
--error ER_ALTER_OPERATION_NOT_SUPPORTED
alter table t2 algorithm=inplace, rename key k to kk, add column j int;
drop table t1, t2;
--echo # 3.b) Basic tests for InnoDB. More tests can be found in
--echo # innodb.innodb_rename_index*
create table t1 (i int, key k(i)) engine=innodb;
insert into t1 values (1);
--echo # Basic rename, inplace algorithm should be chosen
--enable_info
alter table t1 algorithm=inplace, rename key k to kk;
--disable_info
show create table t1;
--echo # copy algorithm should work as well.
--enable_info
alter table t1 algorithm=copy, rename key kk to kkk;
--disable_info
show create table t1;
drop table t1;
--echo #
--echo # 4) Additional coverage for complex cases in which code
--echo # in ALTER TABLE comparing old and new table version
--echo # got confused.
--echo #
--echo # Once InnoDB starts to support in-place index renaming the result
--echo # of below statements should stay the same. Information about
--echo # indexes returned by SHOW CREATE TABLE (from .FRM) and by
--echo # InnoDB (from InnoDB data-dictionary) should be consistent.
--echo #
create table t1 ( a int, b int, c int, d int,
primary key (a), index i1 (b), index i2 (c) ) engine=innodb;
alter table t1 add index i1 (d), rename index i1 to x;
show create table t1;
select i.name as k, f.name as c from information_schema.innodb_sys_tables as t,
information_schema.innodb_sys_indexes as i,
information_schema.innodb_sys_fields as f
where t.name='test/t1' and t.table_id = i.table_id and i.index_id = f.index_id
order by k, c;
drop table t1;
create table t1 (a int, b int, c int, d int,
primary key (a), index i1 (b), index i2 (c)) engine=innodb;
alter table t1 add index i1 (d), rename index i1 to i2, drop index i2;
show create table t1;
select i.name as k, f.name as c from information_schema.innodb_sys_tables as t,
information_schema.innodb_sys_indexes as i,
information_schema.innodb_sys_fields as f
where t.name='test/t1' and t.table_id = i.table_id and i.index_id = f.index_id
order by k, c;
drop table t1;
--echo # --echo #
--echo # End of 10.5 tests --echo # End of 10.5 tests
--echo # --echo #
...@@ -98,3 +98,12 @@ disconnect con1; ...@@ -98,3 +98,12 @@ disconnect con1;
connection default; connection default;
UNLOCK TABLES; UNLOCK TABLES;
DROP TABLE t1; DROP TABLE t1;
#
# MDEV-7318 RENAME INDEX
#
CREATE TABLE t (c1 INT, c2 INT, KEY i2 (c2)) ENGINE=INNODB;
SET DEBUG_DBUG= '+d,ib_rename_index_fail1';
ALTER TABLE t RENAME INDEX i2 to x, ALGORITHM=INPLACE;
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
SET DEBUG_DBUG= '-d,ib_rename_index_fail1';
DROP TABLE t;
This diff is collapsed.
...@@ -132,3 +132,15 @@ DROP TABLE t1; ...@@ -132,3 +132,15 @@ DROP TABLE t1;
# Wait till all disconnects are completed # Wait till all disconnects are completed
--source include/wait_until_count_sessions.inc --source include/wait_until_count_sessions.inc
--echo #
--echo # MDEV-7318 RENAME INDEX
--echo #
CREATE TABLE t (c1 INT, c2 INT, KEY i2 (c2)) ENGINE=INNODB;
SET DEBUG_DBUG= '+d,ib_rename_index_fail1';
-- error ER_LOCK_DEADLOCK
ALTER TABLE t RENAME INDEX i2 to x, ALGORITHM=INPLACE;
SET DEBUG_DBUG= '-d,ib_rename_index_fail1';
DROP TABLE t;
This diff is collapsed.
...@@ -4585,7 +4585,8 @@ handler::check_if_supported_inplace_alter(TABLE *altered_table, ...@@ -4585,7 +4585,8 @@ handler::check_if_supported_inplace_alter(TABLE *altered_table,
ALTER_DROP_CHECK_CONSTRAINT | ALTER_DROP_CHECK_CONSTRAINT |
ALTER_PARTITIONED | ALTER_PARTITIONED |
ALTER_VIRTUAL_GCOL_EXPR | ALTER_VIRTUAL_GCOL_EXPR |
ALTER_RENAME; ALTER_RENAME |
ALTER_RENAME_INDEX;
/* Is there at least one operation that requires copy algorithm? */ /* Is there at least one operation that requires copy algorithm? */
if (ha_alter_info->handler_flags & ~inplace_offline_operations) if (ha_alter_info->handler_flags & ~inplace_offline_operations)
......
...@@ -25,6 +25,7 @@ Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root) ...@@ -25,6 +25,7 @@ Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
:drop_list(rhs.drop_list, mem_root), :drop_list(rhs.drop_list, mem_root),
alter_list(rhs.alter_list, mem_root), alter_list(rhs.alter_list, mem_root),
key_list(rhs.key_list, mem_root), key_list(rhs.key_list, mem_root),
alter_rename_key_list(rhs.alter_rename_key_list, mem_root),
create_list(rhs.create_list, mem_root), create_list(rhs.create_list, mem_root),
check_constraint_list(rhs.check_constraint_list, mem_root), check_constraint_list(rhs.check_constraint_list, mem_root),
flags(rhs.flags), partition_flags(rhs.partition_flags), flags(rhs.flags), partition_flags(rhs.partition_flags),
...@@ -46,6 +47,7 @@ Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root) ...@@ -46,6 +47,7 @@ Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
list_copy_and_replace_each_value(drop_list, mem_root); list_copy_and_replace_each_value(drop_list, mem_root);
list_copy_and_replace_each_value(alter_list, mem_root); list_copy_and_replace_each_value(alter_list, mem_root);
list_copy_and_replace_each_value(key_list, mem_root); list_copy_and_replace_each_value(key_list, mem_root);
list_copy_and_replace_each_value(alter_rename_key_list, mem_root);
list_copy_and_replace_each_value(create_list, mem_root); list_copy_and_replace_each_value(create_list, mem_root);
/* partition_names are not deeply copied currently */ /* partition_names are not deeply copied currently */
} }
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
class Alter_drop; class Alter_drop;
class Alter_column; class Alter_column;
class Alter_rename_key;
class Key; class Key;
/** /**
...@@ -87,6 +88,8 @@ class Alter_info ...@@ -87,6 +88,8 @@ class Alter_info
List<Alter_column> alter_list; List<Alter_column> alter_list;
// List of keys, used by both CREATE and ALTER TABLE. // List of keys, used by both CREATE and ALTER TABLE.
List<Key> key_list; List<Key> key_list;
// List of keys to be renamed.
List<Alter_rename_key> alter_rename_key_list;
// List of columns, used by both CREATE and ALTER TABLE. // List of columns, used by both CREATE and ALTER TABLE.
List<Create_field> create_list; List<Create_field> create_list;
...@@ -123,6 +126,7 @@ class Alter_info ...@@ -123,6 +126,7 @@ class Alter_info
drop_list.empty(); drop_list.empty();
alter_list.empty(); alter_list.empty();
key_list.empty(); key_list.empty();
alter_rename_key_list.empty();
create_list.empty(); create_list.empty();
check_constraint_list.empty(); check_constraint_list.empty();
flags= 0; flags= 0;
......
...@@ -355,6 +355,21 @@ class Alter_column :public Sql_alloc { ...@@ -355,6 +355,21 @@ class Alter_column :public Sql_alloc {
}; };
class Alter_rename_key : public Sql_alloc
{
public:
LEX_CSTRING old_name;
LEX_CSTRING new_name;
Alter_rename_key(LEX_CSTRING old_name_arg, LEX_CSTRING new_name_arg)
: old_name(old_name_arg), new_name(new_name_arg) {}
Alter_rename_key *clone(MEM_ROOT *mem_root) const
{ return new (mem_root) Alter_rename_key(*this); }
};
class Key :public Sql_alloc, public DDL_options { class Key :public Sql_alloc, public DDL_options {
public: public:
enum Keytype { PRIMARY, UNIQUE, MULTIPLE, FULLTEXT, SPATIAL, FOREIGN_KEY}; enum Keytype { PRIMARY, UNIQUE, MULTIPLE, FULLTEXT, SPATIAL, FOREIGN_KEY};
......
...@@ -6601,7 +6601,7 @@ static int compare_uint(const uint *s, const uint *t) ...@@ -6601,7 +6601,7 @@ static int compare_uint(const uint *s, const uint *t)
enum class Compare_keys : uint32_t enum class Compare_keys : uint32_t
{ {
Equal, Equal= 0,
EqualButKeyPartLength, EqualButKeyPartLength,
EqualButComment, EqualButComment,
NotEqual NotEqual
...@@ -7998,6 +7998,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table, ...@@ -7998,6 +7998,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
List<Create_field> new_create_list; List<Create_field> new_create_list;
/* New key definitions are added here */ /* New key definitions are added here */
List<Key> new_key_list; List<Key> new_key_list;
List<Alter_rename_key> rename_key_list(alter_info->alter_rename_key_list);
List_iterator<Alter_drop> drop_it(alter_info->drop_list); List_iterator<Alter_drop> drop_it(alter_info->drop_list);
List_iterator<Create_field> def_it(alter_info->create_list); List_iterator<Create_field> def_it(alter_info->create_list);
List_iterator<Alter_column> alter_it(alter_info->alter_list); List_iterator<Alter_column> alter_it(alter_info->alter_list);
...@@ -8446,6 +8447,39 @@ mysql_prepare_alter_table(THD *thd, TABLE *table, ...@@ -8446,6 +8447,39 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
continue; continue;
} }
/* If this index is to stay in the table check if it has to be renamed. */
List_iterator<Alter_rename_key> rename_key_it(rename_key_list);
Alter_rename_key *rename_key;
while ((rename_key= rename_key_it++))
{
if (!my_strcasecmp(system_charset_info, key_name, rename_key->old_name.str))
{
if (!my_strcasecmp(system_charset_info, key_name, primary_key_name))
{
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), rename_key->old_name.str);
goto err;
}
else if (!my_strcasecmp(system_charset_info, rename_key->new_name.str,
primary_key_name))
{
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), rename_key->new_name.str);
goto err;
}
key_name= rename_key->new_name.str;
rename_key_it.remove();
/*
If the user has explicitly renamed the key, we should no longer
treat it as generated. Otherwise this key might be automatically
dropped by mysql_prepare_create_table() and this will confuse
code in fill_alter_inplace_info().
*/
key_info->flags&= ~HA_GENERATED_KEY;
break;
}
}
if (key_info->algorithm == HA_KEY_ALG_LONG_HASH) if (key_info->algorithm == HA_KEY_ALG_LONG_HASH)
{ {
setup_keyinfo_hash(key_info); setup_keyinfo_hash(key_info);
...@@ -8772,6 +8806,13 @@ mysql_prepare_alter_table(THD *thd, TABLE *table, ...@@ -8772,6 +8806,13 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
} }
} }
if (rename_key_list.elements)
{
my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), rename_key_list.head()->old_name.str,
table->s->table_name.str);
goto err;
}
if (!create_info->comment.str) if (!create_info->comment.str)
{ {
create_info->comment.str= table->s->comment.str; create_info->comment.str= table->s->comment.str;
......
...@@ -7732,6 +7732,16 @@ alter_list_item: ...@@ -7732,6 +7732,16 @@ alter_list_item:
if (unlikely(Lex->add_alter_list($3, $5))) if (unlikely(Lex->add_alter_list($3, $5)))
MYSQL_YYABORT; MYSQL_YYABORT;
} }
| RENAME key_or_index field_ident TO_SYM field_ident
{
LEX *lex=Lex;
Alter_rename_key *ak= new (thd->mem_root)
Alter_rename_key($3, $5);
if (ak == NULL)
MYSQL_YYABORT;
lex->alter_info.alter_rename_key_list.push_back(ak);
lex->alter_info.flags|= ALTER_RENAME_INDEX;
}
| CONVERT_SYM TO_SYM charset charset_name_or_default opt_collate | CONVERT_SYM TO_SYM charset charset_name_or_default opt_collate
{ {
if (!$4) if (!$4)
......
...@@ -3330,6 +3330,14 @@ innobase_check_index_keys( ...@@ -3330,6 +3330,14 @@ innobase_check_index_keys(
} }
} }
for (const Alter_inplace_info::Rename_key_pair& pair :
info->rename_keys) {
if (0 == strcmp(key.name.str,
pair.old_key->name.str)) {
goto name_ok;
}
}
my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0),
key.name.str); key.name.str);
return(ER_WRONG_NAME_FOR_INDEX); return(ER_WRONG_NAME_FOR_INDEX);
......
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