Skip to main content

uqa_execution/catalog/
analysis.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use 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(
14        &self,
15        resolution: &RelationNameResolution,
16        name: &str,
17    ) -> Result<Option<uqa_sql::ast::CompositeTypeReference>, SQLError> {
18        let Some((canonical, _)) = self
19            .relation_kind_resolution(resolution, name)?
20            .into_found()
21        else {
22            return Ok(None);
23        };
24        let relation =
25            uqa_core::RelationIdentity::from_legacy_name(&canonical).map_err(SQLError::Internal)?;
26        let snapshot = self.snapshot();
27        let oids = snapshot
28            .tables
29            .get(&relation)
30            .map(|table| table.catalog_oids)
31            .or_else(|| {
32                snapshot
33                    .definitions
34                    .views
35                    .get(&relation)
36                    .map(|view| view.relation_oids())
37            })
38            .or_else(|| {
39                snapshot
40                    .definitions
41                    .foreign_tables
42                    .get(&relation)
43                    .map(super::foreign::StoredForeignTable::relation_oids)
44            });
45        Ok(oids.and_then(|oids| oids.row_type_reference(&relation)))
46    }
47
48    fn relation_dependency(
49        &self,
50        resolution: &RelationNameResolution,
51        name: &str,
52    ) -> Result<Option<u32>, SQLError> {
53        let Some((canonical, kind)) = self
54            .relation_kind_resolution(resolution, name)?
55            .into_found()
56        else {
57            return Ok(None);
58        };
59        super::projection::resolved_relation_oid(self, resolution, &canonical, kind)
60            .and_then(|oid| {
61                u32::try_from(oid)
62                    .map_err(|_| SQLError::Internal(format!("invalid relation OID {oid}")))
63            })
64            .map(Some)
65    }
66
67    fn prepared_dependency_snapshot(
68        &self,
69        dependencies: &uqa_sql::prepared::dependencies::PreparedAnalysisDependencies,
70    ) -> Result<Option<uqa_sql::prepared::dependencies::PreparedDependencySnapshot>, SQLError> {
71        super::prepared_dependencies::capture(self, dependencies).map(Some)
72    }
73
74    fn effective_search_path(
75        &self,
76        resolution: &RelationNameResolution,
77    ) -> Result<Option<uqa_sql::catalog::resolution::EffectiveSearchPath>, SQLError> {
78        let role = &resolution.current_user;
79        Ok(Some(uqa_sql::catalog::resolution::EffectiveSearchPath {
80            schemas: super::namespaces::current_schema_names(self, resolution, role, true),
81            creation_namespace: super::namespaces::current_schema_name(self, resolution, role),
82        }))
83    }
84
85    fn table_resolved(
86        &self,
87        resolution: &RelationNameResolution,
88        name: &str,
89    ) -> Result<Option<TableDefinition>, SQLError> {
90        CatalogReadView::table_resolved(self, resolution, name).map(|table| {
91            table.map(|table| TableDefinition {
92                columns: table.columns.clone(),
93                columns_declared: table.columns_declared,
94            })
95        })
96    }
97
98    fn table_name_resolved(
99        &self,
100        resolution: &RelationNameResolution,
101        name: &str,
102    ) -> Result<Option<String>, SQLError> {
103        CatalogReadView::table_name_resolved(self, resolution, name)
104    }
105
106    fn view_resolved(
107        &self,
108        resolution: &RelationNameResolution,
109        name: &str,
110    ) -> Result<Option<ViewDefinition>, SQLError> {
111        CatalogReadView::view_resolved(self, resolution, name).map(|view| {
112            view.map(|view| ViewDefinition {
113                query: view.query.clone(),
114                output_columns: view.output_columns.clone(),
115                materialized: view.kind == crate::catalog::view::StoredViewKind::Materialized,
116                materialized_column_types: view.materialized_column_types.clone(),
117                materialized_column_collations: view.materialized_column_collations.clone(),
118            })
119        })
120    }
121
122    fn foreign_table_resolved(
123        &self,
124        resolution: &RelationNameResolution,
125        name: &str,
126    ) -> Result<Option<TableDefinition>, SQLError> {
127        CatalogReadView::foreign_table_resolved(self, resolution, name).map(|table| {
128            table.map(|table| TableDefinition {
129                columns: Arc::new(table.columns.clone()),
130                columns_declared: true,
131            })
132        })
133    }
134
135    fn sequence_exists(
136        &self,
137        resolution: &RelationNameResolution,
138        name: &str,
139    ) -> Result<bool, SQLError> {
140        CatalogReadView::sequence_resolved(self, resolution, name)
141            .map(|sequence| sequence.is_some())
142    }
143
144    fn virtual_relation_schema(
145        &self,
146        resolution: &RelationNameResolution,
147        name: &str,
148    ) -> Result<Option<Vec<(String, ColumnType)>>, SQLError> {
149        super::schema::virtual_relation_schema(self, resolution, name)
150    }
151
152    fn sql_functions(
153        &self,
154        resolution: &RelationNameResolution,
155        name: &str,
156    ) -> Result<Option<Vec<Arc<uqa_sql::routines::SQLUserFunction>>>, SQLError> {
157        CatalogReadView::sql_functions(self, resolution, name)
158    }
159
160    fn unopenable_relation(
161        &self,
162        resolution: &RelationNameResolution,
163        name: &str,
164    ) -> Result<Option<uqa_sql::catalog::analysis::UnopenableRelation>, SQLError> {
165        Ok(
166            match self
167                .relation_kind_resolution(resolution, name)?
168                .into_found()
169            {
170                Some((canonical, "composite type")) => {
171                    let relation = uqa_core::RelationIdentity::from_legacy_name(&canonical)
172                        .map_err(SQLError::Internal)?;
173                    Some(uqa_sql::catalog::analysis::UnopenableRelation {
174                        name: relation.name,
175                        kinds: "composite types",
176                    })
177                }
178                _ => None,
179            },
180        )
181    }
182}