Skip to main content

uqa_sql/ast/
relation_hierarchy.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Inheritance and declarative-partitioning catalog nodes.
8
9use serde::{Deserialize, Serialize};
10
11use super::{AutoIncrement, Expr, ForeignKey, TableKeyConstraint};
12
13/// Metadata shared by ordinary inheritance and declarative partitioning.
14#[derive(Debug, Clone, Default, Serialize, Deserialize)]
15pub struct TableHierarchy {
16    /// Direct parents in declaration order. Names are parse-time identities in the statement AST and canonical catalog identities after registration.
17    #[serde(default, skip_serializing_if = "Vec::is_empty")]
18    pub parents: Vec<String>,
19    /// Durable `pg_inherits.inhseqno` values aligned with `parents`.
20    /// Catalogs written before ALTER inheritance support leave this empty and
21    /// therefore use the declaration-order sequence `1..=parents.len()`.
22    #[serde(default, skip_serializing_if = "Vec::is_empty")]
23    pub parent_sequence_numbers: Vec<i32>,
24    /// Partition key owned by a partitioned relation.
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub partition_spec: Option<PartitionSpec>,
27    /// Bound owned by a child declared with `PARTITION OF`.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub partition_bound: Option<PartitionBound>,
30    /// Columns declared by this relation before inherited columns were merged into its stored row type. `PostgreSQL` exposes this provenance through `pg_attribute.attislocal`; keeping it in durable hierarchy metadata distinguishes an explicitly redeclared inherited column from a purely inherited one after reopen.
31    #[serde(default, skip_serializing_if = "Vec::is_empty")]
32    pub local_columns: Vec<String>,
33    /// Original local sequence metadata hidden while an attached partition uses a parent's identity generator. `PostgreSQL` keeps a pre-existing SERIAL default and restores its behavior after DETACH.
34    #[serde(default, skip_serializing_if = "Vec::is_empty")]
35    pub partition_identity_overrides: Vec<PartitionIdentityOverride>,
36    /// Key constraints copied from a partitioned parent while this relation is attached. The exact copies are retained so DETACH removes only inherited entries and preserves equivalent constraints declared locally before ATTACH.
37    #[serde(default, skip_serializing_if = "Vec::is_empty")]
38    pub partition_inherited_key_constraints: Vec<TableKeyConstraint>,
39    /// Foreign keys copied from a partitioned parent while this relation is attached. The exact copies are retained so DETACH removes only inherited entries and preserves equivalent constraints declared locally before ATTACH.
40    #[serde(default, skip_serializing_if = "Vec::is_empty")]
41    pub partition_inherited_foreign_keys: Vec<ForeignKey>,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct PartitionIdentityOverride {
46    pub column: String,
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub original: Option<AutoIncrement>,
49}
50
51impl TableHierarchy {
52    #[must_use]
53    pub const fn is_partition(&self) -> bool {
54        self.partition_bound.is_some()
55    }
56
57    #[must_use]
58    pub fn parent_sequence_number(&self, index: usize) -> i32 {
59        self.parent_sequence_numbers
60            .get(index)
61            .copied()
62            .unwrap_or_else(|| i32::try_from(index + 1).unwrap_or(i32::MAX))
63    }
64
65    #[must_use]
66    pub fn next_parent_sequence_number(&self) -> i32 {
67        self.parents
68            .iter()
69            .enumerate()
70            .map(|(index, _)| self.parent_sequence_number(index))
71            .max()
72            .unwrap_or(0)
73            .saturating_add(1)
74    }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78pub enum PartitionStrategy {
79    List,
80    Range,
81    Hash,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct PartitionSpec {
86    pub strategy: PartitionStrategy,
87    pub keys: Vec<Expr>,
88}
89
90#[derive(Debug, Clone, Serialize, Deserialize)]
91pub enum PartitionBound {
92    Default,
93    List(Vec<Expr>),
94    Range {
95        lower: Vec<PartitionRangeDatum>,
96        upper: Vec<PartitionRangeDatum>,
97    },
98    Hash {
99        modulus: i32,
100        remainder: i32,
101    },
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub enum PartitionRangeDatum {
106    MinValue,
107    Value(Expr),
108    MaxValue,
109}