-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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(function): add greatest function #12474
Merged
Merged
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
69761df
feat(function): add greatest function
rluvaton 7d63bd2
Merge branch 'apache:main' into add-greatest
rluvaton 4904440
remove unused
rluvaton 5e3a260
fix finding common supertype in greatest
rluvaton b929239
allow single argument for greatest
rluvaton 966116b
assert that both array have the same length
rluvaton d065d4a
use logical null count
rluvaton ccbea44
remove unused import
rluvaton ed2dbad
add docs
rluvaton 4df3c39
add greatest slt tests
rluvaton 1c51c87
add greatest slt tests
rluvaton e6c5841
Merge remote-tracking branch 'refs/remotes/upstream/main' into add-gr…
rluvaton 655419a
fix merge conflicts
rluvaton 47652e6
add docs
rluvaton cfa6185
revert manual docs changes
rluvaton fda197f
Merge remote-tracking branch 'upstream/main' into add-greatest
rluvaton 03fbd8f
Update based on cr
rluvaton df5ad68
fix lint
rluvaton 2d83abf
run fmt
rluvaton 0db1fd0
run clippy
rluvaton d985681
Uppdated docs using `./dev/update_function_docs.sh`
rluvaton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::any::Any; | ||
|
||
use arrow::array::{make_comparator, Array, ArrayRef, BooleanArray}; | ||
use arrow::compute::kernels::cmp; | ||
use arrow::compute::kernels::zip::zip; | ||
use arrow::compute::SortOptions; | ||
use arrow::datatypes::DataType; | ||
use arrow_buffer::BooleanBuffer; | ||
use datafusion_common::{exec_err, plan_err, Result, ScalarValue}; | ||
use datafusion_expr::{ColumnarValue}; | ||
use datafusion_expr::{ScalarUDFImpl, Signature, Volatility}; | ||
use datafusion_expr::binary::type_union_resolution; | ||
|
||
const SORT_OPTIONS: SortOptions = SortOptions { | ||
// We want greatest first | ||
descending: false, | ||
|
||
// NULL will be less than any other value | ||
nulls_first: true, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct GreatestFunc { | ||
signature: Signature, | ||
} | ||
|
||
impl Default for GreatestFunc { | ||
fn default() -> Self { | ||
GreatestFunc::new() | ||
} | ||
} | ||
|
||
impl GreatestFunc { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::user_defined(Volatility::Immutable), | ||
} | ||
} | ||
} | ||
|
||
fn get_logical_null_count(arr: &dyn Array) -> usize { | ||
arr.logical_nulls().map(|n| n.null_count()).unwrap_or_default() | ||
|
||
} | ||
|
||
/// Return boolean array where `arr[i] = lhs[i] >= rhs[i]` for all i, where `arr` is the result array | ||
/// Nulls are always considered smaller than any other value | ||
fn get_larger(lhs: &dyn Array, rhs: &dyn Array) -> Result<BooleanArray> { | ||
// Fast path: | ||
// If both arrays are not nested, have the same length and no nulls, we can use the faster vectorised kernel | ||
// - If both arrays are not nested: Nested types, such as lists, are not supported as the null semantics are not well-defined. | ||
// - both array does not have any nulls: cmp::gt_eq will return null if any of the input is null while we want to return false in that case | ||
if !lhs.data_type().is_nested() && get_logical_null_count(lhs) == 0 && get_logical_null_count(rhs) == 0 { | ||
return cmp::gt_eq(&lhs, &rhs).map_err(|e| e.into()); | ||
} | ||
|
||
let cmp = make_comparator(lhs, rhs, SORT_OPTIONS)?; | ||
|
||
if lhs.len() != rhs.len() { | ||
return exec_err!("All arrays should have the same length for greatest comparison") | ||
} | ||
|
||
let values = BooleanBuffer::collect_bool(lhs.len(), |i| cmp(i, i).is_ge()); | ||
|
||
// No nulls as we only want to keep the values that are larger, its either true or false | ||
Ok(BooleanArray::new(values, None)) | ||
} | ||
|
||
/// Return array where the largest value at each index is kept | ||
fn keep_larger(lhs: ArrayRef, rhs: ArrayRef) -> Result<ArrayRef> { | ||
// True for values that we should keep from the left array | ||
let keep_lhs = get_larger(lhs.as_ref(), rhs.as_ref())?; | ||
|
||
let larger = zip(&keep_lhs, &lhs, &rhs)?; | ||
|
||
Ok(larger) | ||
} | ||
|
||
fn keep_larger_scalar<'a>(lhs: &'a ScalarValue, rhs: &'a ScalarValue) -> Result<&'a ScalarValue> { | ||
if !lhs.data_type().is_nested() { | ||
return if lhs >= rhs { | ||
Ok(lhs) | ||
} else { | ||
Ok(rhs) | ||
}; | ||
} | ||
|
||
// If complex type we can't compare directly as we want null values to be smaller | ||
let cmp = make_comparator( | ||
lhs.to_array()?.as_ref(), | ||
rhs.to_array()?.as_ref(), | ||
SORT_OPTIONS, | ||
)?; | ||
|
||
if cmp(0, 0).is_ge() { | ||
Ok(lhs) | ||
} else { | ||
Ok(rhs) | ||
} | ||
} | ||
|
||
fn find_coerced_type(data_types: &[DataType]) -> Result<DataType> { | ||
if data_types.is_empty() { | ||
plan_err!("greatest was called without any arguments. It requires at least 1.") | ||
} else if let Some(coerced_type) = type_union_resolution(data_types) { | ||
Ok(coerced_type) | ||
} else { | ||
plan_err!("Cannot find a common type for arguments") | ||
} | ||
} | ||
|
||
impl ScalarUDFImpl for GreatestFunc { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"greatest" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
find_coerced_type(arg_types) | ||
rluvaton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
if args.is_empty() { | ||
return exec_err!("greatest was called with no arguments. It requires at least 1."); | ||
} | ||
|
||
// Some engines (e.g. SQL Server) allow greatest with single arg, it's a noop | ||
if args.len() == 1 { | ||
return Ok(args[0].clone()) | ||
} | ||
|
||
// Split to scalars and arrays for later optimization | ||
let (scalars, arrays): (Vec<_>, Vec<_>) = args.iter().partition(|x| match x { | ||
ColumnarValue::Scalar(_) => true, | ||
ColumnarValue::Array(_) => false, | ||
}); | ||
|
||
let mut arrays_iter = arrays | ||
.iter() | ||
.map(|x| match x { | ||
ColumnarValue::Array(a) => a, | ||
_ => unreachable!(), | ||
}); | ||
|
||
let first_array = arrays_iter.next(); | ||
|
||
let mut largest: ArrayRef; | ||
|
||
// Optimization: merge all scalars into one to avoid recomputing | ||
if !scalars.is_empty() { | ||
let mut scalars_iter = scalars | ||
.iter() | ||
.map(|x| match x { | ||
ColumnarValue::Scalar(s) => s, | ||
_ => unreachable!(), | ||
}); | ||
|
||
// We have at least one scalar | ||
let mut largest_scalar = scalars_iter.next().unwrap(); | ||
|
||
for scalar in scalars_iter { | ||
largest_scalar = keep_larger_scalar(largest_scalar, scalar)?; | ||
} | ||
|
||
// If we only have scalars, return the largest one | ||
if arrays.is_empty() { | ||
return Ok(ColumnarValue::Scalar(largest_scalar.clone())); | ||
} | ||
|
||
// We have at least one array | ||
let first_array = first_array.unwrap(); | ||
|
||
// Start with the largest value | ||
largest = keep_larger( | ||
first_array.clone(), | ||
largest_scalar.to_array_of_size(first_array.len())? | ||
)?; | ||
} else { | ||
// If we only have arrays, start with the first array | ||
// (We must have at least one array) | ||
largest = first_array.unwrap().clone(); | ||
} | ||
|
||
for array in arrays_iter { | ||
largest = keep_larger(array.clone(), largest)?; | ||
} | ||
|
||
Ok(ColumnarValue::Array(largest)) | ||
} | ||
|
||
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { | ||
let coerced_type = find_coerced_type(arg_types)?; | ||
|
||
Ok(vec![coerced_type; arg_types.len()]) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::core; | ||
use arrow::datatypes::DataType; | ||
use datafusion_expr::ScalarUDFImpl; | ||
|
||
#[test] | ||
fn test_greatest_return_types_without_common_supertype_in_arg_type() { | ||
let greatest = core::greatest::GreatestFunc::new(); | ||
let return_type = greatest | ||
.return_type(&[DataType::Decimal128(10, 3), DataType::Decimal128(10, 4)]) | ||
.unwrap(); | ||
assert_eq!(return_type, DataType::Decimal128(11, 4)); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
please add a test with float NaN values.
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.
done