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_core::Value;
10use uqa_execution::{
11    mutation::rules::{RuleContext, RuleExpressions, RuleSecurity, RuleStatements},
12    statement::plan_executor::StatementOutput,
13    PhysicalRow, RowSchema,
14};
15use uqa_sql::catalog::roles::RoleReference;
16use uqa_sql::{
17    ast::Statement, plan::ExpressionPlan, semantics::rules::analysis::RuleAnalysisContext, SQLError,
18};
19impl Engine {
20    pub(crate) fn rule_analysis_context(&self) -> RuleAnalysisContext<'_> {
21        RuleAnalysisContext {
22            rules: self,
23            sources: self,
24            returning: self,
25        }
26    }
27    pub(crate) fn rule_execution_context(&self) -> RuleContext<'_> {
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            expressions: self,
37            returning: self.returning_analysis_context(),
38            assignment: self,
39        }
40    }
41}
42impl RuleSecurity for Engine {
43    fn privilege_subject(&self, table: &str) -> Result<RoleReference, SQLError> {
44        self.event_lookup_context().rule_privilege_subject(table)
45    }
46}
47impl RuleStatements for Engine {
48    fn execute(
49        &self,
50        statement: Statement,
51        privilege_subject: &RoleReference,
52    ) -> Result<StatementOutput, SQLError> {
53        uqa_execution::statement::compiled::execute_output_with_privilege_subject(
54            &self.compiled_statement_context(),
55            statement,
56            &[],
57            privilege_subject,
58        )
59    }
60}
61impl RuleExpressions for Engine {
62    fn prepare_condition(&self, expression: &mut ExpressionPlan) -> Result<(), SQLError> {
63        uqa_planner::statement_planning::schema_expressions::optimize_rule_condition(
64            &self.statement_planning_context(),
65            expression,
66        )
67    }
68    fn evaluate_stored(
69        &self,
70        expression: &ExpressionPlan,
71        schema: &RowSchema,
72        row: &PhysicalRow,
73        privilege_subject: &RoleReference,
74    ) -> Result<Value, SQLError> {
75        crate::capabilities::query_expressions::eval_stored_expression_plan_with_row(
76            self,
77            expression,
78            schema,
79            row,
80            &[],
81            Some(privilege_subject),
82        )
83    }
84}
85
86impl Engine {
87    pub(crate) fn view_rule_execution_context(
88        &self,
89    ) -> uqa_execution::mutation::rules::views::ViewRuleContext<
90        '_,
91        crate::session::StatementReadSnapshot,
92    > {
93        uqa_execution::mutation::rules::views::ViewRuleContext {
94            rules: self.rule_execution_context(),
95            views: self.view_row_context(),
96        }
97    }
98}