diff --git a/mysql-test/r/cte_recursive.result b/mysql-test/r/cte_recursive.result index 77b391d6629..aa9ed3fc28c 100644 --- a/mysql-test/r/cte_recursive.result +++ b/mysql-test/r/cte_recursive.result @@ -239,15 +239,15 @@ as ( select * from folks -where name = 'Vasya' and dob = '2000-01-01' +where name = 'Vasya' union select p.* -from folks as p, ancestors AS a -where p.id = a.father +from folks as p, ancestors as fa +where p.id = fa.father union select p.* -from folks as p, ancestors AS a -where p.id = a.mother +from folks as p, ancestors as ma +where p.id = ma.mother ) select * from ancestors; id name dob father mother @@ -346,7 +346,37 @@ id name dob father mother 9 Grandma Ann 1941-10-15 NULL NULL 8 Grandma Sally 1943-08-23 5 6 6 Grandgrandma Martha 1923-05-17 NULL NULL -drop view v1; +create view v2 as +with recursive +ancestors +as +( +select * +from folks +where name = 'Vasya' + union +select p.* +from folks as p, ancestors as fa +where p.id = fa.father +union +select p.* +from folks as p, ancestors as ma +where p.id = ma.mother +) +select * from ancestors; +show create view v2; +View Create View character_set_client collation_connection +v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with recursive ancestors as (select `folks`.`id` AS `id`,`folks`.`name` AS `name`,`folks`.`dob` AS `dob`,`folks`.`father` AS `father`,`folks`.`mother` AS `mother` from `folks` where (`folks`.`name` = 'Vasya') union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from (`folks` `p` join `ancestors` `fa`) where (`p`.`id` = `fa`.`father`) union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from (`folks` `p` join `ancestors` `ma`) where (`p`.`id` = `ma`.`mother`))select `ancestors`.`id` AS `id`,`ancestors`.`name` AS `name`,`ancestors`.`dob` AS `dob`,`ancestors`.`father` AS `father`,`ancestors`.`mother` AS `mother` from `ancestors` latin1 latin1_swedish_ci +select * from v2; +id name dob father mother +100 Vasya 2000-01-01 20 30 +20 Dad 1970-02-02 10 9 +30 Mom 1975-03-03 8 7 +9 Grandma Ann 1941-10-15 NULL NULL +10 Grandpa Bill 1940-04-05 NULL NULL +8 Grandma Sally 1943-08-23 5 6 +6 Grandgrandma Martha 1923-05-17 NULL NULL +drop view v1,v2; explain extended with recursive ancestors diff --git a/mysql-test/t/cte_recursive.test b/mysql-test/t/cte_recursive.test index 47eae971c6d..4982bf78adb 100644 --- a/mysql-test/t/cte_recursive.test +++ b/mysql-test/t/cte_recursive.test @@ -199,15 +199,15 @@ as ( select * from folks - where name = 'Vasya' and dob = '2000-01-01' + where name = 'Vasya' union select p.* - from folks as p, ancestors AS a - where p.id = a.father + from folks as p, ancestors as fa + where p.id = fa.father union select p.* - from folks as p, ancestors AS a - where p.id = a.mother + from folks as p, ancestors as ma + where p.id = ma.mother ) select * from ancestors; @@ -279,8 +279,30 @@ show create view v1; select * from v1; -drop view v1; +create view v2 as +with recursive +ancestors +as +( + select * + from folks + where name = 'Vasya' + union + select p.* + from folks as p, ancestors as fa + where p.id = fa.father + union + select p.* + from folks as p, ancestors as ma + where p.id = ma.mother +) +select * from ancestors; +show create view v2; + +select * from v2; + +drop view v1,v2; explain extended with recursive diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc index 7e60a8d1892..ffc54f50af1 100644 --- a/sql/sql_cte.cc +++ b/sql/sql_cte.cc @@ -330,8 +330,10 @@ With_element *With_clause::find_table_def(TABLE_LIST *table) with_elem != NULL; with_elem= with_elem->next_elem) { - if (my_strcasecmp(system_charset_info, with_elem->query_name->str, table->table_name) == 0) + if (my_strcasecmp(system_charset_info, with_elem->query_name->str, + table->table_name) == 0) { + table->set_derived(); return with_elem; } } @@ -740,6 +742,8 @@ bool st_select_lex::check_unrestricted_recursive() encountered)) return true; with_elem->owner->unrestricted|= unrestricted; + if (with_sum_func) + with_elem->owner->unrestricted|= with_elem->mutually_recursive; return false; } @@ -813,7 +817,7 @@ bool With_element::check_unrestricted_recursive(st_select_lex *sel, { if (tab->outer_join & (JOIN_TYPE_LEFT | JOIN_TYPE_RIGHT)) { - unrestricted|= get_elem_map(); + unrestricted|= mutually_recursive; break; } } diff --git a/sql/sql_union.cc b/sql/sql_union.cc index 902620aaac1..9308dc2a841 100644 --- a/sql/sql_union.cc +++ b/sql/sql_union.cc @@ -205,13 +205,13 @@ select_union_recursive::create_result_table(THD *thd_arg, { if (select_union::create_result_table(thd_arg, column_types, is_union_distinct, options, - alias, bit_fields_as_long, + "", bit_fields_as_long, create_table, keep_row_order)) return true; if (! (incr_table= create_tmp_table(thd_arg, &tmp_table_param, *column_types, (ORDER*) 0, false, 1, - options, HA_POS_ERROR, alias, + options, HA_POS_ERROR, "", !create_table, keep_row_order))) return true;