@@ -193,6 +193,10 @@ 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.
199
+
196
200
pub fn return_type_from_args ( & self , args : ReturnTypeArgs ) -> Result < ReturnInfo > {
197
201
self . inner . return_type_from_args ( args)
198
202
}
@@ -433,7 +437,6 @@ impl ReturnInfo {
433
437
/// # use datafusion_expr::{col, ColumnarValue, Documentation, ScalarFunctionArgs, Signature, Volatility};
434
438
/// # use datafusion_expr::{ScalarUDFImpl, ScalarUDF};
435
439
/// # use datafusion_expr::scalar_doc_sections::DOC_SECTION_MATH;
436
- ///
437
440
/// /// This struct for a simple UDF that adds one to an int32
438
441
/// #[derive(Debug)]
439
442
/// struct AddOne {
@@ -494,7 +497,12 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
494
497
/// Returns this function's name
495
498
fn name ( & self ) -> & str ;
496
499
497
- /// Returns the user-defined display name of the UDF given the arguments
500
+ /// Returns the user-defined display name of function, given the arguments
501
+ ///
502
+ /// This can be used to customize the output column name generated by this
503
+ /// function.
504
+ ///
505
+ /// Defaults to `name(args[0], args[1], ...)`
498
506
fn display_name ( & self , args : & [ Expr ] ) -> Result < String > {
499
507
let names: Vec < String > = args. iter ( ) . map ( ToString :: to_string) . collect ( ) ;
500
508
// TODO: join with ", " to standardize the formatting of Vec<Expr>, <https://github.com/apache/datafusion/issues/10364>
@@ -522,7 +530,7 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
522
530
/// # Notes
523
531
///
524
532
/// If you provide an implementation for [`Self::return_type_from_args`],
525
- /// DataFusion will not call `return_type` (this function). In this case it
533
+ /// DataFusion will not call `return_type` (this function). In such cases
526
534
/// is recommended to return [`DataFusionError::Internal`].
527
535
///
528
536
/// [`DataFusionError::Internal`]: datafusion_common::DataFusionError::Internal
@@ -538,18 +546,24 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
538
546
self . return_type ( arg_types)
539
547
}
540
548
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`).
549
+ /// What type will be returned by this function, given the arguments?
547
550
///
548
551
/// By default, this function calls [`Self::return_type`] with the
549
552
/// types of each argument.
550
553
///
551
- /// This method can be overridden for functions that return different
552
- /// *types* based on the *values* of their arguments.
554
+ /// # Notes
555
+ ///
556
+ /// Most UDFs should implement [`Self::return_type`] and not this
557
+ /// function as the output type for most functions only depends on the types
558
+ /// of their inputs (e.g. `sqrt(f32)` is always `f32`).
559
+ ///
560
+ /// This function can be used for more advanced cases such as:
561
+ ///
562
+ /// 1. specifying nullability
563
+ /// 2. return types based on the **values** of the arguments (rather than
564
+ /// their **types**.
565
+ ///
566
+ /// # Output Type based on Values
553
567
///
554
568
/// For example, the following two function calls get the same argument
555
569
/// types (something and a `Utf8` string) but return different types based
@@ -558,9 +572,9 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
558
572
/// * `arrow_cast(x, 'Int16')` --> `Int16`
559
573
/// * `arrow_cast(x, 'Float32')` --> `Float32`
560
574
///
561
- /// # Notes:
575
+ /// # Requirements
562
576
///
563
- /// This function must consistently return the same type for the same
577
+ /// This function ** must** consistently return the same type for the same
564
578
/// logical input even if the input is simplified (e.g. it must return the same
565
579
/// value for `('foo' | 'bar')` as it does for ('foobar').
566
580
fn return_type_from_args ( & self , args : ReturnTypeArgs ) -> Result < ReturnInfo > {
0 commit comments