Skip to content

Commit 48b4e33

Browse files
committed
Allowed to use WITH clauses before SELECT in CREATE ... SELECT
and INSERT ... SELECT. Added test cases.
1 parent 4368efe commit 48b4e33

File tree

4 files changed

+108
-10
lines changed

4 files changed

+108
-10
lines changed

mysql-test/r/cte_recursive.result

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,54 @@ EXPLAIN
15561556
}
15571557
}
15581558
}
1559+
create table my_ancestors
1560+
with recursive
1561+
ancestor_ids (id)
1562+
as
1563+
(
1564+
select father from folks where name = 'Me'
1565+
union
1566+
select mother from folks where name = 'Me'
1567+
union
1568+
select father from folks, ancestor_ids a where folks.id = a.id
1569+
union
1570+
select mother from folks, ancestor_ids a where folks.id = a.id
1571+
)
1572+
select p.* from folks as p, ancestor_ids as a where p.id = a.id;
1573+
select * from my_ancestors;
1574+
id name dob father mother
1575+
20 Dad 1970-02-02 10 9
1576+
30 Mom 1975-03-03 8 7
1577+
10 Grandpa Bill 1940-04-05 NULL NULL
1578+
9 Grandma Ann 1941-10-15 NULL NULL
1579+
7 Grandma Sally 1943-08-23 NULL 6
1580+
8 Grandpa Ben 1940-10-21 NULL NULL
1581+
6 Grandgrandma Martha 1923-05-17 NULL NULL
1582+
delete from my_ancestors;
1583+
insert into my_ancestors
1584+
with recursive
1585+
ancestor_ids (id)
1586+
as
1587+
(
1588+
select father from folks where name = 'Me'
1589+
union
1590+
select mother from folks where name = 'Me'
1591+
union
1592+
select father from folks, ancestor_ids a where folks.id = a.id
1593+
union
1594+
select mother from folks, ancestor_ids a where folks.id = a.id
1595+
)
1596+
select p.* from folks as p, ancestor_ids as a where p.id = a.id;
1597+
select * from my_ancestors;
1598+
id name dob father mother
1599+
20 Dad 1970-02-02 10 9
1600+
30 Mom 1975-03-03 8 7
1601+
10 Grandpa Bill 1940-04-05 NULL NULL
1602+
9 Grandma Ann 1941-10-15 NULL NULL
1603+
7 Grandma Sally 1943-08-23 NULL 6
1604+
8 Grandpa Ben 1940-10-21 NULL NULL
1605+
6 Grandgrandma Martha 1923-05-17 NULL NULL
1606+
drop table my_ancestors;
15591607
drop table folks;
15601608
#
15611609
# MDEV-10372: [bb-10.2-mdev9864 tree] EXPLAIN with recursive CTE enters endless recursion

mysql-test/t/cte_recursive.test

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,44 @@ select h_name, h_dob, w_name, w_dob
11621162
from ancestor_couples;
11631163

11641164

1165+
create table my_ancestors
1166+
with recursive
1167+
ancestor_ids (id)
1168+
as
1169+
(
1170+
select father from folks where name = 'Me'
1171+
union
1172+
select mother from folks where name = 'Me'
1173+
union
1174+
select father from folks, ancestor_ids a where folks.id = a.id
1175+
union
1176+
select mother from folks, ancestor_ids a where folks.id = a.id
1177+
)
1178+
select p.* from folks as p, ancestor_ids as a where p.id = a.id;
1179+
1180+
select * from my_ancestors;
1181+
1182+
delete from my_ancestors;
1183+
1184+
insert into my_ancestors
1185+
with recursive
1186+
ancestor_ids (id)
1187+
as
1188+
(
1189+
select father from folks where name = 'Me'
1190+
union
1191+
select mother from folks where name = 'Me'
1192+
union
1193+
select father from folks, ancestor_ids a where folks.id = a.id
1194+
union
1195+
select mother from folks, ancestor_ids a where folks.id = a.id
1196+
)
1197+
select p.* from folks as p, ancestor_ids as a where p.id = a.id;
1198+
1199+
select * from my_ancestors;
1200+
1201+
drop table my_ancestors;
1202+
11651203
drop table folks;
11661204

11671205
--echo #

sql/sql_parse.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,7 +3781,8 @@ mysql_execute_command(THD *thd)
37813781
/* Copy temporarily the statement flags to thd for lock_table_names() */
37823782
uint save_thd_create_info_options= thd->lex->create_info.options;
37833783
thd->lex->create_info.options|= create_info.options;
3784-
res= open_and_lock_tables(thd, create_info, lex->query_tables, TRUE, 0);
3784+
if (!(res= check_dependencies_in_with_clauses(lex->with_clauses_list)))
3785+
res= open_and_lock_tables(thd, create_info, lex->query_tables, TRUE, 0);
37853786
thd->lex->create_info.options= save_thd_create_info_options;
37863787
if (res)
37873788
{
@@ -4394,7 +4395,8 @@ mysql_execute_command(THD *thd)
43944395

43954396
unit->set_limit(select_lex);
43964397

4397-
if (!(res= open_and_lock_tables(thd, all_tables, TRUE, 0)))
4398+
if (!(res= check_dependencies_in_with_clauses(lex->with_clauses_list)) &&
4399+
!(res=open_and_lock_tables(thd, all_tables, TRUE, 0)))
43984400
{
43994401
MYSQL_INSERT_SELECT_START(thd->query());
44004402
/*

sql/sql_yacc.yy

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,16 +4810,22 @@ create_like:
48104810

48114811
opt_create_select:
48124812
/* empty */ {}
4813-
| opt_duplicate opt_as create_select_query_expression_body
4813+
| opt_duplicate opt_as create_select_query_expression
48144814
;
48154815

4816-
create_select_query_expression_body:
4817-
SELECT_SYM create_select_part2 opt_table_expression
4816+
create_select_query_expression:
4817+
opt_with_clause SELECT_SYM create_select_part2 opt_table_expression
48184818
create_select_part4
4819-
{ Select->set_braces(0);}
4819+
{
4820+
Select->set_braces(0);
4821+
Select->set_with_clause($1);
4822+
}
48204823
union_clause
4821-
| SELECT_SYM create_select_part2 create_select_part3_union_not_ready
4822-
create_select_part4
4824+
| opt_with_clause SELECT_SYM create_select_part2
4825+
create_select_part3_union_not_ready create_select_part4
4826+
{
4827+
Select->set_with_clause($1);
4828+
}
48234829
| '(' create_select_query_specification ')'
48244830
| '(' create_select_query_specification ')'
48254831
{ Select->set_braces(1);} union_list {}
@@ -5519,7 +5525,11 @@ opt_part_option:
55195525
*/
55205526

55215527
create_select_query_specification:
5522-
SELECT_SYM create_select_part2 create_select_part3 create_select_part4
5528+
SELECT_SYM opt_with_clause create_select_part2 create_select_part3
5529+
create_select_part4
5530+
{
5531+
Select->set_with_clause($2);
5532+
}
55235533
;
55245534

55255535
create_select_part2:
@@ -12308,7 +12318,7 @@ fields:
1230812318
insert_values:
1230912319
VALUES values_list {}
1231012320
| VALUE_SYM values_list {}
12311-
| create_select_query_expression_body {}
12321+
| create_select_query_expression {}
1231212322
;
1231312323

1231412324
values_list:

0 commit comments

Comments
 (0)