array_min/array_max throw if first element is an inner Array with a null#23323
Merged
xiaoxmeng merged 1 commit intoprestodb:masterfrom Aug 15, 2024
Merged
array_min/array_max throw if first element is an inner Array with a null#23323xiaoxmeng merged 1 commit intoprestodb:masterfrom
xiaoxmeng merged 1 commit intoprestodb:masterfrom
Conversation
elharo
reviewed
Jul 30, 2024
Contributor
elharo
left a comment
There was a problem hiding this comment.
Are these functions documented somewhere? If so, does that need to be updated?
| assertFunction("ARRAY_MIN(ARRAY ['puppies', 'kittens'])", createVarcharType(7), "kittens"); | ||
| assertFunction("ARRAY_MIN(ARRAY [TRUE, FALSE])", BOOLEAN, false); | ||
| assertFunction("ARRAY_MIN(ARRAY [NULL, FALSE])", BOOLEAN, null); | ||
| assertFunction("ARRAY_MIN(ARRAY [ARRAY[1, NULL], ARRAY[2, NULL]])", new ArrayType(INTEGER), Lists.newArrayList(1, null)); |
Contributor
There was a problem hiding this comment.
Can we have new test methods instead please? Makes it easier to debug failures.
Contributor
Author
|
@elharo Thanks for the review! The functions are documented here presto/presto-docs/src/main/sphinx/functions/array.rst Lines 123 to 129 in 772213c But it does not say anything about this behavior that would need to be updated. |
Contributor
Author
|
The failing check appears to be some TestHudiIntegration tests that are broken in trunk #22031 |
elharo
reviewed
Aug 13, 2024
presto-main/src/test/java/com/facebook/presto/type/TestArrayOperators.java
Show resolved
Hide resolved
| } | ||
|
|
||
| @Test | ||
| public void testArrayMaxWithNulls() |
Contributor
There was a problem hiding this comment.
These should be four separate test methods so they can pass or fail independently.
0bc5656 to
6cbf1ef
Compare
rschlussel
previously approved these changes
Aug 13, 2024
elharo
previously approved these changes
Aug 13, 2024
6cbf1ef to
3ec0d19
Compare
xiaoxmeng
approved these changes
Aug 15, 2024
rschlussel
approved these changes
Aug 15, 2024
34 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
array_min and array_max will always throw an exception if the first element is an inner array with a null.
Motivation and Context
array_min and array_max always compare the first element with itself. Besides being unnecessary this produces inconsistent behavior where if the first element is an array (or there is an array recursively in a complex type) that contains a null the function will throw "ARRAY comparison not supported for arrays with null elements".
This is because the element is of course equal to itself, but everything in it needs to be compared to verify this.
For other elements, the comparison will exit early as soon as it can be confirmed that it is less than/greater than the current candidate, so it may not throw even if it contains an array with a null element.
Impact
This should slightly improve performance by avoiding an unnecessary comparison, but will also cause the function to throw only if it needs to compare a null element of an inner array in the first element with the corresponding value in a later element.
Test Plan
Added unit tests to verify a null in the first element does not necessarily cause the function to throw, but it does throw if it is necessary to compare that null with another value.
Contributor checklist
Release Notes
Please follow release notes guidelines and fill in the release notes below.