uqa_sql/plan/mutable/
vectors.rs1use super::{
10 Copying, MutableQueryPlan, OwnedScalarExpr, RelationalPlan, RewriteMemory, SQLError, ScalarExpr,
11};
12use uqa_core::memory::ProductionControl;
13
14struct ScalarVector {
15 values: Vec<ScalarExpr>,
16 memory: Option<RewriteMemory>,
17}
18
19impl ScalarVector {
20 fn new(
21 children: impl ExactSizeIterator<Item = OwnedScalarExpr>,
22 control: &ProductionControl<'_>,
23 ) -> Result<Self, SQLError> {
24 control.check()?;
25 let Some(mut copying) = Copying::for_rewrite(*control) else {
26 let values = children
27 .map(|child| {
28 Ok(child
29 .finish(control)?
30 .into_uncontrolled()
31 .expect("ordinary scalar vector"))
32 })
33 .collect::<Result<_, SQLError>>()?;
34 return Ok(Self {
35 values,
36 memory: None,
37 });
38 };
39 let values = copying.map(children, Copying::take_scalar)?;
40 let output = Self {
41 values,
42 memory: Some(copying.into_rewrite_memory()),
43 };
44 control.check()?;
45 Ok(output)
46 }
47}
48
49impl MutableQueryPlan {
50 pub fn replace_group_by(
52 mut self,
53 expressions: impl ExactSizeIterator<Item = OwnedScalarExpr>,
54 control: &ProductionControl<'_>,
55 ) -> Result<Self, SQLError> {
56 self.assert_owner(control);
57 control.check()?;
58 let replacement = ScalarVector::new(expressions, control)?;
59 let RelationalPlan::QueryBlock(block) = &mut self.query.root else {
60 return Err(SQLError::Internal(
61 "grouping keys require a query block".into(),
62 ));
63 };
64 block.group_by = replacement.values;
65 if let Some(memory) = &mut self.memory {
66 let block = memory
67 .field_mut("root")
68 .as_mut()
69 .expect("query root")
70 .field_mut("block")
71 .as_mut()
72 .expect("query block");
73 *block.field_mut("group_by") = replacement.memory;
74 }
75 control.check()?;
76 Ok(self)
77 }
78}