uqa_sql/plan/mutable/
arenas.rs1use super::{memory::RewriteMemory, MutableCommandPlan, MutableQueryPlan};
10use crate::SQLError;
11use uqa_core::memory::ProductionControl;
12
13mod nodes;
14mod owner;
15mod transfer;
16pub use nodes::ScalarArenaTransform;
17pub use owner::ScalarQueryArena;
18
19impl MutableQueryPlan {
20 pub fn prune_subqueries(mut self, control: &ProductionControl<'_>) -> Result<Self, SQLError> {
22 self.assert_owner(control);
23 control.check()?;
24 let mut fields = self.memory.as_mut().map(|memory| {
25 let root = memory.field_mut("root").as_mut().expect("query root");
26 let block = match root {
27 RewriteMemory::Fields { fields, .. } => {
28 fields.iter().any(|field| field.name == "block")
29 }
30 RewriteMemory::Scalar(_) | RewriteMemory::Scalars { .. } => {
31 unreachable!("query root constructor")
32 }
33 };
34 let root = if block {
35 root.field_mut("block").as_mut().expect("query block")
36 } else {
37 root
38 };
39 match root
40 .field_mut("subqueries")
41 .as_mut()
42 .expect("scalar query arena")
43 {
44 RewriteMemory::Fields { fields, .. } => fields,
45 RewriteMemory::Scalar(_) | RewriteMemory::Scalars { .. } => {
46 unreachable!("query arena constructor")
47 }
48 }
49 });
50 super::super::subqueries::prune_query_with_retention(
51 &mut self.query,
52 *control,
53 |arena, keep| {
54 let mut retained = 0;
55 for index in 0..arena.len() {
56 if keep(index) {
57 arena.swap(retained, index);
58 if let Some(fields) = &mut fields {
59 fields.swap(retained, index);
60 }
61 retained += 1;
62 }
63 }
64 arena.truncate(retained);
65 if let Some(fields) = fields {
66 fields.truncate(retained);
67 }
68 },
69 )?;
70 Ok(self)
71 }
72}
73
74impl MutableCommandPlan {
75 pub fn prune_subqueries(mut self, control: &ProductionControl<'_>) -> Result<Self, SQLError> {
77 self.assert_owner(control);
78 control.check()?;
79 if self.command.scalar_subqueries().is_empty() {
80 return Ok(self);
81 }
82 let mut fields = self.memory.as_mut().map(|memory| {
83 let arena = memory
84 .field_mut("value0")
85 .as_mut()
86 .expect("mutation body")
87 .field_mut("subqueries")
88 .as_mut()
89 .expect("mutation arena");
90 match arena {
91 RewriteMemory::Fields { fields, .. } => fields,
92 RewriteMemory::Scalar(_) | RewriteMemory::Scalars { .. } => {
93 unreachable!("query arena constructor")
94 }
95 }
96 });
97 super::super::subqueries::prune_command_with_retention(
98 &mut self.command,
99 *control,
100 |arena, keep| {
101 let mut retained = 0;
102 for index in 0..arena.len() {
103 if keep(index) {
104 arena.swap(retained, index);
105 if let Some(fields) = &mut fields {
106 fields.swap(retained, index);
107 }
108 retained += 1;
109 }
110 }
111 arena.truncate(retained);
112 if let Some(fields) = fields {
113 fields.truncate(retained);
114 }
115 },
116 )?;
117 Ok(self)
118 }
119}