1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

SQL, IB: Copy history via CREATE .. SELECT [closes #157, #152]

This commit is contained in:
Aleksey Midenkov
2017-03-24 16:00:42 +03:00
parent 7a525e7e93
commit 67cd92b6f4
29 changed files with 636 additions and 276 deletions

View File

@ -1,226 +1,281 @@
set @@session.time_zone='+00:00';
select ifnull(max(trx_id), 0) into @start_trx_id from information_schema.innodb_vtq;
create procedure if not exists verify_vtq()
begin
set @i= 0;
select
@i:= @i + 1 as No,
trx_id > 0 as A,
commit_id >= trx_id as B,
begin_ts > '1-1-1 0:0:0' as C,
commit_ts > begin_ts as D
from information_schema.innodb_vtq
where trx_id > @start_trx_id;
select ifnull(max(trx_id), 0)
into @start_trx_id
from information_schema.innodb_vtq;
end~~
create function if not exists default_engine()
returns varchar(255)
deterministic
begin
declare e varchar(255);
select lower(engine) from information_schema.engines where support='DEFAULT' into e;
return e;
end~~
create function if not exists sys_datatype()
returns varchar(255)
deterministic
begin
if default_engine() = 'innodb' then
return 'bigint unsigned';
elseif default_engine() = 'myisam' then
return 'timestamp(6)';
end if;
return NULL;
end~~
create function if not exists sys_commit_ts(sys_field varchar(255))
returns varchar(255)
deterministic
begin
if default_engine() = 'innodb' then
return concat('vtq_commit_ts(', sys_field, ')');
elseif default_engine() = 'myisam' then
return sys_field;
end if;
return NULL;
end~~
create procedure if not exists innodb_verify_vtq(recs int)
begin
declare i int default 1;
if default_engine() = 'innodb' then
call verify_vtq;
elseif default_engine() = 'myisam' then
create temporary table tmp (No int, A bool, B bool, C bool, D bool);
while i <= recs do
insert into tmp values (i, 1, 1, 1, 1);
set i= i + 1;
end while;
select * from tmp;
drop table tmp;
end if;
end~~
drop table if exists t1;
create table t1 (
XNo int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_end timestamp(6) generated always as row end,
x1 int unsigned,
Sys_start SYS_TRX_TYPE generated always as row start,
Sys_end SYS_TRX_TYPE generated always as row end,
period for system_time (Sys_start, Sys_end)
) with system versioning;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`XNo` int(10) unsigned DEFAULT NULL,
`Sys_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`Sys_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`x1` int(10) unsigned DEFAULT NULL,
`Sys_start` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`Sys_end` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`Sys_start`, `Sys_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
# Implicit fields test
create or replace table t1 (
XNo int unsigned
x2 int unsigned
) with system versioning;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`XNo` int(10) unsigned DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`x2` int(10) unsigned DEFAULT NULL,
`sys_trx_start` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`sys_trx_end` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
create or replace table t1 (
XNo int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_start2 timestamp(6) generated always as row start,
Sys_end timestamp(6) generated always as row end,
x3 int unsigned,
Sys_start SYS_TRX_TYPE generated always as row start,
Sys_start2 SYS_TRX_TYPE generated always as row start,
Sys_end SYS_TRX_TYPE generated always as row end,
period for system_time (Sys_start, Sys_end)
) with system versioning;
ERROR HY000: Wrong parameters for `t1`: multiple 'GENERATED ALWAYS AS ROW START'
create or replace table t1 (
XNo int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_end2 timestamp(6) generated always as row end,
x4 int unsigned,
Sys_start SYS_TRX_TYPE generated always as row start,
Sys_end2 SYS_TRX_TYPE generated always as row end,
period for system_time (Sys_start, Sys_end)
) with system versioning;
ERROR HY000: Wrong parameters for `t1`: 'PERIOD FOR SYSTEM_TIME' and 'GENERATED AS ROW END' mismatch
create or replace table t1 (
XNo int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_end timestamp(6) generated always as row end,
Sys_end2 timestamp(6) generated always as row end,
x5 int unsigned,
Sys_start SYS_TRX_TYPE generated always as row start,
Sys_end SYS_TRX_TYPE generated always as row end,
Sys_end2 SYS_TRX_TYPE generated always as row end,
period for system_time (Sys_start, Sys_end)
) with system versioning;
ERROR HY000: Wrong parameters for `t1`: multiple 'GENERATED ALWAYS AS ROW END'
create or replace table t1 (
XNo int unsigned,
x6 int unsigned,
period for system_time (Sys_start, Sys_end)
) with system versioning;
ERROR HY000: Wrong parameters for `t1`: 'GENERATED AS ROW START' column missing
create or replace table t1 (
XNo int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_end timestamp(6) generated always as row end,
Sys_end2 timestamp(6) generated always as row end,
x7 int unsigned,
Sys_start SYS_TRX_TYPE generated always as row start,
Sys_end SYS_TRX_TYPE generated always as row end,
Sys_end2 SYS_TRX_TYPE generated always as row end,
period for system_time (Sys_start, Sys_end)
);
ERROR HY000: Wrong parameters for `t1`: multiple 'GENERATED ALWAYS AS ROW END'
create or replace table t1 (
XNo int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_end timestamp(6) generated always as row end,
x8 int unsigned,
Sys_start SYS_TRX_TYPE generated always as row start,
Sys_end SYS_TRX_TYPE generated always as row end,
period for system_time (sys_insert, sys_remove)
) with system versioning;
ERROR HY000: Wrong parameters for `t1`: 'PERIOD FOR SYSTEM_TIME' and 'GENERATED AS ROW START' mismatch
create or replace table t1 (
XNo int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_end timestamp(6) generated always as row end,
x9 int unsigned,
Sys_start SYS_TRX_TYPE generated always as row start,
Sys_end SYS_TRX_TYPE generated always as row end,
period for system_time (Sys_start, Sys_end)
);
ERROR HY000: Wrong parameters for `t1`: 'WITH SYSTEM VERSIONING' missing
create or replace table t1 (
XNo int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_end timestamp(6) generated always as row end,
x10 int unsigned,
Sys_start SYS_TRX_TYPE generated always as row start,
Sys_end SYS_TRX_TYPE generated always as row end,
period for system_time (Sys_start, Sys_start)
);
ERROR HY000: Wrong parameters for `t1`: 'PERIOD FOR SYSTEM_TIME' columns must be different
create or replace table t1 (
XNo int unsigned,
Sys_start int generated always as row start,
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;
ERROR HY000: `Sys_start` must be of type `TIMESTAMP(6)` for versioned table `t1`
Got one of the listed errors
create or replace table t1 (
XNo int unsigned,
x12 int unsigned,
Sys_start timestamp(6) generated always as row start,
Sys_end int generated always as row end,
Sys_end bigint unsigned generated always as row end,
period for system_time (Sys_start, Sys_end)
) with system versioning;
ERROR HY000: `Sys_end` must be of type `TIMESTAMP(6)` for versioned table `t1`
Got one of the listed errors
create or replace table t1 (
XNo int unsigned,
Sys_start timestamp(6) 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_start` must be of type `BIGINT(20) UNSIGNED` for versioned table `t1`
create or replace table t1 (
XNo int unsigned,
x13 int unsigned,
Sys_start bigint generated always as row start,
Sys_end bigint generated always as row end,
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 (
XNo int unsigned,
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 (
A int with system versioning,
A1 int with system versioning,
B int
);
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`A` int(11) DEFAULT NULL,
`A1` int(11) DEFAULT NULL,
`B` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`sys_trx_start` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`sys_trx_end` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
create or replace table t1 (
A int with system versioning,
A2 int with system versioning,
B int
) with system versioning;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`A` int(11) DEFAULT NULL,
`A2` int(11) DEFAULT NULL,
`B` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`sys_trx_start` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`sys_trx_end` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
create or replace table t1 (
A int,
A3 int,
B int without system versioning
);
ERROR HY000: Wrong parameters for `t1`: 'WITH SYSTEM VERSIONING' missing
create or replace table t1 (
A int,
A4 int,
B int without system versioning
) with system versioning;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`A` int(11) DEFAULT NULL,
`A4` int(11) DEFAULT NULL,
`B` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`sys_trx_start` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`sys_trx_end` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
create or replace table t1 (
A int with system versioning,
A5 int with system versioning,
B int without system versioning
);
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`A` int(11) DEFAULT NULL,
`A5` int(11) DEFAULT NULL,
`B` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`sys_trx_start` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`sys_trx_end` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
create or replace table t1 (
A int with system versioning,
A6 int with system versioning,
B int without system versioning
) with system versioning;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`A` int(11) DEFAULT NULL,
`A6` int(11) DEFAULT NULL,
`B` int(11) DEFAULT NULL WITHOUT SYSTEM VERSIONING,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`sys_trx_start` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`sys_trx_end` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
create or replace table t1 (
A int without system versioning
A7 int without system versioning
);
ERROR HY000: Wrong parameters for `t1`: 'WITH SYSTEM VERSIONING' missing
create or replace table t1 (
A int without system versioning
A8 int without system versioning
) with system versioning;
ERROR HY000: Wrong parameters for `t1`: versioned fields missing
ERROR HY000: Wrong parameters for `t1`: no columns defined with system versioning!
create or replace table t1 (
A int without system versioning with system versioning
A9 int without system versioning with system versioning
);
ERROR HY000: Wrong parameters for `t1`: Versioning specified more than once for the same field
create or replace table t1 (
A int with system versioning without system versioning
A10 int with system versioning without system versioning
);
ERROR HY000: Wrong parameters for `t1`: Versioning specified more than once for the same field
create table t(
a int
a11 int
) without system versioning;
ERROR HY000: Wrong parameters for `t`: 'WITHOUT SYSTEM VERSIONING' is not allowed
create or replace table t1 (
A int
A12 int
) without system versioning with system versioning;
ERROR HY000: Wrong parameters for `t1`: Versioning specified more than once for the same table
create or replace table t1 (
A int
A13 int
) with system versioning without system versioning;
ERROR HY000: Wrong parameters for `t1`: Versioning specified more than once for the same table
create or replace table t1 (
A int
A14 int
) with system versioning with system versioning;
ERROR HY000: Wrong parameters for `t1`: Versioning specified more than once for the same table
create or replace table t1 (
A int
A15 int
) without system versioning without system versioning;
ERROR HY000: Wrong parameters for `t1`: Versioning specified more than once for the same table
create or replace table t1 (a int) with system versioning;
@ -231,9 +286,53 @@ show create table tt1;
Table Create Table
tt1 CREATE TABLE `tt1` (
`a` int(11) DEFAULT NULL,
`sys_trx_start` timestamp(6) GENERATED ALWAYS AS ROW START,
`sys_trx_end` timestamp(6) GENERATED ALWAYS AS ROW END,
`sys_trx_start` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`sys_trx_end` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`sys_trx_start`, `sys_trx_end`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
drop table tt1;
create or replace table t1 (x int) with system versioning;
create or replace table t2 (y int);
create or replace table t3 select * from t1 for system_time all, t2;
show create table t3;
Table Create Table
t3 CREATE TABLE `t3` (
`x` int(11) DEFAULT NULL,
`y` int(11) DEFAULT NULL
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1
create or replace table t2 (
y int,
st SYS_TRX_TYPE generated always as row start,
en SYS_TRX_TYPE generated always as row end,
period for system_time (st, en)
) with system versioning;
create or replace table t3 select * from t2;
show create table t3;
Table Create Table
t3 CREATE TABLE `t3` (
`y` int(11) DEFAULT NULL,
`st` SYS_TRX_TYPE GENERATED ALWAYS AS ROW START,
`en` SYS_TRX_TYPE GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (`st`, `en`)
) ENGINE=INNODB_OR_MYISAM DEFAULT CHARSET=latin1 WITH SYSTEM VERSIONING
create or replace table t3 select x, y, t1.sys_trx_start, t2.en from t1, t2;
ERROR HY000: Wrong parameters for `t3`: system fields selected from different tables
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 t2 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 t2 for system_time all where y = 2 into @st, @en;
select y from t2 for system_time all where st = @st and en = @en;
y
2
drop table t1;
drop table t2;
drop table t3;
drop procedure verify_vtq;
drop procedure innodb_verify_vtq;
drop function default_engine;
drop function sys_commit_ts;
drop function sys_datatype;