Skip to main content

uqa_execution/query/scope/production/
copying.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Independent lexical maps retain private node/name leases while immutable statement inputs stay shared.
8
9use super::{Arc, BindingMap, BindingNameSet, CteRowsMap, CteScope, ProductionControl, SQLError};
10
11impl<S: Clone> CteScope<S> {
12    /// Construct a complete independent lexical destination before exposing it. Plan, catalog, command and immutable row-lock metadata retain their existing owners; producer references remain weak, as in the original reader scopes.
13    pub fn copy_with_control(&self, control: &ProductionControl<'_>) -> Result<Self, SQLError> {
14        let mut output = self.copy_context_with_control(control)?;
15        output.rows = self.rows.copy_with_control(control)?;
16        output.deferred_ctes = self
17            .deferred_ctes
18            .copy_with_control(control, |plan| Ok(plan.clone()))?;
19        output.definition_scopes = self
20            .definition_scopes
21            .copy_with_control(control, |definition| Ok(Arc::clone(definition)))?;
22        output.folded_ctes = self.folded_ctes.copy_with_control(control)?;
23        output.non_returning_ctes = self.non_returning_ctes.copy_with_control(control)?;
24        output.visible_cte_names = self.visible_cte_names.copy_with_control(control)?;
25        output.recursive_control_widths = self
26            .recursive_control_widths
27            .copy_with_control(control, |width| Ok(*width))?;
28        Ok(output)
29    }
30
31    /// Copy only the invoking context so a definition can install its selected lexical inputs directly instead of copying and then discarding the current bindings.
32    pub(in crate::query::scope) fn copy_context_with_control(
33        &self,
34        control: &ProductionControl<'_>,
35    ) -> Result<Self, SQLError> {
36        control.check()?;
37        Ok(Self {
38            producers: self.producers.clone(),
39            rows: CteRowsMap::new(),
40            deferred_ctes: BindingMap::new(),
41            definition_scopes: BindingMap::new(),
42            folded_ctes: BindingNameSet::new(),
43            non_returning_ctes: BindingNameSet::new(),
44            scalar_subqueries: self.scalar_subqueries.clone(),
45            source_outer_rewrites: self.source_outer_rewrites.clone(),
46            lock_identities: self.lock_identities,
47            row_lock: self.row_lock.clone(),
48            visible_cte_names: BindingNameSet::new(),
49            recursive_control_widths: BindingMap::new(),
50            scalar_subquery_arena: self.scalar_subquery_arena,
51            read_command_overlay: self.read_command_overlay,
52            stream_command_progress: self.stream_command_progress,
53            scan_backwards: self.scan_backwards,
54            next_scalar_subquery_arena: Arc::clone(&self.next_scalar_subquery_arena),
55            scalar_subquery_cache: Arc::clone(&self.scalar_subquery_cache),
56            catalog: self.catalog.clone(),
57            catalog_revision: self.catalog_revision,
58            catalog_resolution: self.catalog_resolution.clone(),
59            privilege_subject: self.privilege_subject.clone(),
60            command_cte_snapshot: self.command_cte_snapshot.clone(),
61            statement_commands: self.statement_commands.clone(),
62        })
63    }
64}