pgevolve_core/ir/
aggregate.rs1use serde::{Deserialize, Serialize};
5
6use crate::identifier::{Identifier, QualifiedName};
7use crate::ir::column_type::ColumnType;
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct Aggregate {
12 pub qname: QualifiedName,
14 pub arg_types: Vec<ColumnType>,
16 pub state_type: ColumnType,
18 pub sfunc: QualifiedName,
20 pub finalfunc: Option<QualifiedName>,
22 pub initcond: Option<String>,
24 pub owner: Option<Identifier>,
26 pub comment: Option<String>,
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33
34 fn id(s: &str) -> Identifier {
35 Identifier::from_unquoted(s).unwrap()
36 }
37
38 fn qname(schema: &str, name: &str) -> QualifiedName {
39 QualifiedName::new(id(schema), id(name))
40 }
41
42 fn sample_aggregate() -> Aggregate {
43 Aggregate {
44 qname: qname("app", "my_sum"),
45 arg_types: vec![ColumnType::Integer],
46 state_type: ColumnType::BigInt,
47 sfunc: qname("app", "my_sum_sfunc"),
48 finalfunc: Some(qname("app", "my_sum_final")),
49 initcond: Some("0".to_string()),
50 owner: Some(id("app_owner")),
51 comment: Some("A custom sum aggregate.".to_string()),
52 }
53 }
54
55 #[test]
56 fn aggregate_serde_round_trip() {
57 let agg = sample_aggregate();
58 let json = serde_json::to_string(&agg).unwrap();
59 let back: Aggregate = serde_json::from_str(&json).unwrap();
60 assert_eq!(agg, back);
61 }
62}