@@ -193,6 +193,9 @@ impl ScalarUDF {
193
193
self . inner . return_type_from_exprs ( args, schema, arg_types)
194
194
}
195
195
196
+ /// Return the datatype this function returns given the input argument types.
197
+ ///
198
+ /// See [`ScalarUDFImpl::return_type_from_args`] for more details.
196
199
pub fn return_type_from_args ( & self , args : ReturnTypeArgs ) -> Result < ReturnInfo > {
197
200
self . inner . return_type_from_args ( args)
198
201
}
@@ -433,7 +436,6 @@ impl ReturnInfo {
433
436
/// # use datafusion_expr::{col, ColumnarValue, Documentation, ScalarFunctionArgs, Signature, Volatility};
434
437
/// # use datafusion_expr::{ScalarUDFImpl, ScalarUDF};
435
438
/// # use datafusion_expr::scalar_doc_sections::DOC_SECTION_MATH;
436
- ///
437
439
/// /// This struct for a simple UDF that adds one to an int32
438
440
/// #[derive(Debug)]
439
441
/// struct AddOne {
@@ -494,7 +496,12 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
494
496
/// Returns this function's name
495
497
fn name ( & self ) -> & str ;
496
498
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], ...)`
498
505
fn display_name ( & self , args : & [ Expr ] ) -> Result < String > {
499
506
let names: Vec < String > = args. iter ( ) . map ( ToString :: to_string) . collect ( ) ;
500
507
// 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 {
522
529
/// # Notes
523
530
///
524
531
/// 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
526
533
/// is recommended to return [`DataFusionError::Internal`].
527
534
///
528
535
/// [`DataFusionError::Internal`]: datafusion_common::DataFusionError::Internal
@@ -538,18 +545,24 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
538
545
self . return_type ( arg_types)
539
546
}
540
547
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?
547
549
///
548
550
/// By default, this function calls [`Self::return_type`] with the
549
551
/// types of each argument.
550
552
///
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
553
566
///
554
567
/// For example, the following two function calls get the same argument
555
568
/// types (something and a `Utf8` string) but return different types based
@@ -558,9 +571,9 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
558
571
/// * `arrow_cast(x, 'Int16')` --> `Int16`
559
572
/// * `arrow_cast(x, 'Float32')` --> `Float32`
560
573
///
561
- /// # Notes:
574
+ /// # Requirements
562
575
///
563
- /// This function must consistently return the same type for the same
576
+ /// This function ** must** consistently return the same type for the same
564
577
/// logical input even if the input is simplified (e.g. it must return the same
565
578
/// value for `('foo' | 'bar')` as it does for ('foobar').
566
579
fn return_type_from_args ( & self , args : ReturnTypeArgs ) -> Result < ReturnInfo > {
0 commit comments