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