uqa_sql/plan/mutable/sources/
functions.rs1use super::super::super::copy::Copying;
10use super::{empty_source, MutableQueryPlan, MutableSourcePlan, SQLError, SourcePlan};
11use uqa_core::memory::ProductionControl;
12
13impl SourcePlan {
14 pub fn copy_function_group_member_for_rewrite(
15 &self,
16 index: usize,
17 control: &ProductionControl<'_>,
18 ) -> Result<MutableSourcePlan, SQLError> {
19 control.check()?;
20 let Self::FunctionGroup {
21 functions,
22 alias,
23 ordinality: false,
24 ..
25 } = self
26 else {
27 return Err(SQLError::Internal(
28 "member expansion requires a function group without ordinality".into(),
29 ));
30 };
31 let Some(function) = functions.get(index) else {
32 return Err(SQLError::Internal(
33 "function member is outside its source".into(),
34 ));
35 };
36 let output = if let Some(mut copying) = Copying::for_rewrite(*control) {
37 let source = copying.function_group_member(function, alias.as_deref())?;
38 MutableSourcePlan {
39 source,
40 memory: Some(copying.into_rewrite_memory()),
41 }
42 } else {
43 let function = function.clone();
44 MutableSourcePlan {
45 source: SourcePlan::Function {
46 name: function.name,
47 binding: function.binding,
48 output_name: function.output_name,
49 relations: function.relations,
50 args: function.args,
51 alias: alias.clone(),
52 column_aliases: function.column_aliases,
53 column_types: function.column_types,
54 ordinality: false,
55 },
56 memory: None,
57 }
58 };
59 control.check()?;
60 Ok(output)
61 }
62}
63
64impl MutableSourcePlan {
65 pub fn into_query(
67 mut self,
68 control: &ProductionControl<'_>,
69 ) -> Result<MutableQueryPlan, SQLError> {
70 self.assert_owner(control);
71 control.check()?;
72 let field = match &self.source {
73 SourcePlan::Subquery { .. } => "body",
74 SourcePlan::Table {
75 executable_query: Some(_),
76 ..
77 } => "executable_query",
78 _ => {
79 return Err(SQLError::Internal(
80 "source does not contain an executable query".into(),
81 ));
82 }
83 };
84 let memory = self.memory.as_mut().and_then(|memory| {
85 memory
86 .field_mut(field)
87 .as_mut()
88 .expect("query box")
89 .field_mut("query")
90 .take()
91 });
92 let source = std::mem::replace(&mut self.source, empty_source());
93 let query = match source {
94 SourcePlan::Subquery { body, .. }
95 | SourcePlan::Table {
96 executable_query: Some(body),
97 ..
98 } => *body,
99 _ => unreachable!("selected query source"),
100 };
101 let output = MutableQueryPlan { query, memory };
102 drop(self);
103 control.check()?;
104 Ok(output)
105 }
106}