drop table if exists t1; create function if not exists non_default_engine() returns varchar(255) deterministic begin if default_engine() = 'InnoDB' then return 'MyISAM'; end if; return 'InnoDB'; end~~ create table t1 ( x1 int unsigned, Sys_start SYS_DATATYPE generated always as row start comment 'start', Sys_end SYS_DATATYPE generated always as row end comment 'end', period for system_time (Sys_start, Sys_end) ) with system versioning; show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `x1` int(10) unsigned DEFAULT NULL, `Sys_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START COMMENT 'start', `Sys_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END COMMENT 'end', PERIOD FOR SYSTEM_TIME (`Sys_start`, `Sys_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING # Implicit fields test create or replace table t1 ( x2 int unsigned ) with system versioning; show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `x2` int(10) unsigned DEFAULT NULL, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING create or replace table t1 ( x3 int unsigned, Sys_start SYS_DATATYPE generated always as row start, Sys_end SYS_DATATYPE generated always as row end, period for system_time (x, Sys_end) ) with system versioning; ERROR HY000: PERIOD FOR SYSTEM_TIME must use columns `Sys_start` and `Sys_end` create or replace table t1 ( x4 int unsigned, Sys_start SYS_DATATYPE generated always as row start, Sys_end2 SYS_DATATYPE generated always as row end, period for system_time (Sys_start, Sys_end) ) with system versioning; ERROR HY000: PERIOD FOR SYSTEM_TIME must use columns `Sys_start` and `Sys_end2` create or replace table t1 ( x5 int unsigned, Sys_start SYS_DATATYPE generated always as row start, Sys_end SYS_DATATYPE generated always as row end, period for system_time (Sys_start, x) ) with system versioning; ERROR HY000: PERIOD FOR SYSTEM_TIME must use columns `Sys_start` and `Sys_end` create or replace table t1 ( x6 int unsigned, period for system_time (Sys_start, Sys_end) ) with system versioning; ERROR HY000: Wrong parameters for `t1`: missing 'AS ROW START' create or replace table t1 ( x7 int unsigned, Sys_start SYS_DATATYPE generated always as row start, Sys_end SYS_DATATYPE generated always as row end, period for system_time (Sys_start, Sys_end) ); ERROR HY000: Wrong parameters for `t1`: missing 'WITH SYSTEM VERSIONING' create or replace table t1 ( x8 int unsigned, Sys_start SYS_DATATYPE generated always as row start, Sys_end SYS_DATATYPE generated always as row end, period for system_time (sys_insert, sys_remove) ) with system versioning; ERROR HY000: PERIOD FOR SYSTEM_TIME must use columns `Sys_start` and `Sys_end` create or replace table t1 ( x9 int unsigned, Sys_start SYS_DATATYPE generated always as row start, Sys_end SYS_DATATYPE generated always as row end, period for system_time (Sys_start, Sys_end) ); ERROR HY000: Wrong parameters for `t1`: missing 'WITH SYSTEM VERSIONING' create or replace table t1 ( x10 int unsigned, Sys_start SYS_DATATYPE generated always as row start, Sys_end SYS_DATATYPE generated always as row end, period for system_time (Sys_start, Sys_start) ); ERROR HY000: Wrong parameters for `t1`: missing 'WITH SYSTEM VERSIONING' create or replace table t1 ( x11 int unsigned, Sys_start bigint unsigned generated always as row start, Sys_end timestamp(6) generated always as row end, period for system_time (Sys_start, Sys_end) ) with system versioning; Got one of the listed errors create or replace table t1 ( x12 int unsigned, Sys_start timestamp(6) generated always as row start, Sys_end bigint unsigned generated always as row end, period for system_time (Sys_start, Sys_end) ) with system versioning; Got one of the listed errors create or replace table t1 ( x13 int unsigned, Sys_start bigint generated always as row start, Sys_end bigint unsigned generated always as row end, period for system_time (Sys_start, Sys_end) ) with system versioning engine innodb; ERROR HY000: `Sys_start` must be of type BIGINT(20) UNSIGNED for versioned table `t1` create or replace table t1 ( x14 int unsigned, Sys_start bigint unsigned generated always as row start, Sys_end bigint generated always as row end, period for system_time (Sys_start, Sys_end) ) with system versioning engine innodb; ERROR HY000: `Sys_end` must be of type BIGINT(20) UNSIGNED for versioned table `t1` create or replace table t1 ( A1 int with system versioning, B int ); show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `A1` int(11) DEFAULT NULL, `B` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING create or replace table t1 ( A2 int with system versioning, B int ) with system versioning; show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `A2` int(11) DEFAULT NULL, `B` int(11) DEFAULT NULL, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING create or replace table t1 ( A3 int, B int without system versioning ); create or replace table t1 ( A4 int, B int without system versioning ) with system versioning; show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `A4` int(11) DEFAULT NULL, `B` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING create or replace table t1 ( A5 int with system versioning, B int without system versioning ); show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `A5` int(11) DEFAULT NULL, `B` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING create or replace table t1 ( A6 int with system versioning, B int without system versioning ) with system versioning; show create table t1; Table Create Table t1 CREATE TABLE `t1` ( `A6` int(11) DEFAULT NULL, `B` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING create or replace table t1 ( A7 int without system versioning ); create or replace table t1 ( A8 int without system versioning ) with system versioning; ERROR HY000: Table `t1` has no versioned columns create or replace table t1 (a int) with system versioning; create table tt1 like t1; show create table tt1; Table Create Table tt1 CREATE TABLE `tt1` ( `a` int(11) DEFAULT NULL, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING drop table tt1; create temporary table tt1 like t1; Warnings: Warning 1105 System versioning is stripped from temporary `test.tt1` # Temporary is stripped from versioning show create table tt1; Table Create Table tt1 CREATE TEMPORARY TABLE `tt1` ( `a` int(11) DEFAULT NULL ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 # CREATE TABLE ... SELECT create or replace table t1 (x int) with system versioning; create or replace table t0( y int, st SYS_DATATYPE generated always as row start, en SYS_DATATYPE generated always as row end, period for system_time (st, en) ) with system versioning; ## For non-versioned table: ### 1. implicit system fields are not included create or replace table t2 as select * from t1; show create table t2; Table Create Table t2 CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 ### 2. explicit system fields are included create or replace table t3 as select * from t0; select * from t0; y st en show create table t3; Table Create Table t3 CREATE TABLE `t3` ( `y` int(11) DEFAULT NULL, `st` SYS_DATATYPE, `en` SYS_DATATYPE ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 ## For versioned table insert into t1 values (1); select sys_trx_start from t1 into @sys_trx_start; insert into t0 values (2); select st from t0 into @st; ### 1. implicit system fields are included as implicit create or replace table t2 with system versioning as select * from t1; show create table t2; Table Create Table t2 CREATE TABLE `t2` ( `x` int(11) DEFAULT NULL, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING #### sys_trx_start, sys_trx_end are copied; wildcard not expanded select * from t2 where sys_trx_start = @sys_trx_start; x 1 ### 2. explicit system fields are included as non-system create or replace table t3 with system versioning as select * from t0; show create table t3; Table Create Table t3 CREATE TABLE `t3` ( `y` int(11) DEFAULT NULL, `st` SYS_DATATYPE, `en` SYS_DATATYPE, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING #### st, en are plain fields now select * from t3 where y > 2; y st en select y from t3 where st = @st and sys_trx_start > @st; y 2 ### 3. explicit system fields are kept as system create or replace table t3 ( st SYS_DATATYPE generated always as row start, en SYS_DATATYPE generated always as row end, period for system_time (st, en) ) with system versioning as select * from t0; show create table t3; Table Create Table t3 CREATE TABLE `t3` ( `y` int(11) DEFAULT NULL, `st` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `en` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`st`, `en`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING select y from t3 where st = @st; y 2 ### 4. system fields not or wrongly selected create or replace table t3 with system versioning select x from t1; show create table t3; Table Create Table t3 CREATE TABLE `t3` ( `x` int(11) DEFAULT NULL, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING select * from t3; x 1 create or replace table t3 with system versioning select x, sys_trx_start from t1; ERROR HY000: Wrong parameters for `t3`: missing 'AS ROW END' create or replace table t3 with system versioning select x, sys_trx_end from t1; ERROR HY000: Wrong parameters for `t3`: missing 'AS ROW START' # Prepare checking for historical row delete from t1; select sys_trx_end from t1 for system_time all into @sys_trx_end; delete from t0; select en from t0 for system_time all into @en; ## Combinations of versioned + non-versioned create or replace table t2 (y int); insert into t2 values (3); create or replace table t3 with system versioning select * from t1 for system_time all, t2; show create table t3; Table Create Table t3 CREATE TABLE `t3` ( `x` int(11) DEFAULT NULL, `sys_trx_start` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` SYS_DATATYPE GENERATED ALWAYS AS ROW END, `y` int(11) DEFAULT NULL, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING select * from t3 for system_time all where sys_trx_start = @sys_trx_start and sys_trx_end = @sys_trx_end; x y 1 3 create or replace table t2 like t0; insert into t2 values (1), (2); delete from t2 where y = 2; create or replace table t3 select * from t2 for system_time all; select st, en from t3 where y = 1 into @st, @en; select y from t2 for system_time all where st = @st and en = @en; y 1 select st, en from t3 where y = 2 into @st, @en; select y from t2 for system_time all where st = @st and en = @en; y 2 ## Default engine detection create or replace table t1 (a int) with system versioning engine NON_DEFAULT_ENGINE; create or replace table t2 as select a, sys_trx_start, sys_trx_end from t1 for system_time all; show create table t2; Table Create Table t2 CREATE TABLE `t2` ( `a` int(11) DEFAULT NULL, `sys_trx_start` NON_SYS_DATATYPE DEFAULT NULL, `sys_trx_end` NON_SYS_DATATYPE DEFAULT NULL ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 create or replace table t2 with system versioning as select a, sys_trx_start, sys_trx_end from t1; show create table t2; Table Create Table t2 CREATE TABLE `t2` ( `a` int(11) DEFAULT NULL, `sys_trx_start` NON_SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` NON_SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=NON_DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING create or replace table t2 with system versioning engine DEFAULT_ENGINE as select a, sys_trx_start, sys_trx_end from t1 for system_time all; ERROR HY000: `sys_trx_start` must be of type SYS_DATATYPE for versioned table `t2` create or replace table t1 (a int, id int) with system versioning engine NON_DEFAULT_ENGINE; create or replace table t2 (b int, id int); create or replace table t3 with system versioning as select t2.b, t1.a, t1.sys_trx_start, t1.sys_trx_end from t2 inner join t1 on t2.id=t1.id; show create table t3; Table Create Table t3 CREATE TABLE `t3` ( `b` int(11) DEFAULT NULL, `a` int(11) DEFAULT NULL, `sys_trx_start` NON_SYS_DATATYPE GENERATED ALWAYS AS ROW START, `sys_trx_end` NON_SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`) ) ENGINE=NON_DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING ## Errors create or replace table t (sys_trx_start int); alter table t with system versioning; ERROR 42S21: Duplicate column name 'sys_trx_start' create or replace table t (sys_trx_end int); alter table t with system versioning; ERROR 42S21: Duplicate column name 'sys_trx_end' create or replace temporary table t (x int) with system versioning; ERROR HY000: Incorrect usage of TEMPORARY and WITH SYSTEM VERSIONING create or replace table t1 ( x11 int unsigned, Sys_start0 timestamp(6) generated always as row start, Sys_start timestamp(6) generated always as row start, Sys_end timestamp(6) generated always as row end, period for system_time (Sys_start, Sys_end) ) with system versioning; ERROR HY000: Duplicate ROW START column `Sys_start` ## System fields detection create or replace table t1 (x int) with system versioning; create or replace table t2 ( y int, st SYS_DATATYPE generated always as row start, en SYS_DATATYPE generated always as row end, period for system_time (st, en) ) with system versioning; create or replace table t3 as select x, y, sys_trx_start, sys_trx_end, st, en from t1, t2; show create table t3; Table Create Table t3 CREATE TABLE `t3` ( `x` int(11) DEFAULT NULL, `y` int(11) DEFAULT NULL, `sys_trx_start` SYS_DATATYPE, `sys_trx_end` SYS_DATATYPE, `st` SYS_DATATYPE, `en` SYS_DATATYPE ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 create or replace table t3 ( y int, st SYS_DATATYPE generated always as row start, en SYS_DATATYPE generated always as row end, period for system_time (st, en) ) with system versioning as select x, y, sys_trx_start, sys_trx_end, st, en from t1, t2; show create table t3; Table Create Table t3 CREATE TABLE `t3` ( `x` int(11) DEFAULT NULL, `y` int(11) DEFAULT NULL, `sys_trx_start` SYS_DATATYPE, `sys_trx_end` SYS_DATATYPE, `st` SYS_DATATYPE GENERATED ALWAYS AS ROW START, `en` SYS_DATATYPE GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (`st`, `en`) ) ENGINE=DEFAULT_ENGINE DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING drop database test; create database test;