1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::{fmt, hash, mem};

use itertools::Itertools;

use super::{implementation, Result};
use crate::data_type::{value::Value, DataType};

/// The list of operators
/// inspired by: https://docs.rs/sqlparser/latest/sqlparser/ast/enum.BinaryOperator.html
/// and mostly: https://docs.rs/polars/latest/polars/prelude/enum.AggExpr.html
/// https://docs.rs/polars-lazy/latest/polars_lazy/dsl/enum.AggExpr.html
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Aggregate {
    Min,
    Max,
    Median,
    NUnique,
    First,
    Last,
    Mean,
    List,
    Count,
    Quantile(f64),
    Quantiles(&'static [f64]),
    Sum,
    AggGroups,
    Std,
    Var,
}

// TODO make sure f64::nan do not happen
impl Eq for Aggregate {}

#[allow(clippy::derive_hash_xor_eq)]
impl hash::Hash for Aggregate {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        mem::discriminant(self).hash(state);
        match self {
            Aggregate::Quantile(q) => {
                mem::discriminant(self).hash(state);
                q.to_be_bytes().hash(state);
            }
            Aggregate::Quantiles(v) => {
                mem::discriminant(self).hash(state);
                v.iter().for_each(|q| q.to_be_bytes().hash(state));
            }
            _ => mem::discriminant(self).hash(state),
        }
    }
}

impl Aggregate {
    /// Return the function object implementing the function
    pub fn super_image(self, set: &DataType) -> Result<DataType> {
        Ok(implementation::aggregate(self).super_image(&set)?)
    }

    /// Return the function object implementing the function
    pub fn value(self, arg: &Value) -> Result<Value> {
        Ok(implementation::aggregate(self).value(&arg)?)
    }
}

impl fmt::Display for Aggregate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Aggregate::Min => write!(f, "min"),
            Aggregate::Max => write!(f, "max"),
            Aggregate::Median => write!(f, "median"),
            Aggregate::NUnique => write!(f, "n_unique"),
            Aggregate::First => write!(f, "first"),
            Aggregate::Last => write!(f, "last"),
            Aggregate::Mean => write!(f, "mean"),
            Aggregate::List => write!(f, "list"),
            Aggregate::Count => write!(f, "count"),
            Aggregate::Quantile(q) => write!(f, "quantile<{q}>"),
            Aggregate::Quantiles(v) => write!(
                f,
                "quantiles<{}>",
                v.iter().map(|q| format!("{q}")).join(", ")
            ),
            Aggregate::Sum => write!(f, "sum"),
            Aggregate::AggGroups => write!(f, "agg_groups"),
            Aggregate::Std => write!(f, "std"),
            Aggregate::Var => write!(f, "var"),
        }
    }
}