Open
Labels
bugSomething isn't workingSomething isn't working
Description
Describe the bug
DataFusion now supports providing ordering and nulls information to aggregates, but some aggregates ignore the flags silently
For example
❯ select first_value(column1 ORDER BY column2) FROM (values (1,2), (3,4), (-1,0)) ;
+----------------------+
| FIRST_VALUE(column1) |
+----------------------+
| -1 |
+----------------------+
❯ select first_value(column1 ORDER BY column2) IGNORE NULLS FROM (values (1,2), (3,4), (null,0)) ;
+----------------------+
| FIRST_VALUE(column1) |
+----------------------+
| 1 |
+----------------------+
1 row in set. Query took 0.002 seconds.To Reproduce
❯ select count(*) from (values (1), (null), (2));
+----------+
| COUNT(*) |
+----------+
| 3 |
+----------+
1 row in set. Query took 0.039 seconds.
❯ select count(*) IGNORE NULLS from (values (1), (null), (2));
+----------+
| COUNT(*) |
+----------+
| 3 |
+----------+
1 row in set. Query took 0.001 seconds.Also, for ordering
❯ select avg(column1 ORDER BY column2) FROM (values (1,2), (3,4), (null,0)) ;
+--------------+
| AVG(column1) |
+--------------+
| 2.0 |
+--------------+
1 row in set. Query took 0.008 seconds.Expected behavior
I expect
❯ select count(*) IGNORE NULLS from (values (1), (null), (2));To error with "IGNORE NULLS is not supported
I also expect
❯ select avg(column1 ORDER BY column2) FROM (values (1,2), (3,4), (null,0)) ;to error with "ORDER BY" not supported for avg
Additional context
@jayzhan211 has some good ideas at #9920 (comment) about how to make checking this easier / harder to miss
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
[-]Aggregates ignore `IGNORE NULLS` and `ORDER BY` on arguments[/-][+]Some aggregates silently ignore `IGNORE NULLS` and `ORDER BY` on arguments[/+]comphead commentedon Apr 3, 2024
@huaxingao this might be interesting
huaxingao commentedon Apr 3, 2024
@comphead Thanks for pinging me! I will work on this issue.
mustafasrepo commentedon Apr 4, 2024
I think for
AVG, orCOUNTtype of aggregates we can safely ignoreORDER BYexpression. Since operation result is same for any permutation of the input. However, forfirst_value,nth_value,last_valuethis is not the case. However, I agree thatIGNORE NULLSshould raise an error.alamb commentedon Apr 4, 2024
That is true, though applying an ORDER BY to the argument of COUNT may be much slower 🤔 (as DataFusion would have to sort the input)
However, we could remove
ORDER BYthat don't change the output as a follow on optimizationalamb commentedon Apr 4, 2024
FWIW @jayzhan211 has a really nice API suggestion here I think https://github.com/apache/arrow-datafusion/pull/9920/files#r1549905825
Specifically, add these two functions to the
AggregateUDFImplalamb commentedon Apr 4, 2024
I wrote some tests for this feature in another PR that we decided not to use. I put them here #9953 in case that is helpful @huaxingao
mustafasrepo commentedon Apr 5, 2024
I think these APIs are very nice, clear. We can add these supports
vbarua commentedon Mar 4, 2025
I opened up an issue that's potentially related to this when it comes to supporting IGNORE NULLS
#15006
Change default of `AggregateUDFImpl::supports_null_handling_clause` t…
Phoenix500526 commentedon Nov 6, 2026
I checked the original examples:
COUNT(*) IGNORE NULLSnow correctly errors after #18441.And after #25711, order-insensitive aggregates simply ignore ORDER BY. I also found an
APPROX_MEDIANordering bug during my verification, and opened #27157 to track it separately. I also submitted PR #27159 to fix it. We can close this issue now or after #27159 is merged.comphead commentedon Nov 6, 2026
we need to check if this still an issue