summaryrefslogtreecommitdiff
path: root/mysql-test/main/cte_recursive.test
diff options
context:
space:
mode:
Diffstat (limited to 'mysql-test/main/cte_recursive.test')
-rw-r--r--mysql-test/main/cte_recursive.test121
1 files changed, 120 insertions, 1 deletions
diff --git a/mysql-test/main/cte_recursive.test b/mysql-test/main/cte_recursive.test
index 109f7d73011..2e95296ef57 100644
--- a/mysql-test/main/cte_recursive.test
+++ b/mysql-test/main/cte_recursive.test
@@ -2324,7 +2324,124 @@ SELECT func();
DROP FUNCTION func;
---echo # Start of 10.3 tests
+--echo #
+--echo # MDEV-17024: two materialized CTEs using the same recursive CTE
+--echo #
+
+create table t1 (id int);
+insert into t1 values (1), (2), (3);
+
+let $q=
+with recursive
+rcte(a) as
+(select 1 union select cast(a+1 as unsigned) from rcte where a < 10),
+cte1 as
+(select count(*) as c1 from rcte,t1 where a between 3 and 5 and id=a-3),
+cte2 as
+(select count(*) as c2 from rcte,t1 where a between 7 and 8 and id=a-7)
+select * from cte1, cte2;
+
+eval $q;
+eval explain extended $q;
+eval prepare stmt from "$q";
+execute stmt;
+execute stmt;
+
+create table t2 (c1 int, c2 int);
+eval create procedure p() insert into t2 $q;
+call p();
+select * from t2;
+
+let $q1=
+with recursive
+rcte(a) as
+(select 1 union select cast(a+1 as unsigned) from rcte where a < 10),
+cte1 as
+(select count(*) as c1 from rcte,t1 where a between 3 and 5 and id=a-3),
+cte2 as
+(select count(*) as c2 from rcte,t1 where a between 7 and 8 and id=a-7)
+select * from cte1;
+
+eval $q1;
+
+let $q2=
+with recursive
+rcte(a) as
+(select 1 union select cast(a+1 as unsigned) from rcte where a < 10),
+cte1 as
+(select count(*) as c1 from t1),
+cte2 as
+(select count(*) as c2 from t2)
+select * from cte1,cte2;
+
+eval $q2;
+
+let $q3=
+with recursive
+rcte(a) as
+(select 1 union select cast(a+1 as unsigned) from rcte where a < 10),
+cte1 as
+(select count(*) as c1 from rcte,t1 where a between 3 and 5 and id=a-3),
+cte2 as
+(select count(*) as c2 from rcte,t1 where a between 7 and 8 and id=a-7)
+select * from cte1, cte2 where cte1.c1 = 3;
+
+eval $q3;
+
+drop procedure p;
+drop table t1,t2;
+
+--echo #
+--echo # MDEV-17201: recursive part with LIMIT
+--echo #
+
+CREATE TABLE purchases (
+ id int unsigned NOT NULL AUTO_INCREMENT,
+ pdate date NOT NULL,
+ quantity int unsigned NOT NULL,
+ p_id int unsigned NOT NULL,
+ PRIMARY KEY (id)
+);
+INSERT INTO purchases(pdate, quantity, p_id) VALUES
+ ('2014-11-01',5 ,1),('2014-11-03', 3 ,1),
+ ('2014-11-01',2 ,2),('2014-11-03', 4 ,2);
+
+CREATE TABLE expired (
+ edate date NOT NULL,
+ quantity int unsigned NOT NULL,
+ p_id int unsigned NOT NULL,
+ PRIMARY KEY (edate,p_id)
+);
+
+INSERT INTO expired VALUES ('2014-11-12', 5 ,1),('2014-11-08', 1 ,2);
+
+WITH RECURSIVE expired_map AS (
+ SELECT edate AS expired_date,
+ CAST(NULL AS date) AS purchase_date,
+ 0 AS quantity,
+ e.p_id,
+ (SELECT MAX(id)+1 FROM purchases p
+ WHERE pdate <= edate AND p.p_id =p_id) AS purchase_processed,
+ quantity AS unresolved
+ FROM expired e
+ UNION
+ ( SELECT expired_date,
+ pdate,
+ IF(p.quantity < m.unresolved, p.quantity, m.unresolved),
+ p.p_id,
+ p.id,
+ IF(p.quantity < m.unresolved, m.unresolved - p.quantity, 0)
+ FROM purchases p JOIN expired_map m ON p.p_id = m.p_id
+ WHERE p.id < m.purchase_processed AND m.unresolved > 0
+ ORDER BY p.id DESC
+ LIMIT 1
+ )
+)
+SELECT * FROM expired_map;
+
+DROP TABLE purchases, expired;
+
+--echo # End of 10.2 tests
--echo #
--echo # MDEV-14217 [db crash] Recursive CTE when SELECT includes new field
@@ -2553,3 +2670,5 @@ WITH RECURSIVE cte AS
SELECT * FROM cte;
DROP TABLE t1;
+
+--echo # End of 10.3 tests