[MDEV-11081] Cursor fetches NULL for aggregate functions Created: 2016-10-18  Updated: 2016-11-23  Resolved: 2016-11-23

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.2
Fix Version/s: 10.2.3

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Igor Babaev
Resolution: Fixed Votes: 0
Labels: regression-10.2


 Description   

This script:

DROP TABLE IF EXISTS t1;
DROP PROCEDURE IF EXISTS p1;
CREATE TABLE t1 (name VARCHAR(10), value INT);
INSERT INTO t1 VALUES ('a',1);
INSERT INTO t1 VALUES ('a',1);
INSERT INTO t1 VALUES ('a',1);
INSERT INTO t1 VALUES ('b',1);
INSERT INTO t1 VALUES ('b',1);
INSERT INTO t1 VALUES ('c',1);
DELIMITER $$
CREATE PROCEDURE p1 ()
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE v_name VARCHAR(10);
  DECLARE v_total INT;
  DECLARE c CURSOR FOR SELECT name, SUM(value) AS total FROM t1 GROUP BY name;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  OPEN c;
read_loop:
  LOOP
    FETCH c INTO v_name, v_total;
    IF done THEN
      LEAVE read_loop;
    END IF;
    SELECT v_name, v_total;
  END LOOP;
  CLOSE c;
END;
$$
DELIMITER ;
CALL p1();
DROP PROCEDURE p1;
DROP TABLE t1;

correctly returns these results in 10.1:

+--------+---------+
| v_name | v_total |
+--------+---------+
| a      |       3 |
+--------+---------+
1 row in set (0.00 sec)
 
+--------+---------+
| v_name | v_total |
+--------+---------+
| b      |       2 |
+--------+---------+
1 row in set (0.00 sec)
 
+--------+---------+
| v_name | v_total |
+--------+---------+
| c      |       1 |
+--------+---------+
1 row in set (0.00 sec)

In 10.2 the same script returns wrong results:

+--------+---------+
| v_name | v_total |
+--------+---------+
| a      |    NULL |
+--------+---------+
1 row in set (0.00 sec)
 
+--------+---------+
| v_name | v_total |
+--------+---------+
| b      |    NULL |
+--------+---------+
1 row in set (0.00 sec)
 
+--------+---------+
| v_name | v_total |
+--------+---------+
| c      |    NULL |
+--------+---------+
1 row in set (0.00 sec)



 Comments   
Comment by Alexander Barkov [ 2016-10-21 ]

It seems the problem was introduced by:

commit 2cfc450bf78c2d951729d1a0e8f731c0d987b1d5
Author: Igor Babaev <igor@askmonty.org>
Date: Tue Feb 9 12:35:59 2016 -0800

Igor, can you please take over this bug?

Comment by Igor Babaev [ 2016-10-21 ]

Alexander,
Where did you get this script?

Comment by Alexander Barkov [ 2016-10-21 ]

I found this while writing tests for "MDEV-10597 Cursors with parameters".

Comment by Igor Babaev [ 2016-11-23 ]

The fix for this bug was pushed into the 10.2 tree.

Generated at Thu Feb 08 07:47:10 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.