jayzhan211 commented on code in PR #8744:
URL: https://github.com/apache/arrow-datafusion/pull/8744#discussion_r1446962462


##########
datafusion/physical-expr/src/array_expressions.rs:
##########
@@ -2611,6 +2618,95 @@ pub fn array_distinct(args: &[ArrayRef]) -> 
Result<ArrayRef> {
     }
 }
 
+/// array_resize SQL function
+pub fn array_resize(arg: &[ArrayRef]) -> Result<ArrayRef> {
+    if arg.len() < 2 || arg.len() > 3 {
+        return exec_err!("array_resize needs two or three arguments");
+    }
+
+    let new_len = as_int64_array(&arg[1])?;
+    let new_element = if arg.len() == 3 {
+        Some(arg[2].clone())
+    } else {
+        None
+    };
+
+    match &arg[0].data_type() {
+        DataType::List(field) => {
+            let array = as_list_array(&arg[0])?;
+            general_list_resize::<i32>(array, new_len, field, new_element)
+        }
+        DataType::LargeList(field) => {
+            let array = as_large_list_array(&arg[0])?;
+            general_list_resize::<i64>(array, new_len, field, new_element)
+        }
+        array_type => exec_err!("array_resize does not support type 
'{array_type:?}'."),
+    }
+}
+
+/// array_resize keep the original array and append the default element to the 
end
+fn general_list_resize<O: OffsetSizeTrait>(
+    array: &GenericListArray<O>,
+    count_array: &Int64Array,
+    field: &FieldRef,
+    default_element: Option<ArrayRef>,
+) -> Result<ArrayRef>
+where
+    O: TryInto<i64>,
+{
+    let mut default_element_array = vec![];
+
+    let data_type = array.value_type();
+    let default_element = if let Some(default_element) = default_element {
+        default_element
+    } else {
+        let null_scalar = ScalarValue::try_from(&data_type)?;
+        null_scalar.to_array_of_size(1)?
+    };
+
+    // create a mutable array to store the original data
+    let values = array.values();
+    let original_data = values.to_data();
+    let capacity = Capacities::Array(original_data.len());
+    let mut offsets = vec![O::usize_as(0)];
+    let mut mutable =
+        MutableArrayData::with_capacities(vec![&original_data], false, 
capacity);
+
+    for (row_index, offset_window) in array.offsets().windows(2).enumerate() {
+        let count = count_array.value(row_index).to_usize().ok_or_else(|| {
+            exec_datafusion_err!("array_resize: failed to convert size to 
usize")
+        })?;
+        let count = O::usize_as(count);
+        let start = offset_window[0];
+        let end = if start + count > offset_window[1] {
+            let value = (start + count - 
offset_window[1]).try_into().map_err(|_| {
+                exec_datafusion_err!("array_resize: failed to convert size to 
i64")
+            })?;
+            default_element_array.push(Some(value));
+            offset_window[1]
+        } else {
+            default_element_array.push(None);
+            start + count
+        };
+        mutable.extend(0, (start).to_usize().unwrap(), 
(end).to_usize().unwrap());
+        offsets.push(offsets[row_index] + end - start);
+    }
+
+    let default_element_array = 
Arc::new(Int64Array::from(default_element_array));
+    let default_element_array =
+        general_repeat::<O>(&default_element, &default_element_array)?;
+
+    let data = mutable.freeze();
+    let original_part = Arc::new(GenericListArray::<O>::try_new(
+        field.clone(),
+        OffsetBuffer::<O>::new(offsets.into()),
+        arrow_array::make_array(data),
+        None,
+    )?);
+
+    array_concat(&[original_part, default_element_array])

Review Comment:
   We can avoid array_concat if we construct with MutableArray, 
   
   ```rust
   let mut mutable =
           MutableArrayData::with_capacities(vec![&original_data, 
default_value_data], false, capacity);
   ```
   Then, extend(1, ...) for default value



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