Skip to content

Commit 9ac235a

Browse files
committed
mdev-9864: cleanup, re-factoring.
Added comments. Added reaction for exceeding maximum number of elements in with clause. Added a test case to check this reaction. Added a test case where the specification of a recursive table uses two non-recursive with tables.
1 parent c8f85bf commit 9ac235a

File tree

10 files changed

+341
-79
lines changed

10 files changed

+341
-79
lines changed

mysql-test/r/cte_nonrecursive.result

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,9 @@ select t1.b from t2,t1 where t1.a = t2.c;
797797
id select_type table type possible_keys key key_len ref rows Extra
798798
1 PRIMARY t2 ALL NULL NULL NULL NULL 4
799799
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join)
800+
# too many with elements in with clause
801+
with s65 as (select * from t1), s64 as (select * from t1) , s63 as (select * from t1) , s62 as (select * from t1) , s61 as (select * from t1) , s60 as (select * from t1) , s59 as (select * from t1) , s58 as (select * from t1) , s57 as (select * from t1) , s56 as (select * from t1) , s55 as (select * from t1) , s54 as (select * from t1) , s53 as (select * from t1) , s52 as (select * from t1) , s51 as (select * from t1) , s50 as (select * from t1) , s49 as (select * from t1) , s48 as (select * from t1) , s47 as (select * from t1) , s46 as (select * from t1) , s45 as (select * from t1) , s44 as (select * from t1) , s43 as (select * from t1) , s42 as (select * from t1) , s41 as (select * from t1) , s40 as (select * from t1) , s39 as (select * from t1) , s38 as (select * from t1) , s37 as (select * from t1) , s36 as (select * from t1) , s35 as (select * from t1) , s34 as (select * from t1) , s33 as (select * from t1) , s32 as (select * from t1) , s31 as (select * from t1) , s30 as (select * from t1) , s29 as (select * from t1) , s28 as (select * from t1) , s27 as (select * from t1) , s26 as (select * from t1) , s25 as (select * from t1) , s24 as (select * from t1) , s23 as (select * from t1) , s22 as (select * from t1) , s21 as (select * from t1) , s20 as (select * from t1) , s19 as (select * from t1) , s18 as (select * from t1) , s17 as (select * from t1) , s16 as (select * from t1) , s15 as (select * from t1) , s14 as (select * from t1) , s13 as (select * from t1) , s12 as (select * from t1) , s11 as (select * from t1) , s10 as (select * from t1) , s9 as (select * from t1) , s8 as (select * from t1) , s7 as (select * from t1) , s6 as (select * from t1) , s5 as (select * from t1) , s4 as (select * from t1) , s3 as (select * from t1) , s2 as (select * from t1) , s1 as (select * from t1) select * from s65;
802+
ERROR HY000: Too many WITH elements in WITH clause
800803
drop table t1,t2;
801804
#
802805
# Bug mdev-9937: View used in the specification of with table

mysql-test/r/cte_recursive.result

Lines changed: 99 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ insert into folks values
160160
(6, 'Grandgrandma Martha', '1923-05-17', null, null),
161161
(67, 'Cousin Eddie', '1992-02-28', 25, 27),
162162
(27, 'Auntie Melinda', '1971-03-29', null, null);
163+
# simple recursion with one anchor and one recursive select
164+
# the anchor is the first select in the specification
163165
with recursive
164166
ancestors
165167
as
@@ -182,6 +184,8 @@ id name dob father mother
182184
7 Grandma Sally 1943-08-23 NULL 6
183185
8 Grandpa Ben 1940-10-21 NULL NULL
184186
6 Grandgrandma Martha 1923-05-17 NULL NULL
187+
# simple recursion with one anchor and one recursive select
188+
# the anchor is the last select in the specification
185189
with recursive
186190
ancestors
187191
as
@@ -204,6 +208,8 @@ id name dob father mother
204208
7 Grandma Sally 1943-08-23 NULL 6
205209
8 Grandpa Ben 1940-10-21 NULL NULL
206210
6 Grandgrandma Martha 1923-05-17 NULL NULL
211+
# simple recursion with one anchor and one recursive select
212+
# the anchor is the first select in the specification
207213
with recursive
208214
ancestors
209215
as
@@ -224,29 +230,7 @@ id name dob father mother
224230
7 Grandma Sally 1943-08-23 NULL 6
225231
8 Grandpa Ben 1940-10-21 NULL NULL
226232
6 Grandgrandma Martha 1923-05-17 NULL NULL
227-
with recursive
228-
ancestors
229-
as
230-
(
231-
select *
232-
from folks
233-
where name = 'Me' or name='Sister Amy'
234-
union
235-
select p.*
236-
from folks as p, ancestors as a
237-
where p.id = a.father or p.id = a.mother
238-
)
239-
select * from ancestors;
240-
id name dob father mother
241-
100 Me 2000-01-01 20 30
242-
98 Sister Amy 2001-06-20 20 30
243-
20 Dad 1970-02-02 10 9
244-
30 Mom 1975-03-03 8 7
245-
10 Grandpa Bill 1940-04-05 NULL NULL
246-
9 Grandma Ann 1941-10-15 NULL NULL
247-
7 Grandma Sally 1943-08-23 NULL 6
248-
8 Grandpa Ben 1940-10-21 NULL NULL
249-
6 Grandgrandma Martha 1923-05-17 NULL NULL
233+
# two recursive definition, one uses another
250234
with recursive
251235
prev_gen
252236
as
@@ -282,6 +266,50 @@ Grandma Ann 1941-10-15
282266
Grandma Sally 1943-08-23
283267
Grandpa Ben 1940-10-21
284268
Grandgrandma Martha 1923-05-17
269+
# recursive definition with two attached non-recursive
270+
with recursive
271+
ancestors(id,name,dob)
272+
as
273+
(
274+
with
275+
father(child_id,id,name,dob)
276+
as
277+
(
278+
select folks.id, f.id, f.name, f.dob
279+
from folks, folks f
280+
where folks.father=f.id
281+
),
282+
mother(child_id,id,name,dob)
283+
as
284+
(
285+
select folks.id, m.id, m.name, m.dob
286+
from folks, folks m
287+
where folks.mother=m.id
288+
)
289+
select folks.id, folks.name, folks.dob
290+
from folks
291+
where name='Me'
292+
union
293+
select f.id, f.name, f.dob
294+
from ancestors a, father f
295+
where f.child_id=a.id
296+
union
297+
select m.id, m.name, m.dob
298+
from ancestors a, mother m
299+
where m.child_id=a.id
300+
)
301+
select ancestors.name, ancestors.dob from ancestors;
302+
name dob
303+
Me 2000-01-01
304+
Dad 1970-02-02
305+
Mom 1975-03-03
306+
Grandpa Bill 1940-04-05
307+
Grandpa Ben 1940-10-21
308+
Grandma Ann 1941-10-15
309+
Grandma Sally 1943-08-23
310+
Grandgrandma Martha 1923-05-17
311+
# simple recursion with one anchor and one recursive select
312+
# the anchor is the first select in the specification
285313
with recursive
286314
descendants
287315
as
@@ -300,6 +328,8 @@ id name dob father mother
300328
20 Dad 1970-02-02 10 9
301329
100 Me 2000-01-01 20 30
302330
98 Sister Amy 2001-06-20 20 30
331+
# simple recursion with one anchor and one recursive select
332+
# the anchor is the first select in the specification
303333
with recursive
304334
descendants
305335
as
@@ -320,6 +350,7 @@ id name dob father mother
320350
100 Me 2000-01-01 20 30
321351
98 Sister Amy 2001-06-20 20 30
322352
67 Cousin Eddie 1992-02-28 25 27
353+
# simple recursive table used three times in the main query
323354
with recursive
324355
ancestors
325356
as
@@ -340,6 +371,7 @@ id name dob father mother id name dob father mother
340371
20 Dad 1970-02-02 10 9 30 Mom 1975-03-03 8 7
341372
10 Grandpa Bill 1940-04-05 NULL NULL 9 Grandma Ann 1941-10-15 NULL NULL
342373
8 Grandpa Ben 1940-10-21 NULL NULL 7 Grandma Sally 1943-08-23 NULL 6
374+
# simple recursive table used three times in the main query
343375
with
344376
ancestor_couples(husband, h_dob, wife, w_dob)
345377
as
@@ -366,6 +398,7 @@ husband h_dob wife w_dob
366398
Dad 1970-02-02 Mom 1975-03-03
367399
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
368400
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
401+
# simple recursion with two selects in recursive part
369402
with recursive
370403
ancestors
371404
as
@@ -392,6 +425,7 @@ id name dob father mother
392425
9 Grandma Ann 1941-10-15 NULL NULL
393426
7 Grandma Sally 1943-08-23 NULL 6
394427
6 Grandgrandma Martha 1923-05-17 NULL NULL
428+
# mutual recursion with renaming
395429
with recursive
396430
ancestor_couples(h_id, h_name, h_dob, h_father, h_mother,
397431
w_id, w_name, w_dob, w_father, w_mother)
@@ -421,6 +455,7 @@ h_name h_dob w_name w_dob
421455
Dad 1970-02-02 Mom 1975-03-03
422456
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
423457
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
458+
# mutual recursion with union all
424459
with recursive
425460
ancestor_couples(h_id, h_name, h_dob, h_father, h_mother,
426461
w_id, w_name, w_dob, w_father, w_mother)
@@ -450,6 +485,37 @@ h_name h_dob w_name w_dob
450485
Dad 1970-02-02 Mom 1975-03-03
451486
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
452487
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
488+
# mutual recursion with renaming
489+
with recursive
490+
ancestor_couples(h_id, h_name, h_dob, h_father, h_mother,
491+
w_id, w_name, w_dob, w_father, w_mother)
492+
as
493+
(
494+
select h.*, w.*
495+
from folks h, folks w, coupled_ancestors a
496+
where a.father = h.id AND a.mother = w.id
497+
union
498+
select h.*, w.*
499+
from folks v, folks h, folks w
500+
where v.name = 'Me' and
501+
(v.father = h.id AND v.mother= w.id)
502+
),
503+
coupled_ancestors (id, name, dob, father, mother)
504+
as
505+
(
506+
select h_id, h_name, h_dob, h_father, h_mother
507+
from ancestor_couples
508+
union
509+
select w_id, w_name, w_dob, w_father, w_mother
510+
from ancestor_couples
511+
)
512+
select h_name, h_dob, w_name, w_dob
513+
from ancestor_couples;
514+
h_name h_dob w_name w_dob
515+
Dad 1970-02-02 Mom 1975-03-03
516+
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
517+
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
518+
# mutual recursion with union all
453519
with recursive
454520
ancestor_couples(h_id, h_name, h_dob, h_father, h_mother,
455521
w_id, w_name, w_dob, w_father, w_mother)
@@ -478,6 +544,7 @@ h_name h_dob w_name w_dob
478544
Dad 1970-02-02 Mom 1975-03-03
479545
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
480546
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
547+
# mutual recursion with one select in the first definition
481548
with recursive
482549
ancestor_couple_ids(h_id, w_id)
483550
as
@@ -507,6 +574,7 @@ h_id w_id
507574
20 30
508575
10 9
509576
8 7
577+
# join of a mutually recursive table with base tables
510578
with recursive
511579
ancestor_couple_ids(h_id, w_id)
512580
as
@@ -537,6 +605,7 @@ name dob name dob
537605
Dad 1970-02-02 Mom 1975-03-03
538606
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
539607
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
608+
# join of two mutually recursive tables
540609
with recursive
541610
ancestor_couple_ids(h_id, w_id)
542611
as
@@ -607,6 +676,7 @@ NULL UNION RESULT <union3,4,5> ALL NULL NULL NULL NULL NULL NULL
607676
2 UNCACHEABLE SUBQUERY <derived3> ALL NULL NULL NULL NULL 12 100.00 Using where
608677
Warnings:
609678
Note 1003 with recursive ancestor_couple_ids as (select `a`.`father` AS `h_id`,`a`.`mother` AS `w_id` from `coupled_ancestors` `a` where ((`a`.`father` is not null) and (`a`.`mother` is not null)))coupled_ancestors as (select `test`.`folks`.`id` AS `id`,`test`.`folks`.`name` AS `name`,`test`.`folks`.`dob` AS `dob`,`test`.`folks`.`father` AS `father`,`test`.`folks`.`mother` AS `mother` from `test`.`folks` where (`test`.`folks`.`name` = 'Me') union all select `test`.`p`.`id` AS `id`,`test`.`p`.`name` AS `name`,`test`.`p`.`dob` AS `dob`,`test`.`p`.`father` AS `father`,`test`.`p`.`mother` AS `mother` from `test`.`folks` `p` join `ancestor_couple_ids` `fa` where (`test`.`p`.`id` = `fa`.`h_id`) union all select `test`.`p`.`id` AS `id`,`test`.`p`.`name` AS `name`,`test`.`p`.`dob` AS `dob`,`test`.`p`.`father` AS `father`,`test`.`p`.`mother` AS `mother` from `test`.`folks` `p` join `ancestor_couple_ids` `ma` where (`test`.`p`.`id` = `ma`.`w_id`)), select `h`.`name` AS `name`,`h`.`dob` AS `dob`,`w`.`name` AS `name`,`w`.`dob` AS `dob` from `ancestor_couple_ids` `c` join `coupled_ancestors` `h` join `coupled_ancestors` `w` where ((`h`.`id` = `c`.`h_id`) and (`w`.`id` = `c`.`w_id`))
679+
# simple mutual recursion
610680
with recursive
611681
ancestor_couple_ids(h_id, w_id)
612682
as
@@ -640,6 +710,7 @@ NULL NULL
640710
NULL NULL
641711
NULL 6
642712
NULL NULL
713+
# join of two mutually recursive tables
643714
with recursive
644715
ancestor_couple_ids(h_id, w_id)
645716
as
@@ -669,6 +740,7 @@ name dob name dob
669740
Dad 1970-02-02 Mom 1975-03-03
670741
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
671742
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
743+
# execution of prepared query using a recursive table
672744
prepare stmt1 from "
673745
with recursive
674746
ancestors
@@ -705,6 +777,7 @@ id name dob father mother
705777
8 Grandpa Ben 1940-10-21 NULL NULL
706778
6 Grandgrandma Martha 1923-05-17 NULL NULL
707779
deallocate prepare stmt1;
780+
# view using a recursive table
708781
create view v1 as
709782
with recursive
710783
ancestors
@@ -786,6 +859,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
786859
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
787860
Warnings:
788861
Note 1003 with recursive ancestors as (select `test`.`folks`.`id` AS `id`,`test`.`folks`.`name` AS `name`,`test`.`folks`.`dob` AS `dob`,`test`.`folks`.`father` AS `father`,`test`.`folks`.`mother` AS `mother` from `test`.`folks` where ((`test`.`folks`.`name` = 'Me') and (`test`.`folks`.`dob` = DATE'2000-01-01')) union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from `test`.`folks` `p` join `ancestors` `a` where ((`a`.`father` = `p`.`id`) or (`a`.`mother` = `p`.`id`)))select `ancestors`.`id` AS `id`,`ancestors`.`name` AS `name`,`ancestors`.`dob` AS `dob`,`ancestors`.`father` AS `father`,`ancestors`.`mother` AS `mother` from `ancestors`
862+
# recursive spec with two anchor selects and two recursive ones
789863
with recursive
790864
ancestor_ids (id)
791865
as
@@ -813,6 +887,7 @@ id name dob father mother
813887
7 Grandma Sally 1943-08-23 NULL 6
814888
8 Grandpa Ben 1940-10-21 NULL NULL
815889
6 Grandgrandma Martha 1923-05-17 NULL NULL
890+
# recursive spec using union all
816891
with recursive
817892
ancestors
818893
as
@@ -1115,6 +1190,7 @@ generation name
11151190
2 Grandma Ann
11161191
2 Grandma Sally
11171192
2 Grandpa Ben
1193+
# query with recursive tables using key access
11181194
alter table folks add primary key (id);
11191195
explain
11201196
with recursive

mysql-test/t/cte_nonrecursive.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,20 @@ explain
469469
with t as (select a, count(*) from t1 where b >= 'c' group by a)
470470
select t1.b from t2,t1 where t1.a = t2.c;
471471

472+
--echo # too many with elements in with clause
473+
let $m= 65;
474+
let $i= $m;
475+
dec $i;
476+
let $q= with s$m as (select * from t1);
477+
while ($i)
478+
{
479+
let $q= $q, s$i as (select * from t1) ;
480+
dec $i;
481+
}
482+
let $q= $q select * from s$m;
483+
--ERROR ER_TOO_MANY_DEFINITIONS_IN_WITH_CLAUSE
484+
eval $q;
485+
472486
drop table t1,t2;
473487

474488
--echo #

0 commit comments

Comments
 (0)