datafusion_functions_window/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#![doc(
19    html_logo_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg",
20    html_favicon_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg"
21)]
22#![cfg_attr(docsrs, feature(doc_auto_cfg))]
23// Make sure fast / cheap clones on Arc are explicit:
24// https://github.com/apache/datafusion/issues/11143
25#![cfg_attr(not(test), deny(clippy::clone_on_ref_ptr))]
26
27//! Window Function packages for [DataFusion].
28//!
29//! This crate contains a collection of various window function packages for DataFusion,
30//! implemented using the extension API.
31//!
32//! [DataFusion]: https://crates.io/crates/datafusion
33//!
34
35use std::sync::Arc;
36
37use log::debug;
38
39use datafusion_expr::registry::FunctionRegistry;
40use datafusion_expr::WindowUDF;
41
42#[macro_use]
43pub mod macros;
44
45pub mod cume_dist;
46pub mod lead_lag;
47pub mod nth_value;
48pub mod ntile;
49pub mod rank;
50pub mod row_number;
51
52pub mod planner;
53
54mod utils;
55
56/// Fluent-style API for creating `Expr`s
57pub mod expr_fn {
58    pub use super::cume_dist::cume_dist;
59    pub use super::lead_lag::lag;
60    pub use super::lead_lag::lead;
61    pub use super::nth_value::{first_value, last_value, nth_value};
62    pub use super::ntile::ntile;
63    pub use super::rank::{dense_rank, percent_rank, rank};
64    pub use super::row_number::row_number;
65}
66
67/// Returns all default window functions
68pub fn all_default_window_functions() -> Vec<Arc<WindowUDF>> {
69    vec![
70        cume_dist::cume_dist_udwf(),
71        row_number::row_number_udwf(),
72        lead_lag::lead_udwf(),
73        lead_lag::lag_udwf(),
74        rank::rank_udwf(),
75        rank::dense_rank_udwf(),
76        rank::percent_rank_udwf(),
77        ntile::ntile_udwf(),
78        nth_value::first_value_udwf(),
79        nth_value::last_value_udwf(),
80        nth_value::nth_value_udwf(),
81    ]
82}
83/// Registers all enabled packages with a [`FunctionRegistry`]
84pub fn register_all(
85    registry: &mut dyn FunctionRegistry,
86) -> datafusion_common::Result<()> {
87    let functions: Vec<Arc<WindowUDF>> = all_default_window_functions();
88
89    functions.into_iter().try_for_each(|fun| {
90        let existing_udwf = registry.register_udwf(fun)?;
91        if let Some(existing_udwf) = existing_udwf {
92            debug!("Overwrite existing UDWF: {}", existing_udwf.name());
93        }
94        Ok(()) as datafusion_common::Result<()>
95    })?;
96
97    Ok(())
98}