gstvg commented on code in PR #21679:
URL: https://github.com/apache/datafusion/pull/21679#discussion_r3152307652


##########
datafusion/functions-nested/src/array_transform.rs:
##########
@@ -0,0 +1,457 @@
+// 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.
+
+//! [`HigherOrderUDF`] definitions for array_transform function.
+
+use arrow::{
+    array::{
+        Array, ArrayRef, AsArray, FixedSizeListArray, LargeListArray, 
ListArray,
+        new_null_array,
+    },
+    datatypes::{DataType, Field, FieldRef},
+};
+use datafusion_common::{
+    Result, exec_err, plan_err,
+    utils::{adjust_offsets_for_slice, list_values, take_function_args},
+};
+use datafusion_expr::{
+    ColumnarValue, Documentation, HigherOrderFunctionArgs, 
HigherOrderReturnFieldArgs,
+    HigherOrderSignature, HigherOrderUDF, ValueOrLambda, Volatility,
+};
+use datafusion_macros::user_doc;
+use std::{fmt::Debug, sync::Arc};
+
+make_udhof_expr_and_func!(
+    ArrayTransform,
+    array_transform,
+    array lambda,
+    "transforms the values of an array",
+    array_transform_udhof
+);
+
+#[user_doc(
+    doc_section(label = "Array Functions"),
+    description = "transforms the values of an array",
+    syntax_example = "array_transform(array, x -> x*2)",
+    sql_example = r#"```sql
+> select array_transform([1, 2, 3, 4, 5], x -> x*2);
++-------------------------------------------+
+| array_transform([1, 2, 3, 4, 5], x -> x*2)       |
++-------------------------------------------+
+| [2, 4, 6, 8, 10]                          |
++-------------------------------------------+
+```"#,
+    argument(
+        name = "array",
+        description = "Array expression. Can be a constant, column, or 
function, and any combination of array operators."
+    ),
+    argument(name = "lambda", description = "Lambda")
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct ArrayTransform {
+    signature: HigherOrderSignature,
+    aliases: Vec<String>,
+}
+
+impl Default for ArrayTransform {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl ArrayTransform {
+    pub fn new() -> Self {
+        Self {
+            signature: 
HigherOrderSignature::user_defined(Volatility::Immutable),
+            aliases: vec![String::from("list_transform")],
+        }
+    }
+}
+
+impl HigherOrderUDF for ArrayTransform {
+    fn name(&self) -> &str {
+        "array_transform"
+    }
+
+    fn aliases(&self) -> &[String] {
+        &self.aliases
+    }
+
+    fn signature(&self) -> &HigherOrderSignature {
+        &self.signature
+    }
+
+    fn coerce_value_types(&self, arg_types: &[DataType]) -> 
Result<Vec<DataType>> {
+        let list = if arg_types.len() == 1 {
+            &arg_types[0]
+        } else {
+            return plan_err!(
+                "{} function requires 1 value arguments, got {}",
+                self.name(),
+                arg_types.len()
+            );
+        };

Review Comment:
   There's indeed a lot improvements to be made here. I started with the code 
below in the past but stopped due to the added code size. Maybe we could left 
it for another PR and add it to #21172, WDYT?
   
   ```rust
   enum HigherOrderTypeSignature {
       /// One or more arguments being either of arbitrary types or being a 
lambda.
       Any(usize),
       /// The acceptable signature and coercions rules are special for this
       /// function.
       ///
       /// If this signature is specified,
       /// DataFusion will call [`HigherOrderUDF::coerce_types`] to prepare 
argument types.
       UserDefined,
       /// One or more arguments being either of arbitrary types or being a 
lambda.
       VariadicAny,
       /// One or more ordered arguments that are a list with a specified class 
or a lambda.
       ExactList(Vec<ValueOrLambda<ListClass>>),
       /// Matches exactly one of a list of [`TypeSignature`]s.
       ///
       /// Coercion is attempted to match the signatures in order, and stops 
after
       /// the first success, if any.
       OneOf(Vec<LambdaTypeSignature>),
   }
   
   enum ListClass {
       /// Only List
       List,
       /// Only LargeList
       LargeList,
       /// Only FixedSizeList
       FixedSizeList
       /// Only ListView
       ListView,
       /// Only LargeListView
       LargeListView,
       /// Either List, LargeList, FixedSizeList, ListView or LargeListView
       Any,
       /// Either List, LargeList, or FixedSizeList
       Contiguos,
       /// Either List or LargeList
       VariableSizedContiguos,
       /// Either ListView or LargeListView
       View,
   }
   ```
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to