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    /// The session's cancellation, which the statement's analysis, planning and execution observe.
23    pub cancellation: &'a uqa_core::CancellationToken,
24}
25
26pub fn execute<S: Clone + Send + Sync + 'static>(
27    context: &CompiledStatementContext<'_, S>,
28    statement: Statement,
29    params: &[SQLParam],
30) -> Result<SQLResult, SQLError> {
31    let plan = UnifiedPlan::lower_with(statement, context.aggregates);
32    execute_plan(context, plan, params)
33}
34
35pub fn execute_plan<S: Clone + Send + Sync + 'static>(
36    context: &CompiledStatementContext<'_, S>,
37    plan: UnifiedPlan,
38    params: &[SQLParam],
39) -> Result<SQLResult, SQLError> {
40    let _statement_thread =
41        super::context::statement_thread_scope(context.statements, context.cancellation);
42    let plan = context.planning.plan_for_execution(plan, params)?;
43    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
44        .execute(&plan)
45}
46
47/// A prepared owner has already analyzed and selected this executable. Capture
48/// fresh execution inputs without binding or optimizing it a second time.
49pub fn execute_selected_plan<S: Clone + Send + Sync + 'static>(
50    context: &CompiledStatementContext<'_, S>,
51    plan: &UnifiedPlan,
52    params: &[SQLParam],
53) -> Result<SQLResult, SQLError> {
54    let _statement_thread =
55        super::context::statement_thread_scope(context.statements, context.cancellation);
56    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
57        .execute(plan)
58}
59
60/// Run a statement once `check` accepts what its analysis derives about its result.
61pub fn execute_checked_plan<S: Clone + Send + Sync + 'static>(
62    context: &CompiledStatementContext<'_, S>,
63    plan: UnifiedPlan,
64    params: &[SQLParam],
65    check: &dyn Fn(&AnalyzedResult) -> Result<(), SQLError>,
66) -> Result<SQLResult, SQLError> {
67    let _statement_thread =
68        super::context::statement_thread_scope(context.statements, context.cancellation);
69    let (plan, result) = context.planning.plan_with_result(plan, params)?;
70    check(&result)?;
71    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
72        .execute(&plan)
73}
74
75pub fn execute_with_privilege_subject<S: Clone + Send + Sync + 'static>(
76    context: &CompiledStatementContext<'_, S>,
77    statement: Statement,
78    params: &[SQLParam],
79    privilege_subject: &RoleReference,
80) -> Result<SQLResult, SQLError> {
81    let _statement_thread =
82        super::context::statement_thread_scope(context.statements, context.cancellation);
83    let mut plan = UnifiedPlan::lower_with(statement, context.aggregates);
84    mark_catalog_statement_relations_bound(&mut plan)?;
85    let plan = context.planning.plan_for_execution(plan, params)?;
86    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
87        .with_privilege_subject(privilege_subject)
88        .execute(&plan)
89}
90
91pub fn execute_optimized_command<S: Clone + Send + Sync + 'static>(
92    context: &CompiledStatementContext<'_, S>,
93    command: &CommandPlan,
94    params: &[SQLParam],
95) -> Result<SQLResult, SQLError> {
96    let _statement_thread =
97        super::context::statement_thread_scope(context.statements, context.cancellation);
98    UnifiedPlanExecutor::new_nested(context.statements.statement_execution_context(), params)
99        .execute(&UnifiedPlan::Command(Box::new(command.clone())))
100}
101
102#[cfg(test)]
103mod tests;