Skip to main content

schema_core/config/
aggregate.rs

1use serde::{Deserialize, Serialize};
2
3use crate::common;
4
5use super::{AggregateKey, Filter, FlussoType};
6
7/// Reduces rows from a related `table` to a single value — a count, sum, or
8/// extreme. The `key` connects the tables; `filters` restrict which rows count.
9#[derive(Debug, Clone, Hash, Serialize, Deserialize)]
10pub struct Aggregate {
11    pub table: common::TableName,
12    pub op: AggregateOp,
13    pub key: AggregateKey,
14    /// The declared result type. Fixed for `count` (`long`) and `avg` (`double`)
15    /// and left `None`; required for `sum` / `min` / `max`, whose result mirrors
16    /// the aggregated column and so must be stated to stay database-free.
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub value_type: Option<FlussoType>,
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub filters: Option<Vec<Filter>>,
21}
22
23#[derive(Debug, Clone, Hash, Serialize, Deserialize)]
24#[serde(rename_all = "snake_case")]
25pub enum AggregateOp {
26    Count,
27    Sum(common::ColumnName),
28    Avg(common::ColumnName),
29    Min(common::ColumnName),
30    Max(common::ColumnName),
31    /// Collect the related table's primary keys into a flat scalar array. The
32    /// element type is stated explicitly (the schema names it) so the array's
33    /// mapping is known without touching the database.
34    Ids {
35        element_type: FlussoType,
36    },
37}