-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
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:
datafusion/datafusion/functions-aggregate/src/macros.rs
Lines 54 to 58 in 80848f2
| 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:
datafusion/datafusion/functions-window/src/row_number.rs
Lines 35 to 54 in e1b992a
| /// 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.