Skip to content

Commit f235d90

Browse files
author
Nisha Gopalakrishnan
committed
BUG#16315351 - ALTER TABLE DROP COLUMN: NO WARNING IF DUPLICATE
INDEX FOR THE SAME COLUMN Analysis ------- When a column associated with a multi-column index is dropped, a NOTE is not reported in case a duplicate index exist on the table after the DROP COLUMN operation. EX: CREATE TABLE t1( fld1 INT, fld2 INT, KEY(fld1, fld2), KEY(fld2)); ALTER TABLE t1 DROP COLUMN fld1; The duplicate index check was not invoked in the above mentioned case. Fix: ---- The flag 'check_for_duplicate_indexes' is set, to trigger the duplicate index check when the column associated with an index is dropped.
1 parent 5ca17b9 commit f235d90

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

sql/handler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,8 @@ typedef struct st_key_create_information
13591359
/**
13601360
A flag to determine if we will check for duplicate indexes.
13611361
This typically means that the key information was specified
1362-
directly by the user (set by the parser).
1362+
directly by the user (set by the parser) or a column
1363+
associated with it was dropped.
13631364
*/
13641365
bool check_for_duplicate_indexes;
13651366
} KEY_CREATE_INFO;

sql/sql_table.cc

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3149,23 +3149,24 @@ void promote_first_timestamp_column(List<Create_field> *column_definitions)
31493149
@param thd Thread context.
31503150
@param key Key to be checked.
31513151
@param key_info Key meta-data info.
3152-
@param key_list List of existing keys.
3152+
@param alter_info List of columns and indexes to create.
31533153
*/
31543154
static void check_duplicate_key(THD *thd,
31553155
Key *key, KEY *key_info,
3156-
List<Key> *key_list)
3156+
Alter_info *alter_info)
31573157
{
31583158
/*
31593159
We only check for duplicate indexes if it is requested and the
31603160
key is not auto-generated.
31613161
31623162
Check is requested if the key was explicitly created or altered
3163-
by the user (unless it's a foreign key).
3163+
(Index is altered/column associated with it is dropped) by the user
3164+
(unless it's a foreign key).
31643165
*/
31653166
if (!key->key_create_info.check_for_duplicate_indexes || key->generated)
31663167
return;
31673168

3168-
List_iterator<Key> key_list_iterator(*key_list);
3169+
List_iterator<Key> key_list_iterator(alter_info->key_list);
31693170
List_iterator<Key_part_spec> key_column_iterator(key->columns);
31703171
Key *k;
31713172

@@ -3174,7 +3175,18 @@ static void check_duplicate_key(THD *thd,
31743175
// Looking for a similar key...
31753176

31763177
if (k == key)
3177-
break;
3178+
{
3179+
/*
3180+
Since the duplicate index might exist before or after
3181+
the modified key in the list, we continue the
3182+
comparison with rest of the keys in case of DROP COLUMN
3183+
operation.
3184+
*/
3185+
if (alter_info->flags & Alter_info::ALTER_DROP_COLUMN)
3186+
continue;
3187+
else
3188+
break;
3189+
}
31783190

31793191
if (k->generated ||
31803192
(key->type != k->type) ||
@@ -4107,7 +4119,7 @@ mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
41074119
}
41084120

41094121
// Check if a duplicate index is defined.
4110-
check_duplicate_key(thd, key, key_info, &alter_info->key_list);
4122+
check_duplicate_key(thd, key, key_info, alter_info);
41114123

41124124
key_info++;
41134125
}
@@ -6888,6 +6900,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
68886900
for (uint i=0 ; i < table->s->keys ; i++,key_info++)
68896901
{
68906902
char *key_name= key_info->name;
6903+
bool index_column_dropped= false;
68916904
Alter_drop *drop;
68926905
drop_it.rewind();
68936906
while ((drop=drop_it++))
@@ -6924,7 +6937,13 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
69246937
break;
69256938
}
69266939
if (!cfield)
6940+
{
6941+
/*
6942+
We are dropping a column associated with an index.
6943+
*/
6944+
index_column_dropped= true;
69276945
continue; // Field is removed
6946+
}
69286947
uint key_part_length=key_part->length;
69296948
if (cfield->field) // Not new field
69306949
{
@@ -6981,12 +7000,6 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
69817000
if (key_info->flags & HA_USES_COMMENT)
69827001
key_create_info.comment= key_info->comment;
69837002

6984-
/*
6985-
We're refreshing an already existing index. Since the index is not
6986-
modified, there is no need to check for duplicate indexes again.
6987-
*/
6988-
key_create_info.check_for_duplicate_indexes= false;
6989-
69907003
if (key_info->flags & HA_SPATIAL)
69917004
key_type= Key::SPATIAL;
69927005
else if (key_info->flags & HA_NOSAME)
@@ -7000,6 +7013,15 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
70007013
key_type= Key::FULLTEXT;
70017014
else
70027015
key_type= Key::MULTIPLE;
7016+
7017+
if (index_column_dropped)
7018+
{
7019+
/*
7020+
We have dropped a column associated with an index,
7021+
this warrants a check for duplicate indexes
7022+
*/
7023+
key_create_info.check_for_duplicate_indexes= true;
7024+
}
70037025

70047026
key= new Key(key_type, key_name, strlen(key_name),
70057027
&key_create_info,

0 commit comments

Comments
 (0)