Skip to main content

uqa_sql/plan/copy/
retained.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Selected query inputs for retained physical construction keep their constructor leases.
8
9use super::{Copying, Result};
10use crate::plan::{AccessPathPlan, ComputePlan, QueryBlockPlan, QueryPlan, RelationalPlan};
11use uqa_core::memory::{OwnedSet, Produced, ProductionControl};
12
13impl QueryPlan {
14    /// Copy the primary query after its WITH definitions have been installed in the lexical scope. Excluded declarations are never allocated; all primary metadata and nested query scopes retain their meaning.
15    pub fn copy_primary_with_control(
16        &self,
17        control: &ProductionControl<'_>,
18    ) -> Result<Produced<Self>> {
19        control.check()?;
20        let Some(mut copying) = Copying::new(*control) else {
21            return Ok(control.finish(
22                Self {
23                    analyzed_output: self.analyzed_output.clone(),
24                    analyzed_source: self.analyzed_source.clone(),
25                    analyzed_grouping: self.analyzed_grouping.clone(),
26                    relations_bound: self.relations_bound,
27                    relation_privilege_subject: self.relation_privilege_subject.clone(),
28                    ctes: Vec::new(),
29                    root: self.root.clone(),
30                },
31                None,
32            )?);
33        };
34        let query =
35            copying.query_fields_with_ctes(self, &[], |copying| copying.relational(&self.root))?;
36        copying.finish(query)
37    }
38
39    /// Produce the ordering/slicing block of a set operation without copying either operand. Scalar-query slots remain in the same order as their original expressions.
40    pub fn set_ordering_with_control(
41        &self,
42        control: &ProductionControl<'_>,
43    ) -> Result<Produced<QueryBlockPlan>> {
44        control.check()?;
45        let RelationalPlan::SetOp {
46            order_by,
47            limit,
48            offset,
49            with_ties,
50            subqueries,
51            ..
52        } = &self.root
53        else {
54            return Err(crate::SQLError::Internal(
55                "set ordering requires a set-operation root".into(),
56            ));
57        };
58        let mut copying = Copying::new(*control);
59        let order_by = match copying.as_mut() {
60            Some(copying) => copying.map(order_by.iter(), Copying::order)?,
61            None => order_by.clone(),
62        };
63        let limit = match copying.as_mut() {
64            Some(copying) => copying.optional_scalar(limit.as_deref())?,
65            None => limit.as_deref().cloned(),
66        };
67        let offset = match copying.as_mut() {
68            Some(copying) => copying.optional_scalar(offset.as_deref())?,
69            None => offset.as_deref().cloned(),
70        };
71        let subqueries = match copying.as_mut() {
72            Some(copying) => copying.map(subqueries.iter(), Copying::query)?,
73            None => subqueries.clone(),
74        };
75        let block = QueryBlockPlan {
76            deferred_outer_rewrites: Vec::new(),
77            privilege_columns: OwnedSet::new(),
78            projections: Vec::new(),
79            from: None,
80            r#where: None,
81            compute: ComputePlan::Project,
82            group_by: Vec::new(),
83            grouping_sets: Vec::new(),
84            group_distinct: false,
85            having: None,
86            order_by,
87            limit,
88            with_ties: *with_ties,
89            offset,
90            distinct: false,
91            distinct_on: Vec::new(),
92            subqueries,
93            access: AccessPathPlan::Row,
94            locking: Vec::new(),
95            windows: Vec::new(),
96        };
97        match copying {
98            Some(copying) => copying.finish(block),
99            None => Ok(control.finish(block, None)?),
100        }
101    }
102}