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_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
34use std::sync::Arc;
35
36use log::debug;
37
38use datafusion_expr::registry::FunctionRegistry;
39use datafusion_expr::WindowUDF;
40
41#[macro_use]
42pub mod macros;
43
44pub mod cume_dist;
45pub mod lead_lag;
46pub mod nth_value;
47pub mod ntile;
48pub mod rank;
49pub mod row_number;
50
51pub mod planner;
52
53mod utils;
54
55/// Fluent-style API for creating `Expr`s
56pub mod expr_fn {
57    pub use super::cume_dist::cume_dist;
58    pub use super::lead_lag::lag;
59    pub use super::lead_lag::lead;
60    pub use super::nth_value::{first_value, last_value, nth_value};
61    pub use super::ntile::ntile;
62    pub use super::rank::{dense_rank, percent_rank, rank};
63    pub use super::row_number::row_number;
64}
65
66/// Returns all default window functions
67pub fn all_default_window_functions() -> Vec<Arc<WindowUDF>> {
68    vec![
69        cume_dist::cume_dist_udwf(),
70        row_number::row_number_udwf(),
71        lead_lag::lead_udwf(),
72        lead_lag::lag_udwf(),
73        rank::rank_udwf(),
74        rank::dense_rank_udwf(),
75        rank::percent_rank_udwf(),
76        ntile::ntile_udwf(),
77        nth_value::first_value_udwf(),
78        nth_value::last_value_udwf(),
79        nth_value::nth_value_udwf(),
80    ]
81}
82/// Registers all enabled packages with a [`FunctionRegistry`]
83pub fn register_all(
84    registry: &mut dyn FunctionRegistry,
85) -> datafusion_common::Result<()> {
86    let functions: Vec<Arc<WindowUDF>> = all_default_window_functions();
87
88    functions.into_iter().try_for_each(|fun| {
89        let existing_udwf = registry.register_udwf(fun)?;
90        if let Some(existing_udwf) = existing_udwf {
91            debug!("Overwrite existing UDWF: {}", existing_udwf.name());
92        }
93        Ok(()) as datafusion_common::Result<()>
94    })?;
95
96    Ok(())
97}