1use crate::Engine;
10use std::{collections::BTreeMap, sync::Arc};
11use uqa_core::RelationIdentity;
12use uqa_execution::schema::constraints::names::catalog::{
13 ConstraintNameCatalog, ConstraintTableMetadata,
14};
15use uqa_sql::{
16 ast::{ColumnDef, Expr, FunctionVolatility},
17 SQLError,
18};
19
20fn constraint_table_metadata(table: &crate::TableState) -> ConstraintTableMetadata {
21 ConstraintTableMetadata {
22 object_id: table.object_id(),
23 columns: table.columns.snapshot(),
24 checks: table.table_checks.snapshot(),
25 foreign_keys: table.foreign_keys.snapshot(),
26 keys: table.key_constraints.snapshot(),
27 hierarchy: table.hierarchy.snapshot(),
28 }
29}
30
31impl ConstraintNameCatalog for Engine {
32 fn constraint_table(&self, relation: &RelationIdentity) -> Option<ConstraintTableMetadata> {
33 let table = self.storage.tables.read().get(relation).cloned()?;
34 Some(constraint_table_metadata(&table))
35 }
36 fn constraint_owner(
37 &self,
38 object_id: [u8; 16],
39 ) -> Option<(RelationIdentity, ConstraintTableMetadata)> {
40 let (relation, table) = self
41 .storage
42 .tables
43 .read()
44 .iter()
45 .find(|(_, table)| table.object_id() == object_id)
46 .map(|(relation, table)| (relation.clone(), Arc::clone(table)))?;
47 Some((relation, constraint_table_metadata(&table)))
48 }
49 fn constraint_schema_tables(&self, schema: &str) -> Vec<ConstraintTableMetadata> {
50 let tables = self
52 .storage
53 .tables
54 .read()
55 .iter()
56 .filter(|(relation, _)| relation.schema == schema)
57 .map(|(_, table)| Arc::clone(table))
58 .collect::<Vec<_>>();
59 tables
60 .iter()
61 .map(|table| constraint_table_metadata(table))
62 .collect()
63 }
64 fn constraint_foreign_tables(
65 &self,
66 ) -> Arc<BTreeMap<RelationIdentity, uqa_execution::catalog::foreign::StoredForeignTable>> {
67 self.durable.foreign_tables.snapshot()
68 }
69 fn constraint_domains(&self) -> Arc<uqa_execution::catalog::domain::DomainRegistry> {
70 self.durable.domains.snapshot()
71 }
72 fn constraint_triggers(&self) -> Arc<uqa_sql::catalog::events::TriggerCatalog> {
73 self.durable.triggers.snapshot()
74 }
75}
76
77impl uqa_sql::assignment::AssignmentContext for Engine {
78 fn evaluate_domain_check(
79 &self,
80 expression: &Expr,
81 row: &dyn uqa_sql::expr::RowLookup,
82 schema: &uqa_sql::RowSchema,
83 ) -> Result<uqa_core::Value, SQLError> {
84 crate::capabilities::query_expressions::eval_lowered_expression_with_schema(
85 self,
86 expression,
87 row,
88 schema,
89 &[],
90 )
91 }
92}
93
94impl uqa_sql::schema::SchemaExpressionCatalog for Engine {
95 fn plan_schema_expression(
96 &self,
97 expression: &Expr,
98 columns: &[ColumnDef],
99 ) -> Result<uqa_sql::schema::expressions::PlannedSchemaExpression, SQLError> {
100 uqa_planner::statement_planning::schema_expressions::plan_schema_expression(
101 &self.statement_planning_context(),
102 self,
103 expression,
104 columns,
105 )
106 }
107
108 fn registered_runtime_function_volatility(&self, name: &str) -> Option<FunctionVolatility> {
109 Engine::registered_runtime_function_volatility(self, name)
110 }
111 fn schema_expression_columns(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, SQLError> {
112 self.describe_table_in_execution(table)
113 .map_err(|error| SQLError::Internal(error.to_string()))
114 }
115}
116
117impl uqa_sql::schema::inheritance::InheritanceCatalog for Engine {
118 fn resolve_parent(&self, name: &str) -> Result<String, SQLError> {
119 uqa_sql::schema::inheritance::inheritance_parent_target(
120 self.resolve_visible_relation_kind(name)?,
121 name,
122 )
123 }
124 fn declared_constraints(
125 &self,
126 table: &str,
127 ) -> Result<uqa_sql::ast::TableConstraintSet, String> {
128 self.try_declared_table_constraints(table)
129 .map_err(|error| error.to_string())
130 }
131 fn check_definitions(&self, table: &str) -> Result<Vec<uqa_sql::ast::TableCheck>, String> {
132 self.check_constraint_definitions_in_execution(table)
133 .map_err(|error| error.to_string())
134 }
135 fn unique_index_keys(&self, table: &str) -> Result<Vec<Vec<uqa_sql::ast::IndexKey>>, String> {
136 uqa_execution::schema::indexes::registry::lifecycle::standalone_unique_index_keys(
137 &self
138 .catalog_read_view()
139 .snapshot()
140 .definitions
141 .catalog_indexes,
142 table,
143 )
144 .map_err(|error| error.to_string())
145 }
146}
147
148impl uqa_sql::schema::indexes::names::IndexNameCatalog for Engine {
149 fn automatic_constraint_names(
150 &self,
151 table: &str,
152 ) -> Result<std::collections::BTreeSet<String>, SQLError> {
153 self.schema_publication_context()
154 .constraint_names()
155 .automatic_names(table)
156 }
157 fn existing_constraint_names(
158 &self,
159 table: &str,
160 ) -> Result<std::collections::BTreeSet<String>, SQLError> {
161 self.schema_publication_context()
162 .constraint_names()
163 .existing_names(table)
164 }
165 fn existing_constraint_keys(
166 &self,
167 table: &str,
168 ) -> Result<Vec<uqa_sql::ast::TableKeyConstraint>, SQLError> {
169 if self.try_resolve_bound_table_name(table)?.is_some() {
170 self.key_constraints_in_execution(table)
171 .map_err(|error| SQLError::Internal(error.to_string()))
172 } else {
173 Ok(Vec::new())
174 }
175 }
176 fn relation_name_available(&self, qualified_name: &str) -> Result<bool, SQLError> {
177 Ok(matches!(
178 self.resolve_bound_relation_kind(qualified_name)?,
179 super::RelationResolution::MissingRelation
180 ))
181 }
182}
183
184impl uqa_execution::schema::indexes::IndexBuildCatalog for Engine {
185 fn table_hierarchy(&self, table: &str) -> Result<uqa_sql::ast::TableHierarchy, SQLError> {
186 self.try_table_hierarchy(table)
187 .map_err(|error| uqa_sql::catalog::errors::storage_error("CREATE UNIQUE INDEX", &error))
188 }
189 fn scan_tables(&self, table: &str) -> Result<Vec<String>, SQLError> {
190 self.hierarchy_scan_tables(table, true)
191 }
192 fn partition_tree(
193 &self,
194 table: &str,
195 ) -> Result<Vec<uqa_sql::semantics::partition::PartitionTreeNode>, SQLError> {
196 uqa_sql::semantics::partition::partition_tree(&self.partition_context(), table, false)
197 }
198}
199
200impl uqa_execution::schema::indexes::unique_build::IndexKeyDescription for Engine {
201 fn describe_index_key(
202 &self,
203 table: &str,
204 keys: &[uqa_sql::ast::IndexKey],
205 key_types: &[uqa_sql::ColumnType],
206 values: &[uqa_core::Value],
207 ) -> Result<Option<String>, SQLError> {
208 self.constraint_execution_context()
209 .describe_index_key(table, keys, key_types, values)
210 }
211}
212
213impl Engine {
214 pub(crate) fn foreign_key_definition_context(
215 &self,
216 ) -> uqa_sql::schema::foreign_keys::ForeignKeyDefinitionContext<'_> {
217 uqa_sql::schema::foreign_keys::ForeignKeyDefinitionContext {
218 catalog: self,
219 columns: self,
220 }
221 }
222}
223impl uqa_sql::schema::foreign_keys::ForeignKeyDefinitionCatalog for Engine {
224 fn resolve_table_reference(&self, name: &str) -> Result<String, SQLError> {
225 self.resolve_visible_table_reference(name)
226 }
227 fn bound_table_name(&self, name: &str) -> Result<Option<String>, SQLError> {
228 self.try_resolve_bound_table_name(name)
229 }
230 fn referenceable_keys(
231 &self,
232 table: &str,
233 ) -> Result<
234 Vec<uqa_sql::ast::TableKeyConstraint>,
235 uqa_sql::assignment::columns::ColumnCatalogError,
236 > {
237 Engine::referenceable_keys(self, table).map_err(|error| Box::new(error) as _)
238 }
239 fn ensure_reference_privilege(&self, table: &str, column: &str) -> Result<(), SQLError> {
240 self.ensure_column_privilege(
241 table,
242 column,
243 uqa_sql::catalog::security::table::TableAclPrivilege::References,
244 )
245 }
246}
247
248impl uqa_execution::schema::columns::ColumnRewritePublication for Engine {
249 fn update_fields(
250 &self,
251 table: &str,
252 id: uqa_core::DocId,
253 values: std::collections::BTreeMap<String, uqa_core::Value>,
254 vectors: uqa_execution::mutation::publication::DocumentVectors,
255 ) -> Result<bool, SQLError> {
256 uqa_execution::schema::columns::generated::update_rewritten_fields(
257 &self.generated_rewrite_context(),
258 table,
259 id,
260 values,
261 vectors,
262 )
263 }
264}
265
266impl Engine {
267 pub(crate) fn schema_dependency_binding_context(
268 &self,
269 ) -> uqa_sql::schema::dependencies::registration::SchemaDependencyBindingContext<'_> {
270 uqa_sql::schema::dependencies::registration::SchemaDependencyBindingContext {
271 references: self,
272 schema: self,
273 bindings: self,
274 }
275 }
276}
277impl uqa_sql::schema::dependencies::regclass::SchemaReferenceCatalog for Engine {
278 fn loaded_relation_name(&self, reference: &str) -> Result<Option<String>, String> {
279 self.resolve_loaded_visible_relation_kind(reference)
280 .map(|resolution| resolution.into_found().map(|(canonical, _)| canonical))
281 .map_err(|error| error.to_string())
282 }
283 fn bound_relation_oid(&self, canonical: &str) -> Result<Option<i64>, String> {
284 uqa_execution::catalog::projection::resolve_bound_regclass_oid(
285 &self.catalog_execution(),
286 canonical,
287 )
288 .map_err(|error| error.to_string())
289 }
290 fn visible_relation_oid(&self, reference: &str) -> Result<Option<i64>, String> {
291 uqa_execution::catalog::projection::resolve_regclass_oid(
292 &self.catalog_execution(),
293 reference,
294 )
295 .map_err(|error| error.to_string())
296 }
297 fn sequence_for_binding(&self, reference: &str) -> Result<String, String> {
298 self.resolve_sequence_reference_for_binding(reference)
299 .map_err(|error| error.to_string())
300 }
301}
302
303impl uqa_sql::schema::constraint_views::StoredTableNames for Engine {
304 fn stored_table_exists(&self, relation: &uqa_core::RelationIdentity) -> bool {
305 self.storage.tables.read().contains_key(relation)
306 }
307 fn stored_table_names(&self) -> Vec<uqa_core::RelationIdentity> {
308 self.storage.tables.read().keys().cloned().collect()
309 }
310}