Skip to main content

uqa_execution/catalog/
services.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Consumer-owned services required to project catalog rows.
8use super::RelationNameResolution;
9use uqa_core::Value;
10use uqa_sql::catalog::roles::RoleReference;
11use uqa_sql::catalog::session::{CursorMetadata, PreparedStatementMetadata};
12use uqa_sql::{
13    ast::{Expr, TriggerEvent},
14    SQLError,
15};
16
17pub trait CatalogSession: Sync {
18    fn current_role(&self) -> RoleReference;
19    fn temporary_schema_name(&self) -> String;
20    fn relation_name_resolution(&self) -> RelationNameResolution;
21    /// The canonical name of the parameter `name` refers to and its value as `SHOW` reports it.
22    fn show_parameter(&self, name: &str) -> Result<(String, String), SQLError>;
23    /// Every parameter that `SHOW ALL` and `pg_settings` report, in name order.
24    fn parameter_settings(&self) -> Vec<uqa_sql::semantics::parameters::setting::ParameterSetting>;
25    fn prepared_statements(&self) -> Vec<PreparedStatementMetadata>;
26    fn cursors(&self) -> Vec<CursorMetadata>;
27}
28pub trait CatalogExpressionEvaluation: Sync {
29    fn evaluate(&self, expression: &Expr) -> Result<Value, SQLError>;
30}
31pub trait RelationCounts: Sync {
32    fn table_doc_count(&self, table: &str) -> Result<u64, SQLError>;
33}
34pub use uqa_sql::catalog::view::ViewMutationCapabilities;
35pub struct ViewCatalogMetadata {
36    pub catalog: ViewMutationCapabilities,
37    pub catalog_columns: Vec<bool>,
38    pub check_option: String,
39}
40pub trait ViewCatalogCapabilities: Sync {
41    fn view_updatability(&self, name: &str) -> Result<ViewCatalogMetadata, SQLError>;
42    fn has_instead_of_trigger(&self, name: &str, event: TriggerEvent) -> Result<bool, SQLError>;
43    fn view_updatability_with_catalog(
44        &self,
45        name: &str,
46        _catalog: &super::CatalogReadView,
47        _resolution: &RelationNameResolution,
48    ) -> Result<ViewCatalogMetadata, SQLError> {
49        self.view_updatability(name)
50    }
51    fn has_instead_of_trigger_with_catalog(
52        &self,
53        name: &str,
54        event: TriggerEvent,
55        _catalog: &super::CatalogReadView,
56        _resolution: &RelationNameResolution,
57    ) -> Result<bool, SQLError> {
58        self.has_instead_of_trigger(name, event)
59    }
60}
61
62pub trait CatalogNamespace: Sync {
63    fn current_schema_names(&self, include_implicit: bool) -> Result<Vec<String>, SQLError>;
64    /// Resolve visibility through a caller-selected catalog while preserving legacy namespace services.
65    fn current_schema_names_with_catalog(
66        &self,
67        _catalog: &super::CatalogReadView,
68        include_implicit: bool,
69    ) -> Result<Vec<String>, SQLError> {
70        self.current_schema_names(include_implicit)
71    }
72}
73
74pub trait CatalogSnapshotSource: Sync {
75    fn catalog_snapshot(&self) -> super::CatalogReadView;
76    /// Capture builtin authority at the same boundary as ordinary catalog reads. Sources may use immutable authority inputs when they prove the existing unrestricted rule without table metadata.
77    fn builtin_routine_permissions(
78        &self,
79        current_role: RoleReference,
80    ) -> super::security::builtin_routines::execution::BuiltinRoutinePermissions {
81        super::security::builtin_routines::execution::BuiltinRoutinePermissions::new(
82            self.catalog_snapshot(),
83            current_role,
84        )
85    }
86    fn refreshed_catalog_snapshot(&self) -> Result<super::CatalogReadView, SQLError>;
87    /// Refresh definitions without capturing a query view that definition coordination will discard.
88    fn refresh_catalog(&self) -> Result<(), SQLError> {
89        self.refreshed_catalog_snapshot().map(|_| ())
90    }
91    /// Attach the original query participant without replacing the retained catalog or observing unused sources.
92    fn bind_query_reads(
93        &self,
94        snapshot: super::CatalogReadView,
95    ) -> Result<super::CatalogReadView, SQLError> {
96        Ok(snapshot)
97    }
98    /// Retain direct catalog rows independently of name binding and SSI attribution.
99    fn retain_catalog_rows(
100        &self,
101        snapshot: super::CatalogReadView,
102    ) -> Result<super::CatalogReadView, SQLError> {
103        Ok(snapshot)
104    }
105    /// Definition coordination uses the refreshed session catalog, independently from an ordinary query's retained snapshot.
106    fn current_catalog_snapshot(&self) -> super::CatalogReadView;
107    /// A session-local revision covering all visible definition changes, including private DDL, rollback and refresh after a catalog lock wait. Sources without this guarantee must return `None`.
108    fn current_catalog_revision(&self) -> Option<u64> {
109        None
110    }
111}
112impl CatalogSnapshotSource for super::CatalogReadView {
113    fn current_catalog_revision(&self) -> Option<u64> {
114        Some(0)
115    }
116    fn refresh_catalog(&self) -> Result<(), SQLError> {
117        Ok(())
118    }
119    fn current_catalog_snapshot(&self) -> super::CatalogReadView {
120        self.clone()
121    }
122    fn refreshed_catalog_snapshot(&self) -> Result<super::CatalogReadView, SQLError> {
123        Ok(self.clone())
124    }
125    fn catalog_snapshot(&self) -> super::CatalogReadView {
126        self.clone()
127    }
128}