Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros for user-defined window functions #12688

Closed
jcsherin opened this issue Sep 30, 2024 · 1 comment · Fixed by #12693
Closed

Macros for user-defined window functions #12688

jcsherin opened this issue Sep 30, 2024 · 1 comment · Fixed by #12693
Labels
enhancement New feature or request

Comments

@jcsherin
Copy link
Contributor

jcsherin commented Sep 30, 2024

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.

@jcsherin
Copy link
Contributor Author

jcsherin commented Oct 1, 2024

@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
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant