Skip to content

Macros for user-defined window functions #12688

Closed

Description

@jcsherin

Is your feature request related to a problem or challenge?

Add macros for creating WindowUDF and WindowFunction expression from user-defined window functions.

This will be similar to existing macros in function aggeregates:

macro_rules! make_udaf_expr_and_func {
($UDAF:ty, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $AGGREGATE_UDF_FN:ident) => {
make_udaf_expr!($EXPR_FN, $($arg)*, $DOC, $AGGREGATE_UDF_FN);
create_func!($UDAF, $AGGREGATE_UDF_FN);
};

Describe the solution you'd like

Existing code which will be replaced by macros:

/// Create a [`WindowFunction`](Expr::WindowFunction) expression for
/// `row_number` user-defined window function.
pub fn row_number() -> Expr {
Expr::WindowFunction(WindowFunction::new(row_number_udwf(), vec![]))
}
/// Singleton instance of `row_number`, ensures the UDWF is only created once.
#[allow(non_upper_case_globals)]
static STATIC_RowNumber: std::sync::OnceLock<std::sync::Arc<datafusion_expr::WindowUDF>> =
std::sync::OnceLock::new();
/// Returns a [`WindowUDF`](datafusion_expr::WindowUDF) for `row_number`
/// user-defined window function.
pub fn row_number_udwf() -> std::sync::Arc<datafusion_expr::WindowUDF> {
STATIC_RowNumber
.get_or_init(|| {
std::sync::Arc::new(datafusion_expr::WindowUDF::from(RowNumber::default()))
})
.clone()
}

New code using a macro:

define_udwf_and_expr!(
    RowNumber,
    row_number,
    "Returns a unique row number for each row in window partition beginning at 1."
);

The above example combines creating both user-defined window function and expression function API. This should work for majority of the cases.

But separate macros will also be provided for cases where they are necessary.

Describe alternatives you've considered

As we begin to convert remaining BuiltinWindowFunction::* to user-defined window functions this will save developer effort.

Additional context

Part of #8709.
Follow-on to PR #12030.

Needed for #12649, #12648.

Activity

  1. jcsherin commented on Oct 1, 2024

    @jcsherin
    ContributorAuthor

    @SteNicholas Sorry for the confusion. This was unassigned but I already have a PR in the works (#12693).

    Here a few more good first issues for user-defined window functions to get you started 😄.

    You can reply a single word "take" in the comments of an issue and it will get automatically assigned to you.

    See https://datafusion.apache.org/contributor-guide/index.html#open-contribution-and-assigning-tickets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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