Skip to main content

uqa_sql/plan/mutable/
parents.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Read-only parent observations remain available while a child owns its moved payload.
8
9use super::{
10    sources::query_source_memory, MutableQueryPlan, MutableSourcePlan, OwnedScalarExpr,
11    QueryChildSlot, QueryPlan, QueryScalarSlot, RelationalPlan,
12};
13use crate::{SQLError, ScalarExpr};
14use uqa_core::{memory::ProductionControl, Value};
15
16impl MutableQueryPlan {
17    /// Rewrite a scalar with a read-only view of the enclosing query. The selected parent slot temporarily contains an allocation-free NULL; the callback receives its actual previous scalar and owner separately.
18    pub fn map_scalar_with_parent(
19        mut self,
20        slot: QueryScalarSlot,
21        control: &ProductionControl<'_>,
22        map: impl FnOnce(&QueryPlan, OwnedScalarExpr) -> Result<OwnedScalarExpr, SQLError>,
23    ) -> Result<Self, SQLError> {
24        self.assert_owner(control);
25        control.check()?;
26        let Some(expression) = slot.expression_mut(&mut self.query) else {
27            return Ok(self);
28        };
29        let memory = self
30            .memory
31            .as_mut()
32            .and_then(|memory| slot.memory_mut(memory).take());
33        let input = OwnedScalarExpr::from_rewrite_parts(
34            std::mem::replace(expression, ScalarExpr::Literal(Value::Null)),
35            memory,
36        );
37        let output = input.rewrite(control, |input| map(&self.query, input))?;
38        let (expression, memory) = output.into_rewrite_parts();
39        *slot
40            .expression_mut(&mut self.query)
41            .expect("original scalar slot") = expression;
42        if let Some(owner) = &mut self.memory {
43            *slot.memory_mut(owner) = memory;
44        }
45        Ok(self)
46    }
47
48    /// Observe an enclosing query's namespace while rewriting one directly owned child. Its original box or arena entry stays allocated in the parent and holds an empty query until the callback returns.
49    pub fn map_query_child_with_parent(
50        mut self,
51        slot: QueryChildSlot,
52        control: &ProductionControl<'_>,
53        map: impl FnOnce(&QueryPlan, Self) -> Result<Self, SQLError>,
54    ) -> Result<Self, SQLError> {
55        self.assert_owner(control);
56        control.check()?;
57        let input = Self {
58            query: std::mem::replace(
59                slot.query_mut(&mut self.query),
60                super::queries::empty_query(),
61            ),
62            memory: self
63                .memory
64                .as_mut()
65                .and_then(|memory| slot.memory_mut(memory).take()),
66        };
67        let output = map(&self.query, input)?;
68        output.assert_owner(control);
69        control.check()?;
70        *slot.query_mut(&mut self.query) = output.query;
71        if let Some(memory) = &mut self.memory {
72            *slot.memory_mut(memory) = output.memory;
73        }
74        Ok(self)
75    }
76
77    /// Observe the query's scalar and analysis inputs while rewriting its source. FROM is temporarily absent; its complete original source is moved into the callback.
78    pub fn map_source_with_parent(
79        mut self,
80        control: &ProductionControl<'_>,
81        map: impl FnOnce(&QueryPlan, MutableSourcePlan) -> Result<MutableSourcePlan, SQLError>,
82    ) -> Result<Self, SQLError> {
83        self.assert_owner(control);
84        control.check()?;
85        let RelationalPlan::QueryBlock(block) = &mut self.query.root else {
86            return Err(SQLError::Internal(
87                "source rewrite requires a query block".into(),
88            ));
89        };
90        let Some(source) = block.from.take() else {
91            return Ok(self);
92        };
93        let memory = self
94            .memory
95            .as_mut()
96            .and_then(|memory| query_source_memory(memory).take());
97        let output = map(&self.query, MutableSourcePlan { source, memory })?;
98        output.assert_owner(control);
99        control.check()?;
100        let RelationalPlan::QueryBlock(block) = &mut self.query.root else {
101            unreachable!("read-only parent cannot change topology");
102        };
103        block.from = Some(output.source);
104        if let Some(memory) = &mut self.memory {
105            *query_source_memory(memory) = output.memory;
106        }
107        Ok(self)
108    }
109}