Skip to main content

uqa_engine/capabilities/
rules.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Capture rule namespaces and expression scopes for execution.
8use crate::Engine;
9use uqa_execution::{
10    mutation::rules::{RuleContext, RuleSecurity, RuleStatements},
11    statement::plan_executor::StatementOutput,
12};
13use uqa_sql::catalog::roles::RoleReference;
14use uqa_sql::{semantics::rules::analysis::RuleAnalysisContext, SQLError};
15impl Engine {
16    pub(crate) fn rule_analysis_context(&self) -> RuleAnalysisContext<'_> {
17        RuleAnalysisContext {
18            rules: self,
19            sources: self,
20            returning: self,
21            domains: self,
22            composites: uqa_sql::expr::EngineHook::composite_types(self),
23        }
24    }
25    pub(crate) fn rule_execution_context(
26        &self,
27    ) -> RuleContext<'_, crate::session::StatementReadSnapshot> {
28        let runtime = self.query_runtime_view();
29        RuleContext {
30            memory: runtime.settings,
31            cancellation: runtime.cancellation,
32            analysis: self.rule_analysis_context(),
33            binding: self.catalog_routine_analysis_context(),
34            security: self,
35            statements: self,
36            returning: self.returning_analysis_context(),
37            assignment: self,
38            projection: None,
39            volatility: self,
40        }
41    }
42}
43impl RuleSecurity for Engine {
44    fn privilege_subject(&self, table: &str) -> Result<RoleReference, SQLError> {
45        self.event_lookup_context().rule_privilege_subject(table)
46    }
47
48    fn privilege_subject_with_control(
49        &self,
50        table: &str,
51        control: &uqa_core::memory::ProductionControl<'_>,
52    ) -> Result<uqa_core::memory::Produced<RoleReference>, SQLError> {
53        self.event_lookup_context()
54            .rule_privilege_subject_with_control(table, control)
55    }
56}
57impl RuleStatements<crate::session::StatementReadSnapshot> for Engine {
58    fn read_control(&self) -> Result<uqa_storage::read_control::StorageReadControl, SQLError> {
59        self.query_retention_control()
60    }
61    fn execute(
62        &self,
63        action: uqa_sql::semantics::rules::binding::RuleAction,
64        params: &[uqa_sql::SQLParam],
65        inherited_ctes: Option<
66            &uqa_execution::query::CteScope<crate::session::StatementReadSnapshot>,
67        >,
68        privilege_subject: &RoleReference,
69        consumer: Option<std::rc::Rc<dyn uqa_execution::query::consumer::QueryRowConsumer + '_>>,
70    ) -> Result<StatementOutput, SQLError> {
71        let planning = self.statement_planning_context();
72        let scopes = inherited_ctes.map(|parent| {
73            uqa_sql::binding::statements::InheritedStatementAnalysisScopes {
74                current: planning.analysis.scopes,
75                parent,
76            }
77        });
78        let planning = uqa_planner::statement_planning::executable::StatementPlanningContext {
79            analysis: uqa_sql::binding::statements::StatementAnalysisContext {
80                scopes: scopes
81                    .as_ref()
82                    .map_or(planning.analysis.scopes, |scope| scope),
83                ..planning.analysis
84            },
85            ..planning
86        };
87        let mut compiled = self.compiled_statement_context();
88        compiled.planning = &planning;
89        uqa_execution::statement::compiled::execute_output_with_privilege_subject(
90            &compiled,
91            action,
92            params,
93            inherited_ctes,
94            privilege_subject,
95            consumer,
96        )
97    }
98}
99
100impl Engine {
101    pub(crate) fn view_rule_execution_context(
102        &self,
103    ) -> uqa_execution::mutation::rules::ViewRuleContext<'_, crate::session::StatementReadSnapshot>
104    {
105        uqa_execution::mutation::rules::ViewRuleContext {
106            rules: self.rule_execution_context(),
107            views: self.view_rewrite_context(),
108        }
109    }
110}