uqa_sql/prepared/
dependencies.rs1use crate::ScalarExpr;
10use std::{any::Any, fmt, sync::Arc};
11use uqa_core::memory::{MemoryReservation, OwnedMap, OwnedSet, Produced, ProductionControl};
12
13mod production;
14
15#[derive(Clone, Debug, Default, PartialEq, Eq)]
17pub struct PreparedAnalysisDependencies {
18 pub relations: OwnedSet<u32>,
19 pub routines: OwnedSet<[u8; 16]>,
20}
21
22#[derive(Clone)]
24pub struct PreparedDependencyRevision(Arc<dyn RevisionEquality>);
25
26trait RevisionEquality: Send + Sync {
27 fn as_any(&self) -> &dyn Any;
28 fn equals(&self, other: &dyn RevisionEquality) -> bool;
29}
30
31struct Revision<T> {
32 value: T,
33 _memory: Option<MemoryReservation>,
34}
35
36impl<T: Eq + Send + Sync + 'static> RevisionEquality for Revision<T> {
37 fn as_any(&self) -> &dyn Any {
38 &self.value
39 }
40
41 fn equals(&self, other: &dyn RevisionEquality) -> bool {
42 other.as_any().downcast_ref::<T>() == Some(&self.value)
43 }
44}
45
46impl PreparedDependencyRevision {
47 #[must_use]
48 pub fn new<T: Eq + Send + Sync + 'static>(revision: T) -> Self {
49 let control = ProductionControl::uncontrolled();
50 Self::from_produced(
51 control.finish(revision, None).expect("ordinary revision"),
52 &control,
53 )
54 .expect("ordinary revision publication")
55 }
56
57 pub fn from_produced<T: Eq + Send + Sync + 'static>(
58 revision: Produced<T>,
59 control: &ProductionControl<'_>,
60 ) -> Result<Self, crate::SQLError> {
61 let header = control.reserve(size_of::<Revision<T>>())?;
62 let (value, memory) = revision.into_parts();
63 Ok(Self(Arc::new(Revision {
64 value,
65 _memory: control.combine(memory, header),
66 })))
67 }
68}
69
70impl PartialEq for PreparedDependencyRevision {
71 fn eq(&self, other: &Self) -> bool {
72 Arc::ptr_eq(&self.0, &other.0) || self.0.equals(other.0.as_ref())
73 }
74}
75
76impl Eq for PreparedDependencyRevision {}
77
78impl fmt::Debug for PreparedDependencyRevision {
79 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
80 formatter
81 .debug_struct("PreparedDependencyRevision")
82 .finish_non_exhaustive()
83 }
84}
85
86#[derive(Clone, Debug, Default, PartialEq, Eq)]
88pub struct PreparedDependencySnapshot {
89 pub global_catalog: Option<PreparedDependencyRevision>,
91 pub relations: OwnedMap<u32, Option<PreparedDependencyRevision>>,
92 pub routines: OwnedMap<[u8; 16], Option<PreparedDependencyRevision>>,
93}
94
95impl PreparedDependencySnapshot {
96 pub fn copy_with_control(
98 &self,
99 control: &ProductionControl<'_>,
100 ) -> Result<Self, crate::SQLError> {
101 let mut output = Self {
102 global_catalog: self.global_catalog.clone(),
103 ..Self::default()
104 };
105 for (key, value) in &self.relations {
106 control.check()?;
107 match control.budget() {
108 Some(budget) => {
109 output
110 .relations
111 .insert_budgeted(*key, value.clone(), budget)?;
112 }
113 None => {
114 output.relations.insert(*key, value.clone());
115 }
116 }
117 }
118 for (key, value) in &self.routines {
119 control.check()?;
120 match control.budget() {
121 Some(budget) => {
122 output
123 .routines
124 .insert_budgeted(*key, value.clone(), budget)?;
125 }
126 None => {
127 output.routines.insert(*key, value.clone());
128 }
129 }
130 }
131 Ok(output)
132 }
133}
134
135impl PreparedAnalysisDependencies {
136 pub(crate) fn include_expression(&mut self, expression: &ScalarExpr) {
138 self.include_expression_with_control(expression, &ProductionControl::uncontrolled())
139 .expect("ordinary expression dependency");
140 }
141}
142
143#[cfg(test)]
144mod tests;