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    fn attach_lock_rows<'a>(
33        &'a self,
34        operator: Box<dyn PhysicalOperator + 'a>,
35        statement: &QueryBlockPlan,
36        params: &'a [SQLParam],
37        scope: &CteScope<S>,
38        max_rows: Option<u64>,
39        recheck: Option<LockRowsRecheckSource<S>>,
40    ) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError>;
41}
42
43#[derive(Clone)]
44pub struct RelationalContext<'a, S: Clone + 'static> {
45    pub catalog: &'a dyn SetFunctionCatalog,
46    pub expressions: &'a dyn QueryExpressionFactory<S>,
47    pub row_locks: &'a dyn RowLockOperatorFactory<S>,
48    pub runtime: QueryRuntimeView<'a>,
49}
50
51impl<S: Clone + 'static> Copy for RelationalContext<'_, S> {}
52
53impl<'a, S: Clone + 'static> RelationalContext<'a, S> {
54    pub fn expression_scope(self, scope: CteScope<S>) -> Arc<dyn SetFunctionRuntime + 'a> {
55        self.expressions.bind_scope(scope)
56    }
57
58    pub fn evaluator(
59        self,
60        params: &'a [SQLParam],
61        scope: &CteScope<S>,
62    ) -> SharedExpressionEvaluator<'a> {
63        crate::query::expression::ScopedExpressionEvaluator::shared(
64            self.expression_scope(scope.clone()),
65            params,
66            self.runtime.cancellation_token(),
67        )
68    }
69}
70
71pub fn build_set_projection<'a, S: Clone + 'static>(
72    operator: Box<dyn PhysicalOperator + 'a>,
73    context: RelationalContext<'a, S>,
74    params: &'a [SQLParam],
75    ctes: &CteScope<S>,
76    evaluator: SharedExpressionEvaluator<'a>,
77    output: SetProjectionOutput,
78) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError> {
79    crate::query::set_projection::build_set_projection(
80        operator,
81        context.catalog,
82        context.expression_scope(ctes.clone()),
83        params,
84        evaluator,
85        output,
86    )
87}
88
89pub fn attach_lock_rows<'a, S: Clone + 'static>(
90    context: RelationalContext<'a, S>,
91    operator: Box<dyn PhysicalOperator + 'a>,
92    statement: &QueryBlockPlan,
93    params: &'a [SQLParam],
94    ctes: &CteScope<S>,
95    max_rows: Option<u64>,
96    recheck: Option<LockRowsRecheckSource<S>>,
97) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError> {
98    context
99        .row_locks
100        .attach_lock_rows(operator, statement, params, ctes, max_rows, recheck)
101}