Skip to main content

uqa_sql/plan/mutable/
executable.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Mutable statement roots publish only after their independent child edits finish.
8
9use super::{MutableCommandPlan, MutableQueryPlan};
10use crate::{
11    plan::{CommandPlan, OwnedExecutablePlan, OwnedQueryPlan, PlanRef, UnifiedPlan},
12    SQLError,
13};
14use uqa_core::memory::ProductionControl;
15
16#[derive(Debug)]
17#[expect(
18    clippy::large_enum_variant,
19    reason = "statement owners remain inline so moving an admitted root requires no additional allocation"
20)]
21pub enum MutableExecutablePlan {
22    Query(MutableQueryPlan),
23    Command(MutableCommandPlan),
24}
25
26impl UnifiedPlan {
27    pub fn copy_for_rewrite(
28        &self,
29        control: &ProductionControl<'_>,
30    ) -> Result<MutableExecutablePlan, SQLError> {
31        PlanRef::from(self).copy_for_rewrite(control)
32    }
33}
34
35impl PlanRef<'_> {
36    pub fn copy_for_rewrite(
37        self,
38        control: &ProductionControl<'_>,
39    ) -> Result<MutableExecutablePlan, SQLError> {
40        Ok(match self {
41            Self::Query(query) => MutableExecutablePlan::Query(query.copy_for_rewrite(control)?),
42            Self::Command(command) => {
43                MutableExecutablePlan::Command(command.copy_for_rewrite(control)?)
44            }
45        })
46    }
47}
48
49impl MutableExecutablePlan {
50    /// Transfer a retained native executable through another entry point using the same allowance and its current cancellation tokens.
51    pub fn transfer(self, control: &ProductionControl<'_>) -> Result<Self, SQLError> {
52        match &self {
53            Self::Query(query) => query.assert_owner(control),
54            Self::Command(command) => command.assert_owner(control),
55        }
56        control.check()?;
57        Ok(self)
58    }
59
60    pub fn as_plan_ref(&self) -> PlanRef<'_> {
61        match self {
62            Self::Query(query) => PlanRef::Query(query),
63            Self::Command(command) => PlanRef::Command(command),
64        }
65    }
66
67    pub fn reserved_bytes(&self) -> usize {
68        match self {
69            Self::Query(query) => query.reserved_bytes(),
70            Self::Command(command) => command.reserved_bytes(),
71        }
72    }
73
74    /// Admit the final statement box and transfer only surviving constructor leases into immutable execution storage.
75    pub fn finish(self, control: &ProductionControl<'_>) -> Result<OwnedExecutablePlan, SQLError> {
76        match self {
77            Self::Query(query) => Ok(OwnedQueryPlan::from_produced(
78                query.finish(control)?,
79                control,
80            )?
81            .into_executable()),
82            Self::Command(command) => {
83                let root = control.reserve(size_of::<CommandPlan>())?;
84                let (command, memory) = command.finish(control)?.into_parts();
85                let memory = control.combine(memory, root);
86                Ok(OwnedExecutablePlan::from(
87                    control.finish(UnifiedPlan::Command(Box::new(command)), memory)?,
88                ))
89            }
90        }
91    }
92}