Skip to main content

uqa_execution/query/
recheck_source.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Immutable query inputs needed by tuple-lock rechecks.
8
9use crate::query::CteScope;
10use uqa_sql::plan::QueryBlockPlan;
11
12/// Everything needed to rebuild the plan below this `LockRows` boundary for a tuple-local recheck. The statement is the query block as it existed before order-set rewrites; the rebuild replays the same construction the original pipeline used, so the recheck output matches the boundary schema.
13pub struct LockRowsRecheckSource<S: Clone> {
14    pub statement: uqa_core::memory::Produced<QueryBlockPlan>,
15    pub ctes: CteScope<S>,
16    pub ordered: bool,
17    pub projections: Vec<crate::query::PhysicalProjection>,
18    pub(super) resources: uqa_core::memory::OwnedProductionControl,
19}
20
21/// Borrow construction inputs until the owning lock boundary establishes that tuple rechecks are needed. Rewritten ordering still supplies the original query block.
22pub struct LockRowsRecheckInput<'a, S: Clone> {
23    statement: &'a QueryBlockPlan,
24    ctes: &'a CteScope<S>,
25    ordered: bool,
26    projections: Vec<crate::query::PhysicalProjection>,
27    resources: uqa_core::memory::OwnedProductionControl,
28}
29
30impl<'a, S: Clone> LockRowsRecheckInput<'a, S> {
31    pub fn new(
32        statement: &'a QueryBlockPlan,
33        ctes: &'a CteScope<S>,
34        ordered: bool,
35        control: &uqa_core::memory::ProductionControl<'_>,
36    ) -> Self {
37        Self::with_projections(statement, ctes, ordered, Vec::new(), control)
38    }
39
40    pub fn with_projections(
41        statement: &'a QueryBlockPlan,
42        ctes: &'a CteScope<S>,
43        ordered: bool,
44        projections: Vec<crate::query::PhysicalProjection>,
45        control: &uqa_core::memory::ProductionControl<'_>,
46    ) -> Self {
47        Self {
48            statement,
49            ctes,
50            ordered,
51            projections,
52            resources: control.capture(),
53        }
54    }
55
56    pub(super) fn retain(self) -> Result<LockRowsRecheckSource<S>, uqa_sql::SQLError> {
57        let statement = self
58            .statement
59            .clone_with_control(&self.resources.control())?;
60        Ok(LockRowsRecheckSource {
61            statement,
62            ctes: self.ctes.copy_with_control(&self.resources.control())?,
63            ordered: self.ordered,
64            projections: self.projections,
65            resources: self.resources,
66        })
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn unused_lock_recheck_inputs_borrow_the_original_statement_and_scope() {
76        let uqa_sql::plan::UnifiedPlan::Query(query) = uqa_sql::plan::UnifiedPlan::lower(
77            uqa_sql::compile(&format!(
78                "SELECT '{}'::text AS payload",
79                "x".repeat(4 << 20)
80            ))
81            .unwrap()
82            .remove(0),
83        ) else {
84            panic!("query")
85        };
86        let uqa_sql::plan::RelationalPlan::QueryBlock(statement) = &query.root else {
87            panic!("block")
88        };
89        let scope = CteScope::<()>::new();
90        let allocation = allocation_counter::measure(|| {
91            let input = LockRowsRecheckInput::new(
92                statement,
93                &scope,
94                false,
95                &uqa_core::memory::ProductionControl::uncontrolled(),
96            );
97            assert!(std::ptr::eq(input.statement, statement.as_ref()));
98            assert!(std::ptr::eq(input.ctes, std::ptr::from_ref(&scope)));
99        });
100        assert_eq!(allocation.bytes_total, 0, "{allocation:?}");
101    }
102}