Commit bc4a8669 authored by Aleksey Midenkov's avatar Aleksey Midenkov

SQL: recursive CTE inner derived vers_conditions [fix #385]

parent 22f45062
...@@ -91,6 +91,23 @@ insert addr values (1, 'Moscow'), (2, 'New York'), (3, 'London'); ...@@ -91,6 +91,23 @@ insert addr values (1, 'Moscow'), (2, 'New York'), (3, 'London');
set @ts=now(6); set @ts=now(6);
delete from emp; delete from emp;
delete from addr; delete from addr;
with recursive
ancestors
as
(
select e.emp_id, e.name, e.mgr
from emp for system_time as of timestamp @ts as e
where name = 'bill'
union
select ee.emp_id, ee.name, ee.mgr
from emp for system_time as of timestamp @ts as ee, ancestors as a
where ee.mgr = a.emp_id
)
select * from ancestors;
emp_id name mgr
1 bill 0
2 bill 1
3 kate 1
insert emp values (4, 'john', 1); insert emp values (4, 'john', 1);
insert addr values (4, 'Paris'); 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;
......
...@@ -90,6 +90,21 @@ insert addr values (1, 'Moscow'), (2, 'New York'), (3, 'London'); ...@@ -90,6 +90,21 @@ insert addr values (1, 'Moscow'), (2, 'New York'), (3, 'London');
set @ts=now(6); set @ts=now(6);
delete from emp; delete from emp;
delete from addr; delete from addr;
with recursive
ancestors
as
(
select e.emp_id, e.name, e.mgr
from emp for system_time as of timestamp @ts as e
where name = 'bill'
union
select ee.emp_id, ee.name, ee.mgr
from emp for system_time as of timestamp @ts as ee, ancestors as a
where ee.mgr = a.emp_id
)
select * from ancestors;
insert emp values (4, 'john', 1); insert emp values (4, 'john', 1);
insert addr values (4, 'Paris'); 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;
......
...@@ -850,8 +850,6 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived) ...@@ -850,8 +850,6 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
{ {
sl->vers_export_outer= impli_table->vers_conditions; sl->vers_export_outer= impli_table->vers_conditions;
} }
else
sl->vers_import_outer= true; // FIXME: is needed?
} }
} // if (sl->table_list.elements > 0) } // if (sl->table_list.elements > 0)
// System Versioning end // System Versioning end
......
...@@ -2313,7 +2313,6 @@ void st_select_lex::init_select() ...@@ -2313,7 +2313,6 @@ void st_select_lex::init_select()
in_tvc= false; in_tvc= false;
vers_saved_where= NULL; vers_saved_where= NULL;
vers_export_outer.empty(); vers_export_outer.empty();
vers_import_outer= false;
versioned_tables= 0; versioned_tables= 0;
} }
......
...@@ -1049,7 +1049,6 @@ class st_select_lex: public st_select_lex_node ...@@ -1049,7 +1049,6 @@ class st_select_lex: public st_select_lex_node
Item *vers_saved_where; Item *vers_saved_where;
public: public:
vers_select_conds_t vers_export_outer; vers_select_conds_t vers_export_outer;
bool vers_import_outer;
uint versioned_tables; uint versioned_tables;
int vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr); int vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr);
/* push new Item_field into item_list */ /* push new Item_field into item_list */
......
...@@ -829,11 +829,18 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr ...@@ -829,11 +829,18 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
vers_select_conds_t &vers_conditions= table->vers_conditions; vers_select_conds_t &vers_conditions= table->vers_conditions;
// propagate system_time from nearest outer SELECT_LEX // propagate system_time from nearest outer SELECT_LEX
if (!vers_conditions && outer_slex && vers_import_outer) if (!vers_conditions && outer_slex)
{ {
TABLE_LIST* derived= master_unit()->derived; TABLE_LIST* derived= master_unit()->derived;
if (derived == table && vers_export_outer) // recursive CTE
{
vers_conditions= vers_export_outer;
}
else
{
// inner SELECT may not be a derived table (derived == NULL) // inner SELECT may not be a derived table (derived == NULL)
while (derived && outer_slex && (!derived->vers_conditions || derived->vers_conditions.from_inner)) while (derived && outer_slex &&
(!derived->vers_conditions || derived->vers_conditions.from_inner))
{ {
derived= outer_slex->master_unit()->derived; derived= outer_slex->master_unit()->derived;
outer_slex= outer_slex->next_select_in_list(); outer_slex= outer_slex->next_select_in_list();
...@@ -844,6 +851,7 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr ...@@ -844,6 +851,7 @@ int SELECT_LEX::vers_setup_conds(THD *thd, TABLE_LIST *tables, COND **where_expr
vers_conditions= derived->vers_conditions; vers_conditions= derived->vers_conditions;
} }
} }
}
// propagate system_time from sysvar // propagate system_time from sysvar
if (!vers_conditions) if (!vers_conditions)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment