From 079c3599711b9cbd3ce323f32cf99693cc3d5e3b Mon Sep 17 00:00:00 2001 From: Galina Shalygina Date: Tue, 19 Dec 2017 11:49:40 +0200 Subject: [PATCH] MDEV-14629: failing assertion when a user-defined variable is defined by the recursive CTE During the user-defined variable defined by the recursive CTE handling procedure check_dependencies_in_with_clauses that checks dependencies between the tables that are defined in the CTE and find recursive definitions wasn't called. --- mysql-test/r/cte_recursive.result | 47 ++++++++++++++++++++++++++++ mysql-test/t/cte_recursive.test | 51 +++++++++++++++++++++++++++++++ sql/sql_parse.cc | 3 ++ 3 files changed, 101 insertions(+) diff --git a/mysql-test/r/cte_recursive.result b/mysql-test/r/cte_recursive.result index b744f24bb57..ece5421e279 100644 --- a/mysql-test/r/cte_recursive.result +++ b/mysql-test/r/cte_recursive.result @@ -2897,3 +2897,50 @@ n 1 2 3 +# +# mdev-14629: a user-defined variable is defined by the recursive CTE +# +set @var= +( +with recursive cte_tab(a) as ( +select 1 +union +select a+1 from cte_tab +where a<3) +select count(*) from cte_tab +); +select @var; +@var +3 +create table t1(a int, b int); +insert into t1 values (3,8),(1,5),(5,7),(7,4),(4,3); +set @var= +( +with recursive summ(a,s) as ( +select 1, 0 union +select t1.b, t1.b+summ.s from summ, t1 +where summ.a=t1.a) +select s from summ +order by a desc +limit 1 +); +select @var; +@var +27 +set @var= +( +with recursive +cte_1 as ( +select 1 +union +select * from cte_2), +cte_2 as ( +select * from cte_1 +union +select a from t1, cte_2 +where t1.a=cte_2.a) +select * from cte_2 +limit 1 +); +ERROR HY000: Unacceptable mutual recursion with anchored table 'cte_1' +drop table t1; diff --git a/mysql-test/t/cte_recursive.test b/mysql-test/t/cte_recursive.test index 36d948251c3..928f8a4334b 100644 --- a/mysql-test/t/cte_recursive.test +++ b/mysql-test/t/cte_recursive.test @@ -1947,3 +1947,54 @@ cte2 as ( ) SELECT * FROM cte1; + +--echo # +--echo # mdev-14629: a user-defined variable is defined by the recursive CTE +--echo # + +set @var= +( + with recursive cte_tab(a) as ( + select 1 + union + select a+1 from cte_tab + where a<3) + select count(*) from cte_tab +); + +select @var; + +create table t1(a int, b int); +insert into t1 values (3,8),(1,5),(5,7),(7,4),(4,3); + +set @var= +( + with recursive summ(a,s) as ( + select 1, 0 union + select t1.b, t1.b+summ.s from summ, t1 + where summ.a=t1.a) + select s from summ + order by a desc + limit 1 +); + +select @var; + +--ERROR ER_UNACCEPTABLE_MUTUAL_RECURSION +set @var= +( + with recursive + cte_1 as ( + select 1 + union + select * from cte_2), + cte_2 as ( + select * from cte_1 + union + select a from t1, cte_2 + where t1.a=cte_2.a) + select * from cte_2 + limit 1 +); + +drop table t1; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index d964831a098..7a4530082eb 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -4818,6 +4818,9 @@ end_with_restore_list: { List *lex_var_list= &lex->var_list; + if (check_dependencies_in_with_clauses(thd->lex->with_clauses_list)) + goto error; + if ((check_table_access(thd, SELECT_ACL, all_tables, FALSE, UINT_MAX, FALSE) || open_and_lock_tables(thd, all_tables, TRUE, 0))) goto error;