uqa_sql/plan/mutable/
queries.rs1use super::{memory::RewriteMemory, MutableQueryPlan, QueryPlan, RelationalPlan};
10use crate::{plan::CtePlanBody, SQLError};
11use uqa_core::memory::ProductionControl;
12
13#[derive(Clone, Copy, Debug)]
15pub enum QueryChildSlot {
16 Cte(usize),
17 Left,
18 Right,
19 Subquery(usize),
20}
21
22impl MutableQueryPlan {
23 pub fn map_query_child(
25 mut self,
26 slot: QueryChildSlot,
27 control: &ProductionControl<'_>,
28 map: impl FnOnce(Self) -> Result<Self, SQLError>,
29 ) -> Result<Self, SQLError> {
30 self.assert_owner(control);
31 control.check()?;
32 let input = Self {
33 query: std::mem::replace(slot.query_mut(&mut self.query), empty_query()),
34 memory: self
35 .memory
36 .as_mut()
37 .and_then(|memory| slot.memory_mut(memory).take()),
38 };
39 let output = map(input)?;
40 output.assert_owner(control);
41 control.check()?;
42 *slot.query_mut(&mut self.query) = output.query;
43 if let Some(memory) = &mut self.memory {
44 *slot.memory_mut(memory) = output.memory;
45 }
46 Ok(self)
47 }
48}
49
50impl QueryChildSlot {
51 pub(super) fn query_mut(self, query: &mut QueryPlan) -> &mut QueryPlan {
52 match self {
53 Self::Cte(index) => match &mut query.ctes[index].body {
54 CtePlanBody::Query(query) => query,
55 CtePlanBody::Command(_) => panic!("query child selector requires a query CTE"),
56 },
57 Self::Left | Self::Right => match &mut query.root {
58 RelationalPlan::SetOp { left, right, .. } => {
59 if matches!(self, Self::Left) {
60 left
61 } else {
62 right
63 }
64 }
65 _ => panic!("set child selector requires a set-operation root"),
66 },
67 Self::Subquery(index) => match &mut query.root {
68 RelationalPlan::QueryBlock(block) => &mut block.subqueries[index],
69 RelationalPlan::SetOp { subqueries, .. }
70 | RelationalPlan::Values { subqueries, .. } => &mut subqueries[index],
71 },
72 }
73 }
74
75 pub(super) fn memory_mut(self, memory: &mut RewriteMemory) -> &mut Option<RewriteMemory> {
76 let input = match self {
77 Self::Cte(index) => memory
78 .field_mut("ctes")
79 .as_mut()
80 .expect("CTE vector")
81 .item_mut(index)
82 .as_mut()
83 .expect("CTE item")
84 .field_mut("body")
85 .as_mut()
86 .expect("CTE body"),
87 Self::Left | Self::Right => memory
88 .field_mut("root")
89 .as_mut()
90 .expect("query root")
91 .field_mut(if matches!(self, Self::Left) {
92 "left"
93 } else {
94 "right"
95 })
96 .as_mut()
97 .expect("set input"),
98 Self::Subquery(index) => {
99 let root = memory.field_mut("root").as_mut().expect("query root");
100 let block = match root {
101 RewriteMemory::Fields { fields, .. } => {
102 fields.iter().any(|field| field.name == "block")
103 }
104 RewriteMemory::Scalar(_) | RewriteMemory::Scalars { .. } => {
105 unreachable!("query root owner")
106 }
107 };
108 let root = if block {
109 root.field_mut("block").as_mut().expect("query block")
110 } else {
111 root
112 };
113 root.field_mut("subqueries")
114 .as_mut()
115 .expect("query arena")
116 .item_mut(index)
117 .as_mut()
118 .expect("query arena item")
119 }
120 };
121 input.field_mut("query")
122 }
123}
124
125pub(super) fn empty_query() -> QueryPlan {
126 QueryPlan {
127 analyzed_output: None,
128 analyzed_source: None,
129 analyzed_grouping: None,
130 relations_bound: false,
131 relation_privilege_subject: None,
132 ctes: Vec::new(),
133 root: RelationalPlan::Values {
134 rows: Vec::new(),
135 subqueries: Vec::new(),
136 },
137 }
138}