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


##########
datafusion/expr/src/built_in_function.rs:
##########
@@ -710,30 +704,6 @@ impl BuiltinScalarFunction {
             BuiltinScalarFunction::Digest => {
                 utf8_or_binary_to_binary_type(&input_expr_types[0], "digest")
             }
-            BuiltinScalarFunction::Encode => Ok(match input_expr_types[0] {

Review Comment:
   This metadata information about the functions is now moved into the 
`functions/encoding/meta` module



##########
datafusion/expr/src/udf.rs:
##########
@@ -84,9 +94,60 @@ impl ScalarUDF {
         }
     }
 
+    /// Create a new `ScalarUDF` from a `FuncImpl`
+    pub fn new_from_impl(
+        arc_fun: Arc<dyn FunctionImplementation + Send + Sync>,
+    ) -> ScalarUDF {
+        let captured_self = arc_fun.clone();
+        let return_type: ReturnTypeFunction = Arc::new(move |arg_types| {
+            let return_type = captured_self.return_type(arg_types)?;
+            Ok(Arc::new(return_type))
+        });
+
+        let captured_self = arc_fun.clone();
+        let func: ScalarFunctionImplementation =
+            Arc::new(move |args| captured_self.invoke(args));
+
+        ScalarUDF::new(arc_fun.name(), arc_fun.signature(), &return_type, 
&func)
+    }
+
     /// creates a logical expression with a call of the UDF
     /// This utility allows using the UDF without requiring access to the 
registry.
     pub fn call(&self, args: Vec<Expr>) -> Expr {
         Expr::ScalarUDF(crate::expr::ScalarUDF::new(Arc::new(self.clone()), 
args))
     }
+
+    /// Returns this function's name
+    pub fn name(&self) -> &str {
+        &self.name
+    }
+    /// Returns this function's signature
+    pub fn signature(&self) -> &Signature {
+        &self.signature
+    }
+    /// return the return type of this function given the types of the 
arguments
+    pub fn return_type(&self, args: &[DataType]) -> Result<DataType> {
+        // Old API returns an Arc of the datatype for some reason
+        let res = (self.return_type)(args)?;
+        Ok(res.as_ref().clone())
+    }
+    /// return the implementation of this function
+    pub fn fun(&self) -> &ScalarFunctionImplementation {
+        &self.fun
+    }
+}
+
+/// Convenience trait for implementing ScalarUDF. See 
[`ScalarUDF::from_impl()`]

Review Comment:
   This echo's the trait that @2010YOUY01  proposed in ) 
https://github.com/apache/arrow-datafusion/pull/7752, but does so in a way that 
is backwards compatible (makes a ScalarUDF out of the trait, to retain 
backwards compatibly)



##########
datafusion/functions/src/encoding/mod.rs:
##########
@@ -0,0 +1,43 @@
+// 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.
+
+#[cfg(feature = "encoding_expressions")]
+mod inner;
+#[cfg(feature = "encoding_expressions")]
+mod meta;
+
+use crate::utils::insert;
+use datafusion_expr::ScalarUDF;
+use std::collections::HashMap;
+use std::sync::Arc;
+
+/// Registers the `encode` and `decode` functions with the function registry

Review Comment:
   Here is the conditional registration of these functions based on feature 
flag -- there are probably nicer ways to do this but I don't think it is any 
worse than the current solution.



##########
datafusion/expr/src/expr_fn.rs:
##########
@@ -737,8 +737,9 @@ scalar_expr!(
     "converts the Unicode code point to a UTF8 character"
 );
 scalar_expr!(Digest, digest, input algorithm, "compute the binary hash of 
`input`, using the `algorithm`");
-scalar_expr!(Encode, encode, input encoding, "encode the `input`, using the 
`encoding`. encoding can be base64 or hex");
-scalar_expr!(Decode, decode, input encoding, "decode the`input`, using the 
`encoding`. encoding can be base64 or hex");
+// TODO make a variant of Expr that can invoke a function by name

Review Comment:
   Supporting these functions will take some additional work, but I think it is 
doable



##########
datafusion/functions/src/lib.rs:
##########
@@ -0,0 +1,30 @@
+// 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.
+
+//! Several packages of built in functions for DataFusion
+
+use datafusion_expr::ScalarUDF;
+use std::collections::HashMap;
+use std::sync::Arc;
+
+pub mod encoding;
+pub mod utils;
+
+/// Registers all "built in" functions from this crate with the provided 
registry
+pub fn register_all(registry: &mut HashMap<String, Arc<ScalarUDF>>) {
+    encoding::register(registry);

Review Comment:
   I envision extending this list with other packages over time. 



##########
datafusion/functions/src/encoding/meta.rs:
##########
@@ -0,0 +1,108 @@
+// 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.
+
+//! Metadata information for "encode" and "decode" functions
+use datafusion_common::arrow::datatypes::DataType;
+use datafusion_common::{plan_err, DataFusionError, Result};
+use datafusion_expr::TypeSignature::*;
+use datafusion_expr::{ColumnarValue, FunctionImplementation, Signature, 
Volatility};
+use std::sync::OnceLock;
+use DataType::*;
+
+pub(super) struct EncodeFunc {}
+
+static ENCODE_SIGNATURE: OnceLock<Signature> = OnceLock::new();

Review Comment:
   Here is what `encode` and `decode` look like using the `ScalarUDF` API -- I 
think they are much clearer when all this type information is in one place 
(though I still kept it separate from the implementation to show the 
implementation did not change at all)



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