datafusion_functions_aggregate/
approx_median.rs1use arrow::datatypes::DataType::{Float64, UInt64};
21use arrow::datatypes::{DataType, Field, FieldRef};
22use std::any::Any;
23use std::fmt::Debug;
24use std::sync::Arc;
25
26use datafusion_common::{not_impl_err, plan_err, Result};
27use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
28use datafusion_expr::type_coercion::aggregates::NUMERICS;
29use datafusion_expr::utils::format_state_name;
30use datafusion_expr::{
31 Accumulator, AggregateUDFImpl, Documentation, Signature, Volatility,
32};
33use datafusion_macros::user_doc;
34
35use crate::approx_percentile_cont::ApproxPercentileAccumulator;
36
37make_udaf_expr_and_func!(
38 ApproxMedian,
39 approx_median,
40 expression,
41 "Computes the approximate median of a set of numbers",
42 approx_median_udaf
43);
44
45#[user_doc(
47 doc_section(label = "Approximate Functions"),
48 description = "Returns the approximate median (50th percentile) of input values. It is an alias of `approx_percentile_cont(0.5) WITHIN GROUP (ORDER BY x)`.",
49 syntax_example = "approx_median(expression)",
50 sql_example = r#"```sql
51> SELECT approx_median(column_name) FROM table_name;
52+-----------------------------------+
53| approx_median(column_name) |
54+-----------------------------------+
55| 23.5 |
56+-----------------------------------+
57```"#,
58 standard_argument(name = "expression",)
59)]
60#[derive(PartialEq, Eq, Hash)]
61pub struct ApproxMedian {
62 signature: Signature,
63}
64
65impl Debug for ApproxMedian {
66 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
67 f.debug_struct("ApproxMedian")
68 .field("name", &self.name())
69 .field("signature", &self.signature)
70 .finish()
71 }
72}
73
74impl Default for ApproxMedian {
75 fn default() -> Self {
76 Self::new()
77 }
78}
79
80impl ApproxMedian {
81 pub fn new() -> Self {
83 Self {
84 signature: Signature::uniform(1, NUMERICS.to_vec(), Volatility::Immutable),
85 }
86 }
87}
88
89impl AggregateUDFImpl for ApproxMedian {
90 fn as_any(&self) -> &dyn Any {
92 self
93 }
94
95 fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<FieldRef>> {
96 Ok(vec![
97 Field::new(format_state_name(args.name, "max_size"), UInt64, false),
98 Field::new(format_state_name(args.name, "sum"), Float64, false),
99 Field::new(format_state_name(args.name, "count"), UInt64, false),
100 Field::new(format_state_name(args.name, "max"), Float64, false),
101 Field::new(format_state_name(args.name, "min"), Float64, false),
102 Field::new_list(
103 format_state_name(args.name, "centroids"),
104 Field::new_list_field(Float64, true),
105 false,
106 ),
107 ]
108 .into_iter()
109 .map(Arc::new)
110 .collect())
111 }
112
113 fn name(&self) -> &str {
114 "approx_median"
115 }
116
117 fn signature(&self) -> &Signature {
118 &self.signature
119 }
120
121 fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
122 if !arg_types[0].is_numeric() {
123 return plan_err!("ApproxMedian requires numeric input types");
124 }
125 Ok(arg_types[0].clone())
126 }
127
128 fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
129 if acc_args.is_distinct {
130 return not_impl_err!(
131 "APPROX_MEDIAN(DISTINCT) aggregations are not available"
132 );
133 }
134
135 Ok(Box::new(ApproxPercentileAccumulator::new(
136 0.5_f64,
137 acc_args.exprs[0].data_type(acc_args.schema)?,
138 )))
139 }
140
141 fn documentation(&self) -> Option<&Documentation> {
142 self.doc()
143 }
144}