Weijun-H commented on code in PR #18432:
URL: https://github.com/apache/datafusion/pull/18432#discussion_r2484762681
##########
datafusion/functions-nested/src/extract.rs:
##########
@@ -644,6 +691,109 @@ where
)?))
}
+fn general_list_view_array_slice<O: OffsetSizeTrait>(
+ array: &GenericListViewArray<O>,
+ from_array: &Int64Array,
+ to_array: &Int64Array,
+ stride: Option<&Int64Array>,
+) -> Result<ArrayRef>
+where
+ i64: TryInto<O>,
+{
+ let values = array.values();
+ let original_data = values.to_data();
+ let capacity = Capacities::Array(original_data.len());
+
+ let mut mutable =
+ MutableArrayData::with_capacities(vec![&original_data], true,
capacity);
+
+ // We must build `offsets` and `sizes` buffers manually as ListView does
not enforce
+ // monotonically increasing offsets.
+ let mut offsets = Vec::with_capacity(array.len());
+ let mut sizes = Vec::with_capacity(array.len());
+ let mut current_offset = O::usize_as(0);
+ let mut null_builder = NullBufferBuilder::new(array.len());
+
+ for row_index in 0..array.len() {
+ // Propagate NULL semantics: any NULL input yields a NULL output slot.
+ if array.is_null(row_index)
+ || from_array.is_null(row_index)
+ || to_array.is_null(row_index)
+ {
+ null_builder.append_null();
+ offsets.push(current_offset);
+ sizes.push(O::usize_as(0));
+ continue;
+ }
+ null_builder.append_non_null();
+
+ let len = array.value_size(row_index);
+
+ // Empty arrays always return an empty array.
+ if len == O::usize_as(0) {
+ offsets.push(current_offset);
+ sizes.push(O::usize_as(0));
+ continue;
+ }
+
+ let slice_plan = compute_slice_plan::<O>(
+ len,
+ from_array.value(row_index),
+ to_array.value(row_index),
+ stride.map(|s| s.value(row_index)),
+ )?;
+
+ let start = array.value_offset(row_index);
+ match slice_plan {
+ SlicePlan::Empty => {
+ offsets.push(current_offset);
+ sizes.push(O::usize_as(0));
+ }
+ SlicePlan::Contiguous {
+ start: rel_start,
+ len: slice_len,
+ } => {
+ let start_index = (start + rel_start).to_usize().unwrap();
+ let end_index = (start + rel_start +
slice_len).to_usize().unwrap();
+ mutable.extend(0, start_index, end_index);
+ offsets.push(current_offset);
+ sizes.push(slice_len);
+ current_offset += slice_len;
+ }
+ SlicePlan::Indices(indices) => {
+ let count = indices.len();
+ for rel_index in indices {
+ let absolute_index = (start +
rel_index).to_usize().unwrap();
+ mutable.extend(0, absolute_index, absolute_index + 1);
+ }
+ let length = O::usize_as(count);
+ offsets.push(current_offset);
+ sizes.push(length);
+ current_offset += length;
+ }
+ }
+ }
+
+ let data = mutable.freeze();
+ let field = match array.data_type() {
+ ListView(field) | LargeListView(field) => Arc::clone(field),
+ other => {
+ return Err(internal_datafusion_err!(
+ "array_slice got unexpected data type: {}",
+ other
+ ));
+ }
+ };
Review Comment:
Nice catch!
--
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]