Skip to main content

uqa_engine/capabilities/
column_removal.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Retain the physical relation and its actual metadata/index guards during column deletion.
8use crate::{Engine, TableState};
9use std::{collections::BTreeSet, sync::Arc};
10use uqa_core::{DocId, RelationIdentity};
11use uqa_execution::schema::publication::removal::{
12    ColumnDropCatalog, ColumnDropIndexes, ColumnDropPublicationContext, ColumnDropRows,
13    ColumnDropRules, ColumnDropSequences, ColumnDropTable, IndexRowsRead, IndexRowsWrite,
14    SchemaWrite, VectorIndexesWrite,
15};
16use uqa_sql::{
17    ast::{ColumnDef, ForeignKey, TableCheck, TableKeyConstraint},
18    catalog::events::PreparedRuleColumnDrop,
19    schema::columns::removal_metadata::{
20        ColumnDependencyEntries, ColumnDependencyState, ColumnsRead, ForeignKeysRead,
21        KeyConstraintsRead,
22    },
23    SQLError,
24};
25use uqa_storage::{document_store::Document, StorageBackendResult, ValueIndexKey};
26impl Engine {
27    pub(crate) fn column_drop_publication_context(&self) -> ColumnDropPublicationContext<'_> {
28        ColumnDropPublicationContext {
29            registry: self.index_registry_context(),
30            catalog: self,
31            indexes: self,
32            rows: self,
33            rules: self,
34            sequences: self,
35            views: self,
36            modes: self,
37        }
38    }
39}
40struct ColumnDropBinding<'a> {
41    engine: &'a Engine,
42    state: Arc<TableState>,
43}
44struct ColumnDependencyBinding(Arc<TableState>);
45impl ColumnDependencyState for ColumnDependencyBinding {
46    fn columns(&self) -> ColumnsRead<'_> {
47        Box::new(self.0.columns.read())
48    }
49    fn foreign_keys(&self) -> ForeignKeysRead<'_> {
50        Box::new(self.0.foreign_keys.read())
51    }
52    fn key_constraints(&self) -> KeyConstraintsRead<'_> {
53        Box::new(self.0.key_constraints.read())
54    }
55}
56impl ColumnDependencyState for ColumnDropBinding<'_> {
57    fn columns(&self) -> ColumnsRead<'_> {
58        Box::new(self.state.columns.read())
59    }
60    fn foreign_keys(&self) -> ForeignKeysRead<'_> {
61        Box::new(self.state.foreign_keys.read())
62    }
63    fn key_constraints(&self) -> KeyConstraintsRead<'_> {
64        Box::new(self.state.key_constraints.read())
65    }
66}
67impl ColumnDropCatalog for Engine {
68    fn resolve_table(&self, table: &str, action: &str) -> StorageBackendResult<Option<String>> {
69        self.resolve_table_ddl_target(table, action)
70    }
71    fn table(&self, table: &str) -> StorageBackendResult<Option<Box<dyn ColumnDropTable + '_>>> {
72        self.try_table(table).map(|state| {
73            state.map(|state| {
74                Box::new(ColumnDropBinding {
75                    engine: self,
76                    state,
77                }) as Box<dyn ColumnDropTable>
78            })
79        })
80    }
81    fn entries(&self) -> ColumnDependencyEntries<'_> {
82        self.table_entries()
83            .into_iter()
84            .map(|(name, state)| {
85                (
86                    name,
87                    Box::new(ColumnDependencyBinding(state)) as Box<dyn ColumnDependencyState>,
88                )
89            })
90            .collect()
91    }
92}
93impl ColumnDropTable for ColumnDropBinding<'_> {
94    fn object_id(&self) -> [u8; 16] {
95        self.state.object_id()
96    }
97    fn clear_value_indexes(&self) {
98        self.state.value_indexes.write().clear();
99    }
100    fn write_dropped_attributes(
101        &self,
102    ) -> SchemaWrite<'_, uqa_sql::catalog::relation_attributes::DroppedAttribute> {
103        Box::new(self.state.dropped_attributes.write())
104    }
105    fn hierarchy(&self) -> uqa_sql::ast::TableHierarchy {
106        self.state.hierarchy.read().clone()
107    }
108    fn publish_hierarchy(&self, hierarchy: uqa_sql::ast::TableHierarchy) {
109        *self.state.hierarchy.write() = hierarchy;
110    }
111    fn write_columns(&self) -> SchemaWrite<'_, ColumnDef> {
112        Box::new(self.state.columns.write())
113    }
114    fn write_checks(&self) -> SchemaWrite<'_, TableCheck> {
115        Box::new(self.state.table_checks.write())
116    }
117    fn write_keys(&self) -> SchemaWrite<'_, TableKeyConstraint> {
118        Box::new(self.state.key_constraints.write())
119    }
120    fn write_foreign_keys(&self) -> SchemaWrite<'_, ForeignKey> {
121        Box::new(self.state.foreign_keys.write())
122    }
123    fn remove_column_acl(&self, column: &str) {
124        self.state.security.write().remove_column_acl(column);
125    }
126    fn remove_text_field(&self, column: &str) {
127        self.state
128            .fts_fields
129            .write()
130            .retain(|field| field != column);
131    }
132    fn write_vector_indexes(&self) -> VectorIndexesWrite<'_> {
133        Box::new(self.state.vector_indexes.write())
134    }
135    fn doc_ids(&self) -> StorageBackendResult<Vec<DocId>> {
136        self.state.document_store.read().doc_ids()
137    }
138    fn document(&self, id: DocId) -> StorageBackendResult<Option<Document>> {
139        self.state.document_store.read().get(id)
140    }
141    fn persist_drop(&self, table: &str, column: &str) -> StorageBackendResult<()> {
142        if let Some(catalog) = self.engine.storage.catalog.as_ref() {
143            catalog.drop_column_data(table, column)?;
144        }
145        self.engine.try_save_table_schema(table, &self.state)
146    }
147    fn mark_statistics_dirty(&self, table: &str) -> StorageBackendResult<()> {
148        self.engine.mark_column_stats_dirty(table, &self.state)
149    }
150}
151impl ColumnDropIndexes for Engine {
152    fn read_indexes(&self) -> IndexRowsRead<'_> {
153        Box::new(self.durable.catalog_indexes.read())
154    }
155    fn write_indexes(&self) -> IndexRowsWrite<'_> {
156        Box::new(self.durable.catalog_indexes.write())
157    }
158    fn drop_catalog_index(&self, name: &RelationIdentity) -> StorageBackendResult<()> {
159        if let Some(catalog) = self.storage.catalog.as_ref() {
160            catalog.drop_catalog_index(name)?;
161        }
162        Ok(())
163    }
164    fn remove_value_index(&self, table: &str, key: &ValueIndexKey) -> StorageBackendResult<()> {
165        if let Some(state) = self.try_table(table)? {
166            state.value_indexes.write().remove(key);
167        }
168        Ok(())
169    }
170    fn remove_field_analyzer(&self, table: &str, column: &str) {
171        self.durable
172            .table_field_analyzers
173            .write()
174            .retain(|(name, field), _| !(name == table && field == column));
175    }
176    fn refresh_value_indexes(&self, table: &str) -> StorageBackendResult<()> {
177        self.refresh_value_indexes_for_table(table)
178    }
179}
180impl ColumnDropRows for Engine {
181    fn rewrite(&self, table: &str, id: DocId, document: Document) -> Result<(), SQLError> {
182        self.rewrite_document_for_schema_change(table, id, document)
183    }
184}
185impl ColumnDropRules for Engine {
186    fn prepare(&self, table: &str, column: &str) -> StorageBackendResult<PreparedRuleColumnDrop> {
187        self.event_lookup_context()
188            .prepare_rule_column_drop(table, column)
189            .map_err(uqa_storage::StorageBackendError::Other)
190    }
191    fn finish(&self, prepared: PreparedRuleColumnDrop) -> StorageBackendResult<()> {
192        self.event_lifecycle_context()
193            .finish_rule_column_drop(prepared)
194    }
195}
196impl ColumnDropSequences for Engine {
197    fn owned_by_column(
198        &self,
199        table: [u8; 16],
200        column: [u8; 16],
201    ) -> StorageBackendResult<BTreeSet<String>> {
202        self.sequence_names_owned_by_column(table, column)
203    }
204}