uqa_sql/binding/execution/
block.rs1use crate::{
10 plan::{MutableQueryPlan, QueryBlockPlan, RelationalPlan},
11 routines::RoutineResolution,
12 RowSchema, SQLError, SQLParam,
13};
14use std::borrow::Cow;
15use uqa_core::memory::{Produced, ProductionControl};
16
17enum Storage<'a> {
18 Borrowed(&'a QueryBlockPlan),
19 Owned(Box<Produced<MutableQueryPlan>>),
20}
21
22pub struct ExecutionQueryBlock<'a>(Storage<'a>);
23
24impl<'a> ExecutionQueryBlock<'a> {
25 pub fn borrowed(block: &'a QueryBlockPlan) -> Self {
26 Self(Storage::Borrowed(block))
27 }
28
29 pub(super) fn owned(
30 query: MutableQueryPlan,
31 control: &ProductionControl<'_>,
32 ) -> Result<Self, SQLError> {
33 let memory = control.reserve(size_of::<Produced<MutableQueryPlan>>())?;
34 Ok(Self(Storage::Owned(Box::new(
35 control.finish(query, memory)?,
36 ))))
37 }
38
39 fn into_owned(self, control: &ProductionControl<'_>) -> Result<MutableQueryPlan, SQLError> {
40 control.check()?;
41 match self.0 {
42 Storage::Borrowed(block) => block.copy_for_rewrite(control),
43 Storage::Owned(query) => {
44 let (query, memory) = (*query).into_parts();
45 drop(memory);
46 Ok(query)
47 }
48 }
49 }
50
51 pub fn bind_grouping_names(
52 self,
53 routines: &dyn RoutineResolution,
54 schema: &RowSchema,
55 outer: Option<&RowSchema>,
56 params: &[SQLParam],
57 control: &ProductionControl<'_>,
58 ) -> Result<Self, SQLError> {
59 let query = crate::semantics::grouping_sets::bind_grouping_names_owned(
60 routines,
61 self.into_owned(control)?,
62 schema,
63 outer,
64 params,
65 control,
66 )?;
67 Self::owned(query, control)
68 }
69
70 pub fn resolve_outer_rewrites(
71 self,
72 schema: &RowSchema,
73 control: &ProductionControl<'_>,
74 ) -> Result<Self, SQLError> {
75 control.check()?;
76 if !crate::semantics::view_rewrite::has_resolvable_outer_rewrites(&self, schema) {
77 return Ok(self);
78 }
79 let query = crate::semantics::view_rewrite::resolve_outer_rewrites_owned(
80 self.into_owned(control)?,
81 schema,
82 control,
83 )?;
84 Self::owned(query, control)
85 }
86
87 pub(super) fn into_uncontrolled(self) -> Cow<'a, QueryBlockPlan> {
88 match self.0 {
89 Storage::Borrowed(block) => Cow::Borrowed(block),
90 Storage::Owned(query) => {
91 let query = (*query)
92 .into_uncontrolled()
93 .expect("ordinary execution query owner");
94 let query = query
95 .finish(&ProductionControl::uncontrolled())
96 .expect("ordinary execution query")
97 .into_uncontrolled()
98 .expect("ordinary query fields");
99 let RelationalPlan::QueryBlock(block) = query.root else {
100 unreachable!("execution query block")
101 };
102 Cow::Owned(*block)
103 }
104 }
105 }
106}
107
108impl AsRef<QueryBlockPlan> for ExecutionQueryBlock<'_> {
109 fn as_ref(&self) -> &QueryBlockPlan {
110 match &self.0 {
111 Storage::Borrowed(block) => block,
112 Storage::Owned(query) => {
113 let RelationalPlan::QueryBlock(block) = &query.root else {
114 unreachable!("execution query block")
115 };
116 block
117 }
118 }
119 }
120}
121impl std::ops::Deref for ExecutionQueryBlock<'_> {
122 type Target = QueryBlockPlan;
123 fn deref(&self) -> &Self::Target {
124 self.as_ref()
125 }
126}