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}
77impl<'a> PrivilegeScope<'a> {
78 pub fn new(
79 catalog: &'a dyn PrivilegeCatalog,
80 resolution: RelationNameResolution,
81 inherited: &'a dyn PrivilegeCteCatalog,
82 scalar_subqueries: Vec<QueryPlan>,
83 ) -> Self {
84 Self {
85 catalog,
86 resolution,
87 inherited,
88 scalar_subqueries,
89 local_ctes: BTreeMap::new(),
90 include_authorization_only_columns: true,
91 }
92 }
93}
94
95impl<'a> PrivilegeScope<'a, &'a [QueryPlan]> {
96 pub fn borrowed(
98 catalog: &'a dyn PrivilegeCatalog,
99 resolution: RelationNameResolution,
100 inherited: &'a dyn PrivilegeCteCatalog,
101 scalar_subqueries: &'a [QueryPlan],
102 ) -> Self {
103 Self {
104 catalog,
105 resolution,
106 inherited,
107 scalar_subqueries,
108 local_ctes: BTreeMap::new(),
109 include_authorization_only_columns: true,
110 }
111 }
112}
113
114impl<'a, Queries: AsRef<[QueryPlan]>> PrivilegeScope<'a, Queries> {
115 pub fn insert_deferred(&mut self, plan: CtePlan) {
116 self.local_ctes.insert(plan.name.clone(), Cow::Owned(plan));
117 }
118
119 pub(super) fn insert_deferred_ref(&mut self, plan: &'a CtePlan) {
120 self.local_ctes
121 .insert(plan.name.clone(), Cow::Borrowed(plan));
122 }
123
124 pub(super) fn reborrow(&self) -> PrivilegeScope<'_, &[QueryPlan]> {
125 PrivilegeScope {
126 catalog: self.catalog,
127 resolution: self.resolution.clone(),
128 inherited: self.inherited,
129 scalar_subqueries: self.scalar_subqueries.as_ref(),
130 local_ctes: self
131 .local_ctes
132 .iter()
133 .map(|(name, plan)| (name.clone(), Cow::Borrowed(plan.as_ref())))
134 .collect(),
135 include_authorization_only_columns: self.include_authorization_only_columns,
136 }
137 }
138 pub fn is_visible_cte(&self, name: &str) -> bool {
139 crate::semantics::cte_reference_name(name)
140 .is_some_and(|name| self.local_ctes.contains_key(&name))
141 || self.inherited.is_visible_cte(name)
142 }
143 pub fn materialized_for_scan(&self, name: &str) -> Option<Vec<String>> {
144 let canonical = crate::semantics::cte_reference_name(name)?;
145 if self.local_ctes.contains_key(&canonical) {
146 None
147 } else {
148 self.inherited.materialized_columns(name)
149 }
150 }
151 pub fn deferred_reference(&self, name: &str) -> Option<&CtePlan> {
152 self.local_ctes
153 .get(&crate::semantics::cte_reference_name(name)?)
154 .map(AsRef::as_ref)
155 .or_else(|| self.inherited.deferred_reference(name))
156 }
157
158 pub fn privilege_subject(&self) -> Result<&RoleReference, SQLError> {
159 self.inherited.privilege_subject()
160 }
161}