summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Midenkov <midenok@gmail.com>2021-02-05 01:52:21 +0300
committerAleksey Midenkov <midenok@gmail.com>2021-03-31 21:25:33 +0300
commitaf52a0e516aa044b7fcdf91b3d2a7036c6c7ae9f (patch)
tree9c5f94b2f0d0c77fd1695ae1db0f31aabd9d17e2
parentb9d1c6574b845ddf843dd5a8e506593e74352c69 (diff)
downloadmariadb-git-af52a0e516aa044b7fcdf91b3d2a7036c6c7ae9f.tar.gz
MDEV-24690 Dropping primary key column from versioned table always fails with 1072
Exclude system-invisible key-parts from MDEV-11114 (04b288ae) restriction.
-rw-r--r--mysql-test/suite/versioning/r/alter.result42
-rw-r--r--mysql-test/suite/versioning/t/alter.test29
-rw-r--r--sql/sql_table.cc5
3 files changed, 75 insertions, 1 deletions
diff --git a/mysql-test/suite/versioning/r/alter.result b/mysql-test/suite/versioning/r/alter.result
index 2ecd5152a45..9d79b6e73c5 100644
--- a/mysql-test/suite/versioning/r/alter.result
+++ b/mysql-test/suite/versioning/r/alter.result
@@ -693,3 +693,45 @@ delete from t1;
set statement system_versioning_alter_history=keep for
alter table t1 drop system versioning, modify column a tinyint;
drop table t1;
+#
+# MDEV-24690 Dropping primary key column from versioned table always fails with 1072
+#
+create table t1 (a int, b int primary key) with system versioning;
+alter table t1 drop column b;
+create or replace table t1 (
+a int, b int primary key,
+row_start timestamp(6) as row start,
+row_end timestamp(6) as row end,
+period for system_time(row_start, row_end)
+) with system versioning;
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) NOT NULL,
+ `row_start` timestamp(6) GENERATED ALWAYS AS ROW START,
+ `row_end` timestamp(6) GENERATED ALWAYS AS ROW END,
+ PRIMARY KEY (`b`,`row_end`),
+ PERIOD FOR SYSTEM_TIME (`row_start`, `row_end`)
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
+alter table t1 drop column b;
+ERROR 42000: Key column 'b' doesn't exist in table
+create or replace table t1 (
+a int, b int primary key,
+row_start timestamp(6) as row start invisible,
+row_end timestamp(6) as row end invisible,
+period for system_time(row_start, row_end)
+) with system versioning;
+show create table t1;
+Table Create Table
+t1 CREATE TABLE `t1` (
+ `a` int(11) DEFAULT NULL,
+ `b` int(11) NOT NULL,
+ `row_start` timestamp(6) GENERATED ALWAYS AS ROW START INVISIBLE,
+ `row_end` timestamp(6) GENERATED ALWAYS AS ROW END INVISIBLE,
+ PRIMARY KEY (`b`,`row_end`),
+ PERIOD FOR SYSTEM_TIME (`row_start`, `row_end`)
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
+alter table t1 drop column b;
+ERROR 42000: Key column 'b' doesn't exist in table
+drop table t1;
diff --git a/mysql-test/suite/versioning/t/alter.test b/mysql-test/suite/versioning/t/alter.test
index 9d8f0a1bb7b..90ecbfd9f79 100644
--- a/mysql-test/suite/versioning/t/alter.test
+++ b/mysql-test/suite/versioning/t/alter.test
@@ -592,3 +592,32 @@ alter table t1 drop system versioning, modify column a tinyint;
# cleanup
drop table t1;
+
+--echo #
+--echo # MDEV-24690 Dropping primary key column from versioned table always fails with 1072
+--echo #
+create table t1 (a int, b int primary key) with system versioning;
+alter table t1 drop column b;
+
+create or replace table t1 (
+ a int, b int primary key,
+ row_start timestamp(6) as row start,
+ row_end timestamp(6) as row end,
+ period for system_time(row_start, row_end)
+) with system versioning;
+show create table t1;
+--error ER_KEY_COLUMN_DOES_NOT_EXITS
+alter table t1 drop column b;
+
+create or replace table t1 (
+a int, b int primary key,
+ row_start timestamp(6) as row start invisible,
+ row_end timestamp(6) as row end invisible,
+ period for system_time(row_start, row_end)
+) with system versioning;
+show create table t1;
+--error ER_KEY_COLUMN_DOES_NOT_EXITS
+alter table t1 drop column b;
+
+# cleanup
+drop table t1;
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 61b6023c6a1..01812e039be 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -8375,6 +8375,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
}
const char *dropped_key_part= NULL;
+ bool user_keyparts= false; // some user-defined keyparts left
KEY_PART_INFO *key_part= key_info->key_part;
key_parts.empty();
bool delete_index_stat= FALSE;
@@ -8450,6 +8451,8 @@ 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)
+ user_keyparts= true;
}
if (table->s->tmp_table == NO_TMP_TABLE)
{
@@ -8493,7 +8496,7 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
key_type= Key::PRIMARY;
else
key_type= Key::UNIQUE;
- if (dropped_key_part)
+ if (dropped_key_part && user_keyparts)
{
my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), dropped_key_part);
goto err;