1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-01 03:47:19 +03:00

MDEV-17201 dropped anchor rows with non-null recursion query

The function st_select_lex_unit::exec_recursive() missed resetting of
select_limit_cnt and offset_limit_cnt before execution of union parts.
As a result recursive CTEs specified by UNIONs whose SELECTs contained
LIMIT/OFFSET could return wrong sets of records.
This commit is contained in:
Igor Babaev
2018-09-17 23:15:56 -07:00
parent 65474d92f5
commit 6858d5346c
3 changed files with 99 additions and 0 deletions

View File

@ -2390,3 +2390,53 @@ 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;