Skip to main content

uqa_sql/plan/mutable/sources/
generated.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Captured input rows and lexical joins keep independently movable source and scalar allocation owners.
8
9use super::MutableSourcePlan;
10use crate::{
11    ast::{InternalRelationId, JoinKind},
12    plan::{copy::Copying, JoinExecutionStrategy, OwnedScalarExpr, SourcePlan},
13    SQLError,
14};
15use uqa_core::memory::ProductionControl;
16
17impl MutableSourcePlan {
18    /// Construct a relation reference from borrowed names without first materializing an ordinary source.
19    pub fn table(
20        name: &str,
21        qualifier: &str,
22        alias: Option<&str>,
23        include_descendants: bool,
24        control: &ProductionControl<'_>,
25    ) -> Result<Self, SQLError> {
26        control.check()?;
27        if let Some(mut copying) = Copying::for_rewrite(*control) {
28            let source = copying.table_source(name, qualifier, alias, include_descendants)?;
29            return Ok(Self {
30                source,
31                memory: Some(copying.into_rewrite_memory()),
32            });
33        }
34        Ok(Self {
35            source: SourcePlan::Table {
36                name: name.into(),
37                executable_query: None,
38                qualifier: qualifier.into(),
39                alias: alias.map(str::to_owned),
40                column_aliases: Vec::new(),
41                bound_columns: None,
42                include_descendants,
43            },
44            memory: None,
45        })
46    }
47
48    pub fn captured_values(
49        values: impl ExactSizeIterator<Item = OwnedScalarExpr>,
50        relation: InternalRelationId,
51        control: &ProductionControl<'_>,
52    ) -> Result<Self, SQLError> {
53        control.check()?;
54        if let Some(mut copying) = Copying::for_rewrite(*control) {
55            let source = copying.captured_values_source(values, relation)?;
56            return Ok(Self {
57                source,
58                memory: Some(copying.into_rewrite_memory()),
59            });
60        }
61        let mut row = Vec::with_capacity(values.len());
62        for value in values {
63            row.push(
64                value
65                    .finish(control)?
66                    .into_uncontrolled()
67                    .expect("ordinary capture value"),
68            );
69        }
70        Ok(Self {
71            source: SourcePlan::Values {
72                lateral: false,
73                internal_column_types: vec![None; row.len()],
74                internal_column_collations: Vec::new(),
75                rows: vec![row],
76                alias: None,
77                column_aliases: Vec::new(),
78                internal_relation: Some(relation),
79            },
80            memory: None,
81        })
82    }
83
84    pub fn cross_join(
85        left: Self,
86        right: Self,
87        lateral: bool,
88        control: &ProductionControl<'_>,
89    ) -> Result<Self, SQLError> {
90        control.check()?;
91        if let Some(mut copying) = Copying::for_rewrite(*control) {
92            let source = copying.cross_join_source(left, right, lateral)?;
93            return Ok(Self {
94                source,
95                memory: Some(copying.into_rewrite_memory()),
96            });
97        }
98        let (left, left_owner) = left.into_rewrite_parts(control)?;
99        let (right, right_owner) = right.into_rewrite_parts(control)?;
100        assert!(
101            left_owner.is_none() && right_owner.is_none(),
102            "ordinary source operands"
103        );
104        Ok(Self {
105            source: SourcePlan::Join {
106                comma: false,
107                left: Box::new(left),
108                right: Box::new(right),
109                kind: JoinKind::Cross,
110                on: None,
111                using: None,
112                natural: false,
113                alias: None,
114                column_aliases: Vec::new(),
115                lateral,
116                strategy: JoinExecutionStrategy::Auto,
117            },
118            memory: None,
119        })
120    }
121}