2017-02-09 12:01:05 +03:00
|
|
|
create table emp
|
|
|
|
(
|
|
|
|
emp_id int,
|
|
|
|
name varchar(127),
|
|
|
|
mgr int
|
|
|
|
) with system versioning;
|
|
|
|
|
|
|
|
insert into emp values (1, 'bill', 0),
|
|
|
|
(2, 'bill', 1),
|
|
|
|
(3, 'kate', 1);
|
|
|
|
set @ts=now(6);
|
|
|
|
delete from emp;
|
|
|
|
insert into emp values (4, 'john', 1);
|
|
|
|
|
|
|
|
with ancestors as (select * from emp) select * from ancestors;
|
|
|
|
set @tmp= "with ancestors as (select * from emp) select * from ancestors";
|
|
|
|
prepare stmt from @tmp; execute stmt; drop prepare stmt;
|
|
|
|
|
2017-04-28 12:07:04 +03:00
|
|
|
with ancestors as (select * from emp for system_time all) select * from ancestors;
|
|
|
|
set @tmp= "with ancestors as (select * from emp for system_time all) select * from ancestors";
|
2017-02-09 12:01:05 +03:00
|
|
|
prepare stmt from @tmp; execute stmt; drop prepare stmt;
|
|
|
|
|
|
|
|
with recursive ancestors as (select * from emp) select * from ancestors;
|
|
|
|
set @tmp= "with recursive ancestors as (select * from emp) select * from ancestors";
|
|
|
|
prepare stmt from @tmp; execute stmt; drop prepare stmt;
|
|
|
|
|
|
|
|
select emp_id from (select emp_id from emp where sys_trx_end>'2031-1-1') as tmp;
|
|
|
|
set @tmp= "select emp_id from (select emp_id from emp where sys_trx_end>'2031-1-1') as tmp";
|
|
|
|
prepare stmt from @tmp; execute stmt; drop prepare stmt;
|
|
|
|
|
|
|
|
with recursive
|
|
|
|
ancestors
|
|
|
|
as
|
|
|
|
(
|
|
|
|
select e.emp_id, e.name, e.mgr
|
|
|
|
from emp as e
|
|
|
|
where name = 'john'
|
|
|
|
union
|
|
|
|
select ee.emp_id, ee.name, ee.mgr
|
|
|
|
from emp as ee, ancestors as a
|
|
|
|
where ee.mgr = a.emp_id
|
|
|
|
)
|
|
|
|
select * from ancestors;
|
|
|
|
set @tmp= "
|
|
|
|
with recursive
|
|
|
|
ancestors
|
|
|
|
as
|
|
|
|
(
|
|
|
|
select e.emp_id, e.name, e.mgr
|
|
|
|
from emp as e
|
|
|
|
where name = 'john'
|
|
|
|
union
|
|
|
|
select ee.emp_id, ee.name, ee.mgr
|
|
|
|
from emp as ee, ancestors as a
|
|
|
|
where ee.mgr = a.emp_id
|
|
|
|
)
|
2017-04-28 12:07:04 +03:00
|
|
|
select * from ancestors";
|
2017-02-09 12:01:05 +03:00
|
|
|
prepare stmt from @tmp; execute stmt; drop prepare stmt;
|
|
|
|
|
2017-12-11 14:09:58 +01:00
|
|
|
#385
|
2017-02-09 12:01:05 +03:00
|
|
|
with recursive
|
|
|
|
ancestors
|
|
|
|
as
|
|
|
|
(
|
|
|
|
select e.emp_id, e.name, e.mgr
|
2017-12-11 14:09:58 +01:00
|
|
|
from emp for system_time as of timestamp @ts as e
|
2017-02-09 12:01:05 +03:00
|
|
|
where name = 'bill'
|
|
|
|
union
|
|
|
|
select ee.emp_id, ee.name, ee.mgr
|
2017-12-15 18:12:18 +03:00
|
|
|
from emp for system_time as of timestamp @ts as ee,
|
2017-12-14 13:43:37 +03:00
|
|
|
ancestors as a
|
2017-02-09 12:01:05 +03:00
|
|
|
where ee.mgr = a.emp_id
|
|
|
|
)
|
2017-04-28 12:07:04 +03:00
|
|
|
select * from ancestors;
|
2017-02-09 12:01:05 +03:00
|
|
|
set @tmp= "
|
|
|
|
with recursive
|
|
|
|
ancestors
|
|
|
|
as
|
|
|
|
(
|
|
|
|
select e.emp_id, e.name, e.mgr
|
2017-12-11 14:09:58 +01:00
|
|
|
from emp for system_time as of timestamp @ts as e
|
2017-02-09 12:01:05 +03:00
|
|
|
where name = 'bill'
|
|
|
|
union
|
|
|
|
select ee.emp_id, ee.name, ee.mgr
|
2017-12-14 13:43:37 +03:00
|
|
|
from emp for system_time as of timestamp @ts as ee,
|
|
|
|
ancestors as a
|
2017-02-09 12:01:05 +03:00
|
|
|
where ee.mgr = a.emp_id
|
|
|
|
)
|
2017-04-28 12:07:04 +03:00
|
|
|
select * from ancestors";
|
2017-02-09 12:01:05 +03:00
|
|
|
prepare stmt from @tmp; execute stmt; drop prepare stmt;
|
|
|
|
|
|
|
|
drop table emp;
|
2017-04-28 12:07:04 +03:00
|
|
|
|
|
|
|
create or replace table t1 (x int) with system versioning;
|
|
|
|
create or replace table t2 (y int) with system versioning;
|
|
|
|
insert into t1 values (1);
|
|
|
|
set @t0= now(6);
|
2017-12-06 06:14:22 +03:00
|
|
|
delete from t1;
|
2017-04-28 12:07:04 +03:00
|
|
|
insert into t1 values (2);
|
|
|
|
insert into t2 values (10);
|
|
|
|
|
|
|
|
--error ER_VERS_DERIVED_PROHIBITED
|
|
|
|
select * from (select *, t1.sys_trx_end, t1.sys_trx_end as endo from t1) as s0;
|
|
|
|
--error ER_VERS_DERIVED_PROHIBITED
|
|
|
|
select * from (select *, t1.sys_trx_end, t2.sys_trx_start from t1, t2) as s0;
|
|
|
|
|
2017-12-06 06:14:22 +03:00
|
|
|
--echo # SYSTEM_TIME propagation from inner to outer
|
2017-04-28 12:07:04 +03:00
|
|
|
select * from (select * from t1 for system_time as of timestamp @t0, t2) as s0;
|
2017-07-12 10:36:02 +03:00
|
|
|
with s1 as (select * from t1 for system_time as of timestamp @t0, t2) select * from s1;
|
2017-12-06 06:14:22 +03:00
|
|
|
--echo # leading table selection
|
2017-07-12 10:36:02 +03:00
|
|
|
select * from (select *, t1.sys_trx_end from t2, t1 for system_time as of timestamp @t0) as s2;
|
|
|
|
with s3 as (select *, t1.sys_trx_end from t2, t1 for system_time as of timestamp @t0) select * from s3;
|
2017-04-28 12:07:04 +03:00
|
|
|
|
2017-12-06 06:14:22 +03:00
|
|
|
--echo ### VIEW instead of t1
|
2017-04-28 12:07:04 +03:00
|
|
|
set @q= concat("create view vt1 as select * from t1 for system_time as of timestamp '", @t0, "'");
|
|
|
|
prepare q from @q; execute q; drop prepare q;
|
2017-12-06 06:14:22 +03:00
|
|
|
create view vt2 as select * from t1;
|
2017-04-28 12:07:04 +03:00
|
|
|
|
2017-12-06 06:14:22 +03:00
|
|
|
--echo # SYSTEM_TIME propagation from view
|
2017-04-28 12:07:04 +03:00
|
|
|
select * from vt1;
|
2017-12-06 06:14:22 +03:00
|
|
|
--echo # SYSTEM_TIME propagation from inner to outer
|
2017-04-28 12:07:04 +03:00
|
|
|
select * from (select * from vt1, t2) as s0;
|
|
|
|
|
2017-12-06 06:14:22 +03:00
|
|
|
--echo ### SYSTEM_TIME clash
|
|
|
|
--error ER_VERS_SYSTEM_TIME_CLASH
|
2017-12-11 14:09:58 +01:00
|
|
|
select * from (select * from t1 for system_time all) for system_time all as dt0;
|
2017-12-06 06:14:22 +03:00
|
|
|
--error ER_VERS_SYSTEM_TIME_CLASH
|
|
|
|
select * from vt1 for system_time all;
|
|
|
|
--error ER_VERS_SYSTEM_TIME_CLASH
|
|
|
|
with dt1 as (select * from t1 for system_time all)
|
|
|
|
select * from dt1 for system_time all;
|
|
|
|
|
|
|
|
--echo ### UNION
|
|
|
|
set @t1= now(6);
|
|
|
|
delete from t2;
|
|
|
|
insert into t2 values (3);
|
|
|
|
--echo # SYSTEM_TIME is not propagated
|
|
|
|
select x from t1 union
|
|
|
|
select y from t2;
|
|
|
|
select x from t1 for system_time as of @t0 union
|
|
|
|
select y from t2;
|
|
|
|
select x from t1 union
|
|
|
|
select y from t2 for system_time as of @t1;
|
|
|
|
select x from t1 for system_time as of @t0 union
|
|
|
|
select y from t2 for system_time as of @t1;
|
|
|
|
|
2017-12-12 20:27:47 +03:00
|
|
|
--echo # LEFT/RIGHT JOIN
|
|
|
|
create or replace table t1 (x int, y int) with system versioning;
|
|
|
|
create or replace table t2 (x int, y int) with system versioning;
|
|
|
|
|
|
|
|
insert into t1 values (1, 1), (1, 2), (1, 3), (4, 4), (5, 5);
|
|
|
|
insert into t2 values (1, 2), (2, 1), (3, 1);
|
|
|
|
|
2017-12-14 13:43:37 +03:00
|
|
|
--echo ## Outer or inner SYSTEM_TIME produces same expression
|
|
|
|
|
|
|
|
--disable_warnings
|
|
|
|
--disable_query_log
|
|
|
|
explain extended
|
|
|
|
select * from (
|
|
|
|
select t1.x, t1.y as y1, t2.x as x2, t2.y as y2
|
|
|
|
from t1 join t2 on t1.x = t2.x) for system_time as of now() as t;
|
|
|
|
|
|
|
|
let $a=`show warnings`;
|
|
|
|
--echo Query A:
|
|
|
|
echo $a;
|
|
|
|
|
|
|
|
explain extended
|
|
|
|
select * from (
|
|
|
|
select t1.x, t1.y as y1, t2.x as x2, t2.y as y2
|
|
|
|
from t1 for system_time as of now()
|
|
|
|
join t2 for system_time as of now() on t1.x = t2.x) as t;
|
|
|
|
|
|
|
|
let $b=`show warnings`;
|
|
|
|
--echo Query B:
|
|
|
|
echo $b;
|
|
|
|
|
|
|
|
if ($a == $b)
|
|
|
|
{
|
|
|
|
--echo Fine result: queries A and B are equal.
|
|
|
|
}
|
|
|
|
--enable_query_log
|
|
|
|
--enable_warnings
|
|
|
|
|
2017-12-12 20:27:47 +03:00
|
|
|
--echo ## LEFT JOIN: t1, t2 versioned
|
|
|
|
select * from (
|
|
|
|
select t1.x as LJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2
|
|
|
|
from t1 left join t2 on t1.x = t2.x)
|
|
|
|
as derived;
|
|
|
|
|
|
|
|
alter table t2 drop system versioning;
|
|
|
|
|
|
|
|
--echo ## LEFT JOIN: t1 versioned
|
|
|
|
select * from (
|
|
|
|
select t1.x as LJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2
|
|
|
|
from t1 left join t2 on t1.x = t2.x)
|
|
|
|
as derived;
|
|
|
|
|
|
|
|
alter table t1 drop system versioning;
|
|
|
|
alter table t2 add system versioning;
|
|
|
|
|
|
|
|
--echo ## LEFT JOIN: t2 versioned
|
|
|
|
select * from (
|
|
|
|
select t1.x as LJ3_x1, t1.y as y1, t2.x as x2, t2.y as y2
|
|
|
|
from t1 left join t2 on t1.x = t2.x)
|
|
|
|
as derived;
|
|
|
|
|
|
|
|
alter table t1 add system versioning;
|
|
|
|
|
|
|
|
--echo ## RIGHT JOIN: t1, t2 versioned
|
|
|
|
select * from (
|
|
|
|
select t1.x as RJ1_x1, t1.y as y1, t2.x as x2, t2.y as y2
|
|
|
|
from t1 right join t2 on t1.x = t2.x)
|
|
|
|
as derived;
|
|
|
|
|
|
|
|
alter table t2 drop system versioning;
|
|
|
|
|
|
|
|
--echo ## RIGHT JOIN: t1 versioned
|
|
|
|
select * from (
|
|
|
|
select t1.x as RJ2_x1, t1.y as y1, t2.x as x2, t2.y as y2
|
|
|
|
from t1 right join t2 on t1.x = t2.x)
|
|
|
|
as derived;
|
|
|
|
|
|
|
|
alter table t1 drop system versioning;
|
|
|
|
alter table t2 add system versioning;
|
|
|
|
|
|
|
|
--echo ## RIGHT JOIN: t2 versioned
|
|
|
|
select * from (
|
|
|
|
select t1.x as RJ3_x1, t1.y as y1, t2.x as x2, t2.y as y2
|
|
|
|
from t1 right join t2 on t1.x = t2.x)
|
|
|
|
as derived;
|
|
|
|
|
2017-12-12 01:04:07 +03:00
|
|
|
drop table t1, t2;
|
|
|
|
drop view vt1, vt2;
|
2017-12-12 20:27:47 +03:00
|
|
|
|