Skip to main content

uqa_engine/capabilities/
composites.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind composite type DDL to namespace, identity and publication state, and expose composite attributes to value conversion.
8
9use crate::Engine;
10use std::collections::BTreeMap;
11use std::sync::Arc;
12use uqa_execution::catalog::composite_type::{
13    CompositeRegistry, CompositeRegistryPublication, CompositeRegistryRead,
14};
15use uqa_execution::schema::composites::attributes::CompositeAttributeContext;
16use uqa_execution::schema::composites::values::{CompositeValueContext, CompositeValueTables};
17use uqa_execution::schema::composites::CompositeTypeContext;
18use uqa_sql::catalog::roles::RoleDefinition;
19use uqa_sql::expr::composites::{CompositeTypeCatalog, CompositeTypeDescriptor};
20use uqa_sql::SQLError;
21
22impl Engine {
23    pub(crate) fn composite_type_context(&self) -> CompositeTypeContext<'_> {
24        CompositeTypeContext {
25            shells: self.shell_completion_context(),
26            creation: self.relation_creation_context(),
27            identities: self.catalog_identity_reservation_context(),
28            writer: self,
29            types: self,
30            allocate_identity: || {
31                crate::new_nonzero_catalog_identity("composite", "object identity")
32                    .map_err(|error| SQLError::Internal(error.to_string()))
33            },
34            publication: self,
35            enums: self,
36            domains: self,
37            changes: self,
38        }
39    }
40
41    pub(crate) fn composite_value_context(&self) -> CompositeValueContext<'_> {
42        CompositeValueContext {
43            memory: self.session.as_ref(),
44            cancellation: &self.runtime.cancellation,
45            tables: self,
46            reads: self,
47            writes: self,
48            views: self.view_dependency_context(),
49            types: self,
50            catalogs:
51                uqa_execution::schema::composites::catalog_values::CompositeCatalogValueContext {
52                    schema: self.schema_dependency_publication_context(),
53                    domains: self,
54                    events: self.event_catalog_context(),
55                    routines: self.routine_mutation_context(),
56                    indexes: uqa_execution::schema::indexes::routines::IndexRoutineContext {
57                        registry: self,
58                        catalog: self.storage.catalog.as_deref(),
59                    },
60                    index_publication: self,
61                    types: self,
62                    bindings: self,
63                    resolution: self,
64                },
65        }
66    }
67
68    pub(crate) fn composite_attribute_context(&self) -> CompositeAttributeContext<'_> {
69        CompositeAttributeContext {
70            publication: self,
71            values: self.composite_value_context(),
72            changes: self,
73        }
74    }
75
76    /// Composite definitions restore after enums and before domains and relations, whose declared types can refer to them.
77    pub(crate) fn restore_composites_from_catalog(
78        &self,
79        catalog: &dyn uqa_storage::CatalogFacade,
80    ) -> uqa_storage::StorageBackendResult<()> {
81        let registry =
82            uqa_execution::catalog::composite_type::restore(catalog, &self.durable.roles.read())?;
83        *self.durable.composites.write() = registry;
84        Ok(())
85    }
86
87    /// The composite registry generation of the running statement: the pinned query snapshot when one exists, otherwise the session's current definitions.
88    pub(crate) fn composite_registry_snapshot(&self) -> Arc<CompositeRegistry> {
89        self.query_catalog_snapshot.as_ref().map_or_else(
90            || self.durable.composites.snapshot(),
91            |snapshot| Arc::clone(&snapshot.composites),
92        )
93    }
94}
95
96impl CompositeTypeCatalog for Engine {
97    fn composite_type_with_control(
98        &self,
99        type_oid: u32,
100        control: &uqa_core::memory::ProductionControl<'_>,
101    ) -> Result<Option<uqa_sql::expr::composites::OwnedCompositeTypeDescriptor>, SQLError> {
102        if let Some(descriptor) = self
103            .runtime
104            .composite_descriptor_cache
105            .descriptor_with_control(&self.composite_registry_snapshot(), type_oid, control)?
106        {
107            return Ok(Some(descriptor));
108        }
109        uqa_execution::catalog::composite_type::relations::descriptor_with_control(
110            &self.catalog_execution(),
111            type_oid,
112            control,
113        )
114    }
115
116    fn composite_type(
117        &self,
118        type_oid: u32,
119    ) -> Result<Option<Arc<CompositeTypeDescriptor>>, SQLError> {
120        let descriptor = self
121            .runtime
122            .composite_descriptor_cache
123            .descriptor(&self.composite_registry_snapshot(), type_oid);
124        match descriptor {
125            Some(descriptor) => Ok(Some(descriptor)),
126            None => uqa_execution::catalog::composite_type::relations::descriptor(
127                &self.catalog_execution(),
128                type_oid,
129            ),
130        }
131    }
132}
133
134impl CompositeValueTables for Engine {
135    fn composite_value_tables(
136        &self,
137    ) -> Result<Vec<(String, Vec<uqa_sql::ast::ColumnDef>)>, SQLError> {
138        let catalog = self.catalog_read_view();
139        Ok(catalog
140            .snapshot()
141            .tables
142            .iter()
143            .map(|(identity, table)| (identity.qualified_name(), table.columns.as_ref().clone()))
144            .collect())
145    }
146}
147
148impl CompositeRegistryPublication for Engine {
149    fn composite_registry(&self) -> CompositeRegistryRead<'_> {
150        Box::new(self.durable.composites.read())
151    }
152    fn composite_catalog(&self) -> Option<&dyn uqa_storage::CatalogFacade> {
153        self.storage.catalog.as_deref()
154    }
155    fn composite_role_definitions(&self) -> BTreeMap<String, RoleDefinition> {
156        self.durable.roles.read().clone()
157    }
158    fn publish_composite_definitions(&self, registry: CompositeRegistry) {
159        *self.durable.composites.write() = registry;
160    }
161}