Skip to main content

uqa_engine/capabilities/
query_operators.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Supply expression and row-lock construction services from the active engine session.
8
9use crate::{
10    capabilities::{query_scope::CteScope, ScopedEngineHook},
11    session::StatementReadSnapshot,
12    Engine,
13};
14use std::sync::Arc;
15use uqa_execution::query::recheck_source::LockRowsRecheckInput;
16use uqa_execution::query::relational::{
17    QueryExpressionFactory, RelationalContext, RowLockOperatorFactory,
18};
19use uqa_execution::query::set_projection::SetFunctionRuntime;
20use uqa_execution::{PhysicalOperator, ScalarExpr, SharedRowPredicate};
21use uqa_sql::{plan::QueryBlockPlan, SQLError, SQLParam};
22
23impl Engine {
24    pub(crate) fn relational_context(&self) -> RelationalContext<'_, StatementReadSnapshot> {
25        RelationalContext {
26            catalog: self,
27            expressions: self,
28            row_locks: self,
29            runtime: self.query_runtime_view(),
30        }
31    }
32}
33
34impl QueryExpressionFactory<StatementReadSnapshot> for Engine {
35    fn production_resources(&self) -> Result<uqa_core::memory::OwnedProductionControl, SQLError> {
36        let retained = self.query_retention_control()?;
37        Ok(uqa_core::memory::ProductionControl::new(
38            retained.memory(),
39            retained.cancellation(),
40            self.query_runtime_view().cancellation,
41        )
42        .capture())
43    }
44
45    fn optimize_owned_catalog_scalar(
46        &self,
47        expression: uqa_sql::plan::OwnedScalarExpr,
48        control: &uqa_core::memory::ProductionControl<'_>,
49    ) -> Result<uqa_sql::plan::OwnedScalarExpr, SQLError> {
50        uqa_planner::statement_planning::schema_expressions::optimize_owned_catalog_scalar(
51            &self.statement_planning_context(),
52            expression,
53            control,
54        )
55    }
56
57    fn optimize_catalog_scalar(&self, expression: &mut ScalarExpr) -> Result<(), SQLError> {
58        uqa_planner::statement_planning::schema_expressions::optimize_catalog_scalar(
59            &self.statement_planning_context(),
60            expression,
61        )
62    }
63
64    fn bind_scope(&self, scope: &CteScope) -> Result<Arc<dyn SetFunctionRuntime + '_>, SQLError> {
65        let resources = self.production_resources()?;
66        Ok(ScopedEngineHook::owned(self, scope, &resources.control())?)
67    }
68
69    fn prepare_predicate<'a>(
70        &'a self,
71        expression: &ScalarExpr,
72        params: &'a [SQLParam],
73        scope: &CteScope,
74    ) -> Result<Option<SharedRowPredicate<'a>>, SQLError> {
75        uqa_execution::query::subqueries::prepare_correlated_exists_predicate(
76            &self.subquery_services(),
77            expression,
78            params,
79            scope,
80        )
81    }
82}
83
84impl RowLockOperatorFactory<StatementReadSnapshot> for Engine {
85    fn attach_lock_rows<'a>(
86        &'a self,
87        operator: Box<dyn PhysicalOperator + 'a>,
88        statement: &QueryBlockPlan,
89        params: &'a [SQLParam],
90        scope: &CteScope,
91        max_rows: Option<u64>,
92        recheck: Option<LockRowsRecheckInput<'_, StatementReadSnapshot>>,
93    ) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError> {
94        uqa_execution::query::locking::attach_lock_rows(
95            self.row_lock_context(),
96            operator,
97            statement,
98            params,
99            scope,
100            max_rows,
101            recheck,
102        )
103    }
104}