Skip to main content

uqa_engine/session/
schemas.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Schema/catalog enumeration and schema lifecycle.
8
9use super::{CatalogIndexRow, Engine, StorageBackendResult};
10
11pub(crate) use uqa_sql::catalog::is_virtual_system_schema;
12
13impl Engine {
14    pub fn list_catalog_indexes(&self) -> StorageBackendResult<Vec<CatalogIndexRow>> {
15        self.with_catalog_read_snapshot(Self::catalog_indexes_in_execution)
16    }
17
18    pub(crate) fn catalog_indexes_in_execution(
19        &self,
20    ) -> StorageBackendResult<Vec<CatalogIndexRow>> {
21        if let Some(snapshot) = self.query_catalog_snapshot.as_ref() {
22            let mut out = snapshot
23                .catalog_indexes
24                .values()
25                .cloned()
26                .collect::<Vec<_>>();
27            out.sort_by(|a, b| a.relation.cmp(&b.relation));
28            return Ok(out);
29        }
30        self.synchronize_catalog_registries()?;
31        let mut out: Vec<CatalogIndexRow> = self
32            .durable
33            .catalog_indexes
34            .read()
35            .values()
36            .cloned()
37            .collect();
38        out.sort_by(|a, b| a.relation.cmp(&b.relation));
39        Ok(out)
40    }
41
42    /// Create a durable schema catalog object. Returns `true` when a new
43    /// schema was created and `false` only for `IF NOT EXISTS`.
44    pub fn register_schema(&self, name: &str, if_not_exists: bool) -> StorageBackendResult<bool> {
45        self.with_implicit_storage_transaction(|engine| {
46            uqa_execution::schema::namespaces::register_api_schema(
47                &engine.schema_creation_context(),
48                name,
49                if_not_exists,
50            )
51            .map_err(|error| uqa_storage::StorageBackendError::backend("CREATE SCHEMA", error))
52        })
53    }
54
55    /// Drop an empty durable schema. Virtual system namespaces cannot be removed.
56    pub fn drop_schema(&self, name: &str) -> StorageBackendResult<bool> {
57        self.with_implicit_storage_transaction(|engine| {
58            uqa_execution::schema::namespaces::removal::drop_empty_schema(
59                &engine.empty_schema_removal_context(),
60                name,
61            )
62        })
63    }
64
65    pub fn has_schema(&self, name: &str) -> StorageBackendResult<bool> {
66        self.with_catalog_read_snapshot(|engine| {
67            engine.synchronize_catalog_registries()?;
68            Ok(engine.durable.schemas.read().contains_key(name))
69        })
70    }
71
72    /// Whether `name` resolves as a namespace: a durable schema, a virtual
73    /// system schema (`pg_catalog`, `information_schema`, `ag_catalog`), or
74    /// the namespace a named graph owns.
75    pub fn has_namespace(&self, name: &str) -> StorageBackendResult<bool> {
76        self.with_catalog_read_snapshot(|engine| engine.has_namespace_in_execution(name))
77    }
78
79    pub(crate) fn has_namespace_in_execution(&self, name: &str) -> StorageBackendResult<bool> {
80        self.synchronize_catalog_registries()?;
81        Ok(is_virtual_system_schema(name)
82            || self.durable.schemas.read().contains_key(name)
83            || self.durable.graphs.read().contains_key(name))
84    }
85
86    pub(crate) fn validate_stored_schema_name(name: &str) -> StorageBackendResult<()> {
87        crate::capabilities::validate_stored_schema_name(name)
88    }
89
90    pub(crate) fn schema_is_empty(&self, schema: &str) -> bool {
91        !self
92            .storage
93            .tables
94            .read()
95            .keys()
96            .any(|relation| relation.schema == schema)
97            && !self
98                .durable
99                .views
100                .read()
101                .keys()
102                .any(|relation| relation.schema == schema)
103            && !self
104                .durable
105                .sequences
106                .read()
107                .keys()
108                .any(|relation| relation.schema == schema)
109            && !self
110                .durable
111                .foreign_tables
112                .read()
113                .keys()
114                .any(|relation| relation.schema == schema)
115            && !self
116                .durable
117                .catalog_indexes
118                .read()
119                .keys()
120                .any(|relation| relation.schema == schema)
121            && !self
122                .durable
123                .domains
124                .read()
125                .values()
126                .any(|domain| domain.identity.schema == schema)
127            && !self
128                .durable
129                .enums
130                .read()
131                .values()
132                .any(|definition| definition.identity.schema == schema)
133            && !self
134                .durable
135                .composites
136                .read()
137                .values()
138                .any(|definition| definition.identity.schema == schema)
139            && !self.durable.sql_user_functions.read().keys().any(|name| {
140                uqa_sql::schema::namespaces::removal::routine_name_occupies_schema(name, schema)
141            })
142    }
143
144    /// Return every registered schema in sorted order.
145    pub fn list_schemas(&self) -> StorageBackendResult<Vec<String>> {
146        self.with_catalog_read_snapshot(|engine| {
147            if let Some(snapshot) = engine.query_catalog_snapshot.as_ref() {
148                return Ok(snapshot.schemas.keys().cloned().collect());
149            }
150            engine.synchronize_catalog_registries()?;
151            Ok(engine.durable.schemas.read().keys().cloned().collect())
152        })
153    }
154
155    /// Local names of tables whose structural relation identity is owned by
156    /// `schema`. No string-prefix inference participates in this lookup.
157    pub fn tables_in_schema(&self, schema: &str) -> StorageBackendResult<Vec<String>> {
158        self.with_catalog_read_snapshot(|engine| engine.schema_tables_in_execution(schema))
159    }
160
161    pub(crate) fn schema_tables_in_execution(
162        &self,
163        schema: &str,
164    ) -> StorageBackendResult<Vec<String>> {
165        self.synchronize_table_catalog()?;
166        let mut out: Vec<String> = Vec::new();
167        for relation in self.storage.tables.read().keys() {
168            if relation.schema == schema {
169                out.push(relation.name.clone());
170            }
171        }
172        out.sort_unstable();
173        Ok(out)
174    }
175
176    pub fn list_sequences(&self) -> StorageBackendResult<Vec<String>> {
177        self.with_catalog_read_snapshot(|engine| Ok(engine.query_sequence_snapshot()?.names()))
178    }
179}