datafusion_functions_window

Macro define_udwf_and_expr

Source
macro_rules! define_udwf_and_expr {
    ($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr) => { ... };
    ($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr, $CTOR:path) => { ... };
    ($UDWF:ident, $OUT_FN_NAME:ident, [$($PARAM:ident),+], $DOC:expr) => { ... };
    ($UDWF:ident, $OUT_FN_NAME:ident, [$($PARAM:ident),+], $DOC:expr, $CTOR:path) => { ... };
}
Expand description

Defines a user-defined window function.

Combines get_or_init_udwf! and create_udwf_expr! into a single macro for convenience.

§Arguments

  • $UDWF: The struct which defines the Signature of the user-defined window function.
  • $OUT_FN_NAME: The basename to generate a unique function name like $OUT_FN_NAME_udwf.
  • (optional) [$($PARAM:ident),+]: An array of 1 or more parameters for the generated function. The type of parameters is Expr. When omitted this creates a function with zero parameters.
  • $DOC: Doc comments for UDWF.
  • (optional) $CTOR: Pass a custom constructor. When omitted it automatically resolves to $UDWF::default().

§Usage

§Expression API With Zero parameters

  1. Uses default constructor for UDWF.
/// 1. Defines the `simple_udwf()` user-defined window function.
///
/// 2. Defines the expression API:
///     ```
///     pub fn simple() -> datafusion_expr::Expr {
///         simple_udwf().call(vec![])
///     }
///     ```
define_udwf_and_expr!(
    SimpleUDWF,
    simple,
    "a simple user-defined window function"
);
  1. Uses a custom constructor for UDWF.
/// 1. Defines the `row_number_udwf()` user-defined window function.
///
/// 2. Defines the expression API:
///     ```
///     pub fn row_number() -> datafusion_expr::Expr {
///         row_number_udwf().call(vec![])
///     }
///     ```
define_udwf_and_expr!(
    RowNumber,
    row_number,
    "Returns a unique row number for each row in window partition beginning at 1.",
    RowNumber::new // <-- custom constructor
);

§Expression API With Multiple Parameters

  1. Uses default constructor for UDWF
/// 1. Defines the `lead_udwf()` user-defined window function.
///
/// 2. Defines the expression API:
///     ```
///     pub fn lead(
///         expr: datafusion_expr::Expr,
///         offset: datafusion_expr::Expr,
///         default: datafusion_expr::Expr,
///     ) -> datafusion_expr::Expr {
///         lead_udwf().call(vec![expr, offset, default])
///     }
///     ```
define_udwf_and_expr!(
    Lead,
    lead,
    [expr, offset, default],        // <- 3 parameters
    "user-defined window function"
);
  1. Uses custom constructor for UDWF
/// 1. Defines the `lead_udwf()` user-defined window function.
///
/// 2. Defines the expression API:
///     ```
///     pub fn lead(
///         expr: datafusion_expr::Expr,
///         offset: datafusion_expr::Expr,
///         default: datafusion_expr::Expr,
///     ) -> datafusion_expr::Expr {
///         lead_udwf().call(vec![expr, offset, default])
///     }
///     ```
define_udwf_and_expr!(
    Lead,
    lead,
    [expr, offset, default],        // <- 3 parameters
    "user-defined window function",
    Lead::new                       // <- Custom constructor
);