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 visit_bound_table_column_names_with_control(
75        &self,
76        table: &str,
77        control: &uqa_core::memory::ProductionControl<'_>,
78        read: &mut dyn FnMut(&str) -> Result<(), SQLError>,
79    ) -> Result<(), SQLError> {
80        self.table_authorization_context()
81            .visit_bound_table_column_names_with_control(table, control, read)
82    }
83    fn ensure_table_privilege_for_with_control(
84        &self,
85        table: &str,
86        subject: &RoleReference,
87        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
88        control: &uqa_core::memory::ProductionControl<'_>,
89    ) -> Result<(), SQLError> {
90        self.table_authorization_context()
91            .ensure_table_privilege_for_with_control(table, subject, privilege, control)
92    }
93    fn ensure_column_privilege_for_with_control(
94        &self,
95        table: &str,
96        column: &str,
97        subject: &RoleReference,
98        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
99        control: &uqa_core::memory::ProductionControl<'_>,
100    ) -> Result<(), SQLError> {
101        self.table_authorization_context()
102            .ensure_column_privilege_for_with_control(table, column, subject, privilege, control)
103    }
104    fn ensure_any_column_privilege_for_with_control(
105        &self,
106        table: &str,
107        subject: &RoleReference,
108        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
109        control: &uqa_core::memory::ProductionControl<'_>,
110    ) -> Result<(), SQLError> {
111        self.table_authorization_context()
112            .ensure_any_column_privilege_for_with_control(table, subject, privilege, control)
113    }
114    fn bound_table_column_names(&self, table: &str) -> Result<Vec<String>, SQLError> {
115        Engine::bound_table_column_names(self, table)
116    }
117    fn ensure_table_privilege_for(
118        &self,
119        table: &str,
120        subject: &RoleReference,
121        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
122    ) -> Result<(), SQLError> {
123        Engine::ensure_table_privilege_for(self, table, subject, privilege)
124    }
125    fn ensure_column_privilege_for(
126        &self,
127        table: &str,
128        column: &str,
129        subject: &RoleReference,
130        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
131    ) -> Result<(), SQLError> {
132        Engine::ensure_column_privilege_for(self, table, column, subject, privilege)
133    }
134    fn ensure_any_column_privilege_for(
135        &self,
136        table: &str,
137        subject: &RoleReference,
138        privilege: uqa_sql::catalog::security::table::TableAclPrivilege,
139    ) -> Result<(), SQLError> {
140        Engine::ensure_any_column_privilege_for(self, table, subject, privilege)
141    }
142}
143
144impl Engine {
145    pub(crate) fn mutation_statement_context(
146        &self,
147    ) -> uqa_execution::mutation::statement::context::MutationStatementContext<
148        '_,
149        StatementReadSnapshot,
150    > {
151        uqa_execution::mutation::statement::context::MutationStatementContext {
152            query: self.query_execution_context(),
153            mutation: self.mutation_execution_context(),
154            snapshots: self,
155        }
156    }
157}
158impl uqa_execution::mutation::statement::context::MutationSnapshots<StatementReadSnapshot>
159    for Engine
160{
161    fn with_snapshot(
162        &self,
163        snapshot: &StatementReadSnapshot,
164        operation: &mut dyn uqa_execution::mutation::statement::context::ScopedMutationOperation<
165            StatementReadSnapshot,
166        >,
167    ) -> Result<(), SQLError> {
168        let selected = self.statement_read_snapshot_engine(snapshot);
169        let mut context = selected.mutation_statement_context();
170        context.query.generation = Some(snapshot);
171        operation.run(&context)
172    }
173}
174
175impl uqa_execution::mutation::entry::MutationTargetResolution for Engine {
176    fn resolve_target_with_control(
177        &self,
178        name: &str,
179        bound: bool,
180        control: &uqa_core::memory::ProductionControl<'_>,
181    ) -> Result<uqa_core::memory::Produced<String>, SQLError> {
182        self.resolve_mutation_target_name_with_control(name, bound, control)
183    }
184    fn resolve_target(&self, name: &str, bound: bool) -> Result<String, SQLError> {
185        self.resolve_mutation_target_name(name, bound)
186    }
187}
188impl uqa_execution::mutation::entry::MutationCommandBoundary<StatementReadSnapshot> for Engine {
189    fn with_command(
190        &self,
191        command: uqa_execution::mutation::entry::MutationCommand<'_, StatementReadSnapshot>,
192    ) -> Result<uqa_execution::mutation::output::MutationOutput, SQLError> {
193        crate::transactions::run_mutation_command(self, |engine| {
194            command(&uqa_execution::mutation::entry::MutationCommandContext {
195                statement: engine.mutation_statement_context(),
196                inference: engine.inference_context(),
197                returning: engine.returning_analysis_context(),
198                prune_source_outputs:
199                    uqa_planner::mutation_outputs::prune_unused_query_outputs_owned,
200            })
201        })
202    }
203}
204impl Engine {
205    pub(crate) fn mutation_entry_context(
206        &self,
207    ) -> uqa_execution::mutation::entry::MutationEntryContext<'_, StatementReadSnapshot> {
208        uqa_execution::mutation::entry::MutationEntryContext {
209            targets: self,
210            transactions: self,
211            routines: self,
212        }
213    }
214}
215
216impl uqa_execution::statement::context::StatementMutationInputs<StatementReadSnapshot> for Engine {
217    fn mutation_context(
218        &self,
219    ) -> uqa_execution::mutation::entry::MutationEntryContext<'_, StatementReadSnapshot> {
220        self.mutation_entry_context()
221    }
222    fn mutation_initialization_context(
223        &self,
224    ) -> uqa_execution::mutation::statement::context::MutationStatementContext<
225        '_,
226        StatementReadSnapshot,
227    > {
228        self.mutation_statement_context()
229    }
230}