1use super::{CatalogReadView, RelationNameResolution};
8use std::sync::Arc;
9use uqa_sql::catalog::analysis::{AnalysisCatalog, TableDefinition, ViewDefinition};
10use uqa_sql::{ColumnType, SQLError};
11
12impl AnalysisCatalog for CatalogReadView {
13 fn relation_row_type_with_control(
14 &self,
15 resolution: &RelationNameResolution,
16 name: &str,
17 control: &uqa_core::memory::ProductionControl<'_>,
18 ) -> Result<Option<uqa_core::memory::Produced<uqa_sql::ast::CompositeTypeReference>>, SQLError>
19 {
20 let selected = self.relation_kind_resolution_with_control(resolution, name, control)?;
21 let super::RelationResolution::Found(canonical, _) = &*selected else {
22 return Ok(None);
23 };
24 let reference =
25 uqa_core::RelationIdentity::try_borrowed_reference(canonical).map_err(|error| {
26 control
27 .format(format_args!("{error}"))
28 .map(|text| SQLError::Internal(text.into_parts().0))
29 .unwrap_or_else(Into::into)
30 })?;
31 let relation =
32 reference.identity_with_control(uqa_core::RelationName::decoded("public"), control)?;
33 let snapshot = self.snapshot();
34 let oids = snapshot
35 .tables
36 .get(&*relation)
37 .map(|table| table.catalog_oids)
38 .or_else(|| {
39 snapshot
40 .definitions
41 .views
42 .get(&*relation)
43 .map(|view| view.relation_oids())
44 })
45 .or_else(|| {
46 snapshot
47 .definitions
48 .foreign_tables
49 .get(&*relation)
50 .map(super::foreign::StoredForeignTable::relation_oids)
51 });
52 oids.map(|oids| {
53 oids.row_type_reference_with_control(&relation, control)
54 .map_err(Into::into)
55 })
56 .transpose()
57 .map(Option::flatten)
58 }
59
60 fn sequence_exists_with_control(
61 &self,
62 resolution: &RelationNameResolution,
63 name: &str,
64 control: &uqa_core::memory::ProductionControl<'_>,
65 ) -> Result<bool, SQLError> {
66 CatalogReadView::sequence_exists_with_control(self, resolution, name, control)
67 }
68
69 fn virtual_relation_schema_with_control(
70 &self,
71 resolution: &RelationNameResolution,
72 name: &str,
73 control: &uqa_core::memory::ProductionControl<'_>,
74 ) -> Result<
75 Option<uqa_core::memory::Produced<uqa_sql::catalog::analysis::VirtualRelationSchema>>,
76 SQLError,
77 > {
78 if let Some(relation) =
79 self.virtual_relation_resolved_with_control(resolution, name, control)?
80 {
81 return relation.schema_with_control(control).map(Some);
82 }
83 super::graph::age_label_relation_schema_with_control(self, resolution, name, control)
84 }
85
86 fn with_view_definition_with_control(
87 &self,
88 resolution: &RelationNameResolution,
89 name: &str,
90 control: &uqa_core::memory::ProductionControl<'_>,
91 read: &mut dyn FnMut(
92 uqa_sql::catalog::analysis::ViewDefinitionRef<'_>,
93 ) -> Result<(), SQLError>,
94 ) -> Result<(), SQLError> {
95 if let Some(view) =
96 CatalogReadView::view_resolved_with_control(self, resolution, name, control)?
97 {
98 read(uqa_sql::catalog::analysis::ViewDefinitionRef {
99 query: &view.query,
100 output_columns: view.output_columns.as_deref(),
101 materialized: view.kind == crate::catalog::view::StoredViewKind::Materialized,
102 materialized_column_types: &view.materialized_column_types,
103 materialized_column_collations: view.materialized_column_collations.as_deref(),
104 })?;
105 }
106 control.check()?;
107 Ok(())
108 }
109
110 fn with_foreign_table_columns_with_control(
111 &self,
112 resolution: &RelationNameResolution,
113 name: &str,
114 control: &uqa_core::memory::ProductionControl<'_>,
115 read: &mut dyn FnMut(&[uqa_sql::ast::ColumnDef]) -> Result<(), SQLError>,
116 ) -> Result<(), SQLError> {
117 if let Some(table) =
118 CatalogReadView::foreign_table_resolved_with_control(self, resolution, name, control)?
119 {
120 read(&table.columns)?;
121 }
122 control.check()?;
123 Ok(())
124 }
125
126 fn virtual_relation_columns_with_control(
127 &self,
128 resolution: &RelationNameResolution,
129 name: &str,
130 control: &uqa_core::memory::ProductionControl<'_>,
131 ) -> Result<Option<uqa_core::memory::Produced<Vec<String>>>, SQLError> {
132 if let Some(relation) =
133 self.virtual_relation_resolved_with_control(resolution, name, control)?
134 {
135 return relation.column_names_with_control(control).map(Some);
136 }
137 super::graph::age_label_relation_columns_with_control(self, resolution, name, control)
138 }
139
140 fn relation_row_type(
141 &self,
142 resolution: &RelationNameResolution,
143 name: &str,
144 ) -> Result<Option<uqa_sql::ast::CompositeTypeReference>, SQLError> {
145 let Some((canonical, _)) = self
146 .relation_kind_resolution(resolution, name)?
147 .into_found()
148 else {
149 return Ok(None);
150 };
151 let relation =
152 uqa_core::RelationIdentity::from_legacy_name(&canonical).map_err(SQLError::Internal)?;
153 let snapshot = self.snapshot();
154 let oids = snapshot
155 .tables
156 .get(&relation)
157 .map(|table| table.catalog_oids)
158 .or_else(|| {
159 snapshot
160 .definitions
161 .views
162 .get(&relation)
163 .map(|view| view.relation_oids())
164 })
165 .or_else(|| {
166 snapshot
167 .definitions
168 .foreign_tables
169 .get(&relation)
170 .map(super::foreign::StoredForeignTable::relation_oids)
171 });
172 Ok(oids.and_then(|oids| oids.row_type_reference(&relation)))
173 }
174
175 fn relation_dependency(
176 &self,
177 resolution: &RelationNameResolution,
178 name: &str,
179 ) -> Result<Option<u32>, SQLError> {
180 let Some((canonical, kind)) = self
181 .relation_kind_resolution(resolution, name)?
182 .into_found()
183 else {
184 return Ok(None);
185 };
186 super::projection::resolved_relation_oid(self, resolution, &canonical, kind)
187 .and_then(|oid| {
188 u32::try_from(oid)
189 .map_err(|_| SQLError::Internal(format!("invalid relation OID {oid}")))
190 })
191 .map(Some)
192 }
193
194 fn relation_dependency_with_control(
195 &self,
196 resolution: &RelationNameResolution,
197 name: &str,
198 control: &uqa_core::memory::ProductionControl<'_>,
199 ) -> Result<Option<u32>, SQLError> {
200 super::projection::resolved_relation_dependency_with_control(
201 self, resolution, name, control,
202 )
203 }
204
205 fn prepared_dependency_snapshot(
206 &self,
207 dependencies: &uqa_sql::prepared::dependencies::PreparedAnalysisDependencies,
208 ) -> Result<Option<uqa_sql::prepared::dependencies::PreparedDependencySnapshot>, SQLError> {
209 super::prepared_dependencies::capture(self, dependencies).map(Some)
210 }
211
212 fn prepared_dependency_snapshot_with_control(
213 &self,
214 dependencies: &uqa_sql::prepared::dependencies::PreparedAnalysisDependencies,
215 control: &uqa_core::memory::ProductionControl<'_>,
216 ) -> Result<Option<uqa_sql::prepared::dependencies::PreparedDependencySnapshot>, SQLError> {
217 super::prepared_dependencies::capture_with_control(self, dependencies, control).map(Some)
218 }
219
220 fn effective_search_path(
221 &self,
222 resolution: &RelationNameResolution,
223 ) -> Result<Option<uqa_sql::catalog::resolution::EffectiveSearchPath>, SQLError> {
224 let role = &resolution.current_user;
225 Ok(Some(uqa_sql::catalog::resolution::EffectiveSearchPath {
226 schemas: super::namespaces::current_schema_names(self, resolution, role, true),
227 creation_namespace: super::namespaces::current_schema_name(self, resolution, role),
228 }))
229 }
230
231 fn effective_search_path_with_control(
232 &self,
233 resolution: &RelationNameResolution,
234 control: &uqa_core::memory::ProductionControl<'_>,
235 ) -> Result<
236 uqa_core::memory::Produced<Option<uqa_sql::catalog::resolution::EffectiveSearchPath>>,
237 SQLError,
238 > {
239 let role = &resolution.current_user;
240 let schemas = super::namespaces::current_schema_names_with_control(
241 self, resolution, role, true, control,
242 )?;
243 let explicit = super::namespaces::current_schema_names_with_control(
244 self, resolution, role, false, control,
245 )?;
246 let creation = explicit
247 .first()
248 .map(|name| control.copy_text(name))
249 .transpose()?;
250 let (schemas, schema_memory) = schemas.into_parts();
251 let (creation_namespace, creation_memory) = creation.map_or_else(
252 || (None, control.empty_reservation()),
253 |name| {
254 let (name, memory) = name.into_parts();
255 (Some(name), memory)
256 },
257 );
258 Ok(control.finish(
259 Some(uqa_sql::catalog::resolution::EffectiveSearchPath {
260 schemas,
261 creation_namespace,
262 }),
263 control.combine(schema_memory, creation_memory),
264 )?)
265 }
266
267 fn table_resolved(
268 &self,
269 resolution: &RelationNameResolution,
270 name: &str,
271 ) -> Result<Option<TableDefinition>, SQLError> {
272 CatalogReadView::table_resolved(self, resolution, name).map(|table| {
273 table.map(|table| TableDefinition {
274 columns: table.columns.clone(),
275 columns_declared: table.columns_declared,
276 })
277 })
278 }
279
280 fn table_resolved_with_control(
281 &self,
282 resolution: &RelationNameResolution,
283 name: &str,
284 control: &uqa_core::memory::ProductionControl<'_>,
285 ) -> Result<Option<TableDefinition>, SQLError> {
286 CatalogReadView::table_resolved_with_control(self, resolution, name, control).map(|table| {
287 table.map(|table| TableDefinition {
288 columns: table.columns.clone(),
289 columns_declared: table.columns_declared,
290 })
291 })
292 }
293
294 fn table_name_resolved_with_control(
295 &self,
296 resolution: &RelationNameResolution,
297 name: &str,
298 control: &uqa_core::memory::ProductionControl<'_>,
299 ) -> Result<Option<uqa_core::memory::Produced<String>>, SQLError> {
300 CatalogReadView::table_name_resolved_with_control(self, resolution, name, control)
301 }
302
303 fn table_name_resolved(
304 &self,
305 resolution: &RelationNameResolution,
306 name: &str,
307 ) -> Result<Option<String>, SQLError> {
308 CatalogReadView::table_name_resolved(self, resolution, name)
309 }
310
311 fn view_resolved(
312 &self,
313 resolution: &RelationNameResolution,
314 name: &str,
315 ) -> Result<Option<ViewDefinition>, SQLError> {
316 CatalogReadView::view_resolved(self, resolution, name).map(|view| {
317 view.map(|view| ViewDefinition {
318 query: view.query.clone(),
319 output_columns: view.output_columns.clone(),
320 materialized: view.kind == crate::catalog::view::StoredViewKind::Materialized,
321 materialized_column_types: view.materialized_column_types.clone(),
322 materialized_column_collations: view.materialized_column_collations.clone(),
323 })
324 })
325 }
326
327 fn foreign_table_resolved(
328 &self,
329 resolution: &RelationNameResolution,
330 name: &str,
331 ) -> Result<Option<TableDefinition>, SQLError> {
332 CatalogReadView::foreign_table_resolved(self, resolution, name).map(|table| {
333 table.map(|table| TableDefinition {
334 columns: Arc::new(table.columns.clone()),
335 columns_declared: true,
336 })
337 })
338 }
339
340 fn sequence_exists(
341 &self,
342 resolution: &RelationNameResolution,
343 name: &str,
344 ) -> Result<bool, SQLError> {
345 CatalogReadView::sequence_exists_with_control(
346 self,
347 resolution,
348 name,
349 &uqa_core::memory::ProductionControl::uncontrolled(),
350 )
351 }
352
353 fn virtual_relation_schema(
354 &self,
355 resolution: &RelationNameResolution,
356 name: &str,
357 ) -> Result<Option<Vec<(String, ColumnType)>>, SQLError> {
358 super::schema::virtual_relation_schema(self, resolution, name)
359 }
360
361 fn sql_functions(
362 &self,
363 resolution: &RelationNameResolution,
364 name: &str,
365 ) -> Result<Option<Vec<Arc<uqa_sql::routines::SQLUserFunction>>>, SQLError> {
366 CatalogReadView::sql_functions(self, resolution, name)
367 }
368
369 fn unopenable_relation_with_control(
370 &self,
371 resolution: &RelationNameResolution,
372 name: &str,
373 control: &uqa_core::memory::ProductionControl<'_>,
374 ) -> Result<
375 Option<uqa_core::memory::Produced<uqa_sql::catalog::analysis::UnopenableRelation>>,
376 SQLError,
377 > {
378 let selected = self.relation_kind_resolution_with_control(resolution, name, control)?;
379 let super::RelationResolution::Found(canonical, "composite type") = &*selected else {
380 return Ok(None);
381 };
382 let reference =
383 uqa_core::RelationIdentity::try_borrowed_reference(canonical).map_err(|error| {
384 control
385 .format(format_args!("{error}"))
386 .map(|text| SQLError::Internal(text.into_parts().0))
387 .unwrap_or_else(Into::into)
388 })?;
389 let (name, memory) = control
390 .format(format_args!("{}", reference.name))?
391 .into_parts();
392 Ok(Some(control.finish(
393 uqa_sql::catalog::analysis::UnopenableRelation {
394 name,
395 kinds: "composite types",
396 },
397 memory,
398 )?))
399 }
400
401 fn unopenable_relation(
402 &self,
403 resolution: &RelationNameResolution,
404 name: &str,
405 ) -> Result<Option<uqa_sql::catalog::analysis::UnopenableRelation>, SQLError> {
406 Ok(
407 match self
408 .relation_kind_resolution(resolution, name)?
409 .into_found()
410 {
411 Some((canonical, "composite type")) => {
412 let relation = uqa_core::RelationIdentity::from_legacy_name(&canonical)
413 .map_err(SQLError::Internal)?;
414 Some(uqa_sql::catalog::analysis::UnopenableRelation {
415 name: relation.name,
416 kinds: "composite types",
417 })
418 }
419 _ => None,
420 },
421 )
422 }
423}
424
425#[cfg(test)]
426mod tests;