uqa_sql/semantics/privileges/
context.rs1use crate::catalog::roles::RoleReference;
10use crate::{
11 catalog::resolution::RelationNameResolution,
12 plan::{CtePlan, QueryPlan},
13 SQLError,
14};
15use std::borrow::Cow;
16use std::collections::BTreeMap;
17
18#[derive(Clone, Copy)]
19pub enum PrivilegeRelationKind {
20 System(crate::catalog::SystemRelation),
21 Table,
22 View,
23 MaterializedView,
24 ForeignTable,
25}
26impl PrivilegeRelationKind {
27 pub const fn has_system_columns(self) -> bool {
28 match self {
29 Self::System(relation) => matches!(relation.kind().as_bytes(), b"table"),
30 _ => matches!(self, Self::Table | Self::ForeignTable),
31 }
32 }
33 pub const fn description(self) -> &'static str {
34 match self {
35 Self::System(relation) => relation.kind(),
36 Self::Table => "table",
37 Self::View => "view",
38 Self::MaterializedView => "materialized view",
39 Self::ForeignTable => "foreign table",
40 }
41 }
42}
43pub struct PrivilegeRelation {
44 pub canonical: String,
45 pub columns: Vec<String>,
46 pub kind: PrivilegeRelationKind,
47}
48pub trait PrivilegeCatalog {
49 fn relation(
50 &self,
51 resolution: &RelationNameResolution,
52 name: &str,
53 ) -> Result<Option<PrivilegeRelation>, SQLError>;
54 fn has_select_privilege(
55 &self,
56 resolution: &RelationNameResolution,
57 relation: &PrivilegeRelation,
58 column: Option<&str>,
59 subject: &RoleReference,
60 ) -> Result<bool, SQLError>;
61}
62pub trait PrivilegeCteCatalog {
63 fn is_visible_cte(&self, name: &str) -> bool;
64 fn materialized_columns(&self, name: &str) -> Option<Vec<String>>;
65 fn deferred_reference(&self, name: &str) -> Option<&CtePlan>;
66 fn privilege_subject(&self) -> Result<&RoleReference, SQLError>;
67}
68#[derive(Clone)]
69pub struct PrivilegeScope<'a, Queries = Vec<QueryPlan>> {
70 pub catalog: &'a dyn PrivilegeCatalog,
71 pub resolution: RelationNameResolution,
72 pub inherited: &'a dyn PrivilegeCteCatalog,
73 pub scalar_subqueries: Queries,
74 local_ctes: BTreeMap<String, Cow<'a, CtePlan>>,
75 pub(super) include_authorization_only_columns: bool,
76 pub(super) relation_privilege_subject: Option<RoleReference>,
77}
78impl<'a> PrivilegeScope<'a> {
79 pub fn new(
80 catalog: &'a dyn PrivilegeCatalog,
81 resolution: RelationNameResolution,
82 inherited: &'a dyn PrivilegeCteCatalog,
83 scalar_subqueries: Vec<QueryPlan>,
84 ) -> Self {
85 Self {
86 catalog,
87 resolution,
88 inherited,
89 scalar_subqueries,
90 local_ctes: BTreeMap::new(),
91 include_authorization_only_columns: true,
92 relation_privilege_subject: None,
93 }
94 }
95}
96
97impl<'a> PrivilegeScope<'a, &'a [QueryPlan]> {
98 pub fn borrowed(
100 catalog: &'a dyn PrivilegeCatalog,
101 resolution: RelationNameResolution,
102 inherited: &'a dyn PrivilegeCteCatalog,
103 scalar_subqueries: &'a [QueryPlan],
104 ) -> Self {
105 Self {
106 catalog,
107 resolution,
108 inherited,
109 scalar_subqueries,
110 local_ctes: BTreeMap::new(),
111 include_authorization_only_columns: true,
112 relation_privilege_subject: None,
113 }
114 }
115}
116
117impl<'a, Queries: AsRef<[QueryPlan]>> PrivilegeScope<'a, Queries> {
118 pub fn insert_deferred(&mut self, plan: CtePlan) {
119 self.local_ctes.insert(plan.name.clone(), Cow::Owned(plan));
120 }
121
122 pub(super) fn insert_deferred_ref(&mut self, plan: &'a CtePlan) {
123 self.local_ctes
124 .insert(plan.name.clone(), Cow::Borrowed(plan));
125 }
126
127 pub(super) fn reborrow(&self) -> PrivilegeScope<'_, &[QueryPlan]> {
128 PrivilegeScope {
129 catalog: self.catalog,
130 resolution: self.resolution.clone(),
131 inherited: self.inherited,
132 scalar_subqueries: self.scalar_subqueries.as_ref(),
133 local_ctes: self
134 .local_ctes
135 .iter()
136 .map(|(name, plan)| (name.clone(), Cow::Borrowed(plan.as_ref())))
137 .collect(),
138 include_authorization_only_columns: self.include_authorization_only_columns,
139 relation_privilege_subject: self.relation_privilege_subject.clone(),
140 }
141 }
142 pub fn is_visible_cte(&self, name: &str) -> bool {
143 crate::semantics::cte_reference_name(name)
144 .is_some_and(|name| self.local_ctes.contains_key(&name))
145 || self.inherited.is_visible_cte(name)
146 }
147 pub fn materialized_for_scan(&self, name: &str) -> Option<Vec<String>> {
148 let canonical = crate::semantics::cte_reference_name(name)?;
149 if self.local_ctes.contains_key(&canonical) {
150 None
151 } else {
152 self.inherited.materialized_columns(name)
153 }
154 }
155 pub fn deferred_reference(&self, name: &str) -> Option<&CtePlan> {
156 self.local_ctes
157 .get(&crate::semantics::cte_reference_name(name)?)
158 .map(AsRef::as_ref)
159 .or_else(|| self.inherited.deferred_reference(name))
160 }
161
162 pub fn privilege_subject(&self) -> Result<&RoleReference, SQLError> {
163 self.relation_privilege_subject
164 .as_ref()
165 .map_or_else(|| self.inherited.privilege_subject(), Ok)
166 }
167}