[MDEV-24580] Aggregate stored function return always NULL in case of empty input set Created: 2021-01-13  Updated: 2023-04-27

Status: Open
Project: MariaDB Server
Component/s: Data Definition - Procedure
Affects Version/s: 10.5.5, 10.3, 10.4, 10.5
Fix Version/s: 10.4, 10.5

Type: Bug Priority: Major
Reporter: Christian Proust Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 0
Labels: None
Environment:

Centos 8.2.2004


Issue Links:
Relates
relates to MDEV-7773 Aggregate stored functions Closed

 Description   

With an aggregated function, the output results is consistently NULL when the input set is empty.

In this example, the call to aggregate_count(v) should be equivalent to COUNT(NULLIF(v, 1)).

DELIMITER //
-- Function created as per documentation:
-- https://mariadb.com/kb/en/stored-aggregate-functions/#examples
CREATE AGGREGATE FUNCTION aggregate_count(x INT) RETURNS INT
BEGIN
	DECLARE count_students INT DEFAULT 0;
	DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN count_students;
 
 	LOOP
		FETCH GROUP NEXT ROW;
		IF x  THEN
			SET count_students = count_students+1;
		END IF;
	END LOOP;
END //
 
-- Nominal case (no where clause)
WITH q(v) AS (
	VALUES
	(0),
	(1),
	(2)
)
SELECT COUNT(NULLIF(v, 1)), aggregate_count(v)
FROM q
//
 
-- Error case: no value are available in input
WITH q(v) AS (
	VALUES
	(0),
	(1),
	(2)
)
SELECT COUNT(NULLIF(v, 1)), aggregate_count(v)
FROM q
WHERE FALSE 
//
 
DROP FUNCTION aggregate_count //
DELIMITER ;


Generated at Thu Feb 08 09:31:04 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.