Skip to content

Commit 428a922

Browse files
committed
Fixed the bug mdev-12440.
When a CTE referring to another CTE from the same with clause was used twice then the server could not find the second CTE and reported a bogus error message. This happened because for any unit that was created as a clone of a CTE specification the pointer to the WITH clause that owned this CTE was not set.
1 parent 1759e91 commit 428a922

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

mysql-test/r/cte_nonrecursive.result

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,26 @@ show create view v1;
961961
View Create View character_set_client collation_connection
962962
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with alias1 as (select 1 AS `one`), alias2 as (select 2 AS `two`)select `alias1`.`one` AS `one`,`alias2`.`two` AS `two` from (`alias1` join `alias2`) latin1 latin1_swedish_ci
963963
drop view v1;
964+
#
965+
# MDEV-12440: the same CTE table is used in twice
966+
#
967+
create table t1 (a int, b varchar(32));
968+
insert into t1 values
969+
(4,'aaaa' ), (7,'bb'), (1,'ccc'), (4,'dd');
970+
# cte2 is used in the main query and in the spec for ct3
971+
with
972+
cte1 as (select * from t1 where b >= 'c'),
973+
cte2 as (select * from cte1 where a < 7),
974+
cte3 as (select * from cte2 where a > 1)
975+
select * from cte2, cte3 where cte2.a = cte3.a;
976+
a b a b
977+
4 dd 4 dd
978+
# cte2 is used twice in the spec for ct3
979+
with
980+
cte1 as (select * from t1 where b >= 'b'),
981+
cte2 as (select * from cte1 where b > 'c'),
982+
cte3 as (select * from cte2 where a > 1 union select * from cte2 where a > 1)
983+
select * from cte3;
984+
a b
985+
4 dd
986+
drop table t1;

mysql-test/t/cte_nonrecursive.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,27 @@ select * from v1;
642642
show create view v1;
643643

644644
drop view v1;
645+
646+
--echo #
647+
--echo # MDEV-12440: the same CTE table is used in twice
648+
--echo #
649+
650+
create table t1 (a int, b varchar(32));
651+
insert into t1 values
652+
(4,'aaaa' ), (7,'bb'), (1,'ccc'), (4,'dd');
653+
654+
--echo # cte2 is used in the main query and in the spec for ct3
655+
with
656+
cte1 as (select * from t1 where b >= 'c'),
657+
cte2 as (select * from cte1 where a < 7),
658+
cte3 as (select * from cte2 where a > 1)
659+
select * from cte2, cte3 where cte2.a = cte3.a;
660+
661+
--echo # cte2 is used twice in the spec for ct3
662+
with
663+
cte1 as (select * from t1 where b >= 'b'),
664+
cte2 as (select * from cte1 where b > 'c'),
665+
cte3 as (select * from cte2 where a > 1 union select * from cte2 where a > 1)
666+
select * from cte3;
667+
668+
drop table t1;

sql/sql_cte.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
780780
with_table->next_global= spec_tables;
781781
}
782782
res= &lex->unit;
783+
res->set_with_clause(owner);
783784

784785
lex->unit.include_down(with_table->select_lex);
785786
lex->unit.set_slave(with_select);

0 commit comments

Comments
 (0)