qrlew/expr/
aggregate.rs

1use std::{fmt, hash, mem};
2
3use itertools::Itertools;
4
5use super::{implementation, Result};
6use crate::data_type::{value::Value, DataType};
7
8/// The list of operators
9/// inspired by: https://docs.rs/sqlparser/latest/sqlparser/ast/enum.BinaryOperator.html
10/// and mostly: https://docs.rs/polars/latest/polars/prelude/enum.AggExpr.html
11/// https://docs.rs/polars-lazy/latest/polars_lazy/dsl/enum.AggExpr.html
12#[derive(Clone, Copy, Debug, PartialEq)]
13pub enum Aggregate {
14    Min,
15    Max,
16    Median,
17    NUnique,
18    First,
19    Last,
20    Mean,
21    MeanDistinct,
22    List,
23    Count,
24    CountDistinct,
25    Quantile(f64),
26    Quantiles(&'static [f64]),
27    Sum,
28    SumDistinct,
29    AggGroups,
30    Std,
31    StdDistinct,
32    Var,
33    VarDistinct,
34}
35
36// TODO make sure f64::nan do not happen
37impl Eq for Aggregate {}
38
39#[allow(clippy::derive_hash_xor_eq)]
40impl hash::Hash for Aggregate {
41    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
42        mem::discriminant(self).hash(state);
43        match self {
44            Aggregate::Quantile(q) => {
45                mem::discriminant(self).hash(state);
46                q.to_be_bytes().hash(state);
47            }
48            Aggregate::Quantiles(v) => {
49                mem::discriminant(self).hash(state);
50                v.iter().for_each(|q| q.to_be_bytes().hash(state));
51            }
52            _ => mem::discriminant(self).hash(state),
53        }
54    }
55}
56
57impl Aggregate {
58    /// Return the function object implementing the function
59    pub fn super_image(self, set: &DataType) -> Result<DataType> {
60        Ok(implementation::aggregate(self).super_image(&set)?)
61    }
62
63    /// Return the function object implementing the function
64    pub fn value(self, arg: &Value) -> Result<Value> {
65        Ok(implementation::aggregate(self).value(&arg)?)
66    }
67}
68
69impl fmt::Display for Aggregate {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Aggregate::Min => write!(f, "min"),
73            Aggregate::Max => write!(f, "max"),
74            Aggregate::Median => write!(f, "median"),
75            Aggregate::NUnique => write!(f, "n_unique"),
76            Aggregate::First => write!(f, "first"),
77            Aggregate::Last => write!(f, "last"),
78            Aggregate::Mean => write!(f, "mean"),
79            Aggregate::List => write!(f, "list"),
80            Aggregate::Count => write!(f, "count"),
81            Aggregate::Quantile(q) => write!(f, "quantile<{q}>"),
82            Aggregate::Quantiles(v) => write!(
83                f,
84                "quantiles<{}>",
85                v.iter().map(|q| format!("{q}")).join(", ")
86            ),
87            Aggregate::Sum => write!(f, "sum"),
88            Aggregate::AggGroups => write!(f, "agg_groups"),
89            Aggregate::Std => write!(f, "std"),
90            Aggregate::Var => write!(f, "var"),
91            Aggregate::MeanDistinct => write!(f, "mean_distinct"),
92            Aggregate::CountDistinct => write!(f, "count_distinct"),
93            Aggregate::SumDistinct => write!(f, "sum_distinct"),
94            Aggregate::StdDistinct => write!(f, "std_distinct"),
95            Aggregate::VarDistinct => write!(f, "var_distinct"),
96        }
97    }
98}