Skip to main content

uqa_sql/expr/range/
descriptor.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Pinned type metadata for observing admitted range payloads without invoking input or canonicalization.
8
9use crate::{catalog::type_metadata, expr::SQLValueCatalog, ColumnType, SQLError};
10use uqa_core::memory::{Produced, ProductionControl};
11
12#[derive(Debug, Clone)]
13pub struct RangeValueDescriptor {
14    pub range_oid: u32,
15    pub multirange_oid: u32,
16    pub endpoint: ColumnType,
17}
18
19impl RangeValueDescriptor {
20    /// Copy only the selected metadata under the caller's allowance; no historic catalog generation or input callback is retained.
21    pub fn from_type_with_control(
22        ty: &ColumnType,
23        catalog: Option<&dyn SQLValueCatalog>,
24        control: &ProductionControl<'_>,
25    ) -> Result<Option<Produced<Self>>, SQLError> {
26        control.check()?;
27        let selected;
28        let (range_oid, multirange_oid, endpoint) = match ty {
29            ColumnType::Range(subtype) | ColumnType::Multirange(subtype) => {
30                selected = Self {
31                    range_oid: type_metadata::pg_type_oid(&ColumnType::Range(*subtype)) as u32,
32                    multirange_oid: type_metadata::pg_type_oid(&ColumnType::Multirange(*subtype))
33                        as u32,
34                    endpoint: subtype.scalar_type(),
35                };
36                (
37                    selected.range_oid,
38                    selected.multirange_oid,
39                    &selected.endpoint,
40                )
41            }
42            ColumnType::DeclaredRange(reference) | ColumnType::DeclaredMultirange(reference) => {
43                let descriptor = catalog
44                    .map(|catalog| catalog.value_range_type(reference.oid))
45                    .transpose()?
46                    .flatten()
47                    .ok_or_else(|| missing(reference.oid))?;
48                let expected = if matches!(ty, ColumnType::DeclaredRange(_)) {
49                    descriptor.range_oid
50                } else {
51                    descriptor.multirange_oid
52                };
53                if expected != reference.oid {
54                    return Err(SQLError::Internal(
55                        "inconsistent range type descriptor".into(),
56                    ));
57                }
58                let (endpoint, memory) = descriptor
59                    .endpoint
60                    .clone_with_control(control)?
61                    .into_parts();
62                return Ok(Some(control.finish(
63                    Self {
64                        range_oid: descriptor.range_oid,
65                        multirange_oid: descriptor.multirange_oid,
66                        endpoint,
67                    },
68                    memory,
69                )?));
70            }
71            _ => return Ok(None),
72        };
73        let (endpoint, memory) = endpoint.clone_with_control(control)?.into_parts();
74        Ok(Some(control.finish(
75            Self {
76                range_oid,
77                multirange_oid,
78                endpoint,
79            },
80            memory,
81        )?))
82    }
83
84    /// Resolve the associated range's live identity for a multirange descriptor.
85    pub fn range_type_with_control(
86        &self,
87        catalog: Option<&dyn SQLValueCatalog>,
88        control: &ProductionControl<'_>,
89    ) -> Result<Produced<ColumnType>, SQLError> {
90        control.check()?;
91        if let Some(ty) = type_metadata::builtin_scalar_type(self.range_oid) {
92            return Ok(ty.clone_with_control(control)?);
93        }
94        let ty = catalog
95            .map(|catalog| catalog.value_type_by_oid(self.range_oid))
96            .transpose()?
97            .flatten()
98            .ok_or_else(|| missing(self.range_oid))?;
99        if !matches!(ty, ColumnType::DeclaredRange(_)) {
100            return Err(SQLError::Internal(
101                "inconsistent associated range type".into(),
102            ));
103        }
104        Ok(ty.retain_external_with_control(control)?)
105    }
106}
107
108fn missing(oid: u32) -> SQLError {
109    SQLError::Routine {
110        sqlstate: "42704".into(),
111        message: format!("type with OID {oid} does not exist"),
112    }
113}