Skip to main content

uqa_engine/capabilities/
returning.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Capture metadata and statement scopes for RETURNING analysis and row projection.
8use crate::{session::StatementReadSnapshot, Engine};
9use uqa_execution::mutation::returning::ReturningExecutionContext;
10use uqa_sql::{
11    ast::ColumnDef,
12    binding::snapshot::BindingSnapshot,
13    semantics::returning::{ReturningAnalysisContext, ReturningCatalog, ReturningScope},
14    RowSchema, SQLError,
15};
16impl ReturningCatalog for Engine {
17    fn try_describe_table_row_type(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String> {
18        Engine::try_describe_table_row_type(self, table).map_err(|error| error.to_string())
19    }
20    fn with_table_row_type_with_control(
21        &self,
22        name: &str,
23        control: &uqa_core::memory::ProductionControl<'_>,
24        read: &mut dyn FnMut(&[ColumnDef]) -> Result<(), SQLError>,
25    ) -> Result<bool, SQLError> {
26        control.check()?;
27        self.synchronize_table_catalog()
28            .and_then(|()| self.synchronize_table_data())
29            .map_err(|error| {
30                match uqa_execution::storage_errors::storage_error("read table row type", &error) {
31                    SQLError::Internal(_) => match control.format(format_args!("{error}")) {
32                        Ok(message) => SQLError::Internal(message.into_parts().0),
33                        Err(error) => error.into(),
34                    },
35                    diagnostic => diagnostic,
36                }
37            })?;
38        let candidates =
39            uqa_sql::catalog::resolution::candidates::relation_lookup_candidates_with_control(
40                self, name, control,
41            )?;
42        let table = {
43            let tables = self.storage.tables.read();
44            candidates
45                .iter()
46                .find_map(|relation| tables.get(relation).cloned())
47        };
48        let Some(table) = table else {
49            return Ok(false);
50        };
51        control.check()?;
52        read(&table.columns.read())?;
53        Ok(true)
54    }
55    fn try_table_columns(&self, table: &str) -> Result<Vec<String>, String> {
56        Engine::table_columns_in_execution(self, table).map_err(|error| error.to_string())
57    }
58    fn visit_column_names_with_control(
59        &self,
60        table: &str,
61        control: &uqa_core::memory::ProductionControl<'_>,
62        read: &mut dyn FnMut(&str) -> Result<(), SQLError>,
63    ) -> Result<(), SQLError> {
64        self.visit_raw_table_column_names_with_control(table, control, read)
65    }
66    fn view_schema(&self, table: &str) -> Result<Option<RowSchema>, SQLError> {
67        Engine::view_schema(self, table)
68    }
69}
70impl ReturningScope for Engine {
71    fn binding_snapshot(&self) -> Result<BindingSnapshot, SQLError> {
72        let scope = super::query_scope::new_for_current_routine(self)?;
73        uqa_execution::query::binding::binding_context(&scope).map(BindingSnapshot::from)
74    }
75}
76impl Engine {
77    pub(crate) fn returning_analysis_context(&self) -> ReturningAnalysisContext<'_> {
78        ReturningAnalysisContext {
79            catalog: self,
80            routines: self,
81            aggregates: self,
82            scope: self,
83        }
84    }
85    pub(crate) fn returning_execution_context(
86        &self,
87    ) -> ReturningExecutionContext<'_, StatementReadSnapshot> {
88        ReturningExecutionContext {
89            destination: None,
90            rows: self.mutation_row_context(),
91            catalog: self,
92            routines: self,
93            relational: self.relational_context(),
94        }
95    }
96}
97
98impl uqa_execution::mutation::returning::ReturningInputs<StatementReadSnapshot> for Engine {
99    fn returning_execution_context(
100        &self,
101    ) -> uqa_execution::mutation::returning::ReturningExecutionContext<'_, StatementReadSnapshot>
102    {
103        Engine::returning_execution_context(self)
104    }
105    fn returning_analysis_context(
106        &self,
107    ) -> uqa_sql::semantics::returning::ReturningAnalysisContext<'_> {
108        Engine::returning_analysis_context(self)
109    }
110}