1use super::{BindingContext, SchemaScope};
10use crate::{
11 catalog::{analysis::CatalogReadView, resolution::RelationNameResolution},
12 plan::{QueryPlan, RetainedCtePlan, RetainedQueryArena},
13 RowSchema, SQLError,
14};
15use uqa_core::memory::{Produced, ProductionControl};
16
17mod entries;
18pub use entries::{
19 analyze_expression_plan_type_with_control, analyze_query_plan_schema_with_control,
20 analyze_source_plan_schema_with_control, bind_query_plan_schema_with_control,
21 bind_source_plan_schema_in_scope_with_control, bind_source_plan_schema_with_control,
22};
23mod names;
24mod namespace;
25pub use namespace::ControlledStoredQueryNamespace;
26mod resolution;
27pub use names::{BindingIter, BindingMap, BindingNameSet};
28pub use resolution::BindingResolution;
29
30pub struct ControlledBindingSnapshot {
31 pub catalog: CatalogReadView,
32 pub resolution: BindingResolution,
33 pub ctes: BindingMap<RowSchema>,
34 pub deferred_ctes: BindingMap<RetainedCtePlan>,
35 pub non_returning_ctes: BindingNameSet,
36 pub scalar_subqueries: RetainedQueryArena,
37}
38
39pub struct ControlledBindingContext<'a> {
40 pub catalog: CatalogReadView,
41 pub resolution: BindingResolution,
42 pub ctes: BindingMap<RowSchema>,
43 pub deferred_ctes: BindingMap<RetainedCtePlan>,
44 pub non_returning_ctes: BindingNameSet,
45 pub scalar_subqueries: &'a [QueryPlan],
46}
47
48impl ControlledBindingSnapshot {
49 pub fn from_inputs_with_control<'a>(
51 catalog: CatalogReadView,
52 resolution: Produced<RelationNameResolution>,
53 ctes: impl IntoIterator<Item = (&'a str, RowSchema)>,
54 deferred_ctes: impl IntoIterator<Item = (&'a str, &'a RetainedCtePlan)>,
55 non_returning_ctes: impl IntoIterator<Item = &'a str>,
56 scalar_subqueries: RetainedQueryArena,
57 control: &ProductionControl<'_>,
58 ) -> Result<Self, SQLError> {
59 control.check()?;
60 let mut output = Self {
61 catalog,
62 resolution: BindingResolution::from_produced(resolution),
63 ctes: BindingMap::new(),
64 deferred_ctes: BindingMap::new(),
65 non_returning_ctes: BindingNameSet::new(),
66 scalar_subqueries: scalar_subqueries.retain_with_control(control)?,
67 };
68 for (name, schema) in ctes {
69 output.ctes.insert_with_control(name, schema, control)?;
70 }
71 for (name, plan) in deferred_ctes {
72 output
73 .deferred_ctes
74 .insert_with_control(name, plan.clone(), control)?;
75 }
76 for name in non_returning_ctes {
77 output
78 .non_returning_ctes
79 .insert_with_control(name, control)?;
80 }
81 control.check()?;
82 Ok(output)
83 }
84 pub fn context_with_control(
85 &self,
86 control: &ProductionControl<'_>,
87 ) -> Result<ControlledBindingContext<'_>, SQLError> {
88 control.check()?;
89 Ok(ControlledBindingContext {
90 catalog: self.catalog.clone(),
91 resolution: self.resolution.copy_with_control(control)?,
92 ctes: self
93 .ctes
94 .copy_with_control(control, |schema| Ok(schema.clone()))?,
95 deferred_ctes: self
96 .deferred_ctes
97 .copy_with_control(control, |plan| Ok(plan.clone()))?,
98 non_returning_ctes: self.non_returning_ctes.copy_with_control(control)?,
99 scalar_subqueries: &self.scalar_subqueries,
100 })
101 }
102}
103
104impl super::snapshot::BindingSnapshot {
105 pub fn copy_with_control(
106 &self,
107 control: &ProductionControl<'_>,
108 ) -> Result<ControlledBindingSnapshot, SQLError> {
109 ControlledBindingSnapshot::from_inputs_with_control(
110 self.catalog.clone(),
111 self.resolution.copy_with_control(control)?,
112 self.ctes
113 .iter()
114 .map(|(name, schema)| (name.as_str(), schema.clone())),
115 self.deferred_ctes
116 .iter()
117 .map(|(name, plan)| (name.as_str(), plan)),
118 self.non_returning_ctes.iter().map(String::as_str),
119 self.scalar_subqueries.clone(),
120 control,
121 )
122 }
123}
124
125impl<'a> BindingContext<'a> {
126 pub fn copy_with_control(
127 &self,
128 control: &ProductionControl<'_>,
129 ) -> Result<ControlledBindingContext<'a>, SQLError> {
130 control.check()?;
131 let mut ctes = BindingMap::new();
132 let mut deferred = BindingMap::new();
133 let mut non_returning = BindingNameSet::new();
134 let resolution =
135 BindingResolution::from_produced(self.resolution.copy_with_control(control)?);
136 for (name, schema) in &self.ctes {
137 ctes.insert_with_control(name, schema.clone(), control)?;
138 }
139 for (name, plan) in &self.deferred_ctes {
140 deferred.insert_with_control(name, plan.clone(), control)?;
141 }
142 for name in &self.non_returning_ctes {
143 non_returning.insert_with_control(name, control)?;
144 }
145 Ok(ControlledBindingContext {
146 catalog: self.catalog.clone(),
147 resolution,
148 ctes,
149 deferred_ctes: deferred,
150 non_returning_ctes: non_returning,
151 scalar_subqueries: self.scalar_subqueries,
152 })
153 }
154}
155
156impl SchemaScope {
157 pub(super) fn for_analysis_with_control(
158 binding: ControlledBindingContext<'_>,
159 control: &ProductionControl<'_>,
160 ) -> Result<Self, SQLError> {
161 let mut scope = Self::from_context_with_control(binding, control)?;
162 scope.validate_references = true;
163 Ok(scope)
164 }
165 pub(super) fn from_context_with_control(
166 binding: ControlledBindingContext<'_>,
167 control: &ProductionControl<'_>,
168 ) -> Result<Self, SQLError> {
169 control.check()?;
170 let scope = Self {
171 execution_sources: None,
172 collations: super::preparation::PendingCollations::new(control),
173 catalog: binding.catalog,
174 resolution: binding.resolution,
175 ctes: binding.ctes,
176 deferred_ctes: binding.deferred_ctes,
177 non_returning_ctes: binding.non_returning_ctes,
178 production: control.capture(),
179 visiting_views: BindingNameSet::new(),
180 validate_references: false,
181 stored_expression_outer: None,
182 rule_condition_event: None,
183 merge_returning: false,
184 set_placement: crate::semantics::sets::placement::SetPlacement::Expanding,
185 set_calls: 0,
186 stored_whole_rows: [None, None],
187 routine_parameters: None,
188 scalar_binding: super::ScalarBindingMode::Stored,
189 variable_sites: None,
190 preserve_syntax_shape: false,
191 prepared_dependencies: None,
192 composite_dependencies: None,
193 };
194 scope.control().check()?;
195 Ok(scope)
196 }
197 pub(super) fn control(&self) -> ProductionControl<'_> {
198 self.production.control()
199 }
200}
201
202impl ControlledBindingContext<'_> {
203 pub fn copy_with_control(&self, control: &ProductionControl<'_>) -> Result<Self, SQLError> {
204 control.check()?;
205 Ok(Self {
206 catalog: self.catalog.clone(),
207 resolution: self.resolution.copy_with_control(control)?,
208 ctes: self
209 .ctes
210 .copy_with_control(control, |schema| Ok(schema.clone()))?,
211 deferred_ctes: self
212 .deferred_ctes
213 .copy_with_control(control, |plan| Ok(plan.clone()))?,
214 non_returning_ctes: self.non_returning_ctes.copy_with_control(control)?,
215 scalar_subqueries: self.scalar_subqueries,
216 })
217 }
218}