Skip to main content

uqa_engine/capabilities/
query_locking.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind tuple-lock execution to catalog, transaction, and storage state.
8
9use crate::{capabilities::query_scope::CteScope, session::StatementReadSnapshot, Engine};
10use std::{collections::BTreeSet, sync::Arc};
11use uqa_core::DocId;
12use uqa_execution::{
13    query::locking::{
14        context::{
15            LockingCatalog, QueryRowLockSession, RowLockReadSource, RowLockScopeSource,
16            RowRecheckBuilder,
17        },
18        RowLockContext,
19    },
20    row_locks::{retry_cache::RowLockRetryCache, RelationLockMode, RowLockAcquisition},
21    PhysicalOperator,
22};
23use uqa_sql::{
24    ast::{ColumnDef, RelationPersistence, TableKeyConstraint},
25    plan::{QueryBlockPlan, QueryPlan},
26    SQLError, SQLParam,
27};
28use uqa_storage::document_store::Document;
29
30impl Engine {
31    pub(crate) fn row_lock_context(&self) -> RowLockContext<'_, StatementReadSnapshot> {
32        RowLockContext {
33            catalog: self,
34            session: self,
35            rows: self,
36            scopes: self,
37            recheck: self,
38            cancellation: self.query_runtime_view().cancellation,
39        }
40    }
41}
42impl LockingCatalog for Engine {
43    fn hierarchy_scan_tables(
44        &self,
45        table: &str,
46        descendants: bool,
47    ) -> Result<Vec<String>, SQLError> {
48        Engine::query_hierarchy_scan_tables(self, table, descendants)
49    }
50    fn resolve_relation(
51        &self,
52        name: &str,
53        bound: bool,
54    ) -> Result<Option<(String, &'static str)>, SQLError> {
55        self.try_resolve_relation_kind_for_query(name, bound)
56    }
57    fn view_plan(&self, name: &str) -> Result<Option<QueryPlan>, SQLError> {
58        Engine::view_plan(self, name)
59    }
60    fn table_persistence(&self, name: &str) -> Result<Option<RelationPersistence>, String> {
61        Engine::table_persistence(self, name).map_err(|error| error.to_string())
62    }
63    fn referenceable_keys(&self, table: &str) -> Result<Vec<TableKeyConstraint>, String> {
64        Engine::referenceable_keys(self, table).map_err(|error| error.to_string())
65    }
66    fn table_columns(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String> {
67        self.describe_table_in_execution(table)
68            .map_err(|error| error.to_string())
69    }
70}
71impl QueryRowLockSession for Engine {
72    fn lock_relation(&self, table: &str, mode: RelationLockMode) -> Result<(), SQLError> {
73        Engine::lock_relation(self, table, mode)
74    }
75    fn rollback_row_lock_acquisition(&self, acquisition: RowLockAcquisition) {
76        Engine::rollback_row_lock_acquisition(self, acquisition);
77    }
78    fn row_changed_in_open_transaction(
79        &self,
80        table: &str,
81        doc_id: DocId,
82    ) -> Result<bool, SQLError> {
83        Engine::row_changed_in_open_transaction(self, table, doc_id)
84    }
85    fn row_lock_change_requires_recheck(&self) -> Result<bool, SQLError> {
86        Engine::row_lock_change_requires_recheck(self)
87    }
88    fn current_transaction_is_read_only(&self) -> bool {
89        Engine::current_transaction_is_read_only(self)
90    }
91    fn statement_row_lock_cache(&self) -> Result<Arc<RowLockRetryCache>, SQLError> {
92        Engine::statement_row_lock_cache(self)
93    }
94}
95impl RowLockReadSource for Engine {
96    fn get_document(&self, table: &str, doc_id: DocId) -> Result<Option<Document>, SQLError> {
97        Engine::get_live_document(self, table, doc_id)
98    }
99    fn get_document_for_mutation(
100        &self,
101        table: &str,
102        doc_id: DocId,
103    ) -> Result<Option<Document>, SQLError> {
104        Engine::get_document_for_mutation(self, table, doc_id)
105    }
106}
107impl RowLockScopeSource<StatementReadSnapshot> for Engine {
108    fn current_routine_scope(&self) -> CteScope {
109        crate::capabilities::query_scope::new_for_current_routine(self)
110    }
111    fn transition_relation_names(&self) -> BTreeSet<String> {
112        uqa_execution::mutation::triggers::current_transition_relation_names()
113    }
114}
115impl RowRecheckBuilder<StatementReadSnapshot> for Engine {
116    fn build_recheck<'a>(
117        &'a self,
118        statement: &QueryBlockPlan,
119        params: &'a [SQLParam],
120        ctes: &mut CteScope,
121        ordered: bool,
122        projections: &[uqa_execution::query::PhysicalProjection],
123    ) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError> {
124        uqa_execution::query::sources::recheck::build_row_lock_recheck_operator(
125            &self.source_execution_context(),
126            statement,
127            params,
128            ctes,
129            ordered,
130            projections,
131        )
132    }
133}