Skip to main content

uqa_engine/capabilities/
mutation_rows.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind mutation row metadata and expression scopes to the selected engine generation.
8use crate::session::StatementReadSnapshot;
9use crate::Engine;
10use uqa_core::DocId;
11use uqa_execution::mutation::rows::context::{
12    MutationExpressionContext, MutationRowCatalog, MutationRowContext, MutationTupleMetadata,
13};
14use uqa_sql::{ast::ColumnDef, RowSchema, SQLError};
15use uqa_storage::DocumentMetadata;
16
17impl MutationRowCatalog for Engine {
18    fn column_definitions(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String> {
19        self.describe_table_in_execution(table)
20            .map_err(|error| error.to_string())
21    }
22    fn column_names(&self, table: &str) -> Result<Vec<String>, String> {
23        self.table_columns_in_execution(table)
24            .map_err(|error| error.to_string())
25    }
26    fn view_schema(&self, name: &str) -> Result<RowSchema, SQLError> {
27        let definition = self
28            .view_definition(name)?
29            .ok_or_else(|| SQLError::UnknownTable(name.to_string()))?;
30        self.stored_view_schema(&definition)
31    }
32}
33impl MutationTupleMetadata for Engine {
34    fn table_relation_oid(&self, table: &str) -> Result<i64, SQLError> {
35        if self.query_catalog_snapshot.is_some() {
36            return uqa_execution::catalog::projection::table_relation_oid(
37                &self.catalog_execution(),
38                table,
39            );
40        }
41        let relation = uqa_sql::catalog::resolution::bound_relation_identity(table)?;
42        let ordinary = self
43            .storage
44            .tables
45            .read()
46            .get(&relation)
47            .map(|state| state.relation_oids().relation);
48        ordinary
49            .or_else(|| {
50                self.durable
51                    .foreign_tables
52                    .read()
53                    .get(&relation)
54                    .map(|state| state.relation_oids().relation)
55            })
56            .map(i64::from)
57            .ok_or_else(|| SQLError::UnknownTable(table.to_string()))
58    }
59    fn document_metadata(
60        &self,
61        table: &str,
62        doc_id: DocId,
63    ) -> Result<Option<DocumentMetadata>, SQLError> {
64        self.get_query_document_metadata(table, doc_id)
65    }
66    fn tuple_version_xid(&self) -> Result<u32, SQLError> {
67        self.tuple_version_xid()
68    }
69}
70impl Engine {
71    pub(crate) fn mutation_row_context(&self) -> MutationRowContext<'_> {
72        MutationRowContext {
73            catalog: self.catalog_execution(),
74            relations: self,
75            tuples: self,
76        }
77    }
78    pub(crate) fn mutation_expression_context(
79        &self,
80    ) -> MutationExpressionContext<'_, StatementReadSnapshot> {
81        MutationExpressionContext {
82            types: self,
83            expressions: self,
84        }
85    }
86}