alamb commented on code in PR #6903:
URL: https://github.com/apache/arrow-datafusion/pull/6903#discussion_r1262610832
##########
datafusion/core/src/physical_plan/unnest.rs:
##########
@@ -264,41 +267,195 @@ fn unnest_batch<T>(
where
T: ArrayAccessor<Item = ArrayRef>,
{
- let mut batches = Vec::new();
- let mut num_rows = 0;
-
- for row in 0..batch.num_rows() {
- let arrays = batch
- .columns()
- .iter()
- .enumerate()
- .map(|(col_idx, arr)| {
- if col_idx == column.index() {
- // Unnest the value at the given row.
- if list_array.value(row).is_empty() {
- // If nested array is empty add an array with 1 null.
- Ok(new_null_array(list_array.value(row).data_type(),
1))
- } else {
- Ok(list_array.value(row))
- }
- } else {
- // Number of elements to duplicate, use max(1) to handle
null.
- let nested_len = list_array.value(row).len().max(1);
- // Duplicate rows for each value in the nested array.
- if arr.is_null(row) {
- Ok(new_null_array(arr.data_type(), nested_len))
- } else {
- let scalar = ScalarValue::try_from_array(arr, row)?;
- Ok(scalar.to_array_of_size(nested_len))
- }
- }
- })
- .collect::<Result<Vec<_>>>()?;
+ // Create an array with the unnested values of the list array, given the
list
+ // array:
+ //
+ // [1], null, [2, 3, 4], null, [5, 6]
+ //
+ // the result array is:
+ //
+ // 1, null, 2, 3, 4, null, 5, 6
+ //
+ let unnested_array = unnest_array(list_array)?;
+
+ // Create an array with the lengths of each list value in the nested array.
+ // Given the nested array:
+ //
+ // [1], null, [2, 3, 4], null, [5, 6]
+ //
+ // the result array is:
+ //
+ // 1, null, 3, null, 2
+ //
+ // Depending on the list type the result may be Int32Array or Int64Array.
+ let list_lengths = kernels::length::length(list_array)?;
+
+ // Create the indices for the take kernel and then use those indices to
create
+ // the unnested record batch.
+ match list_lengths.data_type() {
+ DataType::Int32 => {
+ let list_lengths = as_primitive_array::<Int32Type>(&list_lengths)?;
+ let indices = create_take_indices(list_lengths,
unnested_array.len());
+ batch_from_indices(batch, schema, column.index(), &unnested_array,
&indices)
Review Comment:
Thanks @vincev -- what I was confused about is that if I look at this
description:
```
// Create an array with the unnested values of the list array, given the
list
// array:
//
// [1], null, [2, 3, 4], null, [5, 6]
//
// the result array is:
//
// 1, null, 2, 3, 4, null, 5, 6
//
let unnested_array = unnest_array(list_array)?;
``
This looks very much the same to me as calling `list_array.vaules()` to get
access to the underlying values:
https://docs.rs/arrow/latest/arrow/array/struct.GenericListArray.html#method.values
In this case the values array would be more like
```
[1, 2, 3, 4, 5, 6]
```
And the offsets of the list array would be would be like (I think):
```
[0, 1, 1, 3, 3, 6]
```
With a null mask showing the second and fourth element are null
So I was thinking you could calculate the take indices directly from the
offsets / nulls without having to copy all the values out of the underlying
array
--
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]