Skip to main content

uqa_sql/ast/
ranges.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Native range families and catalog-bound identities of declared ranges.
8
9use serde::{Deserialize, Serialize};
10
11use super::ColumnType;
12use uqa_core::{
13    memory::{Produced, ProductionControl},
14    ValueRetentionError,
15};
16
17#[cfg(test)]
18mod tests;
19
20/// Physical range containers require at least integer alignment, even when their endpoint is narrower.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
22pub enum RangeAlignment {
23    Integer,
24    Double,
25}
26
27impl RangeAlignment {
28    #[must_use]
29    pub fn for_endpoint(endpoint: &ColumnType) -> Self {
30        if crate::catalog::type_metadata::pg_type_align(endpoint) == "d" {
31            Self::Double
32        } else {
33            Self::Integer
34        }
35    }
36
37    #[must_use]
38    pub const fn catalog_name(self) -> &'static str {
39        match self {
40            Self::Integer => "i",
41            Self::Double => "d",
42        }
43    }
44}
45
46/// Current names and physical layout of one range or multirange type. Identity equality does not depend on a rename or on the endpoint's type identity.
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct RangeTypeReference {
49    pub schema: String,
50    pub name: String,
51    pub oid: u32,
52    pub array_oid: u32,
53    pub alignment: RangeAlignment,
54}
55
56impl PartialEq for RangeTypeReference {
57    fn eq(&self, other: &Self) -> bool {
58        self.oid == other.oid
59    }
60}
61
62impl Eq for RangeTypeReference {}
63
64impl RangeTypeReference {
65    pub fn clone_with_control(
66        &self,
67        control: &ProductionControl<'_>,
68    ) -> Result<Produced<Self>, ValueRetentionError> {
69        let schema = control.copy_text(&self.schema)?;
70        let name = control.copy_text(&self.name)?;
71        let (schema, schema_memory) = schema.into_parts();
72        let (name, name_memory) = name.into_parts();
73        control.finish(
74            Self {
75                schema,
76                name,
77                oid: self.oid,
78                array_oid: self.array_oid,
79                alignment: self.alignment,
80            },
81            control.combine(schema_memory, name_memory),
82        )
83    }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
87pub enum RangeSubtype {
88    Integer,
89    BigInteger,
90    Numeric,
91    Date,
92    Timestamp,
93    TimestampTz,
94}
95
96impl RangeSubtype {
97    #[must_use]
98    pub const fn range_name(self) -> &'static str {
99        match self {
100            Self::Integer => "int4range",
101            Self::BigInteger => "int8range",
102            Self::Numeric => "numrange",
103            Self::Date => "daterange",
104            Self::Timestamp => "tsrange",
105            Self::TimestampTz => "tstzrange",
106        }
107    }
108
109    #[must_use]
110    pub const fn multirange_name(self) -> &'static str {
111        match self {
112            Self::Integer => "int4multirange",
113            Self::BigInteger => "int8multirange",
114            Self::Numeric => "nummultirange",
115            Self::Date => "datemultirange",
116            Self::Timestamp => "tsmultirange",
117            Self::TimestampTz => "tstzmultirange",
118        }
119    }
120
121    #[must_use]
122    pub const fn scalar_type(self) -> ColumnType {
123        match self {
124            Self::Integer => ColumnType::Integer,
125            Self::BigInteger => ColumnType::BigInteger,
126            Self::Numeric => ColumnType::Numeric {
127                precision: None,
128                scale: None,
129            },
130            Self::Date => ColumnType::Date,
131            Self::Timestamp => ColumnType::Timestamp,
132            Self::TimestampTz => ColumnType::TimestampTz,
133        }
134    }
135}