2010YOUY01 commented on code in PR #7978:
URL: https://github.com/apache/arrow-datafusion/pull/7978#discussion_r1378392801
##########
datafusion/expr/src/udf.rs:
##########
@@ -17,12 +17,67 @@
//! Udf module contains foundational types that are used to represent UDFs in
DataFusion.
-use crate::{Expr, ReturnTypeFunction, ScalarFunctionImplementation, Signature};
+use crate::{
+ ColumnarValue, Expr, FuncMonotonicity, ReturnTypeFunction,
+ ScalarFunctionImplementation, Signature, TypeSignature, Volatility,
+};
+use arrow::array::ArrayRef;
+use arrow::datatypes::DataType;
+use datafusion_common::{internal_err, DataFusionError, Result};
+use std::any::Any;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::sync::Arc;
+// TODO(PR): add doc comments
+pub trait ScalarFunctionDef: Any + Sync + Send + std::fmt::Debug {
+ /// Return as [`Any`] so that it can be
+ /// downcast to a specific implementation.
+ fn as_any(&self) -> &dyn Any;
+
+ // May return 1 or more name as aliasing
+ fn name(&self) -> &[&str];
+
+ fn input_type(&self) -> TypeSignature;
+
+ fn return_type(&self) -> FunctionReturnType;
+
+ fn execute(&self, _args: &[ArrayRef]) -> Result<ArrayRef> {
+ internal_err!("This method should be implemented if
`supports_execute_raw()` returns `false`")
+ }
+
+ fn volatility(&self) -> Volatility;
+
+ fn monotonicity(&self) -> Option<FuncMonotonicity>;
+
+ // ===============================
+ // OPTIONAL METHODS START BELOW
+ // ===============================
+
+ /// `execute()` and `execute_raw()` are two possible alternative for
function definition:
+ /// If returns `false`, `execute()` will be used for execution;
+ /// If returns `true`, `execute_raw()` will be called.
+ fn use_execute_raw_instead(&self) -> bool {
+ false
+ }
+
+ /// An alternative function defination than `execute()`
+ fn execute_raw(&self, _args: &[ColumnarValue]) -> Result<ColumnarValue> {
+ internal_err!("This method should be implemented if
`supports_execute_raw()` returns `true`")
+ }
+}
+
+/// Defines the return type behavior of a function.
+pub enum FunctionReturnType {
Review Comment:
The returning `enum FunctionReturnType` approach's advantage is being able
to extend to solve https://github.com/apache/arrow-datafusion/discussions/7657,
otherwise we have to extend the function interface to address that issue
(though I'm not sure if that requirement is common, should we consider that
case?)
And its limitation is harder to use when the return type is actually some
complex lambda, but only for very few array functions.
--
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]