You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
0 commit comments