uqa_sql/plan/scalar/
copy.rs1use super::{
10 owned::{OwnedScalarExpr, ScalarMemory},
11 resources::{Control, Lowering, Result},
12 source::Source,
13 ScalarExpr,
14};
15use uqa_core::memory::{Produced, ProductionControl};
16
17mod iterative;
18
19impl ScalarExpr {
20 pub fn clone_with_control(&self, control: &ProductionControl<'_>) -> Result<Produced<Self>> {
22 iterative::immutable(self, *control)
23 }
24
25 pub fn copy_for_rewrite(&self, control: &ProductionControl<'_>) -> Result<OwnedScalarExpr> {
27 iterative::mutable(self, *control)
28 }
29}
30
31impl Lowering<'_> {
32 pub(super) fn scalar_type_copy(
33 &mut self,
34 ty: Option<&crate::ast::ColumnType>,
35 ) -> Result<Option<crate::ast::ColumnType>> {
36 ty.map(|ty| {
37 let Some(control) = self.control.as_mut() else {
38 return Ok(ty.clone());
39 };
40 let (ty, memory) = ty.clone_with_control(&control.production)?.into_parts();
41 control.memory.absorb(memory.expect("controlled type copy"));
42 Ok(ty)
43 })
44 .transpose()
45 }
46
47 fn scalar_leaf_copy(&mut self, expression: &ScalarExpr) -> Result<ScalarExpr> {
48 self.check()?;
49 Ok(match expression {
50 ScalarExpr::Star => ScalarExpr::Star,
51 ScalarExpr::QualifiedStar(name) => {
52 ScalarExpr::QualifiedStar(self.copy_reference_name(name)?)
53 }
54 ScalarExpr::Default => ScalarExpr::Default,
55 ScalarExpr::Column(name) => ScalarExpr::Column(self.copy_reference_name(name)?),
56 ScalarExpr::Position(position) => ScalarExpr::Position(*position),
57 ScalarExpr::InternalColumn(column) => ScalarExpr::InternalColumn(*column),
58 ScalarExpr::QualifiedColumn { qualifier, column } => ScalarExpr::QualifiedColumn {
59 qualifier: self.copy_text(qualifier)?,
60 column: self.copy_reference_name(column)?,
61 },
62 ScalarExpr::Literal(value) => ScalarExpr::Literal(self.value(Source::Borrowed(value))?),
63 ScalarExpr::RetainedLiteral(literal) => ScalarExpr::RetainedLiteral(literal.clone()),
64 ScalarExpr::TypedLiteral {
65 value,
66 ty,
67 bound_type,
68 parameter_index,
69 collation_oid,
70 composite_source,
71 } => {
72 let bound_type = self.scalar_type_copy(bound_type.as_ref())?;
73 ScalarExpr::TypedLiteral {
74 composite_source: self
75 .composite_source(composite_source.as_ref().map(Source::Borrowed))?,
76 value: self.value(Source::Borrowed(value))?,
77 ty: self.copy_text(ty)?,
78 bound_type,
79 parameter_index: *parameter_index,
80 collation_oid: *collation_oid,
81 }
82 }
83 ScalarExpr::Param(index) => ScalarExpr::Param(*index),
84 ScalarExpr::ScalarSubquery(query) => ScalarExpr::ScalarSubquery(*query),
85 ScalarExpr::Exists { subquery, negated } => ScalarExpr::Exists {
86 subquery: *subquery,
87 negated: *negated,
88 },
89 ScalarExpr::Func { .. }
90 | ScalarExpr::Array(_)
91 | ScalarExpr::CompositeRow { .. }
92 | ScalarExpr::Row(_)
93 | ScalarExpr::Binary { .. }
94 | ScalarExpr::UnaryMinus(_)
95 | ScalarExpr::Not(_)
96 | ScalarExpr::And(_)
97 | ScalarExpr::Or(_)
98 | ScalarExpr::IsNull { .. }
99 | ScalarExpr::Between { .. }
100 | ScalarExpr::InList { .. }
101 | ScalarExpr::WindowCall { .. }
102 | ScalarExpr::Case { .. }
103 | ScalarExpr::Collated { .. }
104 | ScalarExpr::Cast { .. }
105 | ScalarExpr::InSubquery { .. } => unreachable!("scalar leaf constructor"),
106 })
107 }
108}
109
110#[cfg(test)]
111mod tests;