From a9bee9884aed7d9221bf028138349a1e7b5c984c Mon Sep 17 00:00:00 2001 From: Monty Date: Sun, 7 Jun 2020 16:30:50 +0300 Subject: [PATCH] Don't allow ALTER TABLE ... ORDER BY on SEQUENCE objects MDEV-19320 Sequence gets corrupted and produces ER_KEY_NOT_FOUND (Can't find record) after ALTER .. ORDER BY --- mysql-test/suite/sql_sequence/alter.result | 7 +++++++ mysql-test/suite/sql_sequence/alter.test | 11 +++++++++++ sql/sql_sequence.cc | 5 +++++ 3 files changed, 23 insertions(+) diff --git a/mysql-test/suite/sql_sequence/alter.result b/mysql-test/suite/sql_sequence/alter.result index 2ac8ac07994..612e2201d26 100644 --- a/mysql-test/suite/sql_sequence/alter.result +++ b/mysql-test/suite/sql_sequence/alter.result @@ -242,3 +242,10 @@ CREATE SEQUENCE t1 engine=innodb; ALTER IGNORE TABLE t1 ADD CHECK (start_value < minimum_value); ERROR HY000: Sequence 'test.t1' table structure is invalid (Sequence tables cannot have any constraints) DROP SEQUENCE t1; +CREATE SEQUENCE s; +ALTER TABLE s ORDER BY cache_size; +ERROR HY000: Sequence 'test.s' table structure is invalid (ORDER BY) +SELECT NEXTVAL(s); +NEXTVAL(s) +1 +DROP SEQUENCE s; diff --git a/mysql-test/suite/sql_sequence/alter.test b/mysql-test/suite/sql_sequence/alter.test index 7132c45ef07..53f71018337 100644 --- a/mysql-test/suite/sql_sequence/alter.test +++ b/mysql-test/suite/sql_sequence/alter.test @@ -149,3 +149,14 @@ CREATE SEQUENCE t1 engine=innodb; --error ER_SEQUENCE_INVALID_TABLE_STRUCTURE ALTER IGNORE TABLE t1 ADD CHECK (start_value < minimum_value); DROP SEQUENCE t1; + +# +# MDEV-19320 Sequence gets corrupted and produces ER_KEY_NOT_FOUND (Can't +# find record) after ALTER .. ORDER BY +# + +CREATE SEQUENCE s; +--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE +ALTER TABLE s ORDER BY cache_size; +SELECT NEXTVAL(s); +DROP SEQUENCE s; diff --git a/sql/sql_sequence.cc b/sql/sql_sequence.cc index 68d9efb3093..939963de682 100644 --- a/sql/sql_sequence.cc +++ b/sql/sql_sequence.cc @@ -208,6 +208,11 @@ bool check_sequence_fields(LEX *lex, List *fields) reason= "Sequence tables cannot have any constraints"; goto err; } + if (lex->alter_info.flags & ALTER_ORDER) + { + reason= "ORDER BY"; + goto err; + } for (field_no= 0; (field= it++); field_no++) {