datafusion_functions_aggregate/
lib.rs1#![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#![deny(clippy::clone_on_ref_ptr)]
25
26#[macro_use]
64pub mod macros;
65
66pub mod approx_distinct;
67pub mod approx_median;
68pub mod approx_percentile_cont;
69pub mod approx_percentile_cont_with_weight;
70pub mod array_agg;
71pub mod average;
72pub mod bit_and_or_xor;
73pub mod bool_and_or;
74pub mod correlation;
75pub mod count;
76pub mod covariance;
77pub mod first_last;
78pub mod grouping;
79pub mod hyperloglog;
80pub mod median;
81pub mod min_max;
82pub mod nth_value;
83pub mod regr;
84pub mod stddev;
85pub mod string_agg;
86pub mod sum;
87pub mod variance;
88
89pub mod planner;
90
91use crate::approx_percentile_cont::approx_percentile_cont_udaf;
92use crate::approx_percentile_cont_with_weight::approx_percentile_cont_with_weight_udaf;
93use datafusion_common::Result;
94use datafusion_execution::FunctionRegistry;
95use datafusion_expr::AggregateUDF;
96use log::debug;
97use std::sync::Arc;
98
99pub mod expr_fn {
101 pub use super::approx_distinct::approx_distinct;
102 pub use super::approx_median::approx_median;
103 pub use super::approx_percentile_cont::approx_percentile_cont;
104 pub use super::approx_percentile_cont_with_weight::approx_percentile_cont_with_weight;
105 pub use super::array_agg::array_agg;
106 pub use super::average::avg;
107 pub use super::bit_and_or_xor::bit_and;
108 pub use super::bit_and_or_xor::bit_or;
109 pub use super::bit_and_or_xor::bit_xor;
110 pub use super::bool_and_or::bool_and;
111 pub use super::bool_and_or::bool_or;
112 pub use super::correlation::corr;
113 pub use super::count::count;
114 pub use super::count::count_distinct;
115 pub use super::covariance::covar_pop;
116 pub use super::covariance::covar_samp;
117 pub use super::first_last::first_value;
118 pub use super::first_last::last_value;
119 pub use super::grouping::grouping;
120 pub use super::median::median;
121 pub use super::min_max::max;
122 pub use super::min_max::min;
123 pub use super::nth_value::nth_value;
124 pub use super::regr::regr_avgx;
125 pub use super::regr::regr_avgy;
126 pub use super::regr::regr_count;
127 pub use super::regr::regr_intercept;
128 pub use super::regr::regr_r2;
129 pub use super::regr::regr_slope;
130 pub use super::regr::regr_sxx;
131 pub use super::regr::regr_sxy;
132 pub use super::regr::regr_syy;
133 pub use super::stddev::stddev;
134 pub use super::stddev::stddev_pop;
135 pub use super::sum::sum;
136 pub use super::variance::var_pop;
137 pub use super::variance::var_sample;
138}
139
140pub fn all_default_aggregate_functions() -> Vec<Arc<AggregateUDF>> {
142 vec![
143 array_agg::array_agg_udaf(),
144 first_last::first_value_udaf(),
145 first_last::last_value_udaf(),
146 covariance::covar_samp_udaf(),
147 covariance::covar_pop_udaf(),
148 correlation::corr_udaf(),
149 sum::sum_udaf(),
150 min_max::max_udaf(),
151 min_max::min_udaf(),
152 median::median_udaf(),
153 count::count_udaf(),
154 regr::regr_slope_udaf(),
155 regr::regr_intercept_udaf(),
156 regr::regr_count_udaf(),
157 regr::regr_r2_udaf(),
158 regr::regr_avgx_udaf(),
159 regr::regr_avgy_udaf(),
160 regr::regr_sxx_udaf(),
161 regr::regr_syy_udaf(),
162 regr::regr_sxy_udaf(),
163 variance::var_samp_udaf(),
164 variance::var_pop_udaf(),
165 stddev::stddev_udaf(),
166 stddev::stddev_pop_udaf(),
167 approx_median::approx_median_udaf(),
168 approx_distinct::approx_distinct_udaf(),
169 approx_percentile_cont_udaf(),
170 approx_percentile_cont_with_weight_udaf(),
171 string_agg::string_agg_udaf(),
172 bit_and_or_xor::bit_and_udaf(),
173 bit_and_or_xor::bit_or_udaf(),
174 bit_and_or_xor::bit_xor_udaf(),
175 bool_and_or::bool_and_udaf(),
176 bool_and_or::bool_or_udaf(),
177 average::avg_udaf(),
178 grouping::grouping_udaf(),
179 nth_value::nth_value_udaf(),
180 ]
181}
182
183pub fn register_all(registry: &mut dyn FunctionRegistry) -> Result<()> {
185 let functions: Vec<Arc<AggregateUDF>> = all_default_aggregate_functions();
186
187 functions.into_iter().try_for_each(|udf| {
188 let existing_udaf = registry.register_udaf(udf)?;
189 if let Some(existing_udaf) = existing_udaf {
190 debug!("Overwrite existing UDAF: {}", existing_udaf.name());
191 }
192 Ok(()) as Result<()>
193 })?;
194
195 Ok(())
196}
197
198#[cfg(test)]
199mod tests {
200 use crate::all_default_aggregate_functions;
201 use datafusion_common::Result;
202 use std::collections::HashSet;
203
204 #[test]
205 fn test_no_duplicate_name() -> Result<()> {
206 let mut names = HashSet::new();
207 let migrated_functions = ["array_agg", "count", "max", "min"];
208 for func in all_default_aggregate_functions() {
209 if migrated_functions.contains(&func.name().to_lowercase().as_str()) {
212 continue;
213 }
214 assert!(
215 names.insert(func.name().to_string().to_lowercase()),
216 "duplicate function name: {}",
217 func.name()
218 );
219 for alias in func.aliases() {
220 assert!(
221 names.insert(alias.to_string().to_lowercase()),
222 "duplicate function name: {}",
223 alias
224 );
225 }
226 }
227 Ok(())
228 }
229}