1use std::sync::Arc;
10use uqa_core::{
11 memory::{
12 Budgeted, BudgetedVec, MemoryBudget, MemoryError, MemoryReservation, Produced,
13 ProductionControl,
14 },
15 CancellationToken, QueryCancelled, Value, ValueRetentionError,
16};
17
18use crate::ast::{ColumnDef, ColumnType, Expr, FunctionBinding};
19
20mod columns;
21mod domains;
22mod expressions;
23mod identities;
24
25#[derive(Debug, thiserror::Error)]
26pub enum CatalogRetentionError {
27 #[error(transparent)]
28 Memory(#[from] MemoryError),
29 #[error(transparent)]
30 Cancelled(#[from] QueryCancelled),
31 #[error("validated column expression contains a subquery")]
32 UnexpectedSubquery,
33 #[error("malformed {kind} value: {reason}")]
34 Malformed { kind: &'static str, reason: String },
35}
36
37impl From<ValueRetentionError> for CatalogRetentionError {
38 fn from(error: ValueRetentionError) -> Self {
39 match error {
40 ValueRetentionError::Memory(error) => Self::Memory(error),
41 ValueRetentionError::Cancelled(error) => Self::Cancelled(error),
42 ValueRetentionError::Malformed { kind, reason } => Self::Malformed { kind, reason },
43 }
44 }
45}
46
47impl From<CatalogRetentionError> for crate::SQLError {
48 fn from(error: CatalogRetentionError) -> Self {
49 match error {
50 CatalogRetentionError::Memory(error) => Self::Routine {
51 sqlstate: "53200".into(),
52 message: error.to_string(),
53 },
54 CatalogRetentionError::Cancelled(error) => Self::Cancelled(error),
55 CatalogRetentionError::UnexpectedSubquery => Self::Internal(error.to_string()),
56 CatalogRetentionError::Malformed { .. } => Self::Routine {
57 sqlstate: "XX001".into(),
58 message: error.to_string(),
59 },
60 }
61 }
62}
63
64type Result<T> = std::result::Result<T, CatalogRetentionError>;
65
66#[derive(Debug, Clone)]
68pub struct RetainedColumns(Arc<Budgeted<Arc<Vec<ColumnDef>>>>);
69
70impl RetainedColumns {
71 pub fn capture(
73 columns: &Arc<Vec<ColumnDef>>,
74 budget: &MemoryBudget,
75 cancellation: &CancellationToken,
76 ) -> Result<Self> {
77 let mut walker = Walker::new(budget, cancellation);
78 walker.charge(size_of::<Vec<ColumnDef>>())?;
79 walker.children(columns, Node::Column)?;
80 let memory = walker.finish()?;
81 cancellation.check()?;
82 Ok(Self(
83 Budgeted::new(Arc::clone(columns), memory).into_shared()?,
84 ))
85 }
86
87 pub fn as_slice(&self) -> &[ColumnDef] {
88 &self.0
89 }
90
91 pub fn reserved_bytes(&self) -> usize {
92 self.0.reserved_bytes()
93 }
94}
95
96impl std::ops::Deref for RetainedColumns {
97 type Target = [ColumnDef];
98
99 fn deref(&self) -> &Self::Target {
100 self.as_slice()
101 }
102}
103
104impl ColumnDef {
105 pub fn reserve_retained_payload(
107 &self,
108 budget: &MemoryBudget,
109 cancellation: &CancellationToken,
110 ) -> Result<MemoryReservation> {
111 Walker::new(budget, cancellation).root(Node::Column(self))
112 }
113}
114
115impl ColumnType {
116 pub(crate) fn retain_external_with_control(
118 self,
119 control: &ProductionControl<'_>,
120 ) -> Result<Produced<Self>> {
121 struct Handoff {
122 value: ColumnType,
123 memory: Option<MemoryReservation>,
124 }
125 let mut output = Handoff {
126 value: self,
127 memory: control.empty_reservation(),
128 };
129 control.check()?;
130 if let Some(budget) = control.budget() {
131 let mut walker = Walker::with_control(budget, *control);
132 let result = walker
133 .visit(Node::Type(&output.value))
134 .and_then(|()| walker.drain());
135 let Walker {
136 memory, pending, ..
137 } = walker;
138 drop(pending);
139 output
140 .memory
141 .as_mut()
142 .expect("controlled catalog handoff")
143 .absorb(memory);
144 result?;
145 }
146 Ok(control.finish(output.value, output.memory)?)
147 }
148
149 pub fn reserve_retained_payload(
151 &self,
152 budget: &MemoryBudget,
153 cancellation: &CancellationToken,
154 ) -> Result<MemoryReservation> {
155 Walker::new(budget, cancellation).root(Node::Type(self))
156 }
157}
158
159impl Expr {
160 pub fn reserve_column_payload(
162 &self,
163 budget: &MemoryBudget,
164 cancellation: &CancellationToken,
165 ) -> Result<MemoryReservation> {
166 Walker::new(budget, cancellation).root(Node::Expr(self))
167 }
168}
169
170enum Node<'a> {
171 Column(&'a ColumnDef),
172 Domain(&'a crate::catalog::domain::StoredDomain),
173 Type(&'a ColumnType),
174 Expr(&'a Expr),
175 Binding(&'a FunctionBinding),
176}
177
178struct Walker<'a> {
179 memory: MemoryReservation,
180 pending: BudgetedVec<Node<'a>>,
181 control: ProductionControl<'a>,
182}
183
184impl<'a> Walker<'a> {
185 fn new(budget: &'a MemoryBudget, cancellation: &'a CancellationToken) -> Self {
186 Self::with_control(
187 budget,
188 ProductionControl::new(budget, cancellation, cancellation),
189 )
190 }
191
192 fn with_control(budget: &'a MemoryBudget, control: ProductionControl<'a>) -> Self {
193 Self {
194 memory: budget.empty_reservation(),
195 pending: BudgetedVec::new(budget),
196 control,
197 }
198 }
199
200 fn root(mut self, root: Node<'a>) -> Result<MemoryReservation> {
201 self.visit(root)?;
202 self.finish()
203 }
204
205 fn finish(mut self) -> Result<MemoryReservation> {
206 self.drain()?;
207 Ok(self.memory)
208 }
209
210 fn drain(&mut self) -> Result<()> {
211 self.control.check_cancellation()?;
212 while let Some(node) = self.pending.pop() {
213 self.visit(node)?;
214 }
215 Ok(())
216 }
217
218 fn visit(&mut self, node: Node<'a>) -> Result<()> {
219 self.control.check_cancellation()?;
220 match node {
221 Node::Column(column) => self.column(column),
222 Node::Domain(domain) => self.domain(domain),
223 Node::Type(ty) => self.ty(ty),
224 Node::Expr(expr) => self.expr(expr),
225 Node::Binding(binding) => self.binding(binding),
226 }
227 }
228
229 fn node(&mut self, node: Node<'a>) -> Result<()> {
230 self.control.check_cancellation()?;
231 self.pending.push(node)?;
232 Ok(())
233 }
234
235 fn charge(&mut self, bytes: usize) -> Result<()> {
236 self.control.check_cancellation()?;
237 self.memory.grow(bytes)?;
238 Ok(())
239 }
240
241 fn buffer<T>(&mut self, capacity: usize) -> Result<()> {
242 self.charge(
243 capacity
244 .checked_mul(size_of::<T>())
245 .ok_or(MemoryError::SizeOverflow)?,
246 )
247 }
248
249 fn children<T>(&mut self, items: &'a Vec<T>, node: fn(&'a T) -> Node<'a>) -> Result<()> {
250 self.buffer::<T>(items.capacity())?;
251 for item in items {
252 self.node(node(item))?;
253 }
254 Ok(())
255 }
256
257 fn boxed<T>(&mut self, item: &'a T, node: fn(&'a T) -> Node<'a>) -> Result<()> {
258 self.charge(size_of::<T>())?;
259 self.node(node(item))
260 }
261
262 fn text(&mut self, text: &String) -> Result<()> {
263 self.charge(text.capacity())
264 }
265
266 fn optional_text(&mut self, text: Option<&String>) -> Result<()> {
267 if let Some(text) = text {
268 self.text(text)?;
269 }
270 Ok(())
271 }
272
273 fn texts(&mut self, texts: &Vec<String>) -> Result<()> {
274 self.buffer::<String>(texts.capacity())?;
275 for text in texts {
276 self.text(text)?;
277 }
278 Ok(())
279 }
280
281 fn value(&mut self, value: &Value) -> Result<()> {
282 let memory = value.reserve_retained_payload_with_check(self.memory.budget(), || {
283 self.control.check_cancellation()
284 })?;
285 self.memory.absorb(memory);
286 Ok(())
287 }
288
289 fn optional_expr(&mut self, expr: Option<&'a Expr>) -> Result<()> {
290 if let Some(expr) = expr {
291 self.node(Node::Expr(expr))?;
292 }
293 Ok(())
294 }
295
296 fn optional_boxed_expr(&mut self, expr: Option<&'a Expr>) -> Result<()> {
297 if let Some(expr) = expr {
298 self.boxed(expr, Node::Expr)?;
299 }
300 Ok(())
301 }
302}
303
304#[cfg(test)]
305mod tests;