1use crate::Engine;
9use uqa_core::{DocId, PostingList, Predicate, Value};
10use uqa_execution::catalog::security::schema::SchemaAclPrivilege;
11use uqa_execution::{
12 mutation::constraints::context::{
13 ConstraintCatalog, ConstraintContext, ConstraintDiagnosticContext,
14 ConstraintDiagnosticSource, ConstraintTransactions, MutationIndexRead, MutationNamespace,
15 MutationRead,
16 },
17 row_locks::LockAcquire,
18};
19use uqa_sql::catalog::roles::RoleReference;
20use uqa_sql::{
21 ast::{ColumnDef, ColumnType, ForeignKey, TableCheck},
22 catalog::index::EnforcedKey,
23 SQLError,
24};
25use uqa_storage::{document_store::Document, ValueIndexKey};
26impl Engine {
27 pub(crate) fn constraint_execution_context(&self) -> ConstraintContext<'_> {
28 ConstraintContext {
29 catalog: self,
30 values: self,
31 reads: self,
32 indexes: self,
33 transactions: self,
34 locks: self,
35 namespace: self,
36 referrers: self,
37 partitions: self.partition_context(),
38 diagnostics: self,
39 memory: self.session.as_ref(),
40 cancellation: self.query_runtime_view().cancellation,
41 }
42 }
43}
44impl ConstraintDiagnosticSource for Engine {
45 fn diagnostic_context(&self) -> ConstraintDiagnosticContext<'_> {
46 ConstraintDiagnosticContext {
47 catalog: self.catalog_execution(),
48 authorization: self.table_authorization_context(),
49 }
50 }
51}
52impl ConstraintCatalog for Engine {
53 fn try_unique_columns(&self, table: &str) -> Result<Vec<String>, String> {
54 Engine::unique_columns_in_execution(self, table).map_err(|error| error.to_string())
55 }
56 fn try_check_constraint_definitions(&self, table: &str) -> Result<Vec<TableCheck>, String> {
57 Engine::check_constraint_definitions_in_execution(self, table)
58 .map_err(|error| error.to_string())
59 }
60 fn try_foreign_keys(&self, table: &str) -> Result<Vec<ForeignKey>, String> {
61 Engine::foreign_keys_in_execution(self, table).map_err(|error| error.to_string())
62 }
63 fn column_type(&self, table: &str, column: &str) -> Result<Option<ColumnType>, String> {
64 Engine::column_type(self, table, column).map_err(|error| error.to_string())
65 }
66 fn hierarchy_scan_tables(
67 &self,
68 table: &str,
69 descendants: bool,
70 ) -> Result<Vec<String>, SQLError> {
71 Engine::hierarchy_scan_tables(self, table, descendants)
72 }
73}
74impl MutationRead for Engine {
75 fn table_doc_ids(&self, table: &str) -> Result<Vec<DocId>, SQLError> {
76 Engine::query_table_doc_ids(self, table)
77 }
78 fn table_doc_id_page(
79 &self,
80 table: &str,
81 after: Option<DocId>,
82 limit: usize,
83 control: &uqa_storage::read_control::StorageReadControl,
84 ) -> Result<uqa_core::memory::BudgetedVec<DocId>, SQLError> {
85 let state = self.require_query_table(table)?;
86 let changes = self.command_overlay_changes(table)?.unwrap_or_default();
87 let source = state.document_store.read();
88 uqa_execution::query::document_changes::VisibleDocumentIds {
89 source: source.as_ref(),
90 changes: &changes,
91 control,
92 }
93 .page(after, limit, control, true)
94 .map_err(|error| {
95 uqa_execution::storage_errors::storage_error("mutation identity page", &error)
96 })
97 }
98 fn live_table_doc_ids(&self, table: &str) -> Result<Vec<DocId>, SQLError> {
99 Engine::live_table_doc_ids(self, table)
100 }
101 fn live_table_doc_id_page(
102 &self,
103 table: &str,
104 after: Option<DocId>,
105 limit: usize,
106 control: &uqa_storage::read_control::StorageReadControl,
107 ) -> Result<uqa_core::memory::BudgetedVec<DocId>, SQLError> {
108 let state = self.require_table(table)?;
109 let changes = self.command_overlay_changes(table)?.unwrap_or_default();
110 let source = state.document_store.read();
111 uqa_execution::query::document_changes::VisibleDocumentIds {
112 source: source.as_ref(),
113 changes: &changes,
114 control,
115 }
116 .page(after, limit, control, true)
117 .map_err(|error| {
118 uqa_execution::storage_errors::storage_error("column rewrite identity page", &error)
119 })
120 }
121 fn get_document(&self, table: &str, doc_id: DocId) -> Result<Option<Document>, SQLError> {
122 Engine::get_live_document(self, table, doc_id)
123 }
124 fn raw_document(&self, table: &str, doc_id: DocId) -> Result<Option<Document>, SQLError> {
125 Engine::get_raw_live_document(self, table, doc_id)
126 }
127 fn command_overlay_changes(
128 &self,
129 table: &str,
130 ) -> Result<Option<uqa_execution::query::document_changes::DocumentChanges>, SQLError> {
131 Engine::command_overlay_changes(self, table)
132 }
133}
134impl MutationIndexRead for Engine {
135 fn index_definitions(
136 &self,
137 ) -> Result<
138 std::sync::Arc<uqa_execution::catalog::index::physical::PhysicalIndexDefinitions>,
139 SQLError,
140 > {
141 self.physical_index_definitions()
142 .map_err(|error| uqa_sql::catalog::errors::storage_error("index definitions", &error))
143 }
144 fn find_conflict(
145 &self,
146 table: &str,
147 columns: &[String],
148 values: &[Value],
149 ) -> Result<Option<DocId>, SQLError> {
150 Engine::find_mutation_conflict(self, table, columns, values)
151 }
152 fn staged_matches(
153 &self,
154 table: &str,
155 columns: &[String],
156 values: &[Value],
157 ) -> Result<Vec<DocId>, SQLError> {
158 Engine::command_overlay_matches(self, table, columns, values)
159 }
160 fn visit_staged_matches(
161 &self,
162 table: &str,
163 columns: &[String],
164 values: &[Value],
165 visit: &mut dyn FnMut(DocId) -> Result<(), SQLError>,
166 ) -> Result<(), SQLError> {
167 if self.session.command_mutation_overlays.lock().is_empty() {
168 return Ok(());
169 }
170 let table = self.command_overlay_table_name(table)?;
171 let control = self.query_retention_control()?;
172 uqa_execution::mutation::overlay::CommandMutationOverlay::visit_matches_with_catalog(
173 &mut self.session.command_mutation_overlays.lock(),
174 &table,
175 columns,
176 values,
177 &control,
178 Some(self),
179 visit,
180 )
181 }
182 fn staged_expression_matches(
183 &self,
184 table: &str,
185 physical_key: &str,
186 values: &[Value],
187 ) -> Result<uqa_execution::mutation::overlay::CommandIndexProbe, SQLError> {
188 let table = self.command_overlay_table_name(table)?;
189 let control = self.query_retention_control()?;
190 uqa_execution::mutation::overlay::CommandMutationOverlay::expression_matches_with_catalog(
191 &mut self.session.command_mutation_overlays.lock(),
192 &table,
193 physical_key,
194 values,
195 &control,
196 Some(self),
197 )
198 }
199 fn visit_staged_expression_matches(
200 &self,
201 table: &str,
202 physical_key: &str,
203 values: &[Value],
204 visit: &mut dyn FnMut(DocId) -> Result<(), SQLError>,
205 ) -> Result<uqa_execution::query::document_changes::DocumentChanges, SQLError> {
206 let table = self.command_overlay_table_name(table)?;
207 let control = self.query_retention_control()?;
208 uqa_execution::mutation::overlay::CommandMutationOverlay::visit_expression_matches_with_catalog(
209 &mut self.session.command_mutation_overlays.lock(),
210 &table,
211 physical_key,
212 values,
213 &control,
214 Some(self),
215 visit,
216 )
217 }
218 fn visit_value_index_key(
219 &self,
220 table: &str,
221 key: &ValueIndexKey,
222 predicate: &Predicate,
223 visit: &mut dyn FnMut(DocId) -> Result<(), SQLError>,
224 ) -> Result<bool, SQLError> {
225 Engine::visit_value_index_key(self, table, key, predicate, visit)
226 }
227 fn value_index_scan_key(
228 &self,
229 table: &str,
230 key: &ValueIndexKey,
231 predicate: &Predicate,
232 ) -> Result<Option<PostingList>, SQLError> {
233 Engine::value_index_scan_key(self, table, key, predicate)
234 }
235 fn visit_value_index_equal(
236 &self,
237 table: &str,
238 key: &ValueIndexKey,
239 value: &Value,
240 visit: &mut dyn FnMut(DocId) -> Result<(), SQLError>,
241 ) -> Result<bool, SQLError> {
242 Engine::visit_value_index_equal(self, table, key, value, visit)
243 }
244}
245impl ConstraintTransactions for Engine {
246 fn foreign_key_is_deferred(&self, table: &str, key: &ForeignKey) -> Result<bool, SQLError> {
247 Engine::foreign_key_is_deferred(self, table, key)
248 }
249 fn referenced_key_is_deferred(
250 &self,
251 table: &str,
252 key: &ForeignKey,
253 derived: Option<&uqa_sql::ast::ReferencedPartitionConstraint>,
254 ) -> Result<bool, SQLError> {
255 Engine::referenced_key_is_deferred(self, table, key, derived)
256 }
257 fn refresh_explicit_statement_snapshot(&self) -> Result<(), SQLError> {
258 Engine::refresh_explicit_statement_snapshot(self)
259 }
260 fn lock_key_reservation(&self, key: [u8; 32], table: &str) -> Result<LockAcquire, SQLError> {
261 Engine::lock_key_reservation(self, key, table)
262 }
263 fn lock_key_reservations(
264 &self,
265 keys: &[[u8; 32]],
266 table: &str,
267 ) -> Result<Vec<LockAcquire>, SQLError> {
268 Engine::lock_key_reservations(self, keys, table)
269 }
270}
271impl MutationNamespace for Engine {
272 fn current_role(&self) -> RoleReference {
273 Engine::current_role(self)
274 }
275 fn require_schema_privilege(
276 &self,
277 schema: &str,
278 role: &RoleReference,
279 privilege: SchemaAclPrivilege,
280 ) -> Result<(), SQLError> {
281 Engine::require_schema_privilege(self, schema, role, privilege)
282 }
283}
284
285impl uqa_sql::semantics::conflict::ConflictCatalog for Engine {
286 fn try_describe_table(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String> {
287 Engine::describe_table_in_execution(self, table).map_err(|error| error.to_string())
288 }
289 fn with_column_metadata_with_control(
290 &self,
291 table: &str,
292 control: &uqa_core::memory::ProductionControl<'_>,
293 read: &mut dyn FnMut(&[ColumnDef]) -> Result<(), SQLError>,
294 ) -> Result<bool, SQLError> {
295 self.with_table_column_metadata_with_control(table, control, read)
296 }
297
298 fn enforced_keys(&self, table: &str) -> Result<Vec<EnforcedKey>, String> {
299 Engine::enforced_keys(self, table).map_err(|error| error.to_string())
300 }
301 fn try_declared_table_constraints(
302 &self,
303 table: &str,
304 ) -> Result<uqa_sql::ast::TableConstraintSet, String> {
305 Engine::try_declared_table_constraints(self, table).map_err(|error| error.to_string())
306 }
307}
308impl uqa_sql::semantics::conflict::InferenceBindingScope for Engine {
309 fn binding_scope_with_control(
310 &self,
311 control: &uqa_core::memory::ProductionControl<'_>,
312 ) -> Result<uqa_sql::binding::production::ControlledBindingSnapshot, SQLError> {
313 let scope = super::query_scope::new_for_catalog_binding_with_control(self, control)?;
314 uqa_execution::query::binding::binding_snapshot_with_control(&scope, control)
315 }
316 fn binding_scope(&self) -> Result<uqa_sql::binding::snapshot::BindingSnapshot, SQLError> {
317 let scope = super::query_scope::new_for_catalog_binding(self)?;
318 uqa_execution::query::binding::binding_context(&scope)
319 .map(uqa_sql::binding::snapshot::BindingSnapshot::from)
320 }
321}
322impl Engine {
323 pub(crate) fn inference_context(&self) -> uqa_sql::semantics::conflict::InferenceContext<'_> {
324 uqa_sql::semantics::conflict::InferenceContext {
325 catalog: self,
326 aggregates: self,
327 routines: self,
328 binding: self,
329 }
330 }
331}
332
333impl Engine {
334 pub(crate) fn validate_deferred_foreign_key_checks(
335 &self,
336 checks: &[crate::DeferredForeignKeyCheck],
337 targets: Option<&std::collections::BTreeSet<crate::ConstraintIdentity>>,
338 ) -> Result<(), SQLError> {
339 uqa_execution::mutation::constraints::validate_deferred_foreign_key_checks(
340 self.constraint_execution_context(),
341 checks,
342 targets,
343 )
344 }
345}