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::{
12    rows::context::{
13        MutationExpressionContext, MutationRowCatalog, MutationRowContext, MutationTupleMetadata,
14    },
15    views::ViewRowContext,
16};
17use uqa_sql::{ast::ColumnDef, RowSchema, SQLError};
18use uqa_storage::DocumentMetadata;
19
20impl MutationRowCatalog for Engine {
21    fn column_definitions(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String> {
22        self.describe_table_in_execution(table)
23            .map_err(|error| error.to_string())
24    }
25    fn column_names(&self, table: &str) -> Result<Vec<String>, String> {
26        self.table_columns_in_execution(table)
27            .map_err(|error| error.to_string())
28    }
29    fn view_schema(&self, name: &str) -> Result<RowSchema, SQLError> {
30        let definition = self
31            .view_definition(name)?
32            .ok_or_else(|| SQLError::UnknownTable(name.to_string()))?;
33        self.stored_view_schema(&definition)
34    }
35}
36impl MutationTupleMetadata for Engine {
37    fn table_relation_oid(&self, table: &str) -> Result<i64, SQLError> {
38        if self.query_catalog_snapshot.is_some() {
39            return uqa_execution::catalog::projection::table_relation_oid(
40                &self.catalog_execution(),
41                table,
42            );
43        }
44        let relation = uqa_sql::catalog::resolution::bound_relation_identity(table)?;
45        let ordinary = self
46            .storage
47            .tables
48            .read()
49            .get(&relation)
50            .map(|state| state.relation_oids().relation);
51        ordinary
52            .or_else(|| {
53                self.durable
54                    .foreign_tables
55                    .read()
56                    .get(&relation)
57                    .map(|state| state.relation_oids().relation)
58            })
59            .map(i64::from)
60            .ok_or_else(|| SQLError::UnknownTable(table.to_string()))
61    }
62    fn document_metadata(
63        &self,
64        table: &str,
65        doc_id: DocId,
66    ) -> Result<Option<DocumentMetadata>, SQLError> {
67        self.get_query_document_metadata(table, doc_id)
68    }
69    fn tuple_version_xid(&self) -> Result<u32, SQLError> {
70        self.tuple_version_xid()
71    }
72}
73impl Engine {
74    pub(crate) fn mutation_row_context(&self) -> MutationRowContext<'_> {
75        MutationRowContext {
76            catalog: self.catalog_execution(),
77            relations: self,
78            tuples: self,
79        }
80    }
81    pub(crate) fn mutation_expression_context(
82        &self,
83    ) -> MutationExpressionContext<'_, StatementReadSnapshot> {
84        MutationExpressionContext {
85            types: self,
86            expressions: self,
87        }
88    }
89    pub(crate) fn view_row_context(&self) -> ViewRowContext<'_, StatementReadSnapshot> {
90        ViewRowContext {
91            volatility: self,
92            rewrite: self.view_rewrite_context(),
93            rows: self.mutation_row_context(),
94            expressions: self.mutation_expression_context(),
95        }
96    }
97}