Skip to content

Commit

Permalink
MDEV-25327 Unexpected ER_DUP_ENTRY upon dropping PK column from syste…
Browse files Browse the repository at this point in the history
…m-versioned table

When dropped all user key-parts we also drop key-parts of implicit system fields.
  • Loading branch information
midenok committed Apr 19, 2021
1 parent cc3105e commit 562bbf5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
17 changes: 17 additions & 0 deletions mysql-test/suite/versioning/r/alter.result
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,20 @@ create or replace table t1 (x int);
alter table t1 add column y timestamp(6) as row start;
ERROR HY000: Table `t1` is not system-versioned
drop table t1;
#
# MDEV-25327 Unexpected ER_DUP_ENTRY upon dropping PK column from system-versioned table
#
create table t1 (pk int, a int, primary key (pk), key (a))
with system versioning;
insert into t1 values (1, 1), (2, 2);
delete from t1;
set system_versioning_alter_history= keep;
alter table t1 drop pk;
drop table t1;
create table t1 (pk int, a int, primary key (pk), key (a))
with system versioning;
insert into t1 values (1, 2), (2, 8), (3, 4), (4, 4), (5, 0);
delete from t1;
set system_versioning_alter_history= keep;
alter ignore table t1 drop pk;
drop table t1;
22 changes: 22 additions & 0 deletions mysql-test/suite/versioning/t/alter.test
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,25 @@ create or replace table t1 (x int);
alter table t1 add column y timestamp(6) as row start;
# cleanup
drop table t1;


--echo #
--echo # MDEV-25327 Unexpected ER_DUP_ENTRY upon dropping PK column from system-versioned table
--echo #
create table t1 (pk int, a int, primary key (pk), key (a))
with system versioning;
insert into t1 values (1, 1), (2, 2);
delete from t1;
set system_versioning_alter_history= keep;
alter table t1 drop pk;
# cleanup
drop table t1;

create table t1 (pk int, a int, primary key (pk), key (a))
with system versioning;
insert into t1 values (1, 2), (2, 8), (3, 4), (4, 4), (5, 0);
delete from t1;
set system_versioning_alter_history= keep;
alter ignore table t1 drop pk;
# cleanup
drop table t1;
12 changes: 10 additions & 2 deletions sql/sql_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8451,7 +8451,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
key_parts.push_back(new (thd->mem_root) Key_part_spec(&cfield->field_name,
key_part_length, true),
thd->mem_root);
if (cfield->invisible < INVISIBLE_SYSTEM)
if (!(cfield->invisible == INVISIBLE_SYSTEM && cfield->vers_sys_field()))
user_keyparts= true;
}
if (table->s->tmp_table == NO_TMP_TABLE)
Expand All @@ -8463,6 +8463,14 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
(void) delete_statistics_for_index(thd, table, key_info, TRUE);
}

if (!user_keyparts && key_parts.elements)
{
/*
If we dropped all user key-parts we also drop implicit system fields.
*/
key_parts.empty();
}

if (key_parts.elements)
{
KEY_CREATE_INFO key_create_info;
Expand Down Expand Up @@ -8496,7 +8504,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
key_type= Key::PRIMARY;
else
key_type= Key::UNIQUE;
if (dropped_key_part && user_keyparts)
if (dropped_key_part)
{
my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), dropped_key_part);
goto err;
Expand Down

0 comments on commit 562bbf5

Please sign in to comment.