uqa_sql/plan/mutable/
projections.rs1use super::{Copying, MutableQueryPlan, OwnedScalarExpr, RelationalPlan, RewriteMemory};
10use crate::{plan::ProjectionPlan, RowSchema, SQLError};
11use uqa_core::memory::{Budgeted, Produced, ProductionControl};
12
13pub struct OwnedProjection {
14 pub expression: OwnedScalarExpr,
15 pub alias: Option<Produced<String>>,
16}
17
18pub struct OwnedProjectionIter {
19 pub(super) projections: std::vec::IntoIter<ProjectionPlan>,
20 pub(super) memory: Option<RewriteMemory>,
21 pub(super) index: usize,
22}
23
24impl Iterator for OwnedProjectionIter {
25 type Item = OwnedProjection;
26
27 fn next(&mut self) -> Option<OwnedProjection> {
28 let projection = self.projections.next()?;
29 let (scalar, alias) = if let Some(memory) = &mut self.memory {
30 let mut item = memory
31 .item_mut(self.index)
32 .take()
33 .expect("projection owner");
34 let scalar = Some(item.scalar_mut().take().expect("scalar owner"));
35 let alias = item
36 .field_mut("alias")
37 .take()
38 .and_then(RewriteMemory::into_reservation);
39 (scalar, alias)
40 } else {
41 (None, None)
42 };
43 self.index += 1;
44 Some(OwnedProjection {
45 expression: OwnedScalarExpr::from_rewrite_parts(projection.expr, scalar),
46 alias: projection.alias.map(|value| match alias {
47 Some(memory) => Produced::from(Budgeted::new(value, memory)),
48 None => ProductionControl::uncontrolled()
49 .finish(value, None)
50 .expect("ordinary projection alias"),
51 }),
52 })
53 }
54
55 fn size_hint(&self) -> (usize, Option<usize>) {
56 self.projections.size_hint()
57 }
58}
59
60impl ExactSizeIterator for OwnedProjectionIter {}
61
62pub(super) struct ProjectionVector {
63 pub(super) projections: Vec<ProjectionPlan>,
64 pub(super) memory: Option<RewriteMemory>,
65}
66
67impl ProjectionVector {
68 pub(super) fn new(
69 projections: impl ExactSizeIterator<Item = OwnedProjection>,
70 control: &ProductionControl<'_>,
71 ) -> Result<Self, SQLError> {
72 control.check()?;
73 let Some(mut copying) = Copying::for_rewrite(*control) else {
74 let projections = projections
75 .map(|projection| {
76 Ok(ProjectionPlan {
77 expr: projection
78 .expression
79 .finish(control)?
80 .into_uncontrolled()
81 .expect("ordinary projection"),
82 alias: projection
83 .alias
84 .map(|alias| alias.into_uncontrolled().expect("ordinary alias")),
85 })
86 })
87 .collect::<Result<_, SQLError>>()?;
88 return Ok(Self {
89 projections,
90 memory: None,
91 });
92 };
93 let projections = copying.map(projections, Copying::take_projection)?;
94 Ok(Self {
95 projections,
96 memory: Some(copying.into_rewrite_memory()),
97 })
98 }
99}
100
101impl MutableQueryPlan {
102 pub fn map_projection_inputs<I: ExactSizeIterator<Item = OwnedProjection>>(
104 self,
105 control: &ProductionControl<'_>,
106 map: impl FnOnce(OwnedProjectionIter, &mut super::ScalarQueryArena) -> Result<I, SQLError>,
107 ) -> Result<Self, SQLError> {
108 let (mut query, projections) = self.take_projections(control)?;
109 let mut arena = query.take_arena();
110 let projections = map(projections, &mut arena)?;
111 query = query.replace_projections(projections, control)?;
112 query.restore_arena(arena);
113 Ok(query)
114 }
115
116 pub fn take_projections(
118 mut self,
119 control: &ProductionControl<'_>,
120 ) -> Result<(Self, OwnedProjectionIter), SQLError> {
121 self.assert_owner(control);
122 control.check()?;
123 let RelationalPlan::QueryBlock(block) = &mut self.query.root else {
124 return Err(SQLError::Internal(
125 "projection extraction requires a query block".into(),
126 ));
127 };
128 let projections = std::mem::take(&mut block.projections).into_iter();
129 let memory = self
130 .memory
131 .as_mut()
132 .and_then(|memory| projection_memory(memory).take());
133 Ok((
134 self,
135 OwnedProjectionIter {
136 projections,
137 memory,
138 index: 0,
139 },
140 ))
141 }
142
143 pub fn replace_projections(
145 mut self,
146 projections: impl ExactSizeIterator<Item = OwnedProjection>,
147 control: &ProductionControl<'_>,
148 ) -> Result<Self, SQLError> {
149 self.assert_owner(control);
150 control.check()?;
151 let replacement = ProjectionVector::new(projections, control)?;
152 let RelationalPlan::QueryBlock(block) = &mut self.query.root else {
153 return Err(SQLError::Internal(
154 "projection replacement requires a query block".into(),
155 ));
156 };
157 block.projections = replacement.projections;
158 if let Some(memory) = &mut self.memory {
159 *projection_memory(memory) = replacement.memory;
160 }
161 control.check()?;
162 Ok(self)
163 }
164
165 pub fn set_analyzed_output(
167 mut self,
168 schema: Option<RowSchema>,
169 control: &ProductionControl<'_>,
170 ) -> Result<Self, SQLError> {
171 self.assert_owner(control);
172 control.check()?;
173 self.query.analyzed_output = schema;
174 Ok(self)
175 }
176}
177
178fn projection_memory(memory: &mut RewriteMemory) -> &mut Option<RewriteMemory> {
179 memory
180 .field_mut("root")
181 .as_mut()
182 .expect("query root")
183 .field_mut("block")
184 .as_mut()
185 .expect("query block")
186 .field_mut("projections")
187}