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 }
41 }
42}
43impl ConstraintDiagnosticSource for Engine {
44 fn diagnostic_context(&self) -> ConstraintDiagnosticContext<'_> {
45 ConstraintDiagnosticContext {
46 catalog: self.catalog_execution(),
47 authorization: self.table_authorization_context(),
48 }
49 }
50}
51impl ConstraintCatalog for Engine {
52 fn try_unique_columns(&self, table: &str) -> Result<Vec<String>, String> {
53 Engine::unique_columns_in_execution(self, table).map_err(|error| error.to_string())
54 }
55 fn try_check_constraint_definitions(&self, table: &str) -> Result<Vec<TableCheck>, String> {
56 Engine::check_constraint_definitions_in_execution(self, table)
57 .map_err(|error| error.to_string())
58 }
59 fn try_foreign_keys(&self, table: &str) -> Result<Vec<ForeignKey>, String> {
60 Engine::foreign_keys_in_execution(self, table).map_err(|error| error.to_string())
61 }
62 fn column_type(&self, table: &str, column: &str) -> Result<Option<ColumnType>, String> {
63 Engine::column_type(self, table, column).map_err(|error| error.to_string())
64 }
65 fn hierarchy_scan_tables(
66 &self,
67 table: &str,
68 descendants: bool,
69 ) -> Result<Vec<String>, SQLError> {
70 Engine::hierarchy_scan_tables(self, table, descendants)
71 }
72}
73impl MutationRead for Engine {
74 fn table_doc_ids(&self, table: &str) -> Result<Vec<DocId>, SQLError> {
75 Engine::query_table_doc_ids(self, table)
76 }
77 fn live_table_doc_ids(&self, table: &str) -> Result<Vec<DocId>, SQLError> {
78 Engine::live_table_doc_ids(self, table)
79 }
80 fn live_table_doc_id_page(
81 &self,
82 table: &str,
83 after: Option<DocId>,
84 limit: usize,
85 control: &uqa_storage::read_control::StorageReadControl,
86 ) -> Result<uqa_core::memory::BudgetedVec<DocId>, SQLError> {
87 let state = self.require_table(table)?;
88 let changes = self.command_overlay_changes(table)?.unwrap_or_default();
89 let source = state.document_store.read();
90 uqa_execution::query::document_changes::VisibleDocumentIds {
91 source: source.as_ref(),
92 changes: &changes,
93 control,
94 }
95 .page(after, limit, control, true)
96 .map_err(|error| {
97 uqa_sql::catalog::errors::storage_error("column rewrite identity page", &error)
98 })
99 }
100 fn get_document(&self, table: &str, doc_id: DocId) -> Result<Option<Document>, SQLError> {
101 Engine::get_live_document(self, table, doc_id)
102 }
103 fn raw_document(&self, table: &str, doc_id: DocId) -> Result<Option<Document>, SQLError> {
104 Engine::get_raw_live_document(self, table, doc_id)
105 }
106 fn command_overlay_changes(
107 &self,
108 table: &str,
109 ) -> Result<Option<uqa_execution::query::document_changes::DocumentChanges>, SQLError> {
110 Engine::command_overlay_changes(self, table)
111 }
112}
113impl MutationIndexRead for Engine {
114 fn index_definitions(
115 &self,
116 ) -> Result<
117 std::sync::Arc<uqa_execution::catalog::index::physical::PhysicalIndexDefinitions>,
118 SQLError,
119 > {
120 self.physical_index_definitions()
121 .map_err(|error| uqa_sql::catalog::errors::storage_error("index definitions", &error))
122 }
123 fn find_conflict(
124 &self,
125 table: &str,
126 columns: &[String],
127 values: &[Value],
128 ) -> Result<Option<DocId>, SQLError> {
129 Engine::find_mutation_conflict(self, table, columns, values)
130 }
131 fn staged_matches(
132 &self,
133 table: &str,
134 columns: &[String],
135 values: &[Value],
136 ) -> Result<Vec<DocId>, SQLError> {
137 Engine::command_overlay_matches(self, table, columns, values)
138 }
139 fn staged_expression_matches(
140 &self,
141 table: &str,
142 physical_key: &str,
143 values: &[Value],
144 ) -> Result<uqa_execution::mutation::overlay::CommandIndexProbe, SQLError> {
145 let table = self.command_overlay_table_name(table)?;
146 let control = self.query_retention_control()?;
147 uqa_execution::mutation::overlay::CommandMutationOverlay::expression_matches_with_catalog(
148 &mut self.session.command_mutation_overlays.lock(),
149 &table,
150 physical_key,
151 values,
152 &control,
153 Some(self),
154 )
155 }
156 fn value_index_scan_key(
157 &self,
158 table: &str,
159 key: &ValueIndexKey,
160 predicate: &Predicate,
161 ) -> Result<Option<PostingList>, SQLError> {
162 Engine::value_index_scan_key(self, table, key, predicate)
163 }
164}
165impl ConstraintTransactions for Engine {
166 fn foreign_key_is_deferred(&self, table: &str, key: &ForeignKey) -> Result<bool, SQLError> {
167 Engine::foreign_key_is_deferred(self, table, key)
168 }
169 fn referenced_key_is_deferred(
170 &self,
171 table: &str,
172 key: &ForeignKey,
173 derived: Option<&uqa_sql::ast::ReferencedPartitionConstraint>,
174 ) -> Result<bool, SQLError> {
175 Engine::referenced_key_is_deferred(self, table, key, derived)
176 }
177 fn refresh_explicit_statement_snapshot(&self) -> Result<(), SQLError> {
178 Engine::refresh_explicit_statement_snapshot(self)
179 }
180 fn lock_key_reservation(&self, key: [u8; 32], table: &str) -> Result<LockAcquire, SQLError> {
181 Engine::lock_key_reservation(self, key, table)
182 }
183 fn lock_key_reservations(
184 &self,
185 keys: &[[u8; 32]],
186 table: &str,
187 ) -> Result<Vec<LockAcquire>, SQLError> {
188 Engine::lock_key_reservations(self, keys, table)
189 }
190}
191impl MutationNamespace for Engine {
192 fn current_role(&self) -> RoleReference {
193 Engine::current_role(self)
194 }
195 fn require_schema_privilege(
196 &self,
197 schema: &str,
198 role: &RoleReference,
199 privilege: SchemaAclPrivilege,
200 ) -> Result<(), SQLError> {
201 Engine::require_schema_privilege(self, schema, role, privilege)
202 }
203}
204
205impl uqa_sql::semantics::conflict::ConflictCatalog for Engine {
206 fn try_describe_table(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String> {
207 Engine::describe_table_in_execution(self, table).map_err(|error| error.to_string())
208 }
209 fn enforced_keys(&self, table: &str) -> Result<Vec<EnforcedKey>, String> {
210 Engine::enforced_keys(self, table).map_err(|error| error.to_string())
211 }
212 fn try_declared_table_constraints(
213 &self,
214 table: &str,
215 ) -> Result<uqa_sql::ast::TableConstraintSet, String> {
216 Engine::try_declared_table_constraints(self, table).map_err(|error| error.to_string())
217 }
218}
219impl uqa_sql::semantics::conflict::InferenceBindingScope for Engine {
220 fn binding_scope(&self) -> Result<uqa_sql::binding::snapshot::BindingSnapshot, SQLError> {
221 let scope = super::query_scope::new_for_catalog_binding(self);
222 uqa_execution::query::binding::binding_context(&scope)
223 .map(uqa_sql::binding::snapshot::BindingSnapshot::from)
224 }
225}
226impl Engine {
227 pub(crate) fn inference_context(&self) -> uqa_sql::semantics::conflict::InferenceContext<'_> {
228 uqa_sql::semantics::conflict::InferenceContext {
229 catalog: self,
230 aggregates: self,
231 routines: self,
232 binding: self,
233 }
234 }
235}
236
237impl Engine {
238 pub(crate) fn validate_deferred_foreign_key_checks(
239 &self,
240 checks: &[crate::DeferredForeignKeyCheck],
241 targets: Option<&std::collections::BTreeSet<crate::ConstraintIdentity>>,
242 ) -> Result<(), SQLError> {
243 uqa_execution::mutation::constraints::validate_deferred_foreign_key_checks(
244 self.constraint_execution_context(),
245 checks,
246 targets,
247 )
248 }
249}