Skip to content

Commit

Permalink
SQL: derived fixes [related to #185]
Browse files Browse the repository at this point in the history
  • Loading branch information
midenok committed May 5, 2017
1 parent 8a11f9b commit 7e0ff13
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions mysql-test/suite/versioning/r/cte_recursive.result
Expand Up @@ -41,7 +41,7 @@ from emp as e for system_time as of timestamp @ts_1,
ancestors as a
where e.mgr = a.emp_id
)
select * from ancestors;
select * from ancestors for system_time as of now;
emp_id name mgr salary
1 bill NULL 1000
30 jane 1 750
Expand All @@ -59,7 +59,7 @@ from emp as e for system_time as of timestamp @ts_2,
ancestors as a
where e.mgr = a.emp_id
)
select * from ancestors for system_time as of timestamp @ts_2;
select * from ancestors;
emp_id name mgr salary
1 bill NULL 1000
30 jane 1 750
Expand Down
4 changes: 4 additions & 0 deletions mysql-test/suite/versioning/r/view.result
Expand Up @@ -39,9 +39,13 @@ x
drop prepare stmt;
select * from vt1;
x
2
1
prepare stmt from 'select * from vt1';
execute stmt;
x
2
1
drop prepare stmt;
select * from t1 for system_time as of timestamp @t1;
x
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/suite/versioning/t/cte_recursive.test
Expand Up @@ -47,7 +47,7 @@ as
ancestors as a
where e.mgr = a.emp_id
)
select * from ancestors;
select * from ancestors for system_time as of now;

/* Expected 3 rows */
with recursive
Expand All @@ -63,7 +63,7 @@ as
ancestors as a
where e.mgr = a.emp_id
)
select * from ancestors for system_time as of timestamp @ts_2;
select * from ancestors;

drop table emp;
drop table dept;
6 changes: 5 additions & 1 deletion sql/sql_derived.cc
Expand Up @@ -824,8 +824,12 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
if (!expli_end && (res= sl->vers_push_field(thd, impli_table, impli_end)))
goto exit;

if (impli_table->vers_conditions && !derived->is_view())
if (impli_table->vers_conditions)
{
sl->vers_derived_conds= impli_table->vers_conditions;
if (derived->is_view() && !sl->vers_conditions)
sl->vers_conditions.import_outer= true;
}
else if (sl->vers_conditions)
sl->vers_derived_conds= sl->vers_conditions;
else
Expand Down
12 changes: 8 additions & 4 deletions sql/sql_select.cc
Expand Up @@ -771,18 +771,20 @@ 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;
if (outer_slex)
{
if (slex->vers_derived_conds)
{
// Propagate derived conditions to outer SELECT_LEX:
if (!outer_slex->vers_conditions)
{
(outer_slex->vers_conditions= slex->vers_derived_conds).
from_inner= true;
outer_slex->vers_conditions= slex->vers_derived_conds;
outer_slex->vers_conditions.from_inner= true;
outer_slex->vers_conditions.used= true;
}
}
else if (slex->vers_conditions.import_outer)
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))
Expand All @@ -791,6 +793,8 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
{
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();
}
}
}
Expand All @@ -799,7 +803,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= !table->vers_conditions?
vers_select_conds_t &vers_conditions= use_slex_conds || !table->vers_conditions?
(slex->vers_conditions.used= true, slex->vers_conditions) :
table->vers_conditions;

Expand Down

0 comments on commit 7e0ff13

Please sign in to comment.