mariadb/mysql-test/suite/sql_sequence/create.test
Monty 17a87d6063 MDEV-10139 Support for SEQUENCE objects
Working features:
CREATE OR REPLACE [TEMPORARY] SEQUENCE [IF NOT EXISTS] name
    [ INCREMENT [ BY | = ] increment ]
    [ MINVALUE [=] minvalue | NO MINVALUE ]
    [ MAXVALUE [=] maxvalue | NO MAXVALUE ]
    [ START [ WITH | = ] start ] [ CACHE [=] cache ] [ [ NO ] CYCLE ]
    ENGINE=xxx COMMENT=".."
SELECT NEXT VALUE FOR sequence_name;
SELECT NEXTVAL(sequence_name);
SELECT PREVIOUS VALUE FOR sequence_name;
SELECT LASTVAL(sequence_name);

SHOW CREATE SEQUENCE sequence_name;
SHOW CREATE TABLE sequence_name;
CREATE TABLE sequence-structure ... SEQUENCE=1
ALTER TABLE sequence RENAME TO sequence2;
RENAME TABLE sequence TO sequence2;
DROP [TEMPORARY] SEQUENCE  [IF EXISTS] sequence_names

Missing features
- SETVAL(value,sequence_name), to be used with replication.
- Check replication, including checking that sequence tables are marked
  not transactional.
- Check that a commit happens for NEXT VALUE that changes table data (may
  already work)
- ALTER SEQUENCE. ANSI SQL version of setval.
- Share identical sequence entries to not add things twice to table list.
- testing insert/delete/update/truncate/load data
- Run and fix Alibaba sequence tests (part of mysql-test/suite/sql_sequence)
- Write documentation for NEXT VALUE / PREVIOUS_VALUE
- NEXTVAL in DEFAULT
  - Ensure that NEXTVAL in DEFAULT uses database from base table
- Two NEXTVAL for same row should give same answer.
- Oracle syntax sequence_table.nextval, without any FOR or FROM.
- Sequence tables are treated as 'not read constant tables' by SELECT; Would
  be better if we would have a separate list for sequence tables so that
  select doesn't know about them, except if refereed to with FROM.

Other things done:
- Improved output for safemalloc backtrack
- frm_type_enum changed to Table_type
- Removed lex->is_view and replaced with lex->table_type. This allows
  use to more easy check if item is view, sequence or table.
- Added table flag HA_CAN_TABLES_WITHOUT_ROLLBACK, needed for handlers
  that want's to support sequences
- Added handler calls:
 - engine_name(), to simplify getting engine name for partition and sequences
 - update_first_row(), to be able to do efficient sequence implementations.
 - Made binlog_log_row() global to be able to call it from ha_sequence.cc
- Added handler variable: row_already_logged, to be able to flag that the
  changed row is already logging to replication log.
- Added CF_DB_CHANGE and CF_SCHEMA_CHANGE flags to simplify
  deny_updates_if_read_only_option()
- Added sp_add_cfetch() to avoid new conflicts in sql_yacc.yy
- Moved code for add_table_options() out from sql_show.cc::show_create_table()
- Added String::append_longlong() and used it in sql_show.cc to simplify code.
- Added extra option to dd_frm_type() and ha_table_exists to indicate if
  the table is a sequence. Needed by DROP SQUENCE to not drop a table.
2017-04-07 18:09:56 +04:00

374 lines
9.7 KiB
Text

#
# Test create options with sequences
#
--source include/have_innodb.inc
--source include/have_archive.inc
drop table if exists t1;
#
# Check some sample engines
#
create or replace sequence t1 engine=myisam;
show create sequence t1;
show create table t1;
select * from t1;
create or replace sequence t1 engine=innodb;
show create sequence t1;
show create table t1;
select * from t1;
create or replace sequence t1 engine=maria;
show create sequence t1;
show create table t1;
select * from t1;
--error ER_ILLEGAL_HA_CREATE_OPTION
create or replace sequence t1 engine=archive;
#
# The following error should be fixed. We shouldn't delete old table on errors
#
--error ER_NO_SUCH_TABLE
show create table t1;
# Check start values
create or replace sequence t1 start with 10;
show create sequence t1;
select * from t1;
create or replace sequence t1 minvalue=11;
show create sequence t1;
select * from t1;
create or replace sequence t1 maxvalue=13 increment by -1;
show create sequence t1;
select * from t1;
create or replace sequence t1 increment by -1 cache 100;
show create sequence t1;
select * from t1;
create or replace sequence t1 cycle;
show create sequence t1;
select * from t1;
create or replace sequence t1 nocycle;
show create sequence t1;
select * from t1;
show create sequence t1;
create or replace sequence t1 cycle minvalue= 14;
show create sequence t1;
select * from t1;
create or replace sequence t1 cycle increment by -1;
show create sequence t1;
drop sequence t1;
create sequence if not exists t1;
create sequence if not exists t1 start with 10;
select * from t1;
show create sequence t1;
create or replace sequence t1 start with 10 minvalue=10 maxvalue=11 nocache cycle;
show create sequence t1;
select * from t1;
create or replace sequence t1 start with 10 minvalue=-10 maxvalue=11 cache=10 cycle increment by 10;
show create sequence t1;
select * from t1;
# NO MINVALUE, NO MAXVALUE
create or replace sequence t1 start with 10 NO MAXVALUE NO MINVALUE;
# Some edge cases
create or replace sequence t1 start with 10 maxvalue 10;
create or replace sequence t1 start with 10 minvalue 10;
create or replace sequence t1 start with 10 minvalue 10 maxvalue 11 cycle;
create or replace sequence t1 start with 10 maxvalue=9223372036854775806;
create or replace sequence t1 start with 10 minvalue=-9223372036854775807;
drop sequence if exists t1;
create sequence t1 increment by 0;
show create sequence t1;
select * from t1;
drop sequence t1;
#
# Wrong usage and arguments to create sequence
#
create table t1 (a int);
--error ER_WRONG_OBJECT
show create sequence t1;
--error ER_NOT_SEQUENCE2
drop sequence t1;
drop sequence if exists t1;
--error ER_SEQUENCE_INVALID_DATA
create sequence t1 start with 10 maxvalue=9;
--error ER_SEQUENCE_INVALID_DATA
create sequence t1 minvalue= 100 maxvalue=10;
--error ER_SEQUENCE_INVALID_DATA
create sequence t1 start with 9 minvalue=10;
--error ER_PARSE_ERROR
create or replace sequence t1 maxvalue=13, increment by -1;
--error ER_PARSE_ERROR
create or replace sequence t1 start with= 10 maxvalue=13;
--error ER_PARSE_ERROR
create or replace sequence t1 maxvalue=13, increment= -1;
--error ER_PARSE_ERROR
create or replace sequence t1 start with 10 min_value=1 NO MINVALUE;
--error ER_PARSE_ERROR
create or replace sequence t1 start with 10 min_value=1 NO MINVALUE;
--error ER_SEQUENCE_INVALID_DATA
create sequence t1 start with 10 maxvalue=9223372036854775807;
--error ER_PARSE_ERROR
create sequence t1 start with 10 minvalue=-9223372036854775808;
# This should probably give an error
create or replace sequence t1 start with 10 NO MINVALUE minvalue=1;
drop sequence t1;
#
# Test with LIST COLUMNS as first command
#
create sequence t1;
show fields from t1;
flush tables;
show fields from t1;
create or replace sequence t1 engine=aria;
show fields from t1;
show fields from t1;
flush tables;
#
# Test with other create options
#
create or replace sequence t1 comment= "test 1";
show create sequence t1;
show create table t1;
create or replace sequence t1 comment= "test 2" min_rows=1 max_rows=2;
show create sequence t1;
show create table t1;
create or replace sequence t1 start=1 increment= 2;
create or replace sequence t1 start 1 increment 2;
drop sequence t1;
#
# test with create table
#
CREATE TABLE t1 (
`next_value` bigint(21) NOT NULL,
`min_value` bigint(21) NOT NULL,
`max_value` bigint(21) NOT NULL,
`start` bigint(21) NOT NULL,
`increment` bigint(21) NOT NULL,
`cache` bigint(21) NOT NULL,
`cycle` tinyint(1) unsigned NOT NULL,
`round` bigint(21) NOT NULL
) sequence=1;
show create sequence t1;
show create table t1;
drop sequence t1;
# Wrong column name
--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE
CREATE OR REPLACE TABLE t1 (
`next_val` bigint(21) NOT NULL,
`min_value` bigint(21) NOT NULL,
`max_value` bigint(21) NOT NULL,
`start` bigint(21) NOT NULL,
`increment` bigint(21) NOT NULL,
`cache` bigint(21) NOT NULL,
`cycle` tinyint(1) unsigned NOT NULL,
`round` bigint(21) NOT NULL
) sequence=1;
# Wrong type
--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE
CREATE OR REPLACE TABLE t1 (
`next_value` int(21) NOT NULL,
`min_value` bigint(21) NOT NULL,
`max_value` bigint(21) NOT NULL,
`start` bigint(21) NOT NULL,
`increment` bigint(21) NOT NULL,
`cache` bigint(21) NOT NULL,
`cycle` tinyint(1) unsigned NOT NULL,
`round` bigint(21) NOT NULL
) sequence=1;
--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE
CREATE OR REPLACE TABLE t1 (
`next_val` bigint(21) NOT NULL,
`min_value` bigint(21) NOT NULL,
`max_value` bigint(21) NOT NULL,
`start` bigint(21) NOT NULL,
`increment` bigint(21) NOT NULL,
`cache` bigint(21) NOT NULL,
`cycle` bigint(21) unsigned NOT NULL, /* error */
`round` bigint(21) NOT NULL
) sequence=1;
# Missing NOT NULL
--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE
CREATE OR REPLACE TABLE t1 (
`next_value` bigint(21),
`min_value` bigint(21) NOT NULL,
`max_value` bigint(21) NOT NULL,
`start` bigint(21) NOT NULL,
`increment` bigint(21) NOT NULL,
`cache` bigint(21) NOT NULL,
`cycle` tinyint(1) unsigned NOT NULL,
`round` bigint(21) NOT NULL
) sequence=1;
# Extra field
--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE
CREATE OR REPLACE TABLE t1 (
`next_value` bigint(21) NOT NULL,
`min_value` bigint(21) NOT NULL,
`max_value` bigint(21) NOT NULL,
`start` bigint(21) NOT NULL,
`increment` bigint(21) NOT NULL,
`cache` bigint(21) NOT NULL,
`cycle` tinyint(1) unsigned NOT NULL,
`round` bigint(21) NOT NULL,
extra_field bigint(21)
) sequence=1;
# Wrong field order
--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE
CREATE OR REPLACE TABLE t1 (
`min_value` bigint(21) NOT NULL,
`max_value` bigint(21) NOT NULL,
`next_value` bigint(21) NOT NULL,
`start` bigint(21) NOT NULL,
`increment` bigint(21) NOT NULL,
`cache` bigint(21) NOT NULL,
`cycle` tinyint(1) unsigned NOT NULL,
`round` bigint(21) NOT NULL
) sequence=1;
# key
--error ER_SEQUENCE_INVALID_TABLE_STRUCTURE
CREATE OR REPLACE TABLE t1 (
`next_value` bigint(21) NOT NULL,
`min_value` bigint(21) NOT NULL,
`max_value` bigint(21) NOT NULL,
`start` bigint(21) NOT NULL,
`increment` bigint(21) NOT NULL,
`cache` bigint(21) NOT NULL,
`cycle` tinyint(1) unsigned NOT NULL,
`round` bigint(21) NOT NULL,
key key1 (next_value)
) sequence=1;
drop sequence if exists t1;
#
# DROP SEQUENCE
#
create sequence t1;
create sequence t2;
create table t3 (a int) engine=myisam;
select table_catalog, table_schema, table_name, table_type from information_schema.tables where table_catalog="test";
CREATE SEQUENCE s1;
drop sequence s1;
drop sequence if exists t1,t2,t3,t4;
drop table if exists t1,t2,t3;
CREATE TABLE t1 (a int);
CREATE TABLE t2 (a int);
CREATE SEQUENCE s1;
drop table t1,t2,s1;
CREATE TABLE t1 (a int);
CREATE TABLE t2 (a int);
CREATE SEQUENCE s1;
drop table if exists t1,t2,s1,s2;
CREATE TABLE t1 (a int);
CREATE TABLE t2 (a int);
CREATE SEQUENCE s1;
--error ER_UNKNOWN_SEQUENCES
drop sequence t1,t2,s1,s2;
drop table if exists t1,t2;
CREATE TABLE t1 (a int);
CREATE TABLE t2 (a int);
CREATE SEQUENCE s1;
drop sequence if exists t1,t2,s1,s2;
drop table if exists t1,t2;
CREATE TEMPORARY SEQUENCE s1;
DROP SEQUENCE s1;
--error ER_UNKNOWN_SEQUENCES
DROP TEMPORARY SEQUENCE s1;
CREATE TEMPORARY SEQUENCE s1;
CREATE SEQUENCE s2;
CREATE TEMPORARY TABLE t1 (a int);
CREATE TABLE t2 (a int);
--error ER_UNKNOWN_SEQUENCES
DROP TEMPORARY SEQUENCE t1,t2,s1,s2;
--error ER_UNKNOWN_SEQUENCES
DROP TEMPORARY SEQUENCE s1;
DROP TEMPORARY TABLE t1;
--error ER_BAD_TABLE_ERROR
DROP TABLE t1,t2,s1,s2;
create view v1 as (select 1);
CREATE SEQUENCE s1;
--error ER_IT_IS_A_VIEW
DROP SEQUENCE s1,v1;
drop view v1;
#
# CREATE TEMPORARY SEQUENCE
#
CREATE TEMPORARY SEQUENCE t1;
select next value for t1;
drop temporary table t1;
--error ER_NO_SUCH_TABLE
select previous value for t1;
CREATE SEQUENCE t1 start with 1 minvalue 1 maxvalue 10 increment by 1 cache 10;
select next value for t1;
select previous value for t1;
CREATE TEMPORARY SEQUENCE t1 start with 100 minvalue 100 maxvalue 200 increment by 1 cache 10;
select previous value for t1;
select next value for t1;
select previous value for t1;
drop temporary sequence t1;
select previous value for t1;
drop sequence t1;
CREATE TEMPORARY SEQUENCE t1 engine=innodb;
select next value for t1;
drop temporary table t1;
--error ER_NO_SUCH_TABLE
select previous value for t1;
CREATE SEQUENCE t1 start with 1 minvalue 1 maxvalue 10 increment by 1 cache 10 engine=innodb;
select next value for t1;
select previous value for t1;
CREATE TEMPORARY SEQUENCE t1 start with 100 minvalue 100 maxvalue 200 increment by 1 cache 10 engine=innodb;
select previous value for t1;
select next value for t1;
select previous value for t1;
drop temporary sequence t1;
select previous value for t1;
drop sequence t1;
#
# Check that we can't create anything with the sequence engine
#
--error ER_UNKNOWN_STORAGE_ENGINE
create table t1 (a int) engine=sql_sequence;