Skip to main content

uqa_sql/plan/scalar/
lowering.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! AST lowering can publish independently movable scalar children directly.
8
9use super::{
10    resources::{Control, Lowering, Result},
11    source::Source,
12    AggregateClassifier, Expr, OwnedScalarExpr, QueryLowering, ScalarExpr, ScalarMemory,
13};
14use uqa_core::memory::{BudgetedVec, ProductionControl};
15
16impl super::super::ExpressionPlan {
17    /// Lower borrowed scalar syntax directly into a mutable owner. Every child retains its own allocation lease, so subsequent selection or replacement releases discarded payloads without copying the completed scalar tree. Query children retain the scalar-only lowerer's rejection behavior.
18    pub fn lower_scalar_for_rewrite(
19        expression: &Expr,
20        control: &ProductionControl<'_>,
21    ) -> Result<OwnedScalarExpr> {
22        lower_owned(
23            Source::Borrowed(expression),
24            &super::super::NoRegisteredAggregates,
25            &mut Vec::new(),
26            *control,
27        )
28    }
29}
30
31impl Lowering<'_> {
32    pub(super) fn expression(
33        &mut self,
34        expression: Source<'_, Expr>,
35        aggregates: &dyn AggregateClassifier,
36        subqueries: &mut dyn QueryLowering,
37    ) -> Result<ScalarExpr> {
38        if self.owned_children.is_none() {
39            return self.expression_node(expression, aggregates, subqueries);
40        }
41        let control = self
42            .control
43            .as_ref()
44            .expect("mutable scalar lowering")
45            .production;
46        self.owned_children
47            .as_mut()
48            .expect("mutable scalar lowering")
49            .reserve(1)?;
50        let child = lower_owned(expression, aggregates, subqueries, control)?;
51        OwnedScalarExpr::attach_child(self, child)
52    }
53}
54
55fn lower_owned(
56    expression: Source<'_, Expr>,
57    aggregates: &dyn AggregateClassifier,
58    subqueries: &mut dyn QueryLowering,
59    control: ProductionControl<'_>,
60) -> Result<OwnedScalarExpr> {
61    control.check()?;
62    let mut lowering = Lowering {
63        control: control.budget().map(|_| Control::from_production(control)),
64        owned_children: control.budget().map(BudgetedVec::new),
65    };
66    let expression = lowering.expression_node(expression, aggregates, subqueries)?;
67    let output = OwnedScalarExpr::from_rewrite_parts(
68        expression,
69        lowering.control.map(|control| {
70            ScalarMemory::from_parts(
71                control.memory,
72                lowering.owned_children.expect("mutable scalar lowering"),
73            )
74        }),
75    );
76    control.check()?;
77    Ok(output)
78}
79
80pub(in crate::plan) fn lower_scalar_with_arena(
81    expression: &Expr,
82    aggregates: &dyn AggregateClassifier,
83    subqueries: &mut dyn QueryLowering,
84    control: ProductionControl<'_>,
85) -> Result<OwnedScalarExpr> {
86    lower_owned(
87        Source::Borrowed(expression),
88        aggregates,
89        subqueries,
90        control,
91    )
92}
93
94#[cfg(test)]
95mod tests;