Skip to main content

uqa_engine/capabilities/
schema_publication.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind schema publication to one retained table generation and its catalog backend.
8use crate::{Engine, TableState};
9use std::sync::Arc;
10use uqa_execution::schema::publication::{
11    SchemaPublicationContext, TableSchemaCatalog, TableSchemaState,
12};
13use uqa_sql::ast::{ColumnDef, TableConstraintSet};
14use uqa_storage::{StorageBackendError, StorageBackendResult};
15
16impl Engine {
17    /// Give the foreign keys that reference partitioned tables the constraints they derive on the referenced partitions, which earlier releases did not record, when a writer opens the database.
18    pub(crate) fn repair_derived_constraints_on_open(&self) -> StorageBackendResult<()> {
19        let needed =
20            uqa_execution::schema::publication::referenced_partitions::derived_constraints_need_repair(
21                &self.schema_publication_context(),
22            )
23            .map_err(|error| uqa_storage::StorageBackendError::backend("derived constraint repair", error))?;
24        if !needed {
25            return Ok(());
26        }
27        self.with_implicit_storage_transaction(|engine| {
28            uqa_execution::schema::publication::referenced_partitions::repair_derived_constraints(
29                &engine.schema_publication_context(),
30            )
31            .map_err(|error| {
32                uqa_storage::StorageBackendError::backend("derived constraint repair", error)
33            })
34        })
35    }
36
37    pub(crate) fn schema_publication_context(&self) -> SchemaPublicationContext<'_> {
38        SchemaPublicationContext {
39            catalog: self,
40            changes: self,
41            types: self,
42            bindings: self.schema_dependency_binding_context(),
43            identities: self.catalog_identity_reservation_context(),
44            indexes: self.index_registry_context(),
45            partitions: self.partition_context(),
46            referencing: self,
47        }
48    }
49}
50impl uqa_execution::schema::publication::referenced_partitions::ReferencingTableAccess for Engine {
51    fn table_names(&self) -> StorageBackendResult<Vec<String>> {
52        self.table_names_in_execution()
53    }
54    fn lock_relation(
55        &self,
56        table: &str,
57        mode: uqa_execution::row_locks::RelationLockMode,
58    ) -> Result<(), uqa_sql::SQLError> {
59        Engine::lock_relation(self, table, mode)
60    }
61}
62struct SchemaTableBinding<'a> {
63    engine: &'a Engine,
64    name: String,
65    state: Arc<TableState>,
66}
67impl TableSchemaCatalog for Engine {
68    fn resolve_table_name(&self, name: &str) -> StorageBackendResult<Option<String>> {
69        self.try_resolve_table_name(name)
70    }
71    fn table_state(
72        &self,
73        canonical: &str,
74    ) -> StorageBackendResult<Option<Box<dyn TableSchemaState + '_>>> {
75        self.try_table(canonical).map(|state| {
76            state.map(|state| {
77                Box::new(SchemaTableBinding {
78                    engine: self,
79                    name: canonical.to_string(),
80                    state,
81                }) as Box<dyn TableSchemaState>
82            })
83        })
84    }
85}
86impl TableSchemaState for SchemaTableBinding<'_> {
87    fn object_id(&self) -> [u8; 16] {
88        self.state.object_id()
89    }
90    fn dependency_constraints(&self) -> TableConstraintSet {
91        TableConstraintSet {
92            checks: self.state.table_checks.read().clone(),
93            foreign_keys: self.state.foreign_keys.read().clone(),
94            key_constraints: self.state.key_constraints.read().clone(),
95            hierarchy: self.state.hierarchy.read().clone(),
96            columns_declared: None,
97            persistence: self.state.persistence,
98            on_commit: self.state.on_commit,
99            catalog_oids: self.state.recorded_catalog_oids(),
100            row_type_array_name: self.state.row_type_array_name.read().clone(),
101            dropped_attributes: self.state.dropped_attributes.read().clone(),
102        }
103    }
104    fn publish_expressions(&self, columns: &[ColumnDef], checks: &[uqa_sql::ast::TableCheck]) {
105        columns.clone_into(&mut *self.state.columns.write());
106        checks.clone_into(&mut *self.state.table_checks.write());
107    }
108
109    fn write_columns(
110        &self,
111    ) -> Box<dyn uqa_execution::schema::publication::columns::ColumnSchemaWrite + '_> {
112        Box::new(SchemaColumnWrite {
113            columns: self.state.columns.write(),
114        })
115    }
116    fn persist_columns(&self, columns: &[ColumnDef]) -> StorageBackendResult<()> {
117        self.engine
118            .try_save_table_schema_with_columns(&self.name, &self.state, columns)
119    }
120    fn constraint_header(&self) -> TableConstraintSet {
121        TableConstraintSet {
122            columns_declared: Some(*self.state.columns_declared.read()),
123            persistence: self.state.persistence,
124            on_commit: self.state.on_commit,
125            hierarchy: self.state.hierarchy.read().clone(),
126            catalog_oids: self.state.recorded_catalog_oids(),
127            row_type_array_name: self.state.row_type_array_name.read().clone(),
128            dropped_attributes: self.state.dropped_attributes.read().clone(),
129            ..TableConstraintSet::default()
130        }
131    }
132    fn publish_constraints(&self, columns: Vec<ColumnDef>, constraints: TableConstraintSet) {
133        *self.state.row_type_array_name.write() = constraints.row_type_array_name;
134        *self.state.dropped_attributes.write() = constraints.dropped_attributes;
135        *self.state.columns.write() = columns;
136        *self.state.table_checks.write() = constraints.checks;
137        *self.state.foreign_keys.write() = constraints.foreign_keys;
138        *self.state.key_constraints.write() = constraints.key_constraints;
139        *self.state.hierarchy.write() = constraints.hierarchy;
140    }
141
142    fn key_constraints(&self) -> Vec<uqa_sql::ast::TableKeyConstraint> {
143        self.state.key_constraints.read().clone()
144    }
145    fn hierarchy(&self) -> uqa_sql::ast::TableHierarchy {
146        self.state.hierarchy.read().clone()
147    }
148    fn publish_hierarchy(&self, hierarchy: uqa_sql::ast::TableHierarchy) {
149        *self.state.hierarchy.write() = hierarchy;
150    }
151    fn columns(&self) -> Vec<ColumnDef> {
152        self.state.columns.read().clone()
153    }
154    fn constraints(&self) -> TableConstraintSet {
155        TableConstraintSet {
156            columns_declared: Some(*self.state.columns_declared.read()),
157            persistence: self.state.persistence,
158            on_commit: self.state.on_commit,
159            checks: self.state.table_checks.read().clone(),
160            foreign_keys: self.state.foreign_keys.read().clone(),
161            key_constraints: self.state.key_constraints.read().clone(),
162            hierarchy: self.state.hierarchy.read().clone(),
163            catalog_oids: self.state.recorded_catalog_oids(),
164            row_type_array_name: self.state.row_type_array_name.read().clone(),
165            dropped_attributes: self.state.dropped_attributes.read().clone(),
166        }
167    }
168    fn columns_declared(&self) -> bool {
169        *self.state.columns_declared.read()
170    }
171    fn mark_statistics_dirty(&self) -> StorageBackendResult<()> {
172        self.engine.mark_column_stats_dirty(&self.name, &self.state)
173    }
174    fn queue_constraint_publication(&self) -> StorageBackendResult<()> {
175        if self.state.persistence != uqa_sql::ast::RelationPersistence::Temporary {
176            if let Some(catalog) = self.engine.storage.catalog.as_ref() {
177                uqa_execution::catalog::tuples::publication::queue_relation(
178                    catalog.as_ref(),
179                    &uqa_core::RelationIdentity::from_legacy_name(&self.name)
180                        .map_err(StorageBackendError::Other)?,
181                    self.state.object_id(),
182                    true,
183                )?;
184            }
185        }
186        Ok(())
187    }
188    fn persist_candidate(
189        &self,
190        columns: &[ColumnDef],
191        constraints: &TableConstraintSet,
192    ) -> StorageBackendResult<()> {
193        self.engine.try_save_table_schema_with_components(
194            &self.name,
195            &self.state,
196            columns,
197            constraints,
198        )
199    }
200    fn persist_expression_names(
201        &self,
202        columns: &[ColumnDef],
203        constraints: &TableConstraintSet,
204    ) -> StorageBackendResult<()> {
205        self.engine.try_save_table_schema_candidate(
206            &self.name,
207            &self.state,
208            columns,
209            constraints,
210            &self.state.security(),
211            uqa_execution::catalog::definition_revision::TablePublicationKind::ExpressionNames,
212        )
213    }
214    fn persist_declaration(
215        &self,
216        columns: &[ColumnDef],
217        constraints: &TableConstraintSet,
218    ) -> StorageBackendResult<()> {
219        self.engine.try_save_table_schema_candidate(
220            &self.name,
221            &self.state,
222            columns,
223            constraints,
224            &self.state.security(),
225            uqa_execution::catalog::definition_revision::TablePublicationKind::Declaration,
226        )
227    }
228    fn publish_columns(
229        &self,
230        columns_declared: bool,
231        columns: Vec<ColumnDef>,
232        constraints: TableConstraintSet,
233    ) {
234        *self.state.row_type_array_name.write() = constraints.row_type_array_name;
235        *self.state.dropped_attributes.write() = constraints.dropped_attributes;
236        *self.state.columns_declared.write() = columns_declared;
237        *self.state.columns.write() = columns;
238        *self.state.table_checks.write() = constraints.checks;
239        *self.state.foreign_keys.write() = constraints.foreign_keys;
240        *self.state.key_constraints.write() = constraints.key_constraints;
241    }
242    fn persist_next_id(&self) -> StorageBackendResult<()> {
243        self.engine.persist_next_id(&self.name)
244    }
245    fn refresh_value_indexes(&self) -> StorageBackendResult<()> {
246        self.engine.refresh_value_indexes_for_table(&self.name)
247    }
248}
249
250impl uqa_execution::schema::publication::SchemaWriteTransaction for Engine {
251    fn with_schema_write(
252        &self,
253        write: uqa_execution::schema::publication::SchemaWrite<'_>,
254    ) -> StorageBackendResult<()> {
255        self.with_implicit_storage_transaction(|engine| {
256            write(&engine.schema_publication_context())?;
257            engine.publish_catalog_constraint_tuples().map_err(|error| {
258                uqa_storage::StorageBackendError::backend("constraint publication", error)
259            })
260        })
261    }
262}
263
264struct SchemaColumnWrite<'a> {
265    columns: parking_lot::MappedRwLockWriteGuard<'a, Vec<ColumnDef>>,
266}
267impl uqa_execution::schema::publication::columns::ColumnSchemaWrite for SchemaColumnWrite<'_> {
268    fn columns(&self) -> &[ColumnDef] {
269        &self.columns
270    }
271    fn publish(&mut self, columns: Vec<ColumnDef>) {
272        *self.columns = columns;
273    }
274}
275
276impl Engine {
277    pub(crate) fn schema_dependency_publication_context(
278        &self,
279    ) -> uqa_execution::schema::publication::dependencies::SchemaDependencyPublicationContext<'_>
280    {
281        uqa_execution::schema::publication::dependencies::SchemaDependencyPublicationContext {
282            tables: self,
283            foreign: self,
284            changes: self,
285        }
286    }
287}
288impl uqa_execution::schema::publication::dependencies::LoadedTableSchemas for Engine {
289    fn table_schemas(
290        &self,
291    ) -> Vec<uqa_execution::schema::publication::dependencies::LoadedTableSchema<'_>> {
292        self.table_entries()
293            .into_iter()
294            .map(|(name, state)| {
295                (
296                    name.clone(),
297                    Box::new(SchemaTableBinding {
298                        engine: self,
299                        name,
300                        state,
301                    }) as Box<dyn TableSchemaState>,
302                )
303            })
304            .collect()
305    }
306}
307impl uqa_execution::schema::publication::dependencies::ForeignSchemaPublication for Engine {
308    fn foreign_tables(
309        &self,
310    ) -> std::collections::BTreeMap<
311        uqa_core::RelationIdentity,
312        uqa_execution::catalog::foreign::StoredForeignTable,
313    > {
314        self.durable.foreign_tables.read().clone()
315    }
316    fn persist_foreign_table(
317        &self,
318        relation: &uqa_core::RelationIdentity,
319        table: &uqa_execution::catalog::foreign::StoredForeignTable,
320    ) -> StorageBackendResult<()> {
321        self.foreign_definition_context()
322            .persist_foreign_table_definition(relation, table)
323    }
324    fn publish_foreign_tables(
325        &self,
326        updates: Vec<(
327            uqa_core::RelationIdentity,
328            uqa_execution::catalog::foreign::StoredForeignTable,
329        )>,
330    ) {
331        let mut tables = self.durable.foreign_tables.write();
332        for (relation, table) in updates {
333            self.note_prepared_catalog_change(
334                uqa_execution::statement::prepared::invalidation::PreparedCatalogChange::Relation(
335                    table.relation_oids().relation,
336                ),
337            );
338            tables.insert(relation, table);
339        }
340    }
341}
342impl uqa_execution::schema::publication::dependencies::CatalogPublicationChanges for Engine {
343    fn prepared_relation_changed(&self, relation: &uqa_core::RelationIdentity) {
344        self.note_prepared_relation_change(relation);
345    }
346    fn prepared_catalog_changed(
347        &self,
348        change: uqa_execution::statement::prepared::invalidation::PreparedCatalogChange,
349    ) {
350        self.note_prepared_catalog_change(change);
351    }
352    fn table_catalog_changed(&self) {
353        self.note_table_catalog_changed();
354    }
355    fn catalog_registry_changed(&self) {
356        self.note_catalog_registry_changed();
357    }
358}
359
360impl Engine {
361    pub(crate) fn view_sequence_rewrite_context(
362        &self,
363    ) -> uqa_execution::schema::sequences::dependencies::ViewSequenceRewriteContext<'_> {
364        uqa_execution::schema::sequences::dependencies::ViewSequenceRewriteContext {
365            views: self,
366            changes: self,
367        }
368    }
369}
370impl uqa_execution::schema::sequences::dependencies::ViewCatalogPublication for Engine {
371    fn synchronize_catalog(&self) -> StorageBackendResult<()> {
372        self.synchronize_catalog_registries()
373    }
374    fn view_definitions(
375        &self,
376    ) -> uqa_execution::schema::sequences::dependencies::ViewDefinitionsRead<'_> {
377        Box::new(self.durable.views.read())
378    }
379    fn has_catalog(&self) -> bool {
380        self.storage.catalog.is_some()
381    }
382    fn save_view_row(&self, row: &uqa_storage::ViewRow) -> StorageBackendResult<()> {
383        uqa_execution::catalog::view::ViewPublication::save_view(self, row)
384    }
385    fn publish_views(
386        &self,
387        updates: Vec<(
388            uqa_core::RelationIdentity,
389            uqa_sql::catalog::stored_view::StoredView,
390        )>,
391    ) {
392        self.durable.views.write().extend(updates);
393    }
394}