comphead commented on code in PR #8569:
URL: https://github.com/apache/arrow-datafusion/pull/8569#discussion_r1430679163
##########
datafusion/physical-expr/src/array_expressions.rs:
##########
@@ -673,18 +673,72 @@ where
)?))
}
+/// If from_front is true, it is array_pop_front, otherwise array_pop_back
+fn general_pop_list<O: OffsetSizeTrait>(
+ array: &GenericListArray<O>,
+ from_front: bool,
+) -> Result<ArrayRef>
+where
+ i64: TryInto<O>,
+{
+ let (from_array, to_array) = if from_front {
+ let from_array = Int64Array::from(vec![2; array.len()]);
+ let to_array = Int64Array::from(
+ array
+ .iter()
+ .map(|arr| arr.map_or(0, |arr| arr.len() as i64))
+ .collect::<Vec<i64>>(),
+ );
+ (from_array, to_array)
+ } else {
+ let from_array = Int64Array::from(vec![1; array.len()]);
+ let to_array = Int64Array::from(
+ array
+ .iter()
+ .map(|arr| arr.map_or(0, |arr| arr.len() as i64 - 1))
+ .collect::<Vec<i64>>(),
+ );
+ (from_array, to_array)
+ };
+ general_array_slice::<O>(array, &from_array, &to_array)
+}
+
+/// array_pop_front SQL function
+pub fn array_pop_front(args: &[ArrayRef]) -> Result<ArrayRef> {
+ let array_data_type = args[0].data_type();
+ match array_data_type {
+ DataType::List(_) => {
+ let array = as_list_array(&args[0])?;
+ general_pop_list::<i32>(array, true)
+ }
+ DataType::LargeList(_) => {
+ let array = as_large_list_array(&args[0])?;
+ general_pop_list::<i64>(array, true)
+ }
+ _ => exec_err!(
+ "array_pop_front does not support type: {:?}",
+ array_data_type
+ ),
+ }
+}
+
/// array_pop_back SQL function
pub fn array_pop_back(args: &[ArrayRef]) -> Result<ArrayRef> {
- let list_array = as_list_array(&args[0])?;
- let from_array = Int64Array::from(vec![1; list_array.len()]);
- let to_array = Int64Array::from(
- list_array
- .iter()
- .map(|arr| arr.map_or(0, |arr| arr.len() as i64 - 1))
- .collect::<Vec<i64>>(),
- );
- let args = vec![args[0].clone(), Arc::new(from_array), Arc::new(to_array)];
- array_slice(args.as_slice())
+ let array_data_type = args[0].data_type();
+ match array_data_type {
+ DataType::List(_) => {
+ let array = as_list_array(&args[0])?;
+ general_pop_list::<i32>(array, false)
+ }
+ DataType::LargeList(_) => {
+ let array = as_large_list_array(&args[0])?;
+ general_pop_list::<i64>(array, false)
+ }
+ _ => exec_err!(
Review Comment:
that is true, i missed that part. thanks @Weijun-H
what about output error types? in array positions you return `not_impl` and
in this PR its `exec_err`, but likely the conditions looks same and the error
type should also be the same?
--
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]