1use super::SessionExecutionView;
10use crate::Engine;
11use uqa_execution::catalog::{
12 context::CatalogContext,
13 services::{CatalogNamespace, CatalogSession, RelationCounts},
14 RelationNameResolution,
15};
16use uqa_sql::catalog::roles::RoleReference;
17use uqa_sql::catalog::session::{CursorMetadata, PreparedStatementMetadata};
18use uqa_sql::SQLError;
19
20impl Engine {
21 pub(crate) fn catalog_identity_reservation_context(
22 &self,
23 ) -> uqa_execution::catalog::identity::CatalogIdentityReservationContext<'_> {
24 uqa_execution::catalog::identity::CatalogIdentityReservationContext {
25 catalog: self,
26 session: self,
27 locks: self,
28 }
29 }
30
31 pub(crate) fn catalog_execution(&self) -> CatalogContext<'_> {
32 CatalogContext {
33 catalog: self,
34 session: self,
35 namespaces: self,
36 routines: self,
37 expressions: self,
38 counts: self,
39 views: self,
40 cache: &self.runtime.regtype_output_cache,
41 }
42 }
43}
44impl CatalogSession for Engine {
45 fn current_role(&self) -> RoleReference {
46 self.session_execution_view().current_role()
47 }
48 fn temporary_schema_name(&self) -> String {
49 self.session_execution_view().temporary_schema_name()
50 }
51 fn relation_name_resolution(&self) -> RelationNameResolution {
52 self.session_execution_view().relation_name_resolution()
53 }
54
55 fn show_parameter(&self, name: &str) -> Result<(String, String), SQLError> {
56 self.session_execution_view().show_parameter(name)
57 }
58 fn parameter_settings(&self) -> Vec<uqa_sql::semantics::parameters::setting::ParameterSetting> {
59 self.session_execution_view().parameter_settings()
60 }
61 fn explain_settings(
62 &self,
63 control: &uqa_core::memory::ProductionControl<'_>,
64 ) -> Result<
65 uqa_core::memory::Produced<uqa_sql::semantics::parameters::explain::ExplainSettings>,
66 SQLError,
67 > {
68 self.session_execution_view().explain_settings(control)
69 }
70 fn prepared_statements(&self) -> Vec<PreparedStatementMetadata> {
71 self.session_execution_view().prepared_statements()
72 }
73 fn cursors(&self) -> Vec<CursorMetadata> {
74 self.session_execution_view().cursors()
75 }
76}
77impl CatalogSession for SessionExecutionView<'_> {
78 fn current_role(&self) -> RoleReference {
79 SessionExecutionView::current_role(self)
80 }
81 fn temporary_schema_name(&self) -> String {
82 SessionExecutionView::temporary_schema_name(self)
83 }
84 fn relation_name_resolution(&self) -> RelationNameResolution {
85 SessionExecutionView::relation_name_resolution(self)
86 }
87
88 fn show_parameter(&self, name: &str) -> Result<(String, String), SQLError> {
89 SessionExecutionView::show_parameter(self, name)
90 }
91 fn parameter_settings(&self) -> Vec<uqa_sql::semantics::parameters::setting::ParameterSetting> {
92 SessionExecutionView::parameter_settings(self)
93 }
94 fn explain_settings(
95 &self,
96 control: &uqa_core::memory::ProductionControl<'_>,
97 ) -> Result<
98 uqa_core::memory::Produced<uqa_sql::semantics::parameters::explain::ExplainSettings>,
99 SQLError,
100 > {
101 SessionExecutionView::explain_settings(self, control)
102 }
103 fn prepared_statements(&self) -> Vec<PreparedStatementMetadata> {
104 SessionExecutionView::prepared_statements(self)
105 }
106 fn cursors(&self) -> Vec<CursorMetadata> {
107 SessionExecutionView::cursors(self)
108 }
109}
110impl RelationCounts for Engine {
111 fn table_doc_count(&self, name: &str) -> Result<u64, SQLError> {
112 Engine::table_doc_count(self, name)
113 }
114}
115impl CatalogNamespace for Engine {
116 fn current_schema_names(&self, implicit: bool) -> Result<Vec<String>, SQLError> {
117 Engine::current_schema_names_in_execution(self, implicit)
118 .map_err(|error| SQLError::Internal(error.to_string()))
119 }
120 fn current_schema_names_with_catalog(
121 &self,
122 catalog: &uqa_execution::catalog::CatalogReadView,
123 implicit: bool,
124 ) -> Result<Vec<String>, SQLError> {
125 Ok(uqa_execution::catalog::namespaces::current_schema_names(
126 catalog,
127 &self.session_execution_view().relation_name_resolution(),
128 &self.current_role(),
129 implicit,
130 ))
131 }
132}
133
134impl uqa_execution::catalog::services::CatalogSnapshotSource for Engine {
135 fn bind_query_reads(
136 &self,
137 snapshot: uqa_execution::catalog::CatalogReadView,
138 ) -> Result<uqa_execution::catalog::CatalogReadView, SQLError> {
139 let map_error =
140 |error| uqa_execution::storage_errors::storage_error("bind catalog reads", &error);
141 let Some(context) = self.graph_read_context().map_err(map_error)? else {
142 return Ok(snapshot);
143 };
144 Ok(snapshot.with_graph_reads(
145 self.new_graph_store().map_err(map_error)?,
146 context,
147 &self.runtime.cancellation,
148 ))
149 }
150
151 fn retain_catalog_rows(
152 &self,
153 snapshot: uqa_execution::catalog::CatalogReadView,
154 ) -> Result<uqa_execution::catalog::CatalogReadView, SQLError> {
155 if !self.versioned_backend_transactions() || snapshot.has_physical_catalog() {
156 return Ok(snapshot);
157 }
158 let boundary = self
159 .session
160 .transactions
161 .lock()
162 .first()
163 .and_then(|frame| frame.fixed_catalog_records.clone());
164 let backend =
165 self.storage.backend.as_ref().ok_or_else(|| {
166 SQLError::Internal("versioned catalog has no storage backend".into())
167 })?;
168 let source = match boundary {
169 Some(boundary) => backend
170 .open_read_session_with_private_changes(&boundary, &self.runtime.cancellation),
171 None => backend.open_retained_read_session(&self.runtime.cancellation),
172 }
173 .map_err(|error| {
174 uqa_execution::storage_errors::storage_error("retain physical catalog rows", &error)
175 })?;
176 Ok(snapshot.with_physical_catalog(source.catalog))
177 }
178
179 fn refreshed_catalog_snapshot(
180 &self,
181 ) -> Result<uqa_execution::catalog::CatalogReadView, SQLError> {
182 self.refresh_catalog()?;
183 Ok(self.catalog_read_view())
184 }
185
186 fn refresh_catalog(&self) -> Result<(), SQLError> {
187 self.synchronize_table_catalog()
188 .map_err(|error| SQLError::Internal(format!("load table catalog: {error}")))?;
189 self.synchronize_catalog_registries()
190 .map_err(|error| SQLError::Internal(format!("load relation catalog: {error}")))?;
191 Ok(())
192 }
193
194 fn catalog_snapshot(&self) -> uqa_execution::catalog::CatalogReadView {
195 self.catalog_read_view()
196 }
197
198 fn builtin_routine_permissions(
199 &self,
200 current_role: RoleReference,
201 ) -> uqa_execution::catalog::security::builtin_routines::execution::BuiltinRoutinePermissions
202 {
203 use uqa_execution::catalog::security::builtin_routines::execution::BuiltinRoutinePermissions;
204
205 if let Some(snapshot) = &self.query_catalog_snapshot {
206 return BuiltinRoutinePermissions::new(snapshot.read_view.clone(), current_role);
207 }
208 BuiltinRoutinePermissions::try_unrestricted(
209 &self.durable.builtin_routine_security.snapshot(),
210 &self.durable.roles.snapshot(),
211 ¤t_role,
212 )
213 .unwrap_or_else(|| BuiltinRoutinePermissions::new(self.catalog_read_view(), current_role))
214 }
215
216 fn current_catalog_snapshot(&self) -> uqa_execution::catalog::CatalogReadView {
217 self.restored_catalog_read_view()
218 }
219
220 fn current_catalog_revision(&self) -> Option<u64> {
221 Some(self.runtime.regtype_output_cache.revision())
222 }
223}
224
225use uqa_core::Value;
226impl Engine {
227 pub(crate) fn clear_regtype_output_cache(&self) {
228 self.runtime.regtype_output_cache.clear();
229 }
230}
231
232use uqa_execution::catalog::services::{
233 CatalogExpressionEvaluation, ViewCatalogCapabilities, ViewCatalogMetadata,
234};
235use uqa_sql::ast::{Expr, TriggerEvent};
236impl CatalogExpressionEvaluation for Engine {
237 fn evaluate(&self, expression: &Expr) -> Result<Value, SQLError> {
238 crate::capabilities::query_expressions::eval_lowered_expression(self, expression, None, &[])
239 }
240}
241impl ViewCatalogCapabilities for Engine {
242 fn view_updatability_with_catalog(
243 &self,
244 name: &str,
245 catalog: &uqa_execution::catalog::CatalogReadView,
246 resolution: &RelationNameResolution,
247 ) -> Result<ViewCatalogMetadata, SQLError> {
248 let retained = uqa_execution::catalog::view::metadata::RetainedViewCatalog {
249 catalog,
250 resolution,
251 fallback: self,
252 replica: self.session_replication_role_is_replica(),
253 };
254 let metadata = uqa_sql::semantics::view_rewrite::view_updatability(
255 uqa_sql::semantics::view_rewrite::context::ViewRewriteContext {
256 catalog: &retained,
257 ..self.view_rewrite_context()
258 },
259 name,
260 )?;
261 Ok(ViewCatalogMetadata {
262 catalog: metadata.catalog,
263 catalog_columns: metadata.catalog_columns,
264 check_option: metadata.check_option,
265 })
266 }
267 fn has_instead_of_trigger_with_catalog(
268 &self,
269 name: &str,
270 event: TriggerEvent,
271 catalog: &uqa_execution::catalog::CatalogReadView,
272 resolution: &RelationNameResolution,
273 ) -> Result<bool, SQLError> {
274 let retained = uqa_execution::catalog::view::metadata::RetainedViewCatalog {
275 catalog,
276 resolution,
277 fallback: self,
278 replica: self.session_replication_role_is_replica(),
279 };
280 uqa_sql::semantics::view_rewrite::has_instead_of_trigger(
281 uqa_sql::semantics::view_rewrite::context::ViewRewriteContext {
282 catalog: &retained,
283 ..self.view_rewrite_context()
284 },
285 name,
286 event,
287 )
288 }
289 fn view_updatability(&self, name: &str) -> Result<ViewCatalogMetadata, SQLError> {
290 let metadata =
291 uqa_sql::semantics::view_rewrite::view_updatability(self.view_rewrite_context(), name)?;
292 Ok(ViewCatalogMetadata {
293 catalog: metadata.catalog,
294 catalog_columns: metadata.catalog_columns,
295 check_option: metadata.check_option,
296 })
297 }
298 fn has_instead_of_trigger(&self, name: &str, event: TriggerEvent) -> Result<bool, SQLError> {
299 uqa_sql::semantics::view_rewrite::has_instead_of_trigger(
300 self.view_rewrite_context(),
301 name,
302 event,
303 )
304 }
305}
306
307impl Engine {
308 pub(crate) fn publish_catalog_constraint_tuples(&self) -> Result<(), SQLError> {
309 let (Some(catalog), Some(backend)) = (&self.storage.catalog, &self.storage.backend) else {
310 return Ok(());
311 };
312 if !uqa_execution::catalog::tuples::publication::has_pending(catalog.as_ref()).map_err(
313 |error| {
314 uqa_execution::storage_errors::storage_error("inspect pending catalog rows", &error)
315 },
316 )? {
317 return Ok(());
318 }
319 let storage = uqa_storage::PersistentStorageSession::new(catalog.clone(), backend.clone());
320 uqa_execution::catalog::tuples::publication::publish_pending(
321 &self.catalog_execution(),
322 &self.restored_catalog_read_view(),
323 &self.session_execution_view().relation_name_resolution(),
324 &storage,
325 )
326 }
327}
328
329impl Engine {
330 pub(crate) fn initialize_catalog_constraint_tuples(
331 &self,
332 catalog: &std::sync::Arc<dyn uqa_storage::CatalogFacade>,
333 backend: &std::sync::Arc<dyn uqa_storage::PersistentStorageBackend>,
334 ) -> uqa_storage::StorageBackendResult<()> {
335 if !uqa_execution::catalog::tuples::publication::requires_initialization(catalog.as_ref())?
336 {
337 return self.publish_catalog_constraint_tuples().map_err(|error| {
338 uqa_storage::StorageBackendError::backend("finish restored physical catalog", error)
339 });
340 }
341 uqa_execution::catalog::tuples::publication::initialize(
342 &self.catalog_execution(),
343 &self.restored_catalog_read_view(),
344 &self.session_execution_view().relation_name_resolution(),
345 &uqa_storage::PersistentStorageSession::new(catalog.clone(), backend.clone()),
346 )
347 .map_err(|error| {
348 uqa_storage::StorageBackendError::backend("initialize physical catalog", error)
349 })
350 }
351}