Dandandan commented on code in PR #2171:
URL: https://github.com/apache/arrow-datafusion/pull/2171#discussion_r845180292
##########
datafusion/physical-expr/src/conditional_expressions.rs:
##########
@@ -35,36 +33,51 @@ pub fn coalesce(args: &[ColumnarValue]) ->
Result<ColumnarValue> {
)));
}
- let size = match args[0] {
- ColumnarValue::Array(ref a) => a.len(),
- ColumnarValue::Scalar(ref _s) => 1,
- };
- let mut res = new_null_array(&args[0].data_type(), size);
+ let return_type = args[0].data_type();
+ let mut return_array = args.iter().filter_map(|x| match x {
+ ColumnarValue::Array(array) => Some(array.len()),
+ _ => None,
+ });
+
+ if let Some(size) = return_array.next() {
+ // start with nulls as default output
+ let mut current_value = new_null_array(&return_type, size);
+ let mut remainder = BooleanArray::from(vec![true; size]);
- for column_value in args {
- for i in 0..size {
- match column_value {
- ColumnarValue::Array(array_ref) => {
- let curr_null_mask = compute::is_null(res.as_ref())?;
- let arr_not_null_mask = compute::is_not_null(array_ref)?;
- let bool_mask = compute::and(&curr_null_mask,
&arr_not_null_mask)?;
- res = zip(&bool_mask, array_ref, &res)?;
+ for arg in args {
+ match arg {
+ ColumnarValue::Array(ref array) => {
+ let to_apply = and(&remainder,
&is_not_null(array.as_ref())?)?;
+ current_value = zip(&to_apply, array,
current_value.as_ref())?;
+ remainder = and(&remainder, &is_null(array)?)?;
}
- ColumnarValue::Scalar(scalar) => {
- if !scalar.is_null() && res.is_null(i) {
- let vec: Vec<bool> =
- (0..size).into_iter().map(|j| j == i).collect();
- let bool_arr = BooleanArray::from(vec);
- res =
- zip(&bool_arr,
scalar.to_array_of_size(size).as_ref(), &res)?;
+ ColumnarValue::Scalar(value) => {
+ if value.is_null() {
continue;
+ } else {
+ let last_value = value.to_array_of_size(size);
+ current_value =
+ zip(&remainder, &last_value,
current_value.as_ref())?;
+ break;
}
}
}
+ if remainder.iter().all(|x| x == Some(false)) {
Review Comment:
There also seem to be some opportunities to optimize `zip` in arrow-rs for
all-true or all-false.
--
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]