|
| 1 | +use crate::create_func; |
| 2 | +use crate::make_udaf_expr; |
| 3 | +use crate::make_udaf_expr_and_func; |
| 4 | +use datafusion::arrow::datatypes::DataType; |
| 5 | +use datafusion::common::{exec_err, Result}; |
| 6 | +use datafusion::functions_aggregate::first_last::last_value_udaf; |
| 7 | +use datafusion::logical_expr::expr; |
| 8 | +use datafusion::logical_expr::expr::AggregateFunction; |
| 9 | +use datafusion::logical_expr::expr::Sort; |
| 10 | +use datafusion::logical_expr::function; |
| 11 | +use datafusion::logical_expr::function::AccumulatorArgs; |
| 12 | +use datafusion::logical_expr::simplify::SimplifyInfo; |
| 13 | +use datafusion::logical_expr::Expr; |
| 14 | +use datafusion::logical_expr::Volatility; |
| 15 | +use datafusion::logical_expr::{AggregateUDFImpl, Signature}; |
| 16 | +use datafusion::physical_plan::Accumulator; |
| 17 | +use std::any::Any; |
| 18 | +use std::fmt::Debug; |
| 19 | +use std::ops::Deref; |
| 20 | + |
| 21 | +make_udaf_expr_and_func!( |
| 22 | + MaxByFunction, |
| 23 | + max_by, |
| 24 | + x y, |
| 25 | + "Returns the value of the first column corresponding to the maximum value in the second column.", |
| 26 | + max_by_udaf |
| 27 | +); |
| 28 | + |
| 29 | +pub struct MaxByFunction { |
| 30 | + signature: Signature, |
| 31 | +} |
| 32 | + |
| 33 | +impl Debug for MaxByFunction { |
| 34 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 35 | + f.debug_struct("MaxBy") |
| 36 | + .field("name", &self.name()) |
| 37 | + .field("signature", &self.signature) |
| 38 | + .field("accumulator", &"<FUNC>") |
| 39 | + .finish() |
| 40 | + } |
| 41 | +} |
| 42 | +impl Default for MaxByFunction { |
| 43 | + fn default() -> Self { |
| 44 | + Self::new() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl MaxByFunction { |
| 49 | + pub fn new() -> Self { |
| 50 | + Self { |
| 51 | + signature: Signature::user_defined(Volatility::Immutable), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn get_min_max_by_result_type(input_types: &[DataType]) -> Result<Vec<DataType>> { |
| 57 | + match &input_types[0] { |
| 58 | + DataType::Dictionary(_, dict_value_type) => { |
| 59 | + // TODO add checker, if the value type is complex data type |
| 60 | + Ok(vec![dict_value_type.deref().clone()]) |
| 61 | + } |
| 62 | + _ => Ok(input_types.to_vec()), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl AggregateUDFImpl for MaxByFunction { |
| 67 | + fn as_any(&self) -> &dyn Any { |
| 68 | + self |
| 69 | + } |
| 70 | + |
| 71 | + fn name(&self) -> &str { |
| 72 | + "max_by" |
| 73 | + } |
| 74 | + |
| 75 | + fn signature(&self) -> &Signature { |
| 76 | + &self.signature |
| 77 | + } |
| 78 | + |
| 79 | + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { |
| 80 | + Ok(arg_types[0].to_owned()) |
| 81 | + } |
| 82 | + |
| 83 | + fn accumulator(&self, _acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> { |
| 84 | + exec_err!("should not reach here") |
| 85 | + } |
| 86 | + fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { |
| 87 | + get_min_max_by_result_type(arg_types) |
| 88 | + } |
| 89 | + |
| 90 | + fn simplify(&self) -> Option<function::AggregateFunctionSimplification> { |
| 91 | + let simplify = |mut aggr_func: expr::AggregateFunction, _: &dyn SimplifyInfo| { |
| 92 | + let mut order_by = aggr_func.order_by.unwrap_or_default(); |
| 93 | + let (second_arg, first_arg) = (aggr_func.args.remove(1), aggr_func.args.remove(0)); |
| 94 | + |
| 95 | + order_by.push(Sort::new(second_arg, true, false)); |
| 96 | + |
| 97 | + Ok(Expr::AggregateFunction(AggregateFunction::new_udf( |
| 98 | + last_value_udaf(), |
| 99 | + vec![first_arg], |
| 100 | + aggr_func.distinct, |
| 101 | + aggr_func.filter, |
| 102 | + Some(order_by), |
| 103 | + aggr_func.null_treatment, |
| 104 | + ))) |
| 105 | + }; |
| 106 | + Some(Box::new(simplify)) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +make_udaf_expr_and_func!( |
| 111 | + MinByFunction, |
| 112 | + min_by, |
| 113 | + x y, |
| 114 | + "Returns the value of the first column corresponding to the minimum value in the second column.", |
| 115 | + min_by_udaf |
| 116 | +); |
| 117 | + |
| 118 | +pub struct MinByFunction { |
| 119 | + signature: Signature, |
| 120 | +} |
| 121 | + |
| 122 | +impl Debug for MinByFunction { |
| 123 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 124 | + f.debug_struct("MinBy") |
| 125 | + .field("name", &self.name()) |
| 126 | + .field("signature", &self.signature) |
| 127 | + .field("accumulator", &"<FUNC>") |
| 128 | + .finish() |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl Default for MinByFunction { |
| 133 | + fn default() -> Self { |
| 134 | + Self::new() |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl MinByFunction { |
| 139 | + pub fn new() -> Self { |
| 140 | + Self { |
| 141 | + signature: Signature::user_defined(Volatility::Immutable), |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl AggregateUDFImpl for MinByFunction { |
| 147 | + fn as_any(&self) -> &dyn Any { |
| 148 | + self |
| 149 | + } |
| 150 | + |
| 151 | + fn name(&self) -> &str { |
| 152 | + "min_by" |
| 153 | + } |
| 154 | + |
| 155 | + fn signature(&self) -> &Signature { |
| 156 | + &self.signature |
| 157 | + } |
| 158 | + |
| 159 | + fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { |
| 160 | + Ok(arg_types[0].to_owned()) |
| 161 | + } |
| 162 | + |
| 163 | + fn accumulator(&self, _acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> { |
| 164 | + exec_err!("should not reach here") |
| 165 | + } |
| 166 | + |
| 167 | + fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { |
| 168 | + get_min_max_by_result_type(arg_types) |
| 169 | + } |
| 170 | + |
| 171 | + fn simplify(&self) -> Option<function::AggregateFunctionSimplification> { |
| 172 | + let simplify = |mut aggr_func: expr::AggregateFunction, _: &dyn SimplifyInfo| { |
| 173 | + let mut order_by = aggr_func.order_by.unwrap_or_default(); |
| 174 | + let (second_arg, first_arg) = (aggr_func.args.remove(1), aggr_func.args.remove(0)); |
| 175 | + |
| 176 | + order_by.push(Sort::new(second_arg, false, false)); // false for ascending sort |
| 177 | + |
| 178 | + Ok(Expr::AggregateFunction(AggregateFunction::new_udf( |
| 179 | + last_value_udaf(), |
| 180 | + vec![first_arg], |
| 181 | + aggr_func.distinct, |
| 182 | + aggr_func.filter, |
| 183 | + Some(order_by), |
| 184 | + aggr_func.null_treatment, |
| 185 | + ))) |
| 186 | + }; |
| 187 | + Some(Box::new(simplify)) |
| 188 | + } |
| 189 | +} |
0 commit comments