Skip to main content

uqa_sql/plan/copy/
attached_queries.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Enclosing sources and set operations transfer admitted child roots without copying discarded bodies.
8
9use super::{Copying, Result};
10use crate::plan::{OwnedQueryPlan, QueryPlan, RelationalPlan, SourcePlan};
11use crate::SQLError;
12use uqa_core::memory::{Produced, ProductionControl};
13
14impl SourcePlan {
15    /// Copy a subquery source's declaration while transferring the selected executable body and its root allocation.
16    pub fn copy_with_query(
17        &self,
18        query: OwnedQueryPlan,
19        control: &ProductionControl<'_>,
20    ) -> Result<Produced<Self>> {
21        control.check()?;
22        let Self::Subquery {
23            captured_output,
24            lateral,
25            alias,
26            column_aliases,
27            ..
28        } = self
29        else {
30            return Err(SQLError::Internal(
31                "query attachment requires a subquery source".into(),
32            ));
33        };
34        let query = query.into_produced_box(control)?;
35        let Some(mut copying) = Copying::new(*control) else {
36            return Ok(control.finish(
37                Self::Subquery {
38                    captured_output: *captured_output,
39                    lateral: *lateral,
40                    body: query.into_uncontrolled().expect("ordinary source body"),
41                    alias: alias.clone(),
42                    column_aliases: column_aliases.clone(),
43                },
44                None,
45            )?);
46        };
47        let source = Self::Subquery {
48            captured_output: *captured_output,
49            lateral: *lateral,
50            body: copying.take(query),
51            alias: copying.optional_text(alias.as_deref())?,
52            column_aliases: copying.texts(column_aliases)?,
53        };
54        copying.finish(source)
55    }
56}
57
58impl QueryPlan {
59    /// Retain a set operation's declarations and clauses while transferring both independently optimized query inputs.
60    pub fn copy_with_set_inputs(
61        &self,
62        left: OwnedQueryPlan,
63        right: OwnedQueryPlan,
64        control: &ProductionControl<'_>,
65    ) -> Result<OwnedQueryPlan> {
66        control.check()?;
67        let RelationalPlan::SetOp {
68            kind,
69            all,
70            order_by,
71            limit,
72            with_ties,
73            offset,
74            subqueries,
75            ..
76        } = &self.root
77        else {
78            return Err(SQLError::Internal(
79                "set input attachment requires a set operation".into(),
80            ));
81        };
82        let left = left.into_produced_box(control)?;
83        let right = right.into_produced_box(control)?;
84        let Some(mut copying) = Copying::new(*control) else {
85            let query = Self {
86                analyzed_output: self.analyzed_output.clone(),
87                analyzed_source: self.analyzed_source.clone(),
88                analyzed_grouping: self.analyzed_grouping.clone(),
89                relations_bound: self.relations_bound,
90                relation_privilege_subject: self.relation_privilege_subject.clone(),
91                ctes: self.ctes.clone(),
92                root: RelationalPlan::SetOp {
93                    kind: *kind,
94                    all: *all,
95                    left: left.into_uncontrolled().expect("ordinary left input"),
96                    right: right.into_uncontrolled().expect("ordinary right input"),
97                    order_by: order_by.clone(),
98                    limit: limit.clone(),
99                    with_ties: *with_ties,
100                    offset: offset.clone(),
101                    subqueries: subqueries.clone(),
102                },
103            };
104            return OwnedQueryPlan::from_produced(control.finish(query, None)?, control);
105        };
106        let query = copying.query_fields_using(self, |copying| {
107            copying.set_operation_using(
108                &self.root,
109                |copying| Ok(copying.take(left)),
110                |copying| Ok(copying.take(right)),
111            )
112        })?;
113        OwnedQueryPlan::from_produced(copying.finish(query)?, control)
114    }
115}