uqa_sql/plan/mutable/
grouping.rs1use super::{memory::RewriteMemory, MutableQueryPlan, RelationalPlan};
10use crate::SQLError;
11use uqa_core::memory::ProductionControl;
12
13impl MutableQueryPlan {
14 pub fn apply_grouping(mut self, control: &ProductionControl<'_>) -> Result<Self, SQLError> {
15 self.assert_owner(control);
16 control.check()?;
17 let Some(grouping) = self.query.analyzed_grouping.take() else {
18 return Ok(self);
19 };
20 let RelationalPlan::QueryBlock(block) = &mut self.query.root else {
21 return Err(SQLError::Internal(
22 "analyzed grouping belongs to a query block".into(),
23 ));
24 };
25 let mut fields = self.memory.as_mut().map(|memory| {
26 let block = memory
27 .field_mut("root")
28 .as_mut()
29 .expect("query root")
30 .field_mut("block")
31 .as_mut()
32 .expect("query block");
33 match block
34 .field_mut("grouping_sets")
35 .as_mut()
36 .expect("grouping sets")
37 {
38 RewriteMemory::Fields { fields, .. } => fields,
39 RewriteMemory::Scalar(_) | RewriteMemory::Scalars { .. } => {
40 unreachable!("grouping-set vector constructor")
41 }
42 }
43 });
44 grouping.apply_with_retention(block, |sets, keep| {
45 let mut retained = 0;
46 for index in 0..sets.len() {
47 if keep(index) {
48 sets.swap(retained, index);
49 if let Some(fields) = &mut fields {
50 fields.swap(retained, index);
51 }
52 retained += 1;
53 }
54 }
55 sets.truncate(retained);
56 if let Some(fields) = fields {
57 fields.truncate(retained);
58 }
59 })?;
60 if let Some(memory) = &mut self.memory {
61 drop(memory.field_mut("analyzed_grouping").take());
63 }
64 Ok(self)
65 }
66
67 pub fn discard_source_analysis(
69 mut self,
70 control: &ProductionControl<'_>,
71 ) -> Result<Self, SQLError> {
72 self.assert_owner(control);
73 control.check()?;
74 self.query.analyzed_source = None;
75 Ok(self)
76 }
77}