Skip to content

Commit 7e0ff13

Browse files
committed
SQL: derived fixes [related to #185]
1 parent 8a11f9b commit 7e0ff13

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

mysql-test/suite/versioning/r/cte_recursive.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ from emp as e for system_time as of timestamp @ts_1,
4141
ancestors as a
4242
where e.mgr = a.emp_id
4343
)
44-
select * from ancestors;
44+
select * from ancestors for system_time as of now;
4545
emp_id name mgr salary
4646
1 bill NULL 1000
4747
30 jane 1 750
@@ -59,7 +59,7 @@ from emp as e for system_time as of timestamp @ts_2,
5959
ancestors as a
6060
where e.mgr = a.emp_id
6161
)
62-
select * from ancestors for system_time as of timestamp @ts_2;
62+
select * from ancestors;
6363
emp_id name mgr salary
6464
1 bill NULL 1000
6565
30 jane 1 750

mysql-test/suite/versioning/r/view.result

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ x
3939
drop prepare stmt;
4040
select * from vt1;
4141
x
42+
2
43+
1
4244
prepare stmt from 'select * from vt1';
4345
execute stmt;
4446
x
47+
2
48+
1
4549
drop prepare stmt;
4650
select * from t1 for system_time as of timestamp @t1;
4751
x

mysql-test/suite/versioning/t/cte_recursive.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ as
4747
ancestors as a
4848
where e.mgr = a.emp_id
4949
)
50-
select * from ancestors;
50+
select * from ancestors for system_time as of now;
5151

5252
/* Expected 3 rows */
5353
with recursive
@@ -63,7 +63,7 @@ as
6363
ancestors as a
6464
where e.mgr = a.emp_id
6565
)
66-
select * from ancestors for system_time as of timestamp @ts_2;
66+
select * from ancestors;
6767

6868
drop table emp;
6969
drop table dept;

sql/sql_derived.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,12 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
824824
if (!expli_end && (res= sl->vers_push_field(thd, impli_table, impli_end)))
825825
goto exit;
826826

827-
if (impli_table->vers_conditions && !derived->is_view())
827+
if (impli_table->vers_conditions)
828+
{
828829
sl->vers_derived_conds= impli_table->vers_conditions;
830+
if (derived->is_view() && !sl->vers_conditions)
831+
sl->vers_conditions.import_outer= true;
832+
}
829833
else if (sl->vers_conditions)
830834
sl->vers_derived_conds= sl->vers_conditions;
831835
else

sql/sql_select.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -771,18 +771,20 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
771771
}
772772

773773
SELECT_LEX *outer_slex= slex->next_select_in_list();
774+
bool use_slex_conds= false;
774775
if (outer_slex)
775776
{
776777
if (slex->vers_derived_conds)
777778
{
778779
// Propagate derived conditions to outer SELECT_LEX:
779780
if (!outer_slex->vers_conditions)
780781
{
781-
(outer_slex->vers_conditions= slex->vers_derived_conds).
782-
from_inner= true;
782+
outer_slex->vers_conditions= slex->vers_derived_conds;
783+
outer_slex->vers_conditions.from_inner= true;
784+
outer_slex->vers_conditions.used= true;
783785
}
784786
}
785-
else if (slex->vers_conditions.import_outer)
787+
if (slex->vers_conditions.import_outer)
786788
{
787789
// Propagate query conditions from nearest outer SELECT_LEX:
788790
while (outer_slex && (!outer_slex->vers_conditions || outer_slex->vers_conditions.from_inner))
@@ -791,6 +793,8 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
791793
{
792794
slex->vers_conditions= outer_slex->vers_conditions;
793795
outer_slex->vers_conditions.used= true;
796+
DBUG_ASSERT(slex->master_unit()->derived);
797+
use_slex_conds= slex->master_unit()->derived->is_view();
794798
}
795799
}
796800
}
@@ -799,7 +803,7 @@ int vers_setup_select(THD *thd, TABLE_LIST *tables, COND **where_expr,
799803
{
800804
if (table->table && table->table->versioned())
801805
{
802-
vers_select_conds_t &vers_conditions= !table->vers_conditions?
806+
vers_select_conds_t &vers_conditions= use_slex_conds || !table->vers_conditions?
803807
(slex->vers_conditions.used= true, slex->vers_conditions) :
804808
table->vers_conditions;
805809

0 commit comments

Comments
 (0)