datafusion_functions_window/macros.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Convenience macros for defining a user-defined window function
//! and associated expression API (fluent style).
//!
//! See [`define_udwf_and_expr!`] for usage examples.
//!
//! [`define_udwf_and_expr!`]: crate::define_udwf_and_expr!
/// Lazily initializes a user-defined window function exactly once
/// when called concurrently. Repeated calls return a reference to the
/// same instance.
///
/// # Parameters
///
/// * `$UDWF`: The struct which defines the [`Signature`](datafusion_expr::Signature)
/// of the user-defined window function.
/// * `$OUT_FN_NAME`: The basename to generate a unique function name like
/// `$OUT_FN_NAME_udwf`.
/// * `$DOC`: Doc comments for UDWF.
/// * (optional) `$CTOR`: Pass a custom constructor. When omitted it
/// automatically resolves to `$UDWF::default()`.
///
/// # Example
///
/// ```
/// # use std::any::Any;
/// # use datafusion_common::arrow::datatypes::{DataType, Field};
/// # use datafusion_expr::{PartitionEvaluator, Signature, Volatility, WindowUDFImpl};
/// #
/// # use datafusion_functions_window_common::field::WindowUDFFieldArgs;
/// # use datafusion_functions_window::get_or_init_udwf;
/// # use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
/// #
/// /// Defines the `simple_udwf()` user-defined window function.
/// get_or_init_udwf!(
/// SimpleUDWF,
/// simple,
/// "Simple user-defined window function doc comment."
/// );
/// #
/// # assert_eq!(simple_udwf().name(), "simple_user_defined_window_function");
/// #
/// # #[derive(Debug)]
/// # struct SimpleUDWF {
/// # signature: Signature,
/// # }
/// #
/// # impl Default for SimpleUDWF {
/// # fn default() -> Self {
/// # Self {
/// # signature: Signature::any(0, Volatility::Immutable),
/// # }
/// # }
/// # }
/// #
/// # impl WindowUDFImpl for SimpleUDWF {
/// # fn as_any(&self) -> &dyn Any {
/// # self
/// # }
/// # fn name(&self) -> &str {
/// # "simple_user_defined_window_function"
/// # }
/// # fn signature(&self) -> &Signature {
/// # &self.signature
/// # }
/// # fn partition_evaluator(
/// # &self,
/// # _partition_evaluator_args: PartitionEvaluatorArgs,
/// # ) -> datafusion_common::Result<Box<dyn PartitionEvaluator>> {
/// # unimplemented!()
/// # }
/// # fn field(&self, field_args: WindowUDFFieldArgs) -> datafusion_common::Result<Field> {
/// # Ok(Field::new(field_args.name(), DataType::Int64, false))
/// # }
/// # }
/// #
/// ```
#[macro_export]
macro_rules! get_or_init_udwf {
($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr) => {
get_or_init_udwf!($UDWF, $OUT_FN_NAME, $DOC, $UDWF::default);
};
($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr, $CTOR:path) => {
paste::paste! {
#[doc = concat!(" Singleton instance of [`", stringify!($OUT_FN_NAME), "`], ensures the user-defined")]
#[doc = concat!(" window function is only created once.")]
#[allow(non_upper_case_globals)]
static [<STATIC_ $UDWF>]: std::sync::OnceLock<std::sync::Arc<datafusion_expr::WindowUDF>> =
std::sync::OnceLock::new();
#[doc = concat!(" Returns a [`WindowUDF`](datafusion_expr::WindowUDF) for [`", stringify!($OUT_FN_NAME), "`].")]
#[doc = ""]
#[doc = concat!(" ", $DOC)]
pub fn [<$OUT_FN_NAME _udwf>]() -> std::sync::Arc<datafusion_expr::WindowUDF> {
[<STATIC_ $UDWF>]
.get_or_init(|| {
std::sync::Arc::new(datafusion_expr::WindowUDF::from($CTOR()))
})
.clone()
}
}
};
}
/// Create a [`WindowFunction`] expression that exposes a fluent API
/// which you can use to build more complex expressions.
///
/// [`WindowFunction`]: datafusion_expr::Expr::WindowFunction
///
/// # Parameters
///
/// * `$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`.
/// * `$DOC`: Doc comments for 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.
///
/// [`Signature`]: datafusion_expr::Signature
/// [`Expr`]: datafusion_expr::Expr
///
/// # Example
///
/// 1. With Zero Parameters
/// ```
/// # use std::any::Any;
/// # use datafusion_common::arrow::datatypes::{DataType, Field};
/// # use datafusion_expr::{PartitionEvaluator, Signature, Volatility, WindowUDFImpl};
/// # use datafusion_functions_window::{create_udwf_expr, get_or_init_udwf};
/// # use datafusion_functions_window_common::field::WindowUDFFieldArgs;
/// # use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
///
/// # get_or_init_udwf!(
/// # RowNumber,
/// # row_number,
/// # "Returns a unique row number for each row in window partition beginning at 1."
/// # );
/// /// Creates `row_number()` API which has zero parameters:
/// ///
/// /// ```
/// /// /// Returns a unique row number for each row in window partition
/// /// /// beginning at 1.
/// /// pub fn row_number() -> datafusion_expr::Expr {
/// /// row_number_udwf().call(vec![])
/// /// }
/// /// ```
/// create_udwf_expr!(
/// RowNumber,
/// row_number,
/// "Returns a unique row number for each row in window partition beginning at 1."
/// );
/// #
/// # assert_eq!(
/// # row_number().name_for_alias().unwrap(),
/// # "row_number() ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
/// # );
/// #
/// # #[derive(Debug)]
/// # struct RowNumber {
/// # signature: Signature,
/// # }
/// # impl Default for RowNumber {
/// # fn default() -> Self {
/// # Self {
/// # signature: Signature::any(0, Volatility::Immutable),
/// # }
/// # }
/// # }
/// # impl WindowUDFImpl for RowNumber {
/// # fn as_any(&self) -> &dyn Any {
/// # self
/// # }
/// # fn name(&self) -> &str {
/// # "row_number"
/// # }
/// # fn signature(&self) -> &Signature {
/// # &self.signature
/// # }
/// # fn partition_evaluator(
/// # &self,
/// # _partition_evaluator_args: PartitionEvaluatorArgs,
/// # ) -> datafusion_common::Result<Box<dyn PartitionEvaluator>> {
/// # unimplemented!()
/// # }
/// # fn field(&self, field_args: WindowUDFFieldArgs) -> datafusion_common::Result<Field> {
/// # Ok(Field::new(field_args.name(), DataType::UInt64, false))
/// # }
/// # }
/// ```
///
/// 2. With Multiple Parameters
/// ```
/// # use std::any::Any;
/// #
/// # use datafusion_expr::{
/// # PartitionEvaluator, Signature, TypeSignature, Volatility, WindowUDFImpl,
/// # };
/// #
/// # use datafusion_functions_window::{create_udwf_expr, get_or_init_udwf};
/// # use datafusion_functions_window_common::field::WindowUDFFieldArgs;
/// #
/// # use datafusion_common::arrow::datatypes::Field;
/// # use datafusion_common::ScalarValue;
/// # use datafusion_expr::{col, lit};
/// # use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
/// #
/// # get_or_init_udwf!(Lead, lead, "user-defined window function");
/// #
/// /// Creates `lead(expr, offset, default)` with 3 parameters:
/// ///
/// /// ```
/// /// /// Returns a value evaluated at the row that is offset rows
/// /// /// after the current row within the partition.
/// /// 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])
/// /// }
/// /// ```
/// create_udwf_expr!(
/// Lead,
/// lead,
/// [expr, offset, default],
/// "Returns a value evaluated at the row that is offset rows after the current row within the partition."
/// );
/// #
/// # assert_eq!(
/// # lead(col("a"), lit(1i64), lit(ScalarValue::Null))
/// # .name_for_alias()
/// # .unwrap(),
/// # "lead(a,Int64(1),NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
/// # );
/// #
/// # #[derive(Debug)]
/// # struct Lead {
/// # signature: Signature,
/// # }
/// #
/// # impl Default for Lead {
/// # fn default() -> Self {
/// # Self {
/// # signature: Signature::one_of(
/// # vec![
/// # TypeSignature::Any(1),
/// # TypeSignature::Any(2),
/// # TypeSignature::Any(3),
/// # ],
/// # Volatility::Immutable,
/// # ),
/// # }
/// # }
/// # }
/// #
/// # impl WindowUDFImpl for Lead {
/// # fn as_any(&self) -> &dyn Any {
/// # self
/// # }
/// # fn name(&self) -> &str {
/// # "lead"
/// # }
/// # fn signature(&self) -> &Signature {
/// # &self.signature
/// # }
/// # fn partition_evaluator(
/// # &self,
/// # partition_evaluator_args: PartitionEvaluatorArgs,
/// # ) -> datafusion_common::Result<Box<dyn PartitionEvaluator>> {
/// # unimplemented!()
/// # }
/// # fn field(&self, field_args: WindowUDFFieldArgs) -> datafusion_common::Result<Field> {
/// # Ok(Field::new(
/// # field_args.name(),
/// # field_args.get_input_type(0).unwrap(),
/// # false,
/// # ))
/// # }
/// # }
/// ```
#[macro_export]
macro_rules! create_udwf_expr {
// zero arguments
($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr) => {
paste::paste! {
#[doc = " Create a [`WindowFunction`](datafusion_expr::Expr::WindowFunction) expression for"]
#[doc = concat!(" `", stringify!($UDWF), "` user-defined window function.")]
#[doc = ""]
#[doc = concat!(" ", $DOC)]
pub fn $OUT_FN_NAME() -> datafusion_expr::Expr {
[<$OUT_FN_NAME _udwf>]().call(vec![])
}
}
};
// 1 or more arguments
($UDWF:ident, $OUT_FN_NAME:ident, [$($PARAM:ident),+], $DOC:expr) => {
paste::paste! {
#[doc = " Create a [`WindowFunction`](datafusion_expr::Expr::WindowFunction) expression for"]
#[doc = concat!(" `", stringify!($UDWF), "` user-defined window function.")]
#[doc = ""]
#[doc = concat!(" ", $DOC)]
pub fn $OUT_FN_NAME(
$($PARAM: datafusion_expr::Expr),+
) -> datafusion_expr::Expr {
[<$OUT_FN_NAME _udwf>]()
.call(vec![$($PARAM),+])
}
}
};
}
/// 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()`.
///
/// [`Signature`]: datafusion_expr::Signature
/// [`Expr`]: datafusion_expr::Expr
///
/// # Usage
///
/// ## Expression API With Zero parameters
/// 1. Uses default constructor for UDWF.
///
/// ```
/// # use std::any::Any;
/// # use datafusion_common::arrow::datatypes::{DataType, Field};
/// # use datafusion_expr::{PartitionEvaluator, Signature, Volatility, WindowUDFImpl};
/// #
/// # use datafusion_functions_window_common::field::WindowUDFFieldArgs;
/// # use datafusion_functions_window::{define_udwf_and_expr, get_or_init_udwf, create_udwf_expr};
/// # use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
/// #
/// /// 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"
/// );
/// #
/// # assert_eq!(simple_udwf().name(), "simple_user_defined_window_function");
/// #
/// # #[derive(Debug)]
/// # struct SimpleUDWF {
/// # signature: Signature,
/// # }
/// #
/// # impl Default for SimpleUDWF {
/// # fn default() -> Self {
/// # Self {
/// # signature: Signature::any(0, Volatility::Immutable),
/// # }
/// # }
/// # }
/// #
/// # impl WindowUDFImpl for SimpleUDWF {
/// # fn as_any(&self) -> &dyn Any {
/// # self
/// # }
/// # fn name(&self) -> &str {
/// # "simple_user_defined_window_function"
/// # }
/// # fn signature(&self) -> &Signature {
/// # &self.signature
/// # }
/// # fn partition_evaluator(
/// # &self,
/// # partition_evaluator_args: PartitionEvaluatorArgs,
/// # ) -> datafusion_common::Result<Box<dyn PartitionEvaluator>> {
/// # unimplemented!()
/// # }
/// # fn field(&self, field_args: WindowUDFFieldArgs) -> datafusion_common::Result<Field> {
/// # Ok(Field::new(field_args.name(), DataType::Int64, false))
/// # }
/// # }
/// #
/// ```
///
/// 2. Uses a custom constructor for UDWF.
///
/// ```
/// # use std::any::Any;
/// # use datafusion_common::arrow::datatypes::{DataType, Field};
/// # use datafusion_expr::{PartitionEvaluator, Signature, Volatility, WindowUDFImpl};
/// # use datafusion_functions_window::{create_udwf_expr, define_udwf_and_expr, get_or_init_udwf};
/// # use datafusion_functions_window_common::field::WindowUDFFieldArgs;
/// # use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
/// #
/// /// 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
/// );
/// #
/// # assert_eq!(
/// # row_number().name_for_alias().unwrap(),
/// # "row_number() ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
/// # );
/// #
/// # #[derive(Debug)]
/// # struct RowNumber {
/// # signature: Signature,
/// # }
/// # impl RowNumber {
/// # fn new() -> Self {
/// # Self {
/// # signature: Signature::any(0, Volatility::Immutable),
/// # }
/// # }
/// # }
/// # impl WindowUDFImpl for RowNumber {
/// # fn as_any(&self) -> &dyn Any {
/// # self
/// # }
/// # fn name(&self) -> &str {
/// # "row_number"
/// # }
/// # fn signature(&self) -> &Signature {
/// # &self.signature
/// # }
/// # fn partition_evaluator(
/// # &self,
/// # _partition_evaluator_args: PartitionEvaluatorArgs,
/// # ) -> datafusion_common::Result<Box<dyn PartitionEvaluator>> {
/// # unimplemented!()
/// # }
/// # fn field(&self, field_args: WindowUDFFieldArgs) -> datafusion_common::Result<Field> {
/// # Ok(Field::new(field_args.name(), DataType::UInt64, false))
/// # }
/// # }
/// ```
///
/// ## Expression API With Multiple Parameters
/// 3. Uses default constructor for UDWF
///
/// ```
/// # use std::any::Any;
/// #
/// # use datafusion_expr::{
/// # PartitionEvaluator, Signature, TypeSignature, Volatility, WindowUDFImpl,
/// # };
/// #
/// # use datafusion_functions_window::{create_udwf_expr, define_udwf_and_expr, get_or_init_udwf};
/// # use datafusion_functions_window_common::field::WindowUDFFieldArgs;
/// #
/// # use datafusion_common::arrow::datatypes::Field;
/// # use datafusion_common::ScalarValue;
/// # use datafusion_expr::{col, lit};
/// # use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
/// #
/// /// 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"
/// );
/// #
/// # assert_eq!(
/// # lead(col("a"), lit(1i64), lit(ScalarValue::Null))
/// # .name_for_alias()
/// # .unwrap(),
/// # "lead(a,Int64(1),NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
/// # );
/// #
/// # #[derive(Debug)]
/// # struct Lead {
/// # signature: Signature,
/// # }
/// #
/// # impl Default for Lead {
/// # fn default() -> Self {
/// # Self {
/// # signature: Signature::one_of(
/// # vec![
/// # TypeSignature::Any(1),
/// # TypeSignature::Any(2),
/// # TypeSignature::Any(3),
/// # ],
/// # Volatility::Immutable,
/// # ),
/// # }
/// # }
/// # }
/// #
/// # impl WindowUDFImpl for Lead {
/// # fn as_any(&self) -> &dyn Any {
/// # self
/// # }
/// # fn name(&self) -> &str {
/// # "lead"
/// # }
/// # fn signature(&self) -> &Signature {
/// # &self.signature
/// # }
/// # fn partition_evaluator(
/// # &self,
/// # _partition_evaluator_args: PartitionEvaluatorArgs,
/// # ) -> datafusion_common::Result<Box<dyn PartitionEvaluator>> {
/// # unimplemented!()
/// # }
/// # fn field(&self, field_args: WindowUDFFieldArgs) -> datafusion_common::Result<Field> {
/// # Ok(Field::new(
/// # field_args.name(),
/// # field_args.get_input_type(0).unwrap(),
/// # false,
/// # ))
/// # }
/// # }
/// ```
/// 4. Uses custom constructor for UDWF
///
/// ```
/// # use std::any::Any;
/// #
/// # use datafusion_expr::{
/// # PartitionEvaluator, Signature, TypeSignature, Volatility, WindowUDFImpl,
/// # };
/// #
/// # use datafusion_functions_window::{create_udwf_expr, define_udwf_and_expr, get_or_init_udwf};
/// # use datafusion_functions_window_common::field::WindowUDFFieldArgs;
/// #
/// # use datafusion_common::arrow::datatypes::Field;
/// # use datafusion_common::ScalarValue;
/// # use datafusion_expr::{col, lit};
/// # use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
/// #
/// /// 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
/// );
/// #
/// # assert_eq!(
/// # lead(col("a"), lit(1i64), lit(ScalarValue::Null))
/// # .name_for_alias()
/// # .unwrap(),
/// # "lead(a,Int64(1),NULL) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"
/// # );
/// #
/// # #[derive(Debug)]
/// # struct Lead {
/// # signature: Signature,
/// # }
/// #
/// # impl Lead {
/// # fn new() -> Self {
/// # Self {
/// # signature: Signature::one_of(
/// # vec![
/// # TypeSignature::Any(1),
/// # TypeSignature::Any(2),
/// # TypeSignature::Any(3),
/// # ],
/// # Volatility::Immutable,
/// # ),
/// # }
/// # }
/// # }
/// #
/// # impl WindowUDFImpl for Lead {
/// # fn as_any(&self) -> &dyn Any {
/// # self
/// # }
/// # fn name(&self) -> &str {
/// # "lead"
/// # }
/// # fn signature(&self) -> &Signature {
/// # &self.signature
/// # }
/// # fn partition_evaluator(
/// # &self,
/// # _partition_evaluator_args: PartitionEvaluatorArgs,
/// # ) -> datafusion_common::Result<Box<dyn PartitionEvaluator>> {
/// # unimplemented!()
/// # }
/// # fn field(&self, field_args: WindowUDFFieldArgs) -> datafusion_common::Result<Field> {
/// # Ok(Field::new(
/// # field_args.name(),
/// # field_args.get_input_type(0).unwrap(),
/// # false,
/// # ))
/// # }
/// # }
/// ```
#[macro_export]
macro_rules! define_udwf_and_expr {
// Defines UDWF with default constructor
// Defines expression API with zero parameters
($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr) => {
get_or_init_udwf!($UDWF, $OUT_FN_NAME, $DOC);
create_udwf_expr!($UDWF, $OUT_FN_NAME, $DOC);
};
// Defines UDWF by passing a custom constructor
// Defines expression API with zero parameters
($UDWF:ident, $OUT_FN_NAME:ident, $DOC:expr, $CTOR:path) => {
get_or_init_udwf!($UDWF, $OUT_FN_NAME, $DOC, $CTOR);
create_udwf_expr!($UDWF, $OUT_FN_NAME, $DOC);
};
// Defines UDWF with default constructor
// Defines expression API with multiple parameters
($UDWF:ident, $OUT_FN_NAME:ident, [$($PARAM:ident),+], $DOC:expr) => {
get_or_init_udwf!($UDWF, $OUT_FN_NAME, $DOC);
create_udwf_expr!($UDWF, $OUT_FN_NAME, [$($PARAM),+], $DOC);
};
// Defines UDWF by passing a custom constructor
// Defines expression API with multiple parameters
($UDWF:ident, $OUT_FN_NAME:ident, [$($PARAM:ident),+], $DOC:expr, $CTOR:path) => {
get_or_init_udwf!($UDWF, $OUT_FN_NAME, $DOC, $CTOR);
create_udwf_expr!($UDWF, $OUT_FN_NAME, [$($PARAM),+], $DOC);
};
}