You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/06/01 11:16:58 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6384: feat: New functions and operations for working with arrays

alamb commented on code in PR #6384:
URL: https://github.com/apache/arrow-datafusion/pull/6384#discussion_r1212990411


##########
datafusion/physical-expr/src/array_expressions.rs:
##########
@@ -77,11 +81,40 @@ fn array_array(args: &[ArrayRef]) -> Result<ArrayRef> {
     // do not accept 0 arguments.
     if args.is_empty() {
         return Err(DataFusionError::Internal(
-            "array requires at least one argument".to_string(),
+            "Array requires at least one argument".to_string(),
         ));
     }
 
-    let res = match args[0].data_type() {
+    let data_type = args[0].data_type();
+    let res = match data_type {
+        DataType::List(..) => {
+            let arrays =

Review Comment:
   @tustvold  can you offer some suggestions on using the arrow-rs API to build list arrays? Is this the best way to use that API?



##########
datafusion/core/tests/sqllogictests/test_files/array.slt:
##########
@@ -0,0 +1,206 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+
+#   http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+#############
+## Array expressions Tests
+#############
+
+# array scalar function #1

Review Comment:
   These are great @izveigor  -- thank you so much
   
   the only thing I recommend is adding some additional tests that have `null` in the lists. 



##########
datafusion/expr/src/function.rs:
##########
@@ -322,10 +404,21 @@ pub fn signature(fun: &BuiltinScalarFunction) -> Signature {
 
     // for now, the list is small, as we do not have many built-in functions.
     match fun {
-        BuiltinScalarFunction::MakeArray => Signature::variadic(
-            array_expressions::SUPPORTED_ARRAY_TYPES.to_vec(),
-            fun.volatility(),
-        ),
+        BuiltinScalarFunction::ArrayAppend => Signature::any(2, fun.volatility()),

Review Comment:
   🤔  Given the element type of the list is part of its `DataType`, you probably can'y use the existing Signatures
   
   Perhaps you could add a new `Signature::any_list` or something that would only check that the datatype matched `DataType::list` 🤔 



##########
datafusion/physical-expr/src/array_expressions.rs:
##########
@@ -115,3 +149,1560 @@ pub fn array(values: &[ColumnarValue]) -> Result<ColumnarValue> {
         .collect();
     Ok(ColumnarValue::Array(array_array(arrays.as_slice())?))
 }
+
+macro_rules! downcast_arg {
+    ($ARG:expr, $ARRAY_TYPE:ident) => {{
+        $ARG.as_any().downcast_ref::<$ARRAY_TYPE>().ok_or_else(|| {
+            DataFusionError::Internal(format!(
+                "could not cast to {}",
+                type_name::<$ARRAY_TYPE>()
+            ))
+        })?
+    }};
+}
+
+macro_rules! append {
+    ($ARRAY:expr, $ELEMENT:expr, $ARRAY_TYPE:ident) => {{
+        let child_array =
+            downcast_arg!(downcast_arg!($ARRAY, ListArray).values(), $ARRAY_TYPE);
+        let element = downcast_arg!($ELEMENT, $ARRAY_TYPE);
+        let concat = compute::concat(&[child_array, element])?;
+        let mut scalars = vec![];
+        for i in 0..concat.len() {
+            scalars.push(ColumnarValue::Scalar(ScalarValue::try_from_array(
+                &concat, i,
+            )?));
+        }
+        scalars
+    }};
+}
+
+/// Array_append SQL function
+pub fn array_append(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    if args.len() != 2 {
+        return Err(DataFusionError::Internal(format!(
+            "Array_append function requires two arguments, got {}",
+            args.len()
+        )));
+    }
+
+    let arr = match &args[0] {

Review Comment:
   I think you can use https://docs.rs/datafusion/latest/datafusion/physical_plan/enum.ColumnarValue.html#method.into_array here



##########
datafusion/physical-expr/src/array_expressions.rs:
##########
@@ -115,3 +149,1560 @@ pub fn array(values: &[ColumnarValue]) -> Result<ColumnarValue> {
         .collect();
     Ok(ColumnarValue::Array(array_array(arrays.as_slice())?))
 }
+
+macro_rules! downcast_arg {
+    ($ARG:expr, $ARRAY_TYPE:ident) => {{
+        $ARG.as_any().downcast_ref::<$ARRAY_TYPE>().ok_or_else(|| {
+            DataFusionError::Internal(format!(
+                "could not cast to {}",
+                type_name::<$ARRAY_TYPE>()
+            ))
+        })?
+    }};
+}
+
+macro_rules! append {
+    ($ARRAY:expr, $ELEMENT:expr, $ARRAY_TYPE:ident) => {{
+        let child_array =
+            downcast_arg!(downcast_arg!($ARRAY, ListArray).values(), $ARRAY_TYPE);
+        let element = downcast_arg!($ELEMENT, $ARRAY_TYPE);
+        let concat = compute::concat(&[child_array, element])?;
+        let mut scalars = vec![];
+        for i in 0..concat.len() {
+            scalars.push(ColumnarValue::Scalar(ScalarValue::try_from_array(
+                &concat, i,
+            )?));
+        }
+        scalars
+    }};
+}
+
+/// Array_append SQL function
+pub fn array_append(args: &[ColumnarValue]) -> Result<ColumnarValue> {

Review Comment:
   The difference is that if you take `ColumnarValue` we could specialize the kernels to do something faster with scalar (single) values rather than expanding them out to arrays (aka making copies).
   
   For the initial implementation I think converting them all to arrays is the best approach as it is simplest



##########
datafusion/physical-expr/src/functions.rs:
##########
@@ -2785,73 +2807,6 @@ mod tests {
         Ok(())
     }
 
-    fn generic_test_array(

Review Comment:
   I am not sure



##########
datafusion/physical-expr/src/array_expressions.rs:
##########
@@ -77,11 +81,40 @@ fn array_array(args: &[ArrayRef]) -> Result<ArrayRef> {
     // do not accept 0 arguments.
     if args.is_empty() {
         return Err(DataFusionError::Internal(
-            "array requires at least one argument".to_string(),
+            "Array requires at least one argument".to_string(),
         ));
     }
 
-    let res = match args[0].data_type() {
+    let data_type = args[0].data_type();
+    let res = match data_type {
+        DataType::List(..) => {

Review Comment:
   As FixedSizedList and List are different data types, if people have data that came from a Parquet file or something that is a FixedSizedList these functions likely wont work, 
   
   However, perhaps eventually we can add coercion rules to coerce (automatically cast) FixedSizeList to List 



##########
datafusion/expr/src/function.rs:
##########
@@ -322,10 +404,21 @@ pub fn signature(fun: &BuiltinScalarFunction) -> Signature {
 
     // for now, the list is small, as we do not have many built-in functions.
     match fun {
-        BuiltinScalarFunction::MakeArray => Signature::variadic(
-            array_expressions::SUPPORTED_ARRAY_TYPES.to_vec(),
-            fun.volatility(),
-        ),
+        BuiltinScalarFunction::ArrayAppend => Signature::any(2, fun.volatility()),

Review Comment:
   🤔  Given the element type of the list is part of its `DataType`, you probably can'y use the existing Signatures
   
   Perhaps you could add a new `Signature::any_list` or something that would only check that the datatype matched `DataType::list` 🤔 



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org