Skip to main content

uqa_sql/semantics/view_rewrite/
bindings.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Ordinary and native view callers lend the same inherited CTE namespace.
8
9use crate::{
10    binding::{
11        production::{
12            BindingMap, BindingNameSet, ControlledBindingContext, ControlledBindingSnapshot,
13        },
14        snapshot::BindingSnapshot,
15    },
16    SQLError,
17};
18use uqa_core::memory::ProductionControl;
19
20#[derive(Clone, Copy)]
21pub enum ViewRewriteBindings<'a> {
22    Ordinary(&'a BindingSnapshot),
23    Controlled(&'a ControlledBindingSnapshot),
24}
25
26impl ViewRewriteBindings<'_> {
27    pub(super) fn inherit(
28        self,
29        scope: &mut ControlledBindingContext<'_>,
30        control: &ProductionControl<'_>,
31    ) -> Result<(), SQLError> {
32        control.check()?;
33        match self {
34            Self::Ordinary(parent) => {
35                scope.ctes = BindingMap::new();
36                scope.deferred_ctes = BindingMap::new();
37                scope.non_returning_ctes = BindingNameSet::new();
38                for (name, schema) in &parent.ctes {
39                    scope
40                        .ctes
41                        .insert_with_control(name, schema.clone(), control)?;
42                }
43                for (name, plan) in &parent.deferred_ctes {
44                    scope
45                        .deferred_ctes
46                        .insert_with_control(name, plan.clone(), control)?;
47                }
48                for name in &parent.non_returning_ctes {
49                    scope
50                        .non_returning_ctes
51                        .insert_with_control(name, control)?;
52                }
53            }
54            Self::Controlled(parent) => {
55                scope.ctes = parent
56                    .ctes
57                    .copy_with_control(control, |schema| Ok(schema.clone()))?;
58                scope.deferred_ctes = parent
59                    .deferred_ctes
60                    .copy_with_control(control, |plan| Ok(plan.clone()))?;
61                scope.non_returning_ctes = parent.non_returning_ctes.copy_with_control(control)?;
62            }
63        }
64        Ok(())
65    }
66}