Skip to content

Commit 0a6e6d7

Browse files
committed
Fixed numerous problems for mutually recursive CTE.
Actually mutually recursive CTE were not functional. Now the code for mutually recursive CTE looks like functional, but still needs re-writing. Added many new test cases for mutually recursive CTE.
1 parent 6c6c3af commit 0a6e6d7

File tree

8 files changed

+653
-32
lines changed

8 files changed

+653
-32
lines changed

mysql-test/r/cte_recursive.result

Lines changed: 250 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ id name dob father mother
372372
100 Me 2000-01-01 20 30
373373
20 Dad 1970-02-02 10 9
374374
30 Mom 1975-03-03 8 7
375-
9 Grandma Ann 1941-10-15 NULL NULL
376375
10 Grandpa Bill 1940-04-05 NULL NULL
377376
8 Grandpa Ben 1940-10-21 NULL NULL
377+
9 Grandma Ann 1941-10-15 NULL NULL
378378
7 Grandma Sally 1943-08-23 NULL 6
379379
6 Grandgrandma Martha 1923-05-17 NULL NULL
380380
with recursive
@@ -406,6 +406,254 @@ h_name h_dob w_name w_dob
406406
Dad 1970-02-02 Mom 1975-03-03
407407
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
408408
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
409+
with recursive
410+
ancestor_couples(h_id, h_name, h_dob, h_father, h_mother,
411+
w_id, w_name, w_dob, w_father, w_mother)
412+
as
413+
(
414+
select h.*, w.*
415+
from folks h, folks w, coupled_ancestors a
416+
where a.father = h.id AND a.mother = w.id
417+
union
418+
select h.*, w.*
419+
from folks v, folks h, folks w
420+
where v.name = 'Me' and
421+
(v.father = h.id AND v.mother= w.id)
422+
),
423+
coupled_ancestors (id, name, dob, father, mother)
424+
as
425+
(
426+
select h_id, h_name, h_dob, h_father, h_mother
427+
from ancestor_couples
428+
union all
429+
select w_id, w_name, w_dob, w_father, w_mother
430+
from ancestor_couples
431+
)
432+
select h_name, h_dob, w_name, w_dob
433+
from ancestor_couples;
434+
h_name h_dob w_name w_dob
435+
Dad 1970-02-02 Mom 1975-03-03
436+
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
437+
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
438+
with recursive
439+
ancestor_couples(h_id, h_name, h_dob, h_father, h_mother,
440+
w_id, w_name, w_dob, w_father, w_mother)
441+
as
442+
(
443+
select h.*, w.*
444+
from folks h, folks w, coupled_ancestors a
445+
where a.father = h.id AND a.mother = w.id
446+
),
447+
coupled_ancestors (id, name, dob, father, mother)
448+
as
449+
(
450+
select *
451+
from folks
452+
where name = 'Me'
453+
union all
454+
select h_id, h_name, h_dob, h_father, h_mother
455+
from ancestor_couples
456+
union all
457+
select w_id, w_name, w_dob, w_father, w_mother
458+
from ancestor_couples
459+
)
460+
select h_name, h_dob, w_name, w_dob
461+
from ancestor_couples;
462+
h_name h_dob w_name w_dob
463+
Dad 1970-02-02 Mom 1975-03-03
464+
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
465+
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
466+
with recursive
467+
ancestor_couple_ids(h_id, w_id)
468+
as
469+
(
470+
select a.father, a.mother
471+
from coupled_ancestors a
472+
where a.father is not null and a.mother is not null
473+
),
474+
coupled_ancestors (id, name, dob, father, mother)
475+
as
476+
(
477+
select *
478+
from folks
479+
where name = 'Me'
480+
union all
481+
select p.*
482+
from folks p, ancestor_couple_ids fa
483+
where p.id = fa.h_id
484+
union all
485+
select p.*
486+
from folks p, ancestor_couple_ids ma
487+
where p.id = ma.w_id
488+
)
489+
select *
490+
from ancestor_couple_ids;
491+
h_id w_id
492+
20 30
493+
10 9
494+
8 7
495+
with recursive
496+
ancestor_couple_ids(h_id, w_id)
497+
as
498+
(
499+
select a.father, a.mother
500+
from coupled_ancestors a
501+
where a.father is not null and a.mother is not null
502+
),
503+
coupled_ancestors (id, name, dob, father, mother)
504+
as
505+
(
506+
select *
507+
from folks
508+
where name = 'Me'
509+
union all
510+
select p.*
511+
from folks p, ancestor_couple_ids fa
512+
where p.id = fa.h_id
513+
union all
514+
select p.*
515+
from folks p, ancestor_couple_ids ma
516+
where p.id = ma.w_id
517+
)
518+
select h.name, h.dob, w.name, w.dob
519+
from ancestor_couple_ids c, folks h, folks w
520+
where c.h_id = h.id and c.w_id= w.id;
521+
name dob name dob
522+
Dad 1970-02-02 Mom 1975-03-03
523+
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
524+
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
525+
with recursive
526+
ancestor_couple_ids(h_id, w_id)
527+
as
528+
(
529+
select a.father, a.mother
530+
from coupled_ancestors a
531+
where a.father is not null and a.mother is not null
532+
),
533+
coupled_ancestors (id, name, dob, father, mother)
534+
as
535+
(
536+
select *
537+
from folks
538+
where name = 'Me'
539+
union all
540+
select p.*
541+
from folks p, ancestor_couple_ids fa
542+
where p.id = fa.h_id
543+
union all
544+
select p.*
545+
from folks p, ancestor_couple_ids ma
546+
where p.id = ma.w_id
547+
)
548+
select h.name, h.dob, w.name, w.dob
549+
from ancestor_couple_ids c, coupled_ancestors h, coupled_ancestors w
550+
where c.h_id = h.id and c.w_id= w.id;
551+
name dob name dob
552+
Dad 1970-02-02 Mom 1975-03-03
553+
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
554+
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
555+
explain extended
556+
with recursive
557+
ancestor_couple_ids(h_id, w_id)
558+
as
559+
(
560+
select a.father, a.mother
561+
from coupled_ancestors a
562+
where a.father is not null and a.mother is not null
563+
),
564+
coupled_ancestors (id, name, dob, father, mother)
565+
as
566+
(
567+
select *
568+
from folks
569+
where name = 'Me'
570+
union all
571+
select p.*
572+
from folks p, ancestor_couple_ids fa
573+
where p.id = fa.h_id
574+
union all
575+
select p.*
576+
from folks p, ancestor_couple_ids ma
577+
where p.id = ma.w_id
578+
)
579+
select h.name, h.dob, w.name, w.dob
580+
from ancestor_couple_ids c, coupled_ancestors h, coupled_ancestors w
581+
where c.h_id = h.id and c.w_id= w.id;
582+
id select_type table type possible_keys key key_len ref rows filtered Extra
583+
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 36 100.00
584+
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 468 100.00 Using where; Using join buffer (flat, BNL join)
585+
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 468 100.00 Using where; Using join buffer (incremental, BNL join)
586+
3 SUBQUERY folks ALL NULL NULL NULL NULL 12 100.00 Using where
587+
4 UNCACHEABLE UNION p ALL NULL NULL NULL NULL 12 100.00
588+
4 UNCACHEABLE UNION <derived2> ALL NULL NULL NULL NULL 36 100.00 Using where; Using join buffer (flat, BNL join)
589+
5 UNCACHEABLE UNION <derived2> ALL NULL NULL NULL NULL 2 100.00
590+
5 UNCACHEABLE UNION p ALL NULL NULL NULL NULL 12 100.00 Using where; Using join buffer (flat, BNL join)
591+
NULL UNION RESULT <union3,4,5> ALL NULL NULL NULL NULL NULL NULL
592+
2 UNCACHEABLE SUBQUERY <derived3> ALL NULL NULL NULL NULL 36 100.00 Using where
593+
Warnings:
594+
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 (`fa`.`h_id` = `test`.`p`.`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`))
595+
with recursive
596+
ancestor_couple_ids(h_id, w_id)
597+
as
598+
(
599+
select a.father, a.mother
600+
from coupled_ancestors a
601+
),
602+
coupled_ancestors (id, name, dob, father, mother)
603+
as
604+
(
605+
select *
606+
from folks
607+
where name = 'Me'
608+
union all
609+
select p.*
610+
from folks p, ancestor_couple_ids fa
611+
where p.id = fa.h_id
612+
union all
613+
select p.*
614+
from folks p, ancestor_couple_ids ma
615+
where p.id = ma.w_id
616+
)
617+
select *
618+
from ancestor_couple_ids;
619+
h_id w_id
620+
20 30
621+
10 9
622+
8 7
623+
NULL NULL
624+
NULL NULL
625+
NULL NULL
626+
NULL 6
627+
NULL NULL
628+
with recursive
629+
ancestor_couple_ids(h_id, w_id)
630+
as
631+
(
632+
select a.father, a.mother
633+
from coupled_ancestors a
634+
),
635+
coupled_ancestors (id, name, dob, father, mother)
636+
as
637+
(
638+
select *
639+
from folks
640+
where name = 'Me'
641+
union all
642+
select p.*
643+
from folks p, ancestor_couple_ids fa
644+
where p.id = fa.h_id
645+
union all
646+
select p.*
647+
from folks p, ancestor_couple_ids ma
648+
where p.id = ma.w_id
649+
)
650+
select h.name, h.dob, w.name, w.dob
651+
from ancestor_couple_ids c, coupled_ancestors h, coupled_ancestors w
652+
where c.h_id = h.id and c.w_id= w.id;
653+
name dob name dob
654+
Dad 1970-02-02 Mom 1975-03-03
655+
Grandpa Bill 1940-04-05 Grandma Ann 1941-10-15
656+
Grandpa Ben 1940-10-21 Grandma Sally 1943-08-23
409657
prepare stmt1 from "
410658
with recursive
411659
ancestors
@@ -495,9 +743,9 @@ id name dob father mother
495743
100 Me 2000-01-01 20 30
496744
20 Dad 1970-02-02 10 9
497745
30 Mom 1975-03-03 8 7
498-
9 Grandma Ann 1941-10-15 NULL NULL
499746
10 Grandpa Bill 1940-04-05 NULL NULL
500747
8 Grandpa Ben 1940-10-21 NULL NULL
748+
9 Grandma Ann 1941-10-15 NULL NULL
501749
7 Grandma Sally 1943-08-23 NULL 6
502750
6 Grandgrandma Martha 1923-05-17 NULL NULL
503751
drop view v1,v2;
@@ -571,7 +819,6 @@ id name dob father mother
571819
100 Me 2000-01-01 20 30
572820
20 Dad 1970-02-02 10 9
573821
30 Mom 1975-03-03 8 7
574-
9 Grandma Ann 1941-10-15 NULL NULL
575822
10 Grandpa Bill 1940-04-05 NULL NULL
576823
8 Grandpa Ben 1940-10-21 NULL NULL
577824
9 Grandma Ann 1941-10-15 NULL NULL

0 commit comments

Comments
 (0)