diff --git a/mysql-test/r/cte_recursive.result b/mysql-test/r/cte_recursive.result index 569f892ac39..1b26a776352 100644 --- a/mysql-test/r/cte_recursive.result +++ b/mysql-test/r/cte_recursive.result @@ -1769,3 +1769,40 @@ WITH RECURSIVE cte(f) AS SELECT * FROM cte as t; f DROP TABLE t1; +# +# MDEV-10889: mergeable derived in the spec of recursive CTE +# +create table t1 (a int); +insert into t1 values +(0), (1), (2), (3), (4); +create table t2 (a int); +insert into t2 values +(1), (2), (3), (4), (5); +with recursive +t1 as +( +select x.a from (select a from t2 where t2.a=3) x +union +select t2.a from t1,t2 where t1.a+1=t2.a +) +select * from t1; +a +3 +4 +5 +explain +with recursive +t1 as +( +select x.a from (select a from t2 where t2.a=3) x +union +select t2.a from t1,t2 where t1.a+1=t2.a +) +select * from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY ALL NULL NULL NULL NULL 5 +2 SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where +4 RECURSIVE UNION ALL NULL NULL NULL NULL 5 +4 RECURSIVE UNION t2 ALL NULL NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join) +NULL UNION RESULT ALL NULL NULL NULL NULL NULL +drop table t1,t2; diff --git a/mysql-test/t/cte_recursive.test b/mysql-test/t/cte_recursive.test index 1fc27ff4999..8b7f40ccd73 100644 --- a/mysql-test/t/cte_recursive.test +++ b/mysql-test/t/cte_recursive.test @@ -1304,3 +1304,35 @@ WITH RECURSIVE cte(f) AS (SELECT t1.f FROM t1 UNION ALL SELECT cte.f FROM cte) SELECT * FROM cte as t; DROP TABLE t1; + +--echo # +--echo # MDEV-10889: mergeable derived in the spec of recursive CTE +--echo # + +create table t1 (a int); +insert into t1 values + (0), (1), (2), (3), (4); +create table t2 (a int); +insert into t2 values + (1), (2), (3), (4), (5); + +with recursive +t1 as +( +select x.a from (select a from t2 where t2.a=3) x +union +select t2.a from t1,t2 where t1.a+1=t2.a +) +select * from t1; + +explain +with recursive +t1 as +( +select x.a from (select a from t2 where t2.a=3) x +union +select t2.a from t1,t2 where t1.a+1=t2.a +) +select * from t1; + +drop table t1,t2; diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc index f6447b29827..f203c33780d 100644 --- a/sql/sql_cte.cc +++ b/sql/sql_cte.cc @@ -1106,7 +1106,7 @@ bool With_element::check_unrestricted_recursive(st_select_lex *sel, table_map &unrestricted, table_map &encountered) { - /* Check conditions 1-for restricted specification*/ + /* Check conditions 1 for restricted specification*/ List_iterator ti(sel->leaf_tables); TABLE_LIST *tbl; while ((tbl= ti++)) @@ -1141,7 +1141,7 @@ bool With_element::check_unrestricted_recursive(st_select_lex *sel, encountered|= with_elem->get_elem_map(); } } - for (With_element *with_elem= sel->get_with_element()->owner->with_list.first; + for (With_element *with_elem= owner->with_list.first; with_elem; with_elem= with_elem->next) {