Skip to main content

uqa_sql/plan/scalar/owned/
construction.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Scalar containers admit their own allocation without merging the independently movable children.
8
9use super::{OwnedScalarExpr, Result, ScalarExpr, ScalarMemory};
10use crate::plan::scalar::resources::{Control, Lowering};
11use uqa_core::{
12    memory::{BudgetedVec, ProductionControl},
13    Value,
14};
15
16impl OwnedScalarExpr {
17    /// Construct an unbound typed NULL, admitting the declared type name before copying it. This storage constructor performs no type resolution or value evaluation.
18    pub fn typed_null(ty: &str, control: &ProductionControl<'_>) -> Result<Self> {
19        let (ty, memory) = control.copy_text(ty)?.into_parts();
20        let output = Self {
21            expression: ScalarExpr::TypedLiteral {
22                value: Value::Null,
23                ty,
24                bound_type: None,
25                collation_oid: None,
26                composite_source: None,
27                parameter_index: None,
28            },
29            memory: memory.map(|direct| ScalarMemory {
30                children: Vec::new(),
31                direct,
32            }),
33        };
34        control.check()?;
35        Ok(output)
36    }
37
38    /// Build AND or OR from already owned children. The destination vector and ownership metadata reserve before allocation; input payloads move without copying or changing their allowance.
39    pub fn boolean(
40        children: impl ExactSizeIterator<Item = Self>,
41        conjunction: bool,
42        control: &ProductionControl<'_>,
43    ) -> Result<Self> {
44        control.check()?;
45        let mut lowering = Lowering {
46            control: control.budget().map(|_| Control::from_production(*control)),
47            owned_children: control.budget().map(BudgetedVec::new),
48        };
49        let items = lowering.map(children, Self::attach_child)?;
50        let expression = if conjunction {
51            ScalarExpr::And(items)
52        } else {
53            ScalarExpr::Or(items)
54        };
55        let output = Self {
56            expression,
57            memory: lowering.control.map(|control| {
58                ScalarMemory::from_parts(
59                    control.memory,
60                    lowering
61                        .owned_children
62                        .expect("mutable scalar construction"),
63                )
64            }),
65        };
66        control.check()?;
67        Ok(output)
68    }
69
70    pub(in crate::plan::scalar) fn attach_child(
71        lowering: &mut Lowering<'_>,
72        child: Self,
73    ) -> Result<ScalarExpr> {
74        let control = lowering
75            .control
76            .as_ref()
77            .map_or_else(ProductionControl::uncontrolled, |control| {
78                control.production
79            });
80        child.assert_owner(&control);
81        control.check()?;
82        if let Some(children) = &mut lowering.owned_children {
83            children.reserve(1)?;
84        }
85        let Self { expression, memory } = child;
86        if let Some(children) = &mut lowering.owned_children {
87            children
88                .push(memory)
89                .expect("scalar child slot was admitted");
90        }
91        Ok(expression)
92    }
93}