Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add array_max function support #14470

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

erenavsarogullari
Copy link
Member

Which issue does this PR close?

Closes #14469.

What changes are included in this PR?

Currently, Spark, Snowflake and Presto support array_max function. This can also be useful for DataFusion.

array_max(make_array(3,1,4,2)) => 4
array_max(make_array('h','e','o','l','l',NULL)) => o
array_max(make_array(NULL,NULL)) => NULL

Spark: https://docs.databricks.com/en/sql/language-manual/functions/array_max.html
Snowflake: https://docs.snowflake.com/en/sql-reference/functions/array_max
Presto: https://prestodb.io/docs/current/functions/array.html#array_max-x-x

All potential use-cases have been covered like different data_types, empty array, NULL etc.

Are these changes tested?

Added new UT cases to verify array_max function in terms of different source arrays.

Are there any user-facing changes?

Yes, new SQL function is supported and documentation has also be updated.

@github-actions github-actions bot added documentation Improvements or additions to documentation sqllogictest SQL Logic Tests (.slt) labels Feb 4, 2025
@erenavsarogullari erenavsarogullari changed the title feat: Add array_max function feat: Add array_max function support Feb 4, 2025
@alamb
Copy link
Contributor

alamb commented Feb 5, 2025

FYI @findepi I think you mentioned this feature recently

datafusion/functions-nested/src/max.rs Outdated Show resolved Hide resolved
datafusion/functions-nested/src/max.rs Outdated Show resolved Hide resolved
datafusion/functions-nested/src/max.rs Outdated Show resolved Hide resolved
datafusion/sqllogictest/test_files/array.slt Outdated Show resolved Hide resolved
datafusion/functions-nested/src/max.rs Outdated Show resolved Hide resolved
datafusion/functions-nested/src/max.rs Outdated Show resolved Hide resolved
datafusion/functions-nested/src/max.rs Show resolved Hide resolved
@jayzhan211
Copy link
Contributor

I have the same question for array_min, but if this function is highly interested from many people then adding it to datafusion core is not a bad idea.
#14417 (comment)

@erenavsarogullari
Copy link
Member Author

I have the same question for array_min, but if this function is highly interested from many people then adding it to datafusion core is not a bad idea. #14417 (comment)

Thanks @jayzhan211 for the review. I think we have 2 use cases for array_min/max functions:
They need to be supported natively by DataFusion for Comet users as a clear API and abstraction and also, i think they can be useful for DF users (not using Comet) as part of core module. On the other hand, other query engines (Snowflake, Presto, GoogleSQL) also support them natively in addition to Spark. In terms of these, IMHO, they are candidate for the core module. However, it is also hard to know how much demand we will have from the community so both module options are ok for me.


match arg1.data_type() {
List(_) | LargeList(_) | FixedSizeList(_, _) => {
let input_array = as_list_array(&arg1)?.value(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as_list_array works only for List(_)
the LargeList(_) | FixedSizeList(_, _) codepath doesn't work

AFAIR, it's hard to create instance of FixedSizeList in SQL, so it will be difficult to test.
i'd suggest to leave it out from the PR for now.


match arg1.data_type() {
List(_) | LargeList(_) | FixedSizeList(_, _) => {
let input_array = as_list_array(&arg1)?.value(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is .value(0)??

## array_max
# array_max scalar function #1 (with positive index)
query I
select array_max(make_array(5, 3, 6, 4));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test where query process couple arrays in single array_max call

SELECT input, array_max(input)
FROM (
  SELECT make_array(d-1, d, d-2)
  FROM (VALUES (0), (10), (20), (30), (NULL)) t(d)
)

@jayzhan211
Copy link
Contributor

I have the same question for array_min, but if this function is highly interested from many people then adding it to datafusion core is not a bad idea. #14417 (comment)

Thanks @jayzhan211 for the review. I think we have 2 use cases for array_min/max functions: They need to be supported natively by DataFusion for Comet users as a clear API and abstraction and also, i think they can be useful for DF users (not using Comet) as part of core module. On the other hand, other query engines (Snowflake, Presto, GoogleSQL) also support them natively in addition to Spark. In terms of these, IMHO, they are candidate for the core module. However, it is also hard to know how much demand we will have from the community so both module options are ok for me.

Sounds like Spark crate is a great place for this #5600

@findepi
Copy link
Member

findepi commented Feb 11, 2025

Per project guidelines proposal #13706 it feels to me as belong to core.
It would be great to either finalize those guidelines or close the PR and come up with new guidelines. Debating separately for each individual function is counter productive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation functions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add array_max function support
5 participants