xiedeyantu opened a new issue, #22040:
URL: https://github.com/apache/datafusion/issues/22040

   ### Describe the bug
   
   When a query uses GROUP BY GROUPING SETS(()) and the input relation is 
empty, DataFusion returns 0 rows. This is incorrect.
   According to SQL semantics, the empty grouping set () represents a global 
aggregate, so it should still produce exactly one output row even when there 
are no input rows.
   
   In practice, this affects queries such as:
   ```
   SELECT SUM(empno)
   FROM emp
   WHERE false
   GROUP BY GROUPING SETS(());
   ```
   
   The current result is an empty result set, but it should return one row with 
NULL for SUM(empno).
   
   ### To Reproduce
   
   Steps to reproduce the behavior:
   
   Create a table and insert data.
   Run a query with WHERE false so the input becomes empty.
   Use GROUP BY GROUPING SETS(()).
   Example:
   ```
   CREATE TABLE emp (
     empno INT PRIMARY KEY,
     ename VARCHAR(10) NOT NULL,
     job VARCHAR(10),
     mgr INT,
     hiredate DATE,
     sal DECIMAL(10,2),
     comm DECIMAL(10,2),
     deptno INT
   );
   
   INSERT INTO emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES
   (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800.00, NULL, 20);
   
   SELECT SUM(empno)
   FROM emp
   WHERE false
   GROUP BY GROUPING SETS(());
   ```
   Returns:
   ```
   +----------------+
   | sum(emp.empno) |
   +----------------+
   +----------------+
   0 row(s) fetched.
   ```
   
   ### Expected behavior
   
   Returns:
   ```
   +----------------+
   | sum(emp.empno) |
   +----------------+
   | NULL           |
   +----------------+
   1 row(s) fetched.
   ```
   
   ### Additional context
   
   _No response_


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to