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

   ### Describe the bug
   
   `COUNT(... ORDER BY ...)` incorrectly treats aggregate ordering keys as 
additional counted arguments. This has two user-visible consequences:
   
   - A non-grouped count can return a value that is too small when an ordering 
key is null, even though the counted expression is non-null.
   - A grouped, single-argument count can panic because its groups accumulator 
expects one input array but receives the counted expression and ordering key.
   
   The query may also perform an unnecessary sort even though `COUNT` does not 
depend on input order.
   
   ### To Reproduce
   
   A non-grouped count can return the wrong result:
   
   ```sql
   SELECT COUNT(a + 0 ORDER BY b)
   FROM (VALUES (1, NULL), (2, 20)) AS t(a, b);
   ```
   
   This returns `1` instead of `2`: the first row is incorrectly excluded 
because `b` is null. The null-preserving `a + 0` expression prevents the count 
from being answered from input statistics, ensuring the accumulator executes.
   
   A grouped query can instead panic:
   
   ```sql
   SELECT c, COUNT(a ORDER BY b)
   FROM (VALUES (1, NULL, 10), (2, 20, 10)) AS t(a, b, c)
   GROUP BY c;
   ```
   
   The panic reports that `CountGroupsAccumulator` received two arguments 
instead of one.
   
   ### Expected behavior
   
   The first query should return `2`, and the grouped query should return one 
row with a count of `2`. Nulls in ordering keys must not affect `COUNT`; only 
nulls in counted expressions should exclude rows.
   
   ### Additional context
   
   `COUNT` inherited the default hard ordering requirement. #24997 marks it as 
order-insensitive so ordering keys are neither sorted for nor passed to its 
accumulators.


-- 
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