rluvaton commented on code in PR #22847:
URL: https://github.com/apache/datafusion/pull/22847#discussion_r3380456755
##########
datafusion/physical-expr/src/higher_order_function.rs:
##########
@@ -503,8 +503,87 @@ fn wrapped_lambda(expr: &Arc<dyn PhysicalExpr>) ->
Option<&LambdaExpr> {
}
}
+
+fn support_clear_non_empty_null_values(data_type: &DataType) -> bool {
+ match data_type {
+ DataType::List(_) | DataType::LargeList(_) | DataType::Map(_, _) =>
true,
+ _ => false,
+ }
+}
+
+/// For lists and large lists, truncates the sublist of null values
+/// For maps, truncates the submaps of null values
+/// Otherwise returns an error
+fn remove_non_empty_null_values(array: &ArrayRef) -> Result<ArrayRef> {
+ // todo: handle list view
+ let array = match array.data_type() {
+ DataType::List(_) | DataType::LargeList(_) =>
remove_list_null_values(array)? as ArrayRef,
+ DataType::Map(_, _) =>
Arc::new(remove_map_non_empty_null_values(array)?) as ArrayRef,
+ dt => {
+ assert!(!support_clear_non_empty_null_values(dt), "data type
marked as supported but not implemented");
+ return _exec_err!("expected List/LargeList/Map, got {dt}")
+ },
+ };
+
+ assert!(support_clear_non_empty_null_values(array.data_type()), "data type
marked as not supported but implemented");
+
+ Ok(array)
+}
+
+fn remove_map_non_empty_null_values(map: &MapArray) -> Result<MapArray> {
+ if let Some(nulls) = map.nulls()
+ && nulls.null_count() > 0
+ {
+ let lengths =
Int32Array::from_iter(map.offsets().lengths().zip(nulls.iter()).map(|(len,
is_valid)| if is_valid { Some(len as i32)} else {None}));
+ let zero: &dyn Datum = &Int32Array::new_scalar(0);
+
+ let (mut valid_or_empty, _nulls) = eq(&lengths, zero)?.into_parts();
+ valid_or_empty |= nulls.inner();
+ let valid_or_empty = BooleanArray::from(valid_or_empty);
+
+ if valid_or_empty.has_false() {
+ let array_data = map.entries().to_data();
+ let offsets = map.offsets();
+ let capacity = offsets[offsets.len() - 1] - offsets[0];
+ let mut mutable_array_data =
+ MutableArrayData::new(vec![&array_data], false, capacity as
usize);
+
+ let (valid_or_empty, _nulls) = valid_or_empty.into_parts();
+
+ for (start, end) in valid_or_empty.set_slices() {
+ mutable_array_data.extend(
+ 0,
+ offsets[start] as usize,
+ offsets[end] as usize
+ );
+ }
+
+ let lengths = std::iter::zip(offsets.lengths(), nulls)
+ .map(|(length, is_valid)| if is_valid { length } else { 0 });
+
+ let offsets = OffsetBuffer::from_lengths(lengths);
+ let entries = make_array(mutable_array_data.freeze());
+ let entries = entries.as_struct().clone();
+
+ let DataType::Map(field, ordered) = map.data_type() else {
+ unreachable!("map array data type is not Map");
+ };
+
+ return Ok(MapArray::try_new(
+ Arc::clone(field),
+ offsets,
+ entries,
+ map.nulls().cloned(),
+ *ordered
+ )?);
+ }
+ }
+ Ok(map.clone())
+}
Review Comment:
I want to remove this once my arrow pr that does that is merged and released:
- https://github.com/apache/arrow-rs/pull/9970
so I did not make it public or modify the old function to make sure to not
increase the breaking change scope when removing this function and the
`remove_list_null_values`
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]