Skip to main content

alopex_sql/planner/
aggregate_expr.rs

1use crate::planner::typed_expr::TypedExpr;
2use crate::planner::types::ResolvedType;
3
4/// Supported aggregate function types.
5#[derive(Debug, Clone, PartialEq)]
6pub enum AggregateFunction {
7    Count,
8    Sum,
9    Total,
10    Avg,
11    Min,
12    Max,
13    GroupConcat { separator: Option<String> },
14    StringAgg { separator: Option<String> },
15}
16
17/// Aggregate expression definition.
18#[derive(Debug, Clone)]
19pub struct AggregateExpr {
20    pub function: AggregateFunction,
21    pub arg: Option<TypedExpr>,
22    pub distinct: bool,
23    pub result_type: ResolvedType,
24}
25
26impl AggregateExpr {
27    pub fn count_star() -> Self {
28        Self {
29            function: AggregateFunction::Count,
30            arg: None,
31            distinct: false,
32            result_type: ResolvedType::BigInt,
33        }
34    }
35
36    pub fn count(arg: TypedExpr, distinct: bool) -> Self {
37        Self {
38            function: AggregateFunction::Count,
39            arg: Some(arg),
40            distinct,
41            result_type: ResolvedType::BigInt,
42        }
43    }
44
45    pub fn sum(arg: TypedExpr) -> Self {
46        Self {
47            function: AggregateFunction::Sum,
48            arg: Some(arg),
49            distinct: false,
50            result_type: ResolvedType::Double,
51        }
52    }
53
54    pub fn total(arg: TypedExpr) -> Self {
55        Self {
56            function: AggregateFunction::Total,
57            arg: Some(arg),
58            distinct: false,
59            result_type: ResolvedType::Double,
60        }
61    }
62
63    pub fn avg(arg: TypedExpr) -> Self {
64        Self {
65            function: AggregateFunction::Avg,
66            arg: Some(arg),
67            distinct: false,
68            result_type: ResolvedType::Double,
69        }
70    }
71
72    pub fn min(arg: TypedExpr) -> Self {
73        let result_type = arg.resolved_type.clone();
74        Self {
75            function: AggregateFunction::Min,
76            arg: Some(arg),
77            distinct: false,
78            result_type,
79        }
80    }
81
82    pub fn max(arg: TypedExpr) -> Self {
83        let result_type = arg.resolved_type.clone();
84        Self {
85            function: AggregateFunction::Max,
86            arg: Some(arg),
87            distinct: false,
88            result_type,
89        }
90    }
91}