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

   ### Is your feature request related to a problem or challenge?
   
   A common pattern is to `GROUP BY` a unique id and then get one arbitrary 
value of another column per group (for example, any tag value for each id). 
Which value comes back doesn't matter, so ordering is irrelevant.
   
   DataFusion has no function for this today, and the workarounds are heavier 
than needed:
   
   - `first_value(x)` / `last_value(x)` can do this, but they do extra work 
`any_value` doesn't need.
   - `min(x)` / `max(x)` are often used instead, but they don't really mean 
"any value", and they still compare every row of every batch.
   
   What's missing is a function whose state is just "one value, captured once", 
so that once a group has a value, later rows for that group cost nothing.
   
   `ANY_VALUE` is also a standard function (SQL:2023) and is already supported 
by spark, duckdb, postgres, and others.
   
   ### Describe the solution you'd like
   
   Add a built-in `any_value(expression)` aggregate that returns an arbitrary 
value from each group, ignoring NULLs (it returns NULL only when every value in 
the group is NULL).
   
   ### Describe alternatives you've considered
   
   _No response_
   
   ### Additional context
   
   `ANY_VALUE` is a standard aggregate (SQL:2023) and is widely supported, all 
returning an arbitrary non-NULL value from the group:
   
   - PostgreSQL: 
https://www.postgresql.org/docs/current/functions-aggregate.html
   - DuckDB: 
https://duckdb.org/docs/current/sql/functions/aggregates#any_valuearg
   - Spark SQL: https://spark.apache.org/docs/latest/api/sql/#any_value
   
   NULL handling varies slightly across engines; this proposal follows the 
ignore-NULLs behavior shared by the above (a NULL is returned only when every 
value in the group is NULL). `RESPECT NULLS | IGNORE NULLS` could be added 
later if needed.


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