Skip to content

Commit 061d282

Browse files
committed
Fixed bug mdev-10923.
The code for st_select_lex::find_table_def_in_with_clauses() did not take into account the fact that the specs for mergeable CTEs were cloned and were not processed by the function With_element::check_dependencies_in_spec().
1 parent 903f34c commit 061d282

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

mysql-test/r/cte_nonrecursive.result

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,3 +895,35 @@ SELECT * FROM t1, t2 WHERE a2 = i1 and b2 >= i1 AND i1 IN ( SELECT i3 FROM t3 )
895895
;
896896
i1 a2 b2
897897
DROP TABLE t1,t2,t3;
898+
#
899+
# MDEV-10923: mergeable CTE used twice in the query
900+
#
901+
create table employees (
902+
name varchar(32),
903+
dept varchar(32),
904+
country varchar(8)
905+
);
906+
insert into employees
907+
values
908+
('Sergei Golubchik', 'Development', 'DE'),
909+
('Claudio Nanni', 'Support', 'ES'),
910+
('Sergei Petrunia', 'Development', 'RU');
911+
with eng as
912+
(
913+
select * from employees
914+
where dept in ('Development','Support')
915+
),
916+
eu_eng as
917+
(
918+
select * from eng where country IN ('DE','ES','RU')
919+
)
920+
select * from eu_eng T1
921+
where
922+
not exists (select 1 from eu_eng T2
923+
where T2.country=T1.country
924+
and T2.name <> T1.name);
925+
name dept country
926+
Sergei Golubchik Development DE
927+
Claudio Nanni Support ES
928+
Sergei Petrunia Development RU
929+
drop table employees;

mysql-test/t/cte_nonrecursive.test

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,36 @@ UNION
576576
SELECT * FROM t1, t2 WHERE a2 = i1 and b2 >= i1 AND i1 IN ( SELECT i3 FROM t3 )
577577
;
578578
DROP TABLE t1,t2,t3;
579+
580+
--echo #
581+
--echo # MDEV-10923: mergeable CTE used twice in the query
582+
--echo #
583+
584+
create table employees (
585+
name varchar(32),
586+
dept varchar(32),
587+
country varchar(8)
588+
);
589+
590+
insert into employees
591+
values
592+
('Sergei Golubchik', 'Development', 'DE'),
593+
('Claudio Nanni', 'Support', 'ES'),
594+
('Sergei Petrunia', 'Development', 'RU');
595+
596+
with eng as
597+
(
598+
select * from employees
599+
where dept in ('Development','Support')
600+
),
601+
eu_eng as
602+
(
603+
select * from eng where country IN ('DE','ES','RU')
604+
)
605+
select * from eu_eng T1
606+
where
607+
not exists (select 1 from eu_eng T2
608+
where T2.country=T1.country
609+
and T2.name <> T1.name);
610+
611+
drop table employees;

sql/sql_cte.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -928,13 +928,18 @@ With_element *st_select_lex::find_table_def_in_with_clauses(TABLE_LIST *table)
928928
/*
929929
If sl->master_unit() is the spec of a with element then the search for
930930
a definition was already done by With_element::check_dependencies_in_spec
931-
and it was unsuccesful.
931+
and it was unsuccesful. Yet for units cloned from the spec it has not
932+
been done yet.
932933
*/
933-
if (with_elem)
934+
if (with_elem && sl->master_unit() == with_elem->spec)
934935
break;
935936
With_clause *with_clause=sl->get_with_clause();
936-
if (with_clause && (found= with_clause->find_table_def(table,NULL)))
937-
break;
937+
if (with_clause)
938+
{
939+
With_element *barrier= with_clause->with_recursive ? NULL : with_elem;
940+
if ((found= with_clause->find_table_def(table, barrier)))
941+
break;
942+
}
938943
master_unit= sl->master_unit();
939944
/* Do not look for the table's definition beyond the scope of the view */
940945
if (master_unit->is_view)

0 commit comments

Comments
 (0)