Skip to main content

uqa_sql/plan/scalar/owned/
metadata.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Analyzed metadata changes retain child payloads and admit only their new constructors.
8
9use super::{OwnedScalarExpr, Result, ScalarExpr, ScalarMemory};
10use crate::{catalog::collations::CollationSelection, ir::ResultCollation, ColumnType};
11use uqa_core::{
12    memory::{Produced, ProductionControl},
13    Value,
14};
15
16impl OwnedScalarExpr {
17    /// Wrap the surviving expression in the exact type and collation selected during ordered analysis.
18    pub fn collated(
19        child: Self,
20        ty: Produced<ColumnType>,
21        collation: ResultCollation,
22        control: &ProductionControl<'_>,
23    ) -> Result<Self> {
24        child.assert_owner(control);
25        control.check()?;
26        let mut memory = control.empty_reservation().map(|direct| ScalarMemory {
27            children: Vec::new(),
28            direct,
29        });
30        if let Some(memory) = &mut memory {
31            memory.reserve_children(1)?;
32        }
33        let boxed = control.reserve(size_of::<ScalarExpr>())?;
34        let (ty, type_memory) = ty.into_parts();
35        let Self {
36            expression,
37            memory: child_memory,
38        } = child;
39        let mut output = Self {
40            expression: ScalarExpr::Literal(Value::Null),
41            memory,
42        };
43        output.expression = ScalarExpr::Collated {
44            expr: Box::new(expression),
45            ty,
46            collation,
47        };
48        if let Some(memory) = &mut output.memory {
49            memory
50                .push_child(child_memory)
51                .expect("admitted collation child");
52            memory.direct.absorb(
53                control
54                    .combine(boxed, type_memory)
55                    .expect("admitted collation metadata"),
56            );
57        }
58        control.check()?;
59        Ok(output)
60    }
61
62    /// Set a selected call's inline collation identity without rebuilding its allocating metadata.
63    pub fn set_call_input_collation(
64        mut self,
65        collation: Option<CollationSelection>,
66        control: &ProductionControl<'_>,
67    ) -> Result<Self> {
68        self.assert_owner(control);
69        control.check()?;
70        let ScalarExpr::Func {
71            binding: Some(binding),
72            ..
73        } = &mut self.expression
74        else {
75            panic!("input collation requires a selected call")
76        };
77        binding.input_collation = collation;
78        Ok(self)
79    }
80
81    /// Set a typed literal's inline collation identity while retaining its exact value and type owners.
82    pub fn set_literal_collation(
83        mut self,
84        oid: u32,
85        control: &ProductionControl<'_>,
86    ) -> Result<Self> {
87        self.assert_owner(control);
88        control.check()?;
89        let ScalarExpr::TypedLiteral { collation_oid, .. } = &mut self.expression else {
90            panic!("literal collation requires a typed literal")
91        };
92        *collation_oid = Some(oid);
93        Ok(self)
94    }
95}
96
97impl OwnedScalarExpr {
98    /// Set only a cast's display origin; its destination spelling and complete child retain their existing owners.
99    pub fn set_cast_implicit(
100        mut self,
101        implicit: bool,
102        control: &ProductionControl<'_>,
103    ) -> Result<Self> {
104        self.assert_owner(control);
105        control.check()?;
106        let ScalarExpr::Cast {
107            implicit: current, ..
108        } = &mut self.expression
109        else {
110            panic!("cast origin requires a cast")
111        };
112        *current = implicit;
113        Ok(self)
114    }
115}