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    Avg,
10    Min,
11    Max,
12}
13
14/// Aggregate expression definition.
15#[derive(Debug, Clone)]
16pub struct AggregateExpr {
17    pub function: AggregateFunction,
18    pub arg: Option<TypedExpr>,
19    pub distinct: bool,
20    pub result_type: ResolvedType,
21}
22
23impl AggregateExpr {
24    pub fn count_star() -> Self {
25        Self {
26            function: AggregateFunction::Count,
27            arg: None,
28            distinct: false,
29            result_type: ResolvedType::BigInt,
30        }
31    }
32
33    pub fn count(arg: TypedExpr, distinct: bool) -> Self {
34        Self {
35            function: AggregateFunction::Count,
36            arg: Some(arg),
37            distinct,
38            result_type: ResolvedType::BigInt,
39        }
40    }
41
42    pub fn sum(arg: TypedExpr) -> Self {
43        let result_type = arg.resolved_type.clone();
44        Self {
45            function: AggregateFunction::Sum,
46            arg: Some(arg),
47            distinct: false,
48            result_type,
49        }
50    }
51
52    pub fn avg(arg: TypedExpr) -> Self {
53        Self {
54            function: AggregateFunction::Avg,
55            arg: Some(arg),
56            distinct: false,
57            result_type: ResolvedType::Double,
58        }
59    }
60
61    pub fn min(arg: TypedExpr) -> Self {
62        let result_type = arg.resolved_type.clone();
63        Self {
64            function: AggregateFunction::Min,
65            arg: Some(arg),
66            distinct: false,
67            result_type,
68        }
69    }
70
71    pub fn max(arg: TypedExpr) -> Self {
72        let result_type = arg.resolved_type.clone();
73        Self {
74            function: AggregateFunction::Max,
75            arg: Some(arg),
76            distinct: false,
77            result_type,
78        }
79    }
80}