vincev commented on code in PR #6903:
URL: https://github.com/apache/arrow-datafusion/pull/6903#discussion_r1262623278


##########
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:
   I see, if you prefer I can try and use this approach, please let me know. 



-- 
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]

Reply via email to