Skip to main content

uqa_execution/statement/
compiled.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Execute compiled statements with planning and statement scopes captured at their point of use.
8
9use super::{context::StatementExecutionInputs, plan_executor::UnifiedPlanExecutor};
10use uqa_sql::catalog::roles::RoleReference;
11use uqa_sql::{
12    binding::statements::AnalyzedResult,
13    binding::stored_routines::mark_catalog_statement_relations_bound,
14    plan::{AggregateClassifier, CommandPlan, ExecutablePlanOptimizer, UnifiedPlan},
15    SQLError, SQLParam, SQLResult, Statement,
16};
17
18pub struct CompiledStatementContext<'a, S: Clone + 'static> {
19    pub aggregates: &'a dyn AggregateClassifier,
20    pub planning: &'a dyn ExecutablePlanOptimizer,
21    pub statements: &'a dyn StatementExecutionInputs<S>,
22}
23
24pub fn execute<S: Clone + Send + Sync + 'static>(
25    context: &CompiledStatementContext<'_, S>,
26    statement: Statement,
27    params: &[SQLParam],
28) -> Result<SQLResult, SQLError> {
29    let plan = UnifiedPlan::lower_with(statement, context.aggregates);
30    execute_plan(context, plan, params)
31}
32
33pub fn execute_plan<S: Clone + Send + Sync + 'static>(
34    context: &CompiledStatementContext<'_, S>,
35    plan: UnifiedPlan,
36    params: &[SQLParam],
37) -> Result<SQLResult, SQLError> {
38    let _temporal_input = super::context::temporal_input_scope(context.statements);
39    let plan = context.planning.plan_for_execution(plan, params)?;
40    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
41        .execute(&plan)
42}
43
44/// A prepared owner has already analyzed and selected this executable. Capture
45/// fresh execution inputs without binding or optimizing it a second time.
46pub fn execute_selected_plan<S: Clone + Send + Sync + 'static>(
47    context: &CompiledStatementContext<'_, S>,
48    plan: &UnifiedPlan,
49    params: &[SQLParam],
50) -> Result<SQLResult, SQLError> {
51    let _temporal_input = super::context::temporal_input_scope(context.statements);
52    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
53        .execute(plan)
54}
55
56/// Run a statement once `check` accepts what its analysis derives about its result.
57pub fn execute_checked_plan<S: Clone + Send + Sync + 'static>(
58    context: &CompiledStatementContext<'_, S>,
59    plan: UnifiedPlan,
60    params: &[SQLParam],
61    check: &dyn Fn(&AnalyzedResult) -> Result<(), SQLError>,
62) -> Result<SQLResult, SQLError> {
63    let _temporal_input = super::context::temporal_input_scope(context.statements);
64    let (plan, result) = context.planning.plan_with_result(plan, params)?;
65    check(&result)?;
66    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
67        .execute(&plan)
68}
69
70pub fn execute_with_privilege_subject<S: Clone + Send + Sync + 'static>(
71    context: &CompiledStatementContext<'_, S>,
72    statement: Statement,
73    params: &[SQLParam],
74    privilege_subject: &RoleReference,
75) -> Result<SQLResult, SQLError> {
76    let _temporal_input = super::context::temporal_input_scope(context.statements);
77    let mut plan = UnifiedPlan::lower_with(statement, context.aggregates);
78    mark_catalog_statement_relations_bound(&mut plan)?;
79    let plan = context.planning.plan_for_execution(plan, params)?;
80    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
81        .with_privilege_subject(privilege_subject)
82        .execute(&plan)
83}
84
85pub fn execute_optimized_command<S: Clone + Send + Sync + 'static>(
86    statements: &dyn StatementExecutionInputs<S>,
87    command: &CommandPlan,
88    params: &[SQLParam],
89) -> Result<SQLResult, SQLError> {
90    let _temporal_input = super::context::temporal_input_scope(statements);
91    UnifiedPlanExecutor::new_nested(statements.statement_execution_context(), params)
92        .execute(&UnifiedPlan::Command(Box::new(command.clone())))
93}
94
95#[cfg(test)]
96mod tests;