pgevolve_core/ir/
partition.rs1use serde::{Deserialize, Serialize};
5
6use crate::identifier::{Identifier, QualifiedName};
7use crate::ir::default_expr::NormalizedExpr;
8
9#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub struct PartitionBy {
12 pub strategy: PartitionStrategy,
14 pub columns: Vec<PartitionColumn>,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
20#[serde(rename_all = "snake_case")]
21pub enum PartitionStrategy {
22 Range,
24 List,
26 Hash,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
32pub struct PartitionColumn {
33 pub kind: PartitionColumnKind,
35 pub collation: Option<QualifiedName>,
37 pub opclass: Option<QualifiedName>,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
43#[serde(rename_all = "snake_case")]
44pub enum PartitionColumnKind {
45 Column(Identifier),
47 Expr(NormalizedExpr),
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
55pub struct PartitionOf {
56 pub parent: QualifiedName,
58 pub bounds: PartitionBounds,
60}
61
62#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
64#[serde(rename_all = "snake_case", tag = "kind")]
65pub enum PartitionBounds {
66 Range {
68 from: Vec<BoundDatum>,
70 to: Vec<BoundDatum>,
72 },
73 List {
75 values: Vec<BoundDatum>,
77 },
78 Hash {
80 modulus: u32,
82 remainder: u32,
84 },
85 Default,
87}
88
89#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
91#[serde(rename_all = "snake_case")]
92pub enum BoundDatum {
93 Literal(NormalizedExpr),
95 MinValue,
97 MaxValue,
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn partition_strategy_round_trip() {
107 let s = PartitionStrategy::Range;
108 let json = serde_json::to_string(&s).unwrap();
109 let back: PartitionStrategy = serde_json::from_str(&json).unwrap();
110 assert_eq!(s, back);
111 }
112
113 #[test]
114 fn hash_bounds_round_trip() {
115 let b = PartitionBounds::Hash {
116 modulus: 4,
117 remainder: 1,
118 };
119 let json = serde_json::to_string(&b).unwrap();
120 let back: PartitionBounds = serde_json::from_str(&json).unwrap();
121 assert_eq!(b, back);
122 }
123
124 #[test]
125 fn default_bounds_round_trip() {
126 let b = PartitionBounds::Default;
127 let json = serde_json::to_string(&b).unwrap();
128 let back: PartitionBounds = serde_json::from_str(&json).unwrap();
129 assert_eq!(b, back);
130 }
131}