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