comphead commented on code in PR #8569:
URL: https://github.com/apache/arrow-datafusion/pull/8569#discussion_r1430561586


##########
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:
   Comparing to
   
https://github.com/apache/arrow-datafusion/pull/8571/files#diff-48cc9cf1bfdb0214a9f625b384d1c4fd5967a9da61e8f22a5dc1c4c5800563b4R1301
 
   no data types check, and the output error type mismatches, we may want to 
make array functions to have consistent codebase? 



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