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