vegarsti commented on code in PR #8735:
URL: https://github.com/apache/arrow-rs/pull/8735#discussion_r2477492144
##########
arrow-cast/src/cast/list.rs:
##########
@@ -160,6 +169,48 @@ pub(crate) fn cast_list_values<O: OffsetSizeTrait>(
)?))
}
+/// Helper function to cast the values in a list view to a list
+pub(crate) fn cast_list_view_values<O: OffsetSizeTrait>(
+ array: &dyn Array,
+ to: &FieldRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+ let list_view = array.as_list_view::<O>();
+ let list_view_offsets = list_view.offsets();
+ let sizes = list_view.sizes();
+ let source_values = list_view.values();
+
+ // Construct the indices and offsets for the new list array by iterating
over the list view subarrays
+ let mut indices = Vec::with_capacity(list_view.values().len());
+ let mut offsets = Vec::with_capacity(list_view.len() + 1);
+ // Add the offset for the first subarray
+ offsets.push(O::usize_as(0));
+ for i in 0..list_view.len() {
+ // For each subarray, add the indices of the values to take
+ let offset = list_view_offsets[i].as_usize();
+ let size = sizes[i].as_usize();
+ let end = offset + size;
+ for j in offset..end {
+ indices.push(j as i32);
+ }
+ // Add the offset for the next subarray
+ offsets.push(O::usize_as(indices.len()));
+ }
+
+ // Take the values from the source values using the indices, creating a
new array
+ let values = arrow_select::take::take(source_values,
&Int32Array::from(indices), None)?;
+
+ // Cast the values to the target data type
+ let values = cast_with_options(&values, to.data_type(), cast_options)?;
Review Comment:
Not sure this is needed since we are constructing `values` above, but this
is the only place where we can use `cast_options`, so it must be doing
something. When we're in this function, the inner data type will not be
different, because we are handling that outside (in `mod.rs`) by doing
```
if list_to.data_type() != list_from.data_type() {
// To transform inner type, can first cast to ListView
with new inner type.
let list_view_to = DataType::ListView(list_to.clone());
let array = cast_with_options(array, &list_view_to,
cast_options)?;
```
##########
arrow-cast/src/cast/list.rs:
##########
@@ -160,6 +169,48 @@ pub(crate) fn cast_list_values<O: OffsetSizeTrait>(
)?))
}
+/// Helper function to cast the values in a list view to a list
+pub(crate) fn cast_list_view_values<O: OffsetSizeTrait>(
+ array: &dyn Array,
+ to: &FieldRef,
+ cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+ let list_view = array.as_list_view::<O>();
+ let list_view_offsets = list_view.offsets();
+ let sizes = list_view.sizes();
+ let source_values = list_view.values();
+
+ // Construct the indices and offsets for the new list array by iterating
over the list view subarrays
+ let mut indices = Vec::with_capacity(list_view.values().len());
+ let mut offsets = Vec::with_capacity(list_view.len() + 1);
+ // Add the offset for the first subarray
+ offsets.push(O::usize_as(0));
+ for i in 0..list_view.len() {
+ // For each subarray, add the indices of the values to take
+ let offset = list_view_offsets[i].as_usize();
+ let size = sizes[i].as_usize();
+ let end = offset + size;
+ for j in offset..end {
+ indices.push(j as i32);
+ }
+ // Add the offset for the next subarray
+ offsets.push(O::usize_as(indices.len()));
+ }
+
+ // Take the values from the source values using the indices, creating a
new array
+ let values = arrow_select::take::take(source_values,
&Int32Array::from(indices), None)?;
+
+ // Cast the values to the target data type
+ let values = cast_with_options(&values, to.data_type(), cast_options)?;
Review Comment:
Not sure this is needed since we are constructing `values` above, but this
is the only place where we can use `cast_options`, so it must be doing
something. When we're in this function, the inner data type will not be
different, because we are handling that outside (in `mod.rs`) by doing
```
if list_to.data_type() != list_from.data_type() {
// To transform inner type, can first cast to ListView with new inner
type.
let list_view_to = DataType::ListView(list_to.clone());
let array = cast_with_options(array, &list_view_to, cast_options)?;
```
--
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]