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
2 DERIVED <derived3> ALL NULL NULL NULL NULL 12 100.00 Using where
695
694
3 DERIVED folks ALL NULL NULL NULL NULL 12 100.00 Using where
696
695
4 RECURSIVE UNION <derived2> ALL NULL NULL NULL NULL 2 100.00
697
696
4 RECURSIVE UNION p ALL NULL NULL NULL NULL 12 100.00 Using where; Using join buffer (flat, BNL join)
698
697
5 RECURSIVE UNION <derived2> ALL NULL NULL NULL NULL 2 100.00
699
698
5 RECURSIVE UNION p ALL NULL NULL NULL NULL 12 100.00 Using where; Using join buffer (flat, BNL join)
700
699
NULL UNION RESULT <union3,4,5> ALL NULL NULL NULL NULL NULL NULL
700
+
2 DERIVED <derived3> ALL NULL NULL NULL NULL 12 100.00 Using where
701
701
Warnings:
702
702
Note 1003 with recursive ancestor_couple_ids(h_id,w_id) as (/* select#2 */ 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(id,name,dob,father,mother) as (/* select#3 */ 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#4 */ 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#5 */ 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#1 */ 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`
703
703
# simple mutual recursion
@@ -1302,12 +1302,12 @@ id select_type table type possible_keys key key_len ref rows Extra
1302
1302
1 PRIMARY <derived4> ALL NULL NULL NULL NULL 24
1303
1303
4 DERIVED folks ALL NULL NULL NULL NULL 12 Using where
1304
1304
6 RECURSIVE UNION <derived3> ALL NULL NULL NULL NULL 12
1305
+
5 RECURSIVE UNION <derived4> ALL NULL NULL NULL NULL 24
1306
+
NULL UNION RESULT <union4,6,5> ALL NULL NULL NULL NULL NULL
1305
1307
3 DERIVED folks ALL NULL NULL NULL NULL 12 Using where
1306
1308
2 RECURSIVE UNION folks ALL PRIMARY NULL NULL NULL 12
1307
1309
2 RECURSIVE UNION <derived3> ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
1308
1310
NULL UNION RESULT <union3,2> ALL NULL NULL NULL NULL NULL
1309
-
5 RECURSIVE UNION <derived4> ALL NULL NULL NULL NULL 24
1310
-
NULL UNION RESULT <union4,6,5> ALL NULL NULL NULL NULL NULL
1311
1311
explain FORMAT=JSON
1312
1312
with recursive
1313
1313
prev_gen
@@ -3370,13 +3370,13 @@ select * from cte1, cte2;
3370
3370
id select_type table type possible_keys key key_len ref rows filtered Extra
3371
3371
1 PRIMARY <derived4> ALL NULL NULL NULL NULL 6 100.00
3372
3372
1 PRIMARY <derived5> ALL NULL NULL NULL NULL 6 100.00 Using join buffer (flat, BNL join)
3373
+
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
3374
+
3 RECURSIVE UNION <derived2> ALL NULL NULL NULL NULL 2 100.00 Using where
3375
+
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
3373
3376
4 DERIVED <derived2> ALL NULL NULL NULL NULL 2 100.00 Using where
3374
3377
4 DERIVED t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
3375
3378
5 DERIVED <derived2> ALL NULL NULL NULL NULL 2 100.00 Using where
3376
3379
5 DERIVED t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
3377
-
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
3378
-
3 RECURSIVE UNION <derived2> ALL NULL NULL NULL NULL 2 100.00 Using where
3379
-
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
3380
3380
Warnings:
3381
3381
Note 1003 with recursive rcte(a) as (/* select#2 */ select 1 AS `a` union /* select#3 */ select cast(`rcte`.`a` + 1 as unsigned) AS `cast(a+1 as unsigned)` from `rcte` where `rcte`.`a` < 10), cte1 as (/* select#4 */ select count(0) AS `c1` from `rcte` join `test`.`t1` where `rcte`.`a` between 3 and 5 and `test`.`t1`.`id` = `rcte`.`a` - 3), cte2 as (/* select#5 */ select count(0) AS `c2` from `rcte` join `test`.`t1` where `rcte`.`a` between 7 and 8 and `test`.`t1`.`id` = `rcte`.`a` - 7)/* select#1 */ select `cte1`.`c1` AS `c1`,`cte2`.`c2` AS `c2` from `cte1` join `cte2`
3382
3382
prepare stmt from "with recursive
@@ -3435,6 +3435,65 @@ cte2 as
3435
3435
(select count(*) as c2 from rcte,t1 where a between 7 and 8 and id=a-7)
3436
3436
select * from cte1, cte2 where cte1.c1 = 3;
3437
3437
c1 c2
3438
+
with recursive
3439
+
rcte(a) as
3440
+
(select 1 union select cast(a+1 as unsigned) from rcte where a < 10),
3441
+
cte1 as
3442
+
(select count(*) as c1 from rcte,t1 where a between 3 and 5 and id=a-3),
3443
+
cte2 as
3444
+
(select count(*) as c2 from rcte,t1 where a between 7 and 8 and id=a-7)
3445
+
select * from cte2, cte1;
3446
+
c2 c1
3447
+
1 2
3448
+
explain extended with recursive
3449
+
rcte(a) as
3450
+
(select 1 union select cast(a+1 as unsigned) from rcte where a < 10),
3451
+
cte1 as
3452
+
(select count(*) as c1 from rcte,t1 where a between 3 and 5 and id=a-3),
3453
+
cte2 as
3454
+
(select count(*) as c2 from rcte,t1 where a between 7 and 8 and id=a-7)
3455
+
select * from cte2, cte1;
3456
+
id select_type table type possible_keys key key_len ref rows filtered Extra
3457
+
1 PRIMARY <derived5> ALL NULL NULL NULL NULL 6 100.00
3458
+
1 PRIMARY <derived4> ALL NULL NULL NULL NULL 6 100.00 Using join buffer (flat, BNL join)
3459
+
2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
3460
+
3 RECURSIVE UNION <derived2> ALL NULL NULL NULL NULL 2 100.00 Using where
3461
+
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
3462
+
5 DERIVED <derived2> ALL NULL NULL NULL NULL 2 100.00 Using where
3463
+
5 DERIVED t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
3464
+
4 DERIVED <derived2> ALL NULL NULL NULL NULL 2 100.00 Using where
3465
+
4 DERIVED t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
3466
+
Warnings:
3467
+
Note 1003 with recursive rcte(a) as (/* select#2 */ select 1 AS `a` union /* select#3 */ select cast(`rcte`.`a` + 1 as unsigned) AS `cast(a+1 as unsigned)` from `rcte` where `rcte`.`a` < 10), cte1 as (/* select#4 */ select count(0) AS `c1` from `rcte` join `test`.`t1` where `rcte`.`a` between 3 and 5 and `test`.`t1`.`id` = `rcte`.`a` - 3), cte2 as (/* select#5 */ select count(0) AS `c2` from `rcte` join `test`.`t1` where `rcte`.`a` between 7 and 8 and `test`.`t1`.`id` = `rcte`.`a` - 7)/* select#1 */ select `cte2`.`c2` AS `c2`,`cte1`.`c1` AS `c1` from `cte2` join `cte1`
3468
+
prepare stmt from "with recursive
3469
+
rcte(a) as
3470
+
(select 1 union select cast(a+1 as unsigned) from rcte where a < 10),
3471
+
cte1 as
3472
+
(select count(*) as c1 from rcte,t1 where a between 3 and 5 and id=a-3),
3473
+
cte2 as
3474
+
(select count(*) as c2 from rcte,t1 where a between 7 and 8 and id=a-7)
3475
+
select * from cte2, cte1";
3476
+
execute stmt;
3477
+
c2 c1
3478
+
1 2
3479
+
execute stmt;
3480
+
c2 c1
3481
+
1 2
3482
+
drop procedure p;
3483
+
drop table t2;
3484
+
create table t2 (c1 int, c2 int);
3485
+
create procedure p() insert into t2 with recursive
3486
+
rcte(a) as
3487
+
(select 1 union select cast(a+1 as unsigned) from rcte where a < 10),
3488
+
cte1 as
3489
+
(select count(*) as c1 from rcte,t1 where a between 3 and 5 and id=a-3),
3490
+
cte2 as
3491
+
(select count(*) as c2 from rcte,t1 where a between 7 and 8 and id=a-7)
3492
+
select * from cte2, cte1;
3493
+
call p();
3494
+
select * from t2;
3495
+
c1 c2
3496
+
1 2
3438
3497
drop procedure p;
3439
3498
drop table t1,t2;
3440
3499
#
@@ -3853,7 +3912,104 @@ ANALYZE
3853
3912
}
3854
3913
drop function f1;
3855
3914
drop table t1,t2;
3856
-
End of 10.2 tests
3915
+
#
3916
+
# MDEV-22748: two materialized CTEs using the same recursive CTE
INSERT INTO t1 VALUES (2018,'2018-01-01','2018-09-20');
3921
+
CREATE TABLE t2 (id int, tm date);
3922
+
INSERT INTO t2 VALUES (1,'2018-08-30'),(2,'2018-08-30'),(3,'2018-08-30');
3923
+
CREATE TABLE t3 (id int, tm date);
3924
+
INSERT INTO t3 VALUES (1,'2018-08-30'),(2,'2018-08-30');
3925
+
WITH RECURSIVE
3926
+
cte AS
3927
+
(SELECT YEAR(t1.d1) AS YEAR, t1.d1 AS st, t1.d1 + INTERVAL 1 MONTH AS fn
3928
+
FROM t1
3929
+
UNION ALL
3930
+
SELECT YEAR(cte.st + INTERVAL 1 MONTH),
3931
+
cte.st + INTERVAL 1 MONTH, t1.d2 + INTERVAL 1 DAY
3932
+
FROM cte JOIN t1
3933
+
WHERE cte.st + INTERVAL 1 MONTH < t1.d2 ),
3934
+
cte2 AS (SELECT YEAR, COUNT(*)
3935
+
FROM cte JOIN t2 ON t2.tm BETWEEN cte.st AND cte.fn),
3936
+
cte3 AS (SELECT YEAR, COUNT(*)
3937
+
FROM cte JOIN t3 ON t3.tm BETWEEN cte.st AND cte.fn)
3938
+
SELECT t1.* FROM t1 JOIN cte2 USING (YEAR) JOIN cte3 USING (YEAR);
3939
+
YEAR d1 d2
3940
+
2018 2018-01-01 2018-09-20
3941
+
EXPLAIN EXTENDED WITH RECURSIVE
3942
+
cte AS
3943
+
(SELECT YEAR(t1.d1) AS YEAR, t1.d1 AS st, t1.d1 + INTERVAL 1 MONTH AS fn
3944
+
FROM t1
3945
+
UNION ALL
3946
+
SELECT YEAR(cte.st + INTERVAL 1 MONTH),
3947
+
cte.st + INTERVAL 1 MONTH, t1.d2 + INTERVAL 1 DAY
3948
+
FROM cte JOIN t1
3949
+
WHERE cte.st + INTERVAL 1 MONTH < t1.d2 ),
3950
+
cte2 AS (SELECT YEAR, COUNT(*)
3951
+
FROM cte JOIN t2 ON t2.tm BETWEEN cte.st AND cte.fn),
3952
+
cte3 AS (SELECT YEAR, COUNT(*)
3953
+
FROM cte JOIN t3 ON t3.tm BETWEEN cte.st AND cte.fn)
3954
+
SELECT t1.* FROM t1 JOIN cte2 USING (YEAR) JOIN cte3 USING (YEAR);
3955
+
id select_type table type possible_keys key key_len ref rows filtered Extra
3956
+
1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
3957
+
1 PRIMARY <derived5> ref key0 key0 5 const 0 0.00
3958
+
1 PRIMARY <derived4> ref key0 key0 5 const 0 0.00
3959
+
2 DERIVED t1 system NULL NULL NULL NULL 1 100.00
3960
+
3 RECURSIVE UNION t1 system NULL NULL NULL NULL 1 100.00
3961
+
3 RECURSIVE UNION <derived2> ALL NULL NULL NULL NULL 2 100.00 Using where
3962
+
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
3963
+
4 DERIVED <derived2> ALL NULL NULL NULL NULL 2 100.00
3964
+
4 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
3965
+
5 DERIVED <derived2> ALL NULL NULL NULL NULL 2 100.00
3966
+
5 DERIVED t3 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
3967
+
Warnings:
3968
+
Note 1003 with recursive cte as (/* select#2 */ select year(`test`.`t1`.`d1`) AS `YEAR`,`test`.`t1`.`d1` AS `st`,`test`.`t1`.`d1` + interval 1 month AS `fn` from `test`.`t1` union all /* select#3 */ select year(`cte`.`st` + interval 1 month) AS `YEAR(cte.st + INTERVAL 1 MONTH)`,`cte`.`st` + interval 1 month AS `cte.st + INTERVAL 1 MONTH`,`test`.`t1`.`d2` + interval 1 day AS `t1.d2 + INTERVAL 1 DAY` from `cte` join `test`.`t1` where `cte`.`st` + interval 1 month < `test`.`t1`.`d2`), cte2 as (/* select#4 */ select `cte`.`YEAR` AS `YEAR`,count(0) AS `COUNT(*)` from `cte` join `test`.`t2` where `test`.`t2`.`tm` between `cte`.`st` and `cte`.`fn`), cte3 as (/* select#5 */ select `cte`.`YEAR` AS `YEAR`,count(0) AS `COUNT(*)` from `cte` join `test`.`t3` where `test`.`t3`.`tm` between `cte`.`st` and `cte`.`fn`)/* select#1 */ select 2018 AS `YEAR`,'2018-01-01' AS `d1`,'2018-09-20' AS `d2` from `cte2` join `cte3` where `cte3`.`YEAR` = 2018 and `cte2`.`YEAR` = 2018
3969
+
PREPARE stmt FROM "WITH RECURSIVE
3970
+
cte AS
3971
+
(SELECT YEAR(t1.d1) AS YEAR, t1.d1 AS st, t1.d1 + INTERVAL 1 MONTH AS fn
3972
+
FROM t1
3973
+
UNION ALL
3974
+
SELECT YEAR(cte.st + INTERVAL 1 MONTH),
3975
+
cte.st + INTERVAL 1 MONTH, t1.d2 + INTERVAL 1 DAY
3976
+
FROM cte JOIN t1
3977
+
WHERE cte.st + INTERVAL 1 MONTH < t1.d2 ),
3978
+
cte2 AS (SELECT YEAR, COUNT(*)
3979
+
FROM cte JOIN t2 ON t2.tm BETWEEN cte.st AND cte.fn),
3980
+
cte3 AS (SELECT YEAR, COUNT(*)
3981
+
FROM cte JOIN t3 ON t3.tm BETWEEN cte.st AND cte.fn)
3982
+
SELECT t1.* FROM t1 JOIN cte2 USING (YEAR) JOIN cte3 USING (YEAR)";
3983
+
EXECUTE stmt;
3984
+
YEAR d1 d2
3985
+
2018 2018-01-01 2018-09-20
3986
+
EXECUTE stmt;
3987
+
YEAR d1 d2
3988
+
2018 2018-01-01 2018-09-20
3989
+
CREATE TABLE t4 (YEAR int(4), d1 date , d2 date);
3990
+
CREATE PROCEDURE p() INSERT INTO t4 WITH RECURSIVE
3991
+
cte AS
3992
+
(SELECT YEAR(t1.d1) AS YEAR, t1.d1 AS st, t1.d1 + INTERVAL 1 MONTH AS fn
3993
+
FROM t1
3994
+
UNION ALL
3995
+
SELECT YEAR(cte.st + INTERVAL 1 MONTH),
3996
+
cte.st + INTERVAL 1 MONTH, t1.d2 + INTERVAL 1 DAY
3997
+
FROM cte JOIN t1
3998
+
WHERE cte.st + INTERVAL 1 MONTH < t1.d2 ),
3999
+
cte2 AS (SELECT YEAR, COUNT(*)
4000
+
FROM cte JOIN t2 ON t2.tm BETWEEN cte.st AND cte.fn),
4001
+
cte3 AS (SELECT YEAR, COUNT(*)
4002
+
FROM cte JOIN t3 ON t3.tm BETWEEN cte.st AND cte.fn)
4003
+
SELECT t1.* FROM t1 JOIN cte2 USING (YEAR) JOIN cte3 USING (YEAR);
4004
+
CALL p();
4005
+
SELECT * FROM t4;
4006
+
YEAR d1 d2
4007
+
2018 2018-01-01 2018-09-20
4008
+
DROP PROCEDURE p;
4009
+
DROP TABLE t1,t2,t3,t4;
4010
+
#
4011
+
# End of 10.2 tests
4012
+
#
3857
4013
#
3858
4014
# MDEV-14217 [db crash] Recursive CTE when SELECT includes new field
0 commit comments