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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use {
    super::ExprNode,
    crate::{
        ast::{Aggregate, CountArgExpr},
        parse_sql::parse_expr,
        result::{Error, Result},
        translate::translate_expr,
    },
};

#[derive(Clone, Debug)]
pub enum AggregateNode<'a> {
    Count(CountArgExprNode<'a>),
    Sum(ExprNode<'a>),
    Min(ExprNode<'a>),
    Max(ExprNode<'a>),
    Avg(ExprNode<'a>),
    Variance(ExprNode<'a>),
    Stdev(ExprNode<'a>),
}

#[derive(Clone, Debug)]
pub enum CountArgExprNode<'a> {
    Text(String),
    Expr(ExprNode<'a>),
}

impl<'a> From<&'a str> for CountArgExprNode<'a> {
    fn from(count_arg_str: &str) -> Self {
        Self::Text(count_arg_str.to_owned())
    }
}

impl<'a> From<ExprNode<'a>> for CountArgExprNode<'a> {
    fn from(expr_node: ExprNode<'a>) -> Self {
        Self::Expr(expr_node)
    }
}

impl<'a> TryFrom<CountArgExprNode<'a>> for CountArgExpr {
    type Error = Error;

    fn try_from(count_expr_node: CountArgExprNode<'a>) -> Result<Self> {
        match count_expr_node {
            CountArgExprNode::Text(s) if &s == "*" => Ok(CountArgExpr::Wildcard),
            CountArgExprNode::Text(s) => {
                let expr = parse_expr(s).and_then(|expr| translate_expr(&expr))?;

                Ok(CountArgExpr::Expr(expr))
            }
            CountArgExprNode::Expr(expr_node) => expr_node.try_into().map(CountArgExpr::Expr),
        }
    }
}

impl<'a> TryFrom<AggregateNode<'a>> for Aggregate {
    type Error = Error;

    fn try_from(aggr_node: AggregateNode<'a>) -> Result<Self> {
        match aggr_node {
            AggregateNode::Count(count_arg_expr_node) => {
                count_arg_expr_node.try_into().map(Aggregate::Count)
            }
            AggregateNode::Sum(expr_node) => expr_node.try_into().map(Aggregate::Sum),
            AggregateNode::Min(expr_node) => expr_node.try_into().map(Aggregate::Min),
            AggregateNode::Max(expr_node) => expr_node.try_into().map(Aggregate::Max),
            AggregateNode::Avg(expr_node) => expr_node.try_into().map(Aggregate::Avg),
            AggregateNode::Variance(expr_node) => expr_node.try_into().map(Aggregate::Variance),
            AggregateNode::Stdev(expr_node) => expr_node.try_into().map(Aggregate::Stdev),
        }
    }
}

impl<'a> ExprNode<'a> {
    pub fn count(self) -> Self {
        count(self)
    }

    pub fn sum(self) -> Self {
        sum(self)
    }

    pub fn min(self) -> Self {
        min(self)
    }

    pub fn max(self) -> Self {
        max(self)
    }

    pub fn avg(self) -> Self {
        avg(self)
    }

    pub fn variance(self) -> Self {
        variance(self)
    }

    pub fn stdev(self) -> Self {
        stdev(self)
    }
}

pub fn count<'a, T: Into<CountArgExprNode<'a>>>(expr: T) -> ExprNode<'a> {
    ExprNode::Aggregate(Box::new(AggregateNode::Count(expr.into())))
}

pub fn sum<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
    ExprNode::Aggregate(Box::new(AggregateNode::Sum(expr.into())))
}

pub fn min<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
    ExprNode::Aggregate(Box::new(AggregateNode::Min(expr.into())))
}

pub fn max<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
    ExprNode::Aggregate(Box::new(AggregateNode::Max(expr.into())))
}

pub fn avg<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
    ExprNode::Aggregate(Box::new(AggregateNode::Avg(expr.into())))
}

pub fn variance<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
    ExprNode::Aggregate(Box::new(AggregateNode::Variance(expr.into())))
}

pub fn stdev<'a, T: Into<ExprNode<'a>>>(expr: T) -> ExprNode<'a> {
    ExprNode::Aggregate(Box::new(AggregateNode::Stdev(expr.into())))
}

#[cfg(test)]
mod tests {
    use crate::ast_builder::{avg, col, count, max, min, stdev, sum, test_expr, variance};

    #[test]
    fn aggregate() {
        let actual = col("id").count();
        let expected = "COUNT(id)";
        test_expr(actual, expected);

        let actual = count("id");
        let expected = "COUNT(id)";
        test_expr(actual, expected);

        let actual = count("*");
        let expected = "COUNT(*)";
        test_expr(actual, expected);

        let actual = col("amount").sum();
        let expected = "SUM(amount)";
        test_expr(actual, expected);

        let actual = sum("amount");
        let expected = "SUM(amount)";
        test_expr(actual, expected);

        let actual = col("budget").min();
        let expected = "MIN(budget)";
        test_expr(actual, expected);
        let actual = min("budget");
        let expected = "MIN(budget)";
        test_expr(actual, expected);

        let actual = col("score").max();
        let expected = "MAX(score)";
        test_expr(actual, expected);

        let actual = max("score");
        let expected = "MAX(score)";
        test_expr(actual, expected);

        let actual = col("grade").avg();
        let expected = "AVG(grade)";
        test_expr(actual, expected);

        let actual = avg("grade");
        let expected = "AVG(grade)";
        test_expr(actual, expected);

        let actual = col("statistic").variance();
        let expected = "VARIANCE(statistic)";
        test_expr(actual, expected);

        let actual = variance("statistic");
        let expected = "VARIANCE(statistic)";
        test_expr(actual, expected);

        let actual = col("scatterplot").stdev();
        let expected = "STDEV(scatterplot)";
        test_expr(actual, expected);

        let actual = stdev("scatterplot");
        let expected = "STDEV(scatterplot)";
        test_expr(actual, expected);
    }
}