Skip to main content

uqa_engine/capabilities/
copy.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind COPY relation metadata, privileges, and nested statements to the active session.
8use crate::{Engine, TableState};
9use std::sync::Arc;
10use uqa_execution::copy::{CopyExecutionContext, CopyPrivileges, CopyStatements};
11use uqa_sql::{
12    assignment::columns::ColumnCatalogError,
13    ast::{ColumnDef, Statement},
14    catalog::security::table::TableAclPrivilege,
15    copy::stream::{CopyCatalog, CopyRelation},
16    SQLError, SQLParam, SQLResult,
17};
18impl Engine {
19    pub(crate) fn copy_execution_context(&self) -> CopyExecutionContext<'_> {
20        CopyExecutionContext {
21            catalog: self,
22            privileges: self,
23            statements: self,
24            output: self,
25        }
26    }
27}
28struct CopyTable(Arc<TableState>);
29impl CopyRelation for CopyTable {
30    fn columns(&self) -> Vec<ColumnDef> {
31        self.0.columns.read().clone()
32    }
33    fn is_partitioned(&self) -> bool {
34        self.0.hierarchy.read().partition_spec.is_some()
35    }
36}
37impl CopyCatalog for Engine {
38    fn resolve_relation_kind(
39        &self,
40        name: &str,
41    ) -> Result<Option<(String, &'static str)>, SQLError> {
42        self.try_resolve_visible_relation_kind(name)
43    }
44    fn table(&self, name: &str) -> Result<Option<Box<dyn CopyRelation + '_>>, ColumnCatalogError> {
45        self.try_table(name)
46            .map(|table| table.map(|table| Box::new(CopyTable(table)) as Box<dyn CopyRelation>))
47            .map_err(|error| Box::new(error) as _)
48    }
49}
50impl CopyPrivileges for Engine {
51    fn ensure_any_column(&self, table: &str, privilege: TableAclPrivilege) -> Result<(), SQLError> {
52        self.ensure_any_column_privilege(table, privilege)
53    }
54    fn ensure_column(
55        &self,
56        table: &str,
57        column: &str,
58        privilege: TableAclPrivilege,
59    ) -> Result<(), SQLError> {
60        self.ensure_column_privilege(table, column, privilege)
61    }
62}
63impl CopyStatements for Engine {
64    fn execute_statement(
65        &self,
66        statement: Statement,
67        params: &[SQLParam],
68    ) -> Result<SQLResult, SQLError> {
69        uqa_execution::statement::compiled::execute(
70            &self.compiled_statement_context(),
71            statement,
72            params,
73        )
74    }
75    fn execute_text(&self, text: &str, params: &[SQLParam]) -> Result<SQLResult, SQLError> {
76        uqa_execution::statement::batch::execute(&self.batch_execution_context(), text, params)
77    }
78}