Skip to main content

uqa_engine/capabilities/
mutation_commands.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind transaction command state and storage point mutations for execution.
8use crate::{session::StatementReadSnapshot, Engine};
9use std::collections::BTreeMap;
10use uqa_core::{DocId, Value};
11use uqa_execution::mutation::{
12    command_scope::MutationCommandState,
13    point_update::{context::PointMutationStorage, RowUpdateVectors},
14};
15use uqa_sql::catalog::roles::RoleReference;
16use uqa_sql::SQLError;
17impl MutationCommandState for Engine {
18    fn prepare_writer(&self) -> Result<(), SQLError> {
19        self.prepare_explicit_transaction_writer().map(|_| ())
20    }
21    fn begin_overlay(&self) {
22        self.mutation_coordinator().begin_command_mutation_overlay();
23    }
24    fn end_overlay(&self) -> uqa_execution::mutation::overlay::CommandMutationOverlay {
25        self.mutation_coordinator().end_command_mutation_overlay()
26    }
27}
28impl PointMutationStorage for Engine {
29    fn find_doc_id_by_field(
30        &self,
31        table: &str,
32        field: &str,
33        value: &Value,
34    ) -> Result<Option<DocId>, SQLError> {
35        Engine::find_mutation_doc_id_by_field(self, table, field, value)
36    }
37    fn patch_document_fields_with_vector_values(
38        &self,
39        table: &str,
40        doc_id: DocId,
41        updates: &BTreeMap<String, Value>,
42        vectors: &RowUpdateVectors,
43    ) -> Result<bool, SQLError> {
44        Engine::patch_document_fields_with_vector_values(self, table, doc_id, updates, vectors)
45    }
46}
47
48impl uqa_sql::semantics::mutation_qualifiers::MutationTargetColumns for Engine {
49    fn try_query_table_columns(&self, table: &str) -> Result<Vec<String>, String> {
50        Engine::try_query_table_columns(self, table).map_err(|error| error.to_string())
51    }
52}
53impl Engine {
54    pub(crate) fn mutation_execution_context(
55        &self,
56    ) -> uqa_execution::mutation::statement::MutationExecutionContext<'_, StatementReadSnapshot>
57    {
58        uqa_execution::mutation::statement::MutationExecutionContext {
59            insert_consumers: self,
60            preparation: self.mutation_preparation_context(),
61            identities: self.insert_identity_context(),
62            rules: self.view_rule_execution_context(),
63            publication: self.mutation_publication_context(),
64            state: self,
65            targets: self,
66            scopes: self,
67            points: self,
68            privileges: self,
69        }
70    }
71}
72
73impl uqa_sql::semantics::mutation_privileges::MutationPrivilegeCatalog for Engine {
74    fn bound_table_column_names(&self, table: &str) -> Result<Vec<String>, SQLError> {
75        Engine::bound_table_column_names(self, table)
76    }
77    fn ensure_table_privilege_for(
78        &self,
79        table: &str,
80        subject: &RoleReference,
81        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
82    ) -> Result<(), SQLError> {
83        Engine::ensure_table_privilege_for(self, table, subject, privilege)
84    }
85    fn ensure_column_privilege_for(
86        &self,
87        table: &str,
88        column: &str,
89        subject: &RoleReference,
90        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
91    ) -> Result<(), SQLError> {
92        Engine::ensure_column_privilege_for(self, table, column, subject, privilege)
93    }
94    fn ensure_any_column_privilege_for(
95        &self,
96        table: &str,
97        subject: &RoleReference,
98        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
99    ) -> Result<(), SQLError> {
100        Engine::ensure_any_column_privilege_for(self, table, subject, privilege)
101    }
102}
103
104impl Engine {
105    pub(crate) fn mutation_statement_context(
106        &self,
107    ) -> uqa_execution::mutation::statement::context::MutationStatementContext<
108        '_,
109        StatementReadSnapshot,
110    > {
111        uqa_execution::mutation::statement::context::MutationStatementContext {
112            query: self.query_execution_context(),
113            mutation: self.mutation_execution_context(),
114            snapshots: self,
115        }
116    }
117}
118impl uqa_execution::mutation::statement::context::MutationSnapshots<StatementReadSnapshot>
119    for Engine
120{
121    fn with_snapshot(
122        &self,
123        snapshot: &StatementReadSnapshot,
124        operation: &mut dyn uqa_execution::mutation::statement::context::ScopedMutationOperation<
125            StatementReadSnapshot,
126        >,
127    ) -> Result<(), SQLError> {
128        let selected = self.statement_read_snapshot_engine(snapshot);
129        let mut context = selected.mutation_statement_context();
130        context.query.generation = Some(snapshot);
131        operation.run(&context)
132    }
133}
134
135impl uqa_execution::mutation::entry::MutationTargetResolution for Engine {
136    fn resolve_target(&self, name: &str, bound: bool) -> Result<String, SQLError> {
137        self.resolve_mutation_target_name(name, bound)
138    }
139}
140impl uqa_execution::mutation::entry::MutationCommandBoundary<StatementReadSnapshot> for Engine {
141    fn with_command(
142        &self,
143        command: uqa_execution::mutation::entry::MutationCommand<'_, StatementReadSnapshot>,
144    ) -> Result<uqa_execution::mutation::output::MutationOutput, SQLError> {
145        crate::transactions::run_mutation_command(self, |engine| {
146            command(&uqa_execution::mutation::entry::MutationCommandContext {
147                statement: engine.mutation_statement_context(),
148                inference: engine.inference_context(),
149                returning: engine.returning_analysis_context(),
150                prune_source_outputs: uqa_planner::mutation_outputs::prune_unused_query_outputs,
151            })
152        })
153    }
154}
155impl Engine {
156    pub(crate) fn mutation_entry_context(
157        &self,
158    ) -> uqa_execution::mutation::entry::MutationEntryContext<'_, StatementReadSnapshot> {
159        uqa_execution::mutation::entry::MutationEntryContext {
160            targets: self,
161            transactions: self,
162            routines: self,
163        }
164    }
165}
166
167impl uqa_execution::statement::context::StatementMutationInputs<StatementReadSnapshot> for Engine {
168    fn mutation_context(
169        &self,
170    ) -> uqa_execution::mutation::entry::MutationEntryContext<'_, StatementReadSnapshot> {
171        self.mutation_entry_context()
172    }
173    fn mutation_initialization_context(
174        &self,
175    ) -> uqa_execution::mutation::statement::context::MutationStatementContext<
176        '_,
177        StatementReadSnapshot,
178    > {
179        self.mutation_statement_context()
180    }
181}