Skip to content

Commit 27db82f

Browse files
authored
Improve ScalarUDFImpl docs (#14248)
* Improve `ScalarUDFImpl` docs * fix clippy
1 parent 0228bee commit 27db82f

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

datafusion/expr/src/udf.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ impl ScalarUDF {
193193
self.inner.return_type_from_exprs(args, schema, arg_types)
194194
}
195195

196+
/// Return the datatype this function returns given the input argument types.
197+
///
198+
/// See [`ScalarUDFImpl::return_type_from_args`] for more details.
196199
pub fn return_type_from_args(&self, args: ReturnTypeArgs) -> Result<ReturnInfo> {
197200
self.inner.return_type_from_args(args)
198201
}
@@ -433,7 +436,6 @@ impl ReturnInfo {
433436
/// # use datafusion_expr::{col, ColumnarValue, Documentation, ScalarFunctionArgs, Signature, Volatility};
434437
/// # use datafusion_expr::{ScalarUDFImpl, ScalarUDF};
435438
/// # use datafusion_expr::scalar_doc_sections::DOC_SECTION_MATH;
436-
///
437439
/// /// This struct for a simple UDF that adds one to an int32
438440
/// #[derive(Debug)]
439441
/// struct AddOne {
@@ -494,7 +496,12 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
494496
/// Returns this function's name
495497
fn name(&self) -> &str;
496498

497-
/// Returns the user-defined display name of the UDF given the arguments
499+
/// Returns the user-defined display name of function, given the arguments
500+
///
501+
/// This can be used to customize the output column name generated by this
502+
/// function.
503+
///
504+
/// Defaults to `name(args[0], args[1], ...)`
498505
fn display_name(&self, args: &[Expr]) -> Result<String> {
499506
let names: Vec<String> = args.iter().map(ToString::to_string).collect();
500507
// TODO: join with ", " to standardize the formatting of Vec<Expr>, <https://github.com/apache/datafusion/issues/10364>
@@ -522,7 +529,7 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
522529
/// # Notes
523530
///
524531
/// If you provide an implementation for [`Self::return_type_from_args`],
525-
/// DataFusion will not call `return_type` (this function). In this case it
532+
/// DataFusion will not call `return_type` (this function). In such cases
526533
/// is recommended to return [`DataFusionError::Internal`].
527534
///
528535
/// [`DataFusionError::Internal`]: datafusion_common::DataFusionError::Internal
@@ -538,18 +545,24 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
538545
self.return_type(arg_types)
539546
}
540547

541-
/// What [`DataType`] will be returned by this function, given the
542-
/// arguments?
543-
///
544-
/// Note most UDFs should implement [`Self::return_type`] and not this
545-
/// function. The output type for most functions only depends on the types
546-
/// of their inputs (e.g. `sqrt(f32)` is always `f32`).
548+
/// What type will be returned by this function, given the arguments?
547549
///
548550
/// By default, this function calls [`Self::return_type`] with the
549551
/// types of each argument.
550552
///
551-
/// This method can be overridden for functions that return different
552-
/// *types* based on the *values* of their arguments.
553+
/// # Notes
554+
///
555+
/// Most UDFs should implement [`Self::return_type`] and not this
556+
/// function as the output type for most functions only depends on the types
557+
/// of their inputs (e.g. `sqrt(f32)` is always `f32`).
558+
///
559+
/// This function can be used for more advanced cases such as:
560+
///
561+
/// 1. specifying nullability
562+
/// 2. return types based on the **values** of the arguments (rather than
563+
/// their **types**.
564+
///
565+
/// # Output Type based on Values
553566
///
554567
/// For example, the following two function calls get the same argument
555568
/// types (something and a `Utf8` string) but return different types based
@@ -558,9 +571,9 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
558571
/// * `arrow_cast(x, 'Int16')` --> `Int16`
559572
/// * `arrow_cast(x, 'Float32')` --> `Float32`
560573
///
561-
/// # Notes:
574+
/// # Requirements
562575
///
563-
/// This function must consistently return the same type for the same
576+
/// This function **must** consistently return the same type for the same
564577
/// logical input even if the input is simplified (e.g. it must return the same
565578
/// value for `('foo' | 'bar')` as it does for ('foobar').
566579
fn return_type_from_args(&self, args: ReturnTypeArgs) -> Result<ReturnInfo> {

0 commit comments

Comments
 (0)