2010YOUY01 commented on code in PR #8046: URL: https://github.com/apache/arrow-datafusion/pull/8046#discussion_r1382627195
########## 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>>) { Review Comment: I think we should support registering a single function here, there might be a use case that the user wants to override only one function from a function package (possibly by changing this interface to something like ``` rust pub fn register_all() { register_package(encoding::all_functions()); register_function(my_encoding::decode()); // override a method in default function package } ``` ########## 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 + } Review Comment: Is that the case this set of interfaces is internal-faced for execution, we might extend it during separating function packages? And `trait FunctionImplementation` is the user-faced API for defining functions in separate crates -- 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]
