-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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: Register Spark array_min/max function with Orderable types #12576
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,7 @@ Array Functions | |
SELECT array_max(array(-1, -2, NULL)); -- -1 | ||
SELECT array_max(array()); -- NULL | ||
SELECT array_max(array(-0.0001, -0.0002, -0.0003, float('nan'))); -- NaN | ||
SELECT array_max(array(array(1), array(NULL))) -- array(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: add |
||
|
||
.. spark:function:: array_min(array(E)) -> E | ||
|
||
|
@@ -105,8 +106,9 @@ Array Functions | |
SELECT array_min(array(-1, -2, NULL)); -- -2 | ||
SELECT array_min(array(NULL, NULL)); -- NULL | ||
SELECT array_min(array()); -- NULL | ||
SELECT array_min(array(4.0, float('nan')]); -- 4.0 | ||
SELECT array_min(array(4.0, float('nan'))); -- 4.0 | ||
SELECT array_min(array(NULL, float('nan'))); -- NaN | ||
SELECT array_min(array(array(1), array(NULL))) -- array(NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
.. spark:function:: array_position(x, element) -> bigint | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,50 @@ struct ArrayMinMaxFunction { | |
assign(out, currentValue); | ||
return true; | ||
} | ||
|
||
bool compare( | ||
exec::GenericView currentValue, | ||
exec::GenericView candidateValue) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this function private? |
||
static constexpr CompareFlags kFlags = { | ||
.nullHandlingMode = CompareFlags::NullHandlingMode::kNullAsValue}; | ||
|
||
auto compareResult = candidateValue.compare(currentValue, kFlags).value(); | ||
if constexpr (isMax) { | ||
return compareResult > 0; | ||
} else { | ||
return compareResult < 0; | ||
} | ||
} | ||
|
||
bool call( | ||
out_type<Orderable<T1>>& out, | ||
const arg_type<Array<Orderable<T1>>>& array) { | ||
// Result is null if array is empty. | ||
if (array.size() == 0) { | ||
return false; | ||
} | ||
|
||
int currentIndex = -1; | ||
for (auto i = 0; i < array.size(); i++) { | ||
if (array[i].has_value()) { | ||
if (currentIndex == -1) { | ||
currentIndex = i; | ||
} else { | ||
auto currentValue = array[currentIndex].value(); | ||
auto candidateValue = array[i].value(); | ||
if (compare(currentValue, candidateValue)) { | ||
currentIndex = i; | ||
} | ||
} | ||
} | ||
} | ||
if (currentIndex == -1) { | ||
// If array contains only NULL elements, return NULL. | ||
return false; | ||
} | ||
out.copy_from(array[currentIndex].value()); | ||
return true; | ||
} | ||
}; | ||
|
||
template <typename TExecCtx> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you add description on how nested element is ordered, e.g. when the arrays contain NULL and NAN, and how arrays of different size are compared?