jayzhan211 commented on code in PR #8744: URL: https://github.com/apache/arrow-datafusion/pull/8744#discussion_r1446966314
########## 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)? Review Comment: Should this be `to_array_of_size(row_len)`? -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org