Skip to content

Commit

Permalink
SQL: system_time propagation from derived table [fixes #228]
Browse files Browse the repository at this point in the history
  • Loading branch information
midenok committed Jul 12, 2017
1 parent dcb5404 commit 60e456d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,36 @@ emp_id name mgr salary
1 bill NULL 1000
30 jane 1 750
20 john 30 500
create or replace table emp ( emp_id int, name varchar(127), mgr int) with system versioning;
create or replace table addr ( emp_id int, address varchar(100)) with system versioning;
insert emp values (1, 'bill', 0), (2, 'bill', 1), (3, 'kate', 1);
insert addr values (1, 'Moscow'), (2, 'New York'), (3, 'London');
set @ts=now(6);
delete from emp;
delete from addr;
insert emp values (4, 'john', 1);
insert addr values (4, 'Paris');
with ancestors as (select * from emp natural join addr) select * from ancestors;
emp_id name mgr address
4 john 1 Paris
with ancestors as (select * from emp natural join addr) select * from ancestors for system_time all;
emp_id name mgr address
1 bill 0 Moscow
2 bill 1 New York
3 kate 1 London
4 john 1 Paris
with ancestors as (select * from emp natural join addr query for system_time all) select * from ancestors;
emp_id name mgr address
1 bill 0 Moscow
2 bill 1 New York
3 kate 1 London
4 john 1 Paris
select * from emp natural join addr query for system_time all;
emp_id name mgr address
1 bill 0 Moscow
2 bill 1 New York
3 kate 1 London
4 john 1 Paris
drop table emp;
drop table dept;
drop table addr;
16 changes: 14 additions & 2 deletions mysql-test/suite/versioning/r/derived.result
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,22 @@ ERROR HY000: Derived table is prohibited: system fields from multiple tables `t1
select * from (select * from t1 for system_time as of timestamp @t0, t2) as s0;
x y
1 10
select * from (select *, t1.sys_trx_end from t2, t1 for system_time as of timestamp @t0) as s0;
with s1 as (select * from t1 for system_time as of timestamp @t0, t2) select * from s1;
x y
1 10
select * from (select *, t1.sys_trx_end from t2, t1 for system_time as of timestamp @t0) as s2;
y x
10 1
with s3 as (select *, t1.sys_trx_end from t2, t1 for system_time as of timestamp @t0) select * from s3;
y x
10 1
select * from (select *, t1.sys_trx_start from t2 for system_time as of now, t1) as s4 query for system_time as of timestamp @t0;
y x
10 1
with s5 as (select *, t1.sys_trx_start from t2 for system_time as of now, t1) select * from s5 for system_time as of timestamp @t0;
y x
10 1
select * from (select *, t1.sys_trx_start from t2 for system_time as of now, t1) as s0 query for system_time as of timestamp @t0;
with s6 as (select *, t1.sys_trx_start from t2 for system_time as of now, t1) select * from s6 query for system_time as of timestamp @t0;
y x
10 1
set @q= concat("create view vt1 as select * from t1 for system_time as of timestamp '", @t0, "'");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--innodb --default-storage-engine=innodb
--plugin-load=versioning
--versioning-hide=implicit
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,20 @@ as
)
select * from ancestors;

create or replace table emp ( emp_id int, name varchar(127), mgr int) with system versioning;
create or replace table addr ( emp_id int, address varchar(100)) with system versioning;
insert emp values (1, 'bill', 0), (2, 'bill', 1), (3, 'kate', 1);
insert addr values (1, 'Moscow'), (2, 'New York'), (3, 'London');
set @ts=now(6);
delete from emp;
delete from addr;
insert emp values (4, 'john', 1);
insert addr values (4, 'Paris');
with ancestors as (select * from emp natural join addr) select * from ancestors;
with ancestors as (select * from emp natural join addr) select * from ancestors for system_time all;
with ancestors as (select * from emp natural join addr query for system_time all) select * from ancestors;
select * from emp natural join addr query for system_time all;

drop table emp;
drop table dept;
drop table addr;
8 changes: 6 additions & 2 deletions mysql-test/suite/versioning/t/derived.test
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ select * from (select *, t1.sys_trx_end, t2.sys_trx_start from t1, t2) as s0;

# system_time propagation from inner to outer
select * from (select * from t1 for system_time as of timestamp @t0, t2) as s0;
with s1 as (select * from t1 for system_time as of timestamp @t0, t2) select * from s1;
# leading table selection
select * from (select *, t1.sys_trx_end from t2, t1 for system_time as of timestamp @t0) as s0;
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;
# system_time propagation from outer to inner
select * from (select *, t1.sys_trx_start from t2 for system_time as of now, t1) as s0 query for system_time as of timestamp @t0;
select * from (select *, t1.sys_trx_start from t2 for system_time as of now, t1) as s4 query for system_time as of timestamp @t0;
with s5 as (select *, t1.sys_trx_start from t2 for system_time as of now, t1) select * from s5 for system_time as of timestamp @t0;
with s6 as (select *, t1.sys_trx_start from t2 for system_time as of now, t1) select * from s6 query for system_time as of timestamp @t0;

# VIEW instead of t1
set @q= concat("create view vt1 as select * from t1 for system_time as of timestamp '", @t0, "'");
Expand Down
31 changes: 21 additions & 10 deletions sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
}

SELECT_LEX *outer_slex= slex->next_select_in_list();
bool use_slex_conds= false;
bool force_slex_conds= false;
if (outer_slex)
{
if (slex->vers_derived_conds)
Expand All @@ -786,15 +786,26 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
}
if (slex->vers_conditions.import_outer)
{
// Propagate query conditions from nearest outer SELECT_LEX:
while (outer_slex && (!outer_slex->vers_conditions || outer_slex->vers_conditions.from_inner))
outer_slex= outer_slex->next_select_in_list();
if (outer_slex)
DBUG_ASSERT(slex->master_unit());
TABLE_LIST* derived= slex->master_unit()->derived;
DBUG_ASSERT(derived);
if (derived->vers_conditions)
{
slex->vers_conditions= outer_slex->vers_conditions;
outer_slex->vers_conditions.used= true;
DBUG_ASSERT(slex->master_unit()->derived);
use_slex_conds= slex->master_unit()->derived->is_view();
slex->vers_conditions= derived->vers_conditions;
derived->vers_conditions.used= true;
force_slex_conds= derived->is_view();
}
else
{
// Propagate query conditions from nearest outer SELECT_LEX:
while (outer_slex && (!outer_slex->vers_conditions || outer_slex->vers_conditions.from_inner))
outer_slex= outer_slex->next_select_in_list();
if (outer_slex)
{
slex->vers_conditions= outer_slex->vers_conditions;
outer_slex->vers_conditions.used= true;
force_slex_conds= derived->is_view();
}
}
}
}
Expand All @@ -803,7 +814,7 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
{
if (table->table && table->table->versioned())
{
vers_select_conds_t &vers_conditions= use_slex_conds || !table->vers_conditions?
vers_select_conds_t &vers_conditions= force_slex_conds || !table->vers_conditions?
(slex->vers_conditions.used= true, slex->vers_conditions) :
table->vers_conditions;

Expand Down

0 comments on commit 60e456d

Please sign in to comment.