Skip to content

Add field trait method to WindowUDFImpl #12373

Closed
@jcsherin

Description

@jcsherin

Is your feature request related to a problem or challenge?

Instead of return_type + nullable. I think field is a better choice. Given windowudf is pretty new so not widely used yet (?). It would be nice to have field(&self, args: FieldArgs) -> Result<Field>

FieldArgs {
 arg_type: &[DataType]
 schema: Schema // for nullable
}

It is too late for AggregateUDF and ScalarUDF :(

Originally posted by @jayzhan211 in #12030 (comment)

This is a proposal to add a field trait method for implementing user-defined window functions. And also remove (or deprecate) the trait methods return_type and nullable from WindowUDFImpl.

Both the return data type and nullability of the result from evaluating the user-defined window function will be specified by the field implementation.

Describe the solution you'd like

The current implementation for a user-defined window function (without field trait method) looks like this:

impl WindowUDFImpl for RowNumber {
    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
        Ok(DataType::UInt64)
    }

    fn nullable(&self) -> bool {
        true
    }
}

The implementation for a user-defined window function after this change:

impl WindowUDFImpl for RowNumber {
    fn field(&self, field_args: WindowUDFFieldArgs) -> Result<Field> {
        Ok(Field::new(
             field_args.name(), /* window function display name */
             DataType::UInt64,  /* result data type */
             false              /* row number is not nullable */
       ))
    }
}

  1. Add the field trait method to WindowUDFImpl:
fn field(&self, field_args: WindowUDFFieldArgs) -> Result<Field>
  1. Add WindowUDFFieldArgs:
/// Contains metadata necessary for defining the field which represents
/// the final result of evaluating a user-defined window function.
pub struct WindowUDFFieldArgs<'a> {
    /// The data types of input expressions to the user-defined window
    /// function.
    input_types: &'a [DataType],
    /// The display name of the user-defined window function.
    function_name: &'a str,
}
  1. Remove/Deprecate return_type trait method from WindowUDFImpl:

    /// What [`DataType`] will be returned by this function, given the types of
    /// the arguments
    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType>;

  2. Remove nullable trait method from WindowUDFImpl:

    /// Allows customizing nullable of column for this window UDF.
    ///
    /// By default, the final result of evaluating the window UDF is
    /// allowed to have null values. But if that is not the case then
    /// it can be customized in the window UDF implementation.
    fn nullable(&self) -> bool {
    true
    }

Describe alternatives you've considered

Make no changes to WindowUDFImpl.

Additional context

Part of #8709.
Follow up to #12030.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions