uqa_execution/relational/project/
production.rs1use super::{Project, ProjectionTarget};
10use crate::{
11 batch::ProjectedSlot, PhysicalOperator, RowSchema, ScalarExpr, SharedExpressionEvaluator,
12};
13use uqa_core::memory::{Produced, ProductionControl, ProductionVec};
14use uqa_sql::{plan::OwnedScalarExpr, ColumnType, SQLError};
15
16struct Column {
17 name: Produced<String>,
18 ty: Option<Produced<ColumnType>>,
19 source: ProjectedSlot,
20}
21struct Internal {
22 column: uqa_sql::ast::InternalColumnRef,
23 ty: Option<Produced<ColumnType>>,
24 source: ProjectedSlot,
25}
26
27impl<'a> Project<'a> {
28 pub fn with_owned_target_evaluator(
30 child: Box<dyn PhysicalOperator + 'a>,
31 projections: impl IntoIterator<
32 Item = Result<(Produced<ProjectionTarget>, OwnedScalarExpr), SQLError>,
33 >,
34 evaluator: SharedExpressionEvaluator<'a>,
35 control: &ProductionControl<'_>,
36 ) -> Result<Self, SQLError> {
37 let (child, (schema, computed)) =
38 crate::physical::construction::OperatorBuild::run(child, |child| {
39 let inputs = ProjectionInputs::collect(projections, control)?;
40 let inputs = inputs.bind(child.row_schema(), &evaluator, control)?;
41 let mut layout = Layout::new(child.row_schema(), &evaluator, control);
42 for projection in inputs {
43 control.check()?;
44 layout.append(projection)?;
45 }
46 layout.finish()
47 })?;
48 Ok(Self {
49 child,
50 input_pending: crate::physical::demand::PendingRows::default(),
51 computed,
52 evaluator,
53 schema,
54 ordering: Vec::new(),
55 work_mem: None,
56 pending: None,
57 })
58 }
59}
60
61type OwnedProjection = (Produced<ProjectionTarget>, OwnedScalarExpr);
62
63struct ProjectionInputs {
65 values: std::vec::IntoIter<OwnedProjection>,
66 _memory: Option<uqa_core::memory::MemoryReservation>,
67}
68
69impl ProjectionInputs {
70 fn from_produced(input: Produced<Vec<OwnedProjection>>) -> Self {
71 let (values, memory) = input.into_parts();
72 Self {
73 values: values.into_iter(),
74 _memory: memory,
75 }
76 }
77
78 fn collect(
79 projections: impl IntoIterator<Item = Result<OwnedProjection, SQLError>>,
80 control: &ProductionControl<'_>,
81 ) -> Result<Self, SQLError> {
82 let mut inputs = ProductionVec::new(*control);
83 for projection in projections {
84 control.check()?;
85 inputs.push_produced(control.finish(projection?, control.empty_reservation())?)?;
86 }
87 Ok(Self::from_produced(inputs.finish()?))
88 }
89
90 fn bind(
91 self,
92 schema: &RowSchema,
93 evaluator: &SharedExpressionEvaluator<'_>,
94 control: &ProductionControl<'_>,
95 ) -> Result<Self, SQLError> {
96 let mut bound = ProductionVec::new(*control);
97 for (target, expression) in self {
98 let expression =
99 evaluator.bind_type_introspection_owned(expression, schema, control)?;
100 bound.push_produced(
101 control.finish((target, expression), control.empty_reservation())?,
102 )?;
103 }
104 Ok(Self::from_produced(bound.finish()?))
105 }
106}
107
108impl Iterator for ProjectionInputs {
109 type Item = OwnedProjection;
110 fn next(&mut self) -> Option<Self::Item> {
111 self.values.next()
112 }
113 fn size_hint(&self) -> (usize, Option<usize>) {
114 self.values.size_hint()
115 }
116}
117impl ExactSizeIterator for ProjectionInputs {}
118
119type Computed = crate::scalar::PreparedExpressions<Produced<Vec<ScalarExpr>>>;
120
121struct Layout<'a, 'e> {
122 input: &'a RowSchema,
123 evaluator: &'a SharedExpressionEvaluator<'e>,
124 control: ProductionControl<'a>,
125 projected: ProductionVec<'a, Column>,
126 internal: ProductionVec<'a, Internal>,
127 computed: ProductionVec<'a, ScalarExpr>,
128}
129
130impl<'a, 'e> Layout<'a, 'e> {
131 fn new(
132 input: &'a RowSchema,
133 evaluator: &'a SharedExpressionEvaluator<'e>,
134 control: &ProductionControl<'a>,
135 ) -> Self {
136 Self {
137 input,
138 evaluator,
139 control: *control,
140 projected: ProductionVec::new(*control),
141 internal: ProductionVec::new(*control),
142 computed: ProductionVec::new(*control),
143 }
144 }
145 fn append(
146 &mut self,
147 (target, expression): (Produced<ProjectionTarget>, OwnedScalarExpr),
148 ) -> Result<(), SQLError> {
149 let input = self.input;
150 let evaluator = self.evaluator;
151 let control = &self.control;
152 if uqa_sql::semantics::visit_bound_projection_star_metadata(
153 &expression,
154 input,
155 input,
156 control,
157 &mut |field| {
158 if field
159 .logical
160 .is_some_and(|position| !evaluator.star_position_visible(input, position))
161 {
162 return Ok(());
163 }
164 let physical = field
165 .logical
166 .and_then(|position| input.physical_slot(position))
167 .or_else(|| {
168 field
169 .qualifier
170 .and_then(|qualifier| input.qualified_slot(qualifier, field.name))
171 });
172 let column = Column {
173 name: control.copy_text(field.name)?,
174 ty: field
175 .ty
176 .map(|ty| ty.clone_with_control(control))
177 .transpose()?,
178 source: ProjectedSlot::BoundInput {
179 physical,
180 collation: field.collation,
181 },
182 };
183 self.projected
184 .push_produced(control.finish(column, control.empty_reservation())?)?;
185 Ok(())
186 },
187 )? {
188 assert!(
189 matches!(&*target, ProjectionTarget::Column(_)),
190 "internal projection cannot expand a wildcard"
191 );
192 return Ok(());
193 }
194 let ty = match evaluator.expression_type_with_control(&expression, input, control) {
195 Ok(ty) => ty,
196 Err(error) if matches!(error.sqlstate(), Some("53200" | "57014")) => return Err(error),
197 Err(_) => None,
198 };
199 let source = if let Some(source) = input.projection_input(&expression) {
200 source
201 } else {
202 let source = ProjectedSlot::Computed {
203 position: self.computed.len(),
204 collation: input.input_collation(&expression),
205 };
206 self.computed.push_produced(expression.finish(control)?)?;
207 source
208 };
209 let (target, memory) = target.into_parts();
210 match target {
211 ProjectionTarget::Column(name) => self.projected.push_produced(control.finish(
212 Column {
213 name: control.finish(name, memory)?,
214 ty,
215 source,
216 },
217 control.empty_reservation(),
218 )?)?,
219 ProjectionTarget::Internal(column) => {
220 let _target = control.finish(column, memory)?;
221 self.internal.push_produced(
222 control.finish(Internal { column, ty, source }, control.empty_reservation())?,
223 )?;
224 }
225 }
226 Ok(())
227 }
228 fn finish(self) -> Result<(RowSchema, Computed), SQLError> {
229 let input = self.input;
230 let control = &self.control;
231 let mut internal_fields = ProductionVec::new(*control);
232 for field in self.internal.iter() {
233 internal_fields.push_copy((field.column, field.ty.as_deref(), field.source))?;
234 }
235 let schema = RowSchema::project_with_sources_with_control(
236 input,
237 self.projected
238 .iter()
239 .map(|column| (column.name.as_str(), column.ty.as_deref(), column.source)),
240 &internal_fields,
241 self.computed.len(),
242 false,
243 control,
244 )?;
245 let computed =
246 crate::scalar::PreparedExpressions::produced_scalars(self.computed.finish()?, control)?;
247 Ok((schema, computed))
248 }
249}
250
251#[cfg(test)]
252mod tests;