jayzhan211 commented on code in PR #8744:
URL: https://github.com/apache/arrow-datafusion/pull/8744#discussion_r1447426362
##########
datafusion/physical-expr/src/array_expressions.rs:
##########
@@ -2611,6 +2618,98 @@ 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 data_type = array.value_type();
+
+ let values = array.values();
+ let original_data = values.to_data();
+
+ // create default element array
+ 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(original_data.len())?
+ };
+ let default_value_data = default_element.to_data();
+
+ // create a mutable array to store the original data
+ let capacity = Capacities::Array(original_data.len() +
default_value_data.len());
+ let mut offsets = vec![O::usize_as(0)];
+ let mut mutable = MutableArrayData::with_capacities(
+ vec![&original_data, &default_value_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")
Review Comment:
does type conversion more like internal_err?
`wasn't expected/anticipated by the implementation
and that is most likely a bug (the error message even encourages users
to open a bug report)`
unlike array.len() != number, which is `error` from user.
--
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]