uqa_sql/plan/mutable/commands/
inputs.rs1use super::{CommandPlan, MutableCommandPlan, RewriteMemory, SQLError};
10use crate::plan::{
11 copy::Copying, MutableSourcePlan, OwnedScalarExpr, ScalarQueryArena, SourcePlan,
12};
13use uqa_core::memory::ProductionControl;
14
15impl MutableCommandPlan {
16 pub fn map_source_input(
18 mut self,
19 control: &ProductionControl<'_>,
20 map: impl FnOnce(Option<MutableSourcePlan>) -> Result<Option<MutableSourcePlan>, SQLError>,
21 ) -> Result<Self, SQLError> {
22 self.assert_owner(control);
23 control.check()?;
24 let input = source(&mut self.command)?.as_mut().map(|source| {
25 let value = std::mem::replace(source.as_mut(), super::super::sources::empty_source());
26 let memory = self
27 .memory
28 .as_mut()
29 .and_then(|memory| super::children::source_memory(memory).take());
30 MutableSourcePlan {
31 source: value,
32 memory,
33 }
34 });
35 let output = map(input)?;
36 control.check()?;
37 if let Some(output) = output {
38 output.assert_owner(control);
39 control.check()?;
40 if let Some(source) = source(&mut self.command)?.as_mut() {
41 **source = output.source;
42 if let Some(memory) = &mut self.memory {
43 *super::children::source_memory(memory) = output.memory;
44 }
45 } else {
46 let (value, memory) = if let Some(mut copying) = Copying::for_rewrite(*control) {
47 let value = copying.boxed_source(output)?;
48 (value, Some(copying.into_rewrite_memory()))
49 } else {
50 let (value, _) = output.into_rewrite_parts(control)?;
51 (Box::new(value), None)
52 };
53 *source(&mut self.command)? = Some(value);
54 if let Some(owner) = &mut self.memory {
55 *body(owner).field_mut("source") = memory;
56 }
57 }
58 } else {
59 *source(&mut self.command)? = None;
60 if let Some(memory) = &mut self.memory {
61 *body(memory).field_mut("source") = None;
62 }
63 }
64 Ok(self)
65 }
66
67 pub fn map_predicate_input(
69 mut self,
70 control: &ProductionControl<'_>,
71 map: impl FnOnce(
72 Option<OwnedScalarExpr>,
73 &mut ScalarQueryArena,
74 ) -> Result<Option<OwnedScalarExpr>, SQLError>,
75 ) -> Result<Self, SQLError> {
76 self.assert_owner(control);
77 control.check()?;
78 let field = if matches!(self.command, CommandPlan::Merge(_)) {
79 "target_predicate"
80 } else {
81 "predicate"
82 };
83 let (predicate, queries) = predicate_and_queries(&mut self.command)?;
84 let predicate = predicate.take().map(|expression| {
85 let memory = self.memory.as_mut().and_then(|memory| {
86 body(memory)
87 .field_mut(field)
88 .as_mut()
89 .expect("predicate constructor")
90 .scalar_mut()
91 .take()
92 });
93 OwnedScalarExpr::from_rewrite_parts(expression, memory)
94 });
95 let mut arena = ScalarQueryArena {
96 queries: std::mem::take(queries),
97 memory: self
98 .memory
99 .as_mut()
100 .and_then(|memory| body(memory).field_mut("subqueries").take()),
101 };
102 let output = map(predicate, &mut arena)?;
103 control.check()?;
104 let (output, memory) = if let Some(mut copying) = Copying::for_rewrite(*control) {
105 let output = output.map(|value| copying.take_scalar(value)).transpose()?;
106 (output, Some(copying.into_rewrite_memory()))
107 } else {
108 let output = output
109 .map(|value| {
110 value
111 .finish(control)
112 .map(|value| value.into_uncontrolled().expect("ordinary predicate"))
113 })
114 .transpose()?;
115 (output, None)
116 };
117 let (predicate, queries) = predicate_and_queries(&mut self.command)?;
118 *predicate = output;
119 *queries = arena.queries;
120 if let Some(owner) = &mut self.memory {
121 let body = body(owner);
122 *body.field_mut(field) = memory;
123 *body.field_mut("subqueries") = arena.memory;
124 }
125 Ok(self)
126 }
127}
128
129fn body(memory: &mut RewriteMemory) -> &mut RewriteMemory {
130 memory.field_mut("value0").as_mut().expect("mutation body")
131}
132
133fn source(command: &mut CommandPlan) -> Result<&mut Option<Box<SourcePlan>>, SQLError> {
134 match command {
135 CommandPlan::Update(plan) => Ok(&mut plan.source),
136 CommandPlan::Delete(plan) => Ok(&mut plan.source),
137 _ => Err(SQLError::Internal(
138 "optional source edit requires UPDATE or DELETE".into(),
139 )),
140 }
141}
142
143fn predicate_and_queries(
144 command: &mut CommandPlan,
145) -> Result<
146 (
147 &mut Option<crate::ScalarExpr>,
148 &mut Vec<crate::plan::QueryPlan>,
149 ),
150 SQLError,
151> {
152 match command {
153 CommandPlan::Update(plan) => Ok((&mut plan.predicate, &mut plan.subqueries)),
154 CommandPlan::Delete(plan) => Ok((&mut plan.predicate, &mut plan.subqueries)),
155 CommandPlan::Merge(plan) => Ok((&mut plan.target_predicate, &mut plan.subqueries)),
156 _ => Err(SQLError::Internal(
157 "qualification edit requires UPDATE, DELETE or MERGE".into(),
158 )),
159 }
160}