quill_sql/function/aggregate/
mod.rs1mod avg;
2mod count;
3
4pub use avg::AvgAccumulator;
5pub use count::CountAccumulator;
6use std::fmt::Debug;
7
8use crate::error::QuillSQLResult;
9use crate::utils::scalar::ScalarValue;
10use strum::{EnumIter, IntoEnumIterator};
11
12#[derive(Clone, PartialEq, Eq, Debug, EnumIter)]
13pub enum AggregateFunctionKind {
14 Count,
15 Avg,
16}
17
18impl AggregateFunctionKind {
19 pub fn create_accumulator(&self) -> Box<dyn Accumulator> {
20 match self {
21 AggregateFunctionKind::Count => Box::new(CountAccumulator::new()),
22 AggregateFunctionKind::Avg => Box::new(AvgAccumulator::new()),
23 }
24 }
25
26 pub fn find(name: &str) -> Option<Self> {
27 AggregateFunctionKind::iter().find(|kind| kind.to_string().eq_ignore_ascii_case(name))
28 }
29}
30
31impl std::fmt::Display for AggregateFunctionKind {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 write!(f, "{self:?}")
34 }
35}
36
37pub trait Accumulator: Send + Sync + Debug {
38 fn update_value(&mut self, value: &ScalarValue) -> QuillSQLResult<()>;
40
41 fn evaluate(&self) -> QuillSQLResult<ScalarValue>;
43}