Skip to main content

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#![cfg_attr(test, allow(clippy::needless_pass_by_value))]
19#![doc(
20    html_logo_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg",
21    html_favicon_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg"
22)]
23#![cfg_attr(docsrs, feature(doc_cfg))]
24// Make sure fast / cheap clones on Arc are explicit:
25// https://github.com/apache/datafusion/issues/11143
26#![cfg_attr(not(test), deny(clippy::clone_on_ref_ptr))]
27// https://github.com/apache/datafusion/issues/18881
28
29//! Window Function packages for [DataFusion].
30//!
31//! This crate contains a collection of various window function packages for DataFusion,
32//! implemented using the extension API.
33//!
34//! [DataFusion]: https://crates.io/crates/datafusion
35
36use std::sync::Arc;
37
38use log::debug;
39
40use datafusion_expr::WindowUDF;
41use datafusion_expr::registry::FunctionRegistry;
42
43#[macro_use]
44pub mod macros;
45
46pub mod cume_dist;
47pub mod lead_lag;
48pub mod nth_value;
49pub mod ntile;
50pub mod rank;
51pub mod row_number;
52
53pub mod planner;
54
55mod utils;
56
57/// Fluent-style API for creating `Expr`s
58pub mod expr_fn {
59    pub use super::cume_dist::cume_dist;
60    pub use super::lead_lag::lag;
61    pub use super::lead_lag::lead;
62    pub use super::nth_value::{first_value, last_value, nth_value};
63    pub use super::ntile::ntile;
64    pub use super::rank::{dense_rank, percent_rank, rank};
65    pub use super::row_number::row_number;
66}
67
68/// Returns all default window functions
69pub fn all_default_window_functions() -> Vec<Arc<WindowUDF>> {
70    vec![
71        cume_dist::cume_dist_udwf(),
72        row_number::row_number_udwf(),
73        lead_lag::lead_udwf(),
74        lead_lag::lag_udwf(),
75        rank::rank_udwf(),
76        rank::dense_rank_udwf(),
77        rank::percent_rank_udwf(),
78        ntile::ntile_udwf(),
79        nth_value::first_value_udwf(),
80        nth_value::last_value_udwf(),
81        nth_value::nth_value_udwf(),
82    ]
83}
84/// Registers all enabled packages with a [`FunctionRegistry`]
85pub fn register_all(
86    registry: &mut dyn FunctionRegistry,
87) -> datafusion_common::Result<()> {
88    let functions: Vec<Arc<WindowUDF>> = all_default_window_functions();
89
90    functions.into_iter().try_for_each(|fun| {
91        let existing_udwf = registry.register_udwf(fun)?;
92        if let Some(existing_udwf) = existing_udwf {
93            debug!("Overwrite existing UDWF: {}", existing_udwf.name());
94        }
95        Ok(()) as datafusion_common::Result<()>
96    })?;
97
98    Ok(())
99}