uqa_sql/plan/mutable/sources/
joins.rs1use super::super::super::copy::Copying;
10use super::{
11 empty_source, MutableSourcePlan, OwnedScalarExpr, SQLError, SourceChildSlot, SourcePlan,
12 SourceScalarSlot,
13};
14use crate::{ast::JoinKind, plan::JoinExecutionStrategy};
15use uqa_core::memory::ProductionControl;
16
17#[derive(Debug)]
18pub struct MutableJoinShell {
19 source: MutableSourcePlan,
20 predicate_slot: bool,
21}
22
23#[derive(Debug)]
24pub struct MutableJoinParts {
25 pub shell: MutableJoinShell,
26 pub left: MutableSourcePlan,
27 pub right: MutableSourcePlan,
28 pub on: Option<OwnedScalarExpr>,
29}
30
31impl MutableSourcePlan {
32 pub fn split_join(
34 mut self,
35 control: &ProductionControl<'_>,
36 ) -> Result<MutableJoinParts, SQLError> {
37 self.assert_owner(control);
38 control.check()?;
39 let SourcePlan::Join {
40 left,
41 right,
42 on,
43 kind: JoinKind::Inner | JoinKind::Cross,
44 using: None,
45 natural: false,
46 alias: None,
47 column_aliases,
48 lateral: false,
49 ..
50 } = &mut self.source
51 else {
52 return Err(SQLError::Internal(
53 "join decomposition requires an undecorated inner join".into(),
54 ));
55 };
56 if !column_aliases.is_empty() {
57 return Err(SQLError::Internal(
58 "join decomposition cannot discard column aliases".into(),
59 ));
60 }
61 let left = MutableSourcePlan {
62 source: std::mem::replace(left.as_mut(), empty_source()),
63 memory: self
64 .memory
65 .as_mut()
66 .and_then(|memory| SourceChildSlot::Left.memory_mut(memory).take()),
67 };
68 let right = MutableSourcePlan {
69 source: std::mem::replace(right.as_mut(), empty_source()),
70 memory: self
71 .memory
72 .as_mut()
73 .and_then(|memory| SourceChildSlot::Right.memory_mut(memory).take()),
74 };
75 let predicate_slot = on.is_some();
76 let on = on.take().map(|expression| {
77 let memory = self
78 .memory
79 .as_mut()
80 .and_then(|memory| SourceScalarSlot::JoinOn.memory_mut(memory).take());
81 OwnedScalarExpr::from_rewrite_parts(expression, memory)
82 });
83 Ok(MutableJoinParts {
84 shell: MutableJoinShell {
85 source: self,
86 predicate_slot,
87 },
88 left,
89 right,
90 on,
91 })
92 }
93}
94
95impl MutableJoinShell {
96 pub fn assemble(
98 mut self,
99 left: MutableSourcePlan,
100 right: MutableSourcePlan,
101 on: Option<OwnedScalarExpr>,
102 strategy: JoinExecutionStrategy,
103 control: &ProductionControl<'_>,
104 ) -> Result<MutableSourcePlan, SQLError> {
105 self.source.assert_owner(control);
106 left.assert_owner(control);
107 right.assert_owner(control);
108 control.check()?;
109 let on = on
110 .map(|on| on.rewrite(control, Ok::<_, SQLError>))
111 .transpose()?;
112 let mut new_predicate_owner = None;
113 let (on, scalar_owner) = match on {
114 Some(on) if !self.predicate_slot && control.budget().is_some() => {
115 let mut copying =
116 Copying::for_rewrite(*control).expect("controlled join predicate");
117 let expression = copying.take_scalar(on)?;
118 new_predicate_owner = Some(copying.into_rewrite_memory());
119 (Some(expression), None)
120 }
121 Some(on) => {
122 let (expression, memory) = on.into_rewrite_parts();
123 (Some(expression), memory)
124 }
125 None => (None, None),
126 };
127 let SourcePlan::Join {
128 comma,
129 kind,
130 left: left_box,
131 right: right_box,
132 on: predicate,
133 strategy: selected,
134 ..
135 } = &mut self.source.source
136 else {
137 unreachable!("join shell")
138 };
139 **left_box = left.source;
140 **right_box = right.source;
141 *predicate = on;
142 *comma = false;
143 *kind = JoinKind::Inner;
144 *selected = strategy;
145 if let Some(memory) = &mut self.source.memory {
146 *SourceChildSlot::Left.memory_mut(memory) = left.memory;
147 *SourceChildSlot::Right.memory_mut(memory) = right.memory;
148 if self.predicate_slot {
149 *SourceScalarSlot::JoinOn.memory_mut(memory) = scalar_owner;
150 } else if let Some(owner) = new_predicate_owner {
151 *memory.field_mut("on") = Some(owner);
152 }
153 }
154 control.check()?;
155 Ok(self.source)
156 }
157}