Skip to content

Commit 828ee5a

Browse files
alambcomphead
andauthored
Docs: Add example of creating a field in return_field_from_args (#16039)
* Docs: Add example of creating a field in `return_field_from_args` * fmt * Update datafusion/expr/src/udf.rs Co-authored-by: Oleks V <[email protected]> * fmt --------- Co-authored-by: Oleks V <[email protected]>
1 parent aa8dfd9 commit 828ee5a

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

datafusion/expr/src/udf.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ pub struct ScalarFunctionArgs<'a, 'b> {
317317
pub struct ReturnFieldArgs<'a> {
318318
/// The data types of the arguments to the function
319319
pub arg_fields: &'a [Field],
320-
/// Is argument `i` to the function a scalar (constant)
320+
/// Is argument `i` to the function a scalar (constant)?
321321
///
322-
/// If argument `i` is not a scalar, it will be None
322+
/// If the argument `i` is not a scalar, it will be None
323323
///
324324
/// For example, if a function is called like `my_function(column_a, 5)`
325325
/// this field will be `[None, Some(ScalarValue::Int32(Some(5)))]`
@@ -451,16 +451,37 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
451451
///
452452
/// # Notes
453453
///
454-
/// Most UDFs should implement [`Self::return_type`] and not this
455-
/// function as the output type for most functions only depends on the types
456-
/// of their inputs (e.g. `sqrt(f32)` is always `f32`).
454+
/// For the majority of UDFs, implementing [`Self::return_type`] is sufficient,
455+
/// as the result type is typically a deterministic function of the input types
456+
/// (e.g., `sqrt(f32)` consistently yields `f32`). Implementing this method directly
457+
/// is generally unnecessary unless the return type depends on runtime values.
457458
///
458459
/// This function can be used for more advanced cases such as:
459460
///
460461
/// 1. specifying nullability
461462
/// 2. return types based on the **values** of the arguments (rather than
462463
/// their **types**.
463464
///
465+
/// # Example creating `Field`
466+
///
467+
/// Note the [`Field`] is ignored, except for structured types such as
468+
/// `DataType::Struct`.
469+
///
470+
/// ```rust
471+
/// # use arrow::datatypes::{DataType, Field};
472+
/// # use datafusion_common::Result;
473+
/// # use datafusion_expr::ReturnFieldArgs;
474+
/// # struct Example{};
475+
/// # impl Example {
476+
/// fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<Field> {
477+
/// // report output is only nullable if any one of the arguments are nullable
478+
/// let nullable = args.arg_fields.iter().any(|f| f.is_nullable());
479+
/// let field = Field::new("ignored_name", DataType::Int32, true);
480+
/// Ok(field)
481+
/// }
482+
/// # }
483+
/// ```
484+
///
464485
/// # Output Type based on Values
465486
///
466487
/// For example, the following two function calls get the same argument

0 commit comments

Comments
 (0)