datafusion_functions_extra/
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
18use log::debug;
19use mode::mode_udaf;
20use std::sync::Arc;
21
22use datafusion::common::Result;
23use datafusion::execution::FunctionRegistry;
24use datafusion::logical_expr::AggregateUDF;
25
26#[macro_use]
27pub mod macros;
28pub mod common;
29pub mod kurtosis;
30pub mod kurtosis_pop;
31pub mod max_min_by;
32pub mod mode;
33pub mod skewness;
34pub mod expr_extra_fn {
35    pub use super::kurtosis::kurtosis;
36    pub use super::kurtosis_pop::kurtosis_pop;
37    pub use super::max_min_by::max_by;
38    pub use super::max_min_by::min_by;
39    pub use super::mode::mode;
40    pub use super::skewness::skewness;
41}
42
43pub fn all_extra_aggregate_functions() -> Vec<Arc<AggregateUDF>> {
44    vec![
45        mode_udaf(),
46        max_min_by::max_by_udaf(),
47        max_min_by::min_by_udaf(),
48        kurtosis::kurtosis_udaf(),
49        skewness::skewness_udaf(),
50        kurtosis_pop::kurtosis_pop_udaf(),
51    ]
52}
53
54/// Registers all enabled packages with a [`FunctionRegistry`]
55pub fn register_all_extra_functions(registry: &mut dyn FunctionRegistry) -> Result<()> {
56    let functions: Vec<Arc<AggregateUDF>> = all_extra_aggregate_functions();
57
58    functions.into_iter().try_for_each(|udf| {
59        let existing_udaf = registry.register_udaf(udf)?;
60        if let Some(existing_udaf) = existing_udaf {
61            debug!("Overwrite existing UDAF: {}", existing_udaf.name());
62        }
63        Ok(()) as Result<()>
64    })?;
65
66    Ok(())
67}