uqa_sql/plan/mutable/sources/
references.rs1use super::MutableSourcePlan;
10use crate::plan::mutable::memory::RewriteMemory;
11use crate::{
12 ast::OperatorJoinRelations,
13 plan::{MutableQueryPlan, SourcePlan},
14 SQLError,
15};
16use uqa_core::memory::{BudgetedVec, Produced, ProductionControl};
17
18impl MutableSourcePlan {
19 pub fn replace_alias(
21 mut self,
22 alias: Produced<String>,
23 control: &ProductionControl<'_>,
24 ) -> Result<Self, SQLError> {
25 self.assert_owner(control);
26 control.check()?;
27 let current = match &mut self.source {
28 SourcePlan::Table { alias, .. }
29 | SourcePlan::Join { alias, .. }
30 | SourcePlan::Values { alias, .. }
31 | SourcePlan::Subquery { alias, .. }
32 | SourcePlan::Function { alias, .. }
33 | SourcePlan::FunctionGroup { alias, .. } => alias,
34 SourcePlan::RetainedRows(_) => {
35 return Err(SQLError::Internal(
36 "retained rows have no source alias".into(),
37 ));
38 }
39 };
40 let (alias, lease) = alias.into_parts();
41 let (alias, lease) = control.finish(alias, lease)?.into_parts();
42 let previous_capacity = current.as_ref().map_or(0, String::capacity);
43 *current = Some(alias);
44 if let Some(memory) = &mut self.memory {
45 let RewriteMemory::Fields { direct, .. } = memory else {
46 unreachable!("source constructor")
47 };
48 drop(direct.split(previous_capacity));
50 direct.absorb(lease.expect("controlled source alias"));
51 }
52 control.check()?;
53 Ok(self)
54 }
55
56 pub fn replace_table_reference(
57 mut self,
58 name: Option<Produced<String>>,
59 qualifier: Option<Produced<String>>,
60 control: &ProductionControl<'_>,
61 ) -> Result<Self, SQLError> {
62 self.assert_owner(control);
63 control.check()?;
64 let SourcePlan::Table {
65 name: current,
66 qualifier: current_qualifier,
67 ..
68 } = &mut self.source
69 else {
70 return Err(SQLError::Internal(
71 "relation-name replacement requires a table source".into(),
72 ));
73 };
74 if let Some(name) = name {
75 install(current, self.memory.as_mut(), "name", name, control)?;
76 }
77 if let Some(qualifier) = qualifier {
78 install(
79 current_qualifier,
80 self.memory.as_mut(),
81 "qualifier",
82 qualifier,
83 control,
84 )?;
85 }
86 Ok(self)
87 }
88
89 pub fn replace_function_references(
90 mut self,
91 index: usize,
92 output_name: Option<Produced<String>>,
93 relations: Option<Produced<OperatorJoinRelations>>,
94 control: &ProductionControl<'_>,
95 ) -> Result<Self, SQLError> {
96 self.assert_owner(control);
97 control.check()?;
98 let (name, current_relations, mut memory) = match &mut self.source {
99 SourcePlan::Function {
100 output_name,
101 relations,
102 ..
103 } if index == 0 => (output_name, relations, self.memory.as_mut()),
104 SourcePlan::FunctionGroup { functions, .. } => {
105 let function = &mut functions[index];
106 let memory = self.memory.as_mut().map(|memory| {
107 memory
108 .field_mut("functions")
109 .as_mut()
110 .expect("function group owner")
111 .item_mut(index)
112 .as_mut()
113 .expect("function member owner")
114 });
115 (&mut function.output_name, &mut function.relations, memory)
116 }
117 _ => {
118 return Err(SQLError::Internal(
119 "function-name replacement requires a function source".into(),
120 ))
121 }
122 };
123 if let Some(output_name) = output_name {
124 install(
125 name,
126 memory.as_deref_mut(),
127 "output_name",
128 output_name,
129 control,
130 )?;
131 }
132 if let Some(relations) = relations {
133 let (relations, lease) = relations.into_parts();
134 install(
135 current_relations,
136 memory,
137 "relations",
138 control.finish(Some(relations), lease)?,
139 control,
140 )?;
141 }
142 Ok(self)
143 }
144}
145
146fn install<T>(
147 target: &mut T,
148 memory: Option<&mut RewriteMemory>,
149 field: &'static str,
150 value: Produced<T>,
151 control: &ProductionControl<'_>,
152) -> Result<(), SQLError> {
153 let (value, lease) = value.into_parts();
154 let (value, lease) = control.finish(value, lease)?.into_parts();
155 *target = value;
156 if let Some(memory) = memory {
157 let direct = lease.expect("controlled source metadata");
158 *memory.field_mut(field) = Some(RewriteMemory::Fields {
159 fields: BudgetedVec::new(direct.budget()),
160 direct,
161 });
162 }
163 Ok(())
164}
165
166impl MutableQueryPlan {
167 pub fn set_relations_bound(
168 mut self,
169 bound: bool,
170 control: &ProductionControl<'_>,
171 ) -> Result<Self, SQLError> {
172 self.assert_owner(control);
173 control.check()?;
174 self.query.relations_bound = bound;
175 Ok(self)
176 }
177}