Skip to main content

uqa_execution/query/relational/
context.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Physical relational assembly with independent expression and lock construction services.
8
9use crate::query::recheck_source::LockRowsRecheckSource;
10use crate::query::runtime::QueryRuntimeView;
11use crate::query::set_projection::{SetFunctionRuntime, SetProjectionOutput};
12use crate::query::CteScope;
13use crate::{PhysicalOperator, ScalarExpr, SharedExpressionEvaluator, SharedRowPredicate};
14use std::sync::Arc;
15use uqa_sql::{plan::QueryBlockPlan, semantics::sets::SetFunctionCatalog, SQLError, SQLParam};
16
17/// Bind scoped expression services without exposing the owning session or storage state.
18pub trait QueryExpressionFactory<S: Clone + 'static>: Sync {
19    fn bind_scope(&self, scope: CteScope<S>) -> Arc<dyn SetFunctionRuntime + '_>;
20    /// Apply the owning planner to a validated schema/procedural execution copy.
21    fn optimize_catalog_scalar(&self, expression: &mut ScalarExpr) -> Result<(), SQLError>;
22    fn prepare_predicate<'a>(
23        &'a self,
24        expression: &ScalarExpr,
25        params: &'a [SQLParam],
26        scope: &CteScope<S>,
27    ) -> Result<Option<SharedRowPredicate<'a>>, SQLError>;
28}
29
30/// Attach the locking boundary using the caller's relation and transaction lock services.
31pub trait RowLockOperatorFactory<S: Clone + 'static>: Sync {
32    /// Accept ownership of the input, including closing it before returning a
33    /// construction error. Successful construction transfers it to the result.
34    fn attach_lock_rows<'a>(
35        &'a self,
36        operator: Box<dyn PhysicalOperator + 'a>,
37        statement: &QueryBlockPlan,
38        params: &'a [SQLParam],
39        scope: &CteScope<S>,
40        max_rows: Option<u64>,
41        recheck: Option<LockRowsRecheckSource<S>>,
42    ) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError>;
43}
44
45#[derive(Clone)]
46pub struct RelationalContext<'a, S: Clone + 'static> {
47    pub catalog: &'a dyn SetFunctionCatalog,
48    pub expressions: &'a dyn QueryExpressionFactory<S>,
49    pub row_locks: &'a dyn RowLockOperatorFactory<S>,
50    pub runtime: QueryRuntimeView<'a>,
51}
52
53impl<S: Clone + 'static> Copy for RelationalContext<'_, S> {}
54
55impl<'a, S: Clone + 'static> RelationalContext<'a, S> {
56    pub fn expression_scope(self, scope: CteScope<S>) -> Arc<dyn SetFunctionRuntime + 'a> {
57        self.expressions.bind_scope(scope)
58    }
59
60    pub fn evaluator(
61        self,
62        params: &'a [SQLParam],
63        scope: &CteScope<S>,
64    ) -> SharedExpressionEvaluator<'a> {
65        crate::query::expression::ScopedExpressionEvaluator::shared(
66            self.expression_scope(scope.clone()),
67            params,
68            self.runtime.cancellation_token(),
69        )
70    }
71}
72
73pub fn build_set_projection<'a, S: Clone + 'static>(
74    operator: Box<dyn PhysicalOperator + 'a>,
75    context: RelationalContext<'a, S>,
76    params: &'a [SQLParam],
77    ctes: &CteScope<S>,
78    evaluator: SharedExpressionEvaluator<'a>,
79    output: SetProjectionOutput,
80) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError> {
81    let (operator, work_mem) = crate::physical::construction::OperatorBuild::run(operator, |_| {
82        context.runtime.work_mem_bytes()
83    })?;
84    crate::query::set_projection::build_set_projection(
85        operator,
86        context.catalog,
87        context.expression_scope(ctes.clone()),
88        params,
89        evaluator,
90        work_mem,
91        output,
92    )
93}
94
95pub fn attach_lock_rows<'a, S: Clone + 'static>(
96    context: RelationalContext<'a, S>,
97    operator: Box<dyn PhysicalOperator + 'a>,
98    statement: &QueryBlockPlan,
99    params: &'a [SQLParam],
100    ctes: &CteScope<S>,
101    max_rows: Option<u64>,
102    recheck: Option<LockRowsRecheckSource<S>>,
103) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError> {
104    context
105        .row_locks
106        .attach_lock_rows(operator, statement, params, ctes, max_rows, recheck)
107}