Skip to main content

uqa_sql/plan/
optimization.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! SQL-plan contract for a caller-selected executable planner.
8
9use super::UnifiedPlan;
10use crate::{binding::statements::AnalyzedResult, SQLError, SQLParam};
11
12/// Analyze and optimize a logical plan using metadata captured for this call.
13pub trait ExecutablePlanOptimizer {
14    /// Reuse analysis only after the caller validates its catalog/scope in the selected snapshot. Optimization still runs for this invocation. The default retains full analysis for implementations without this capability.
15    fn plan_with_cached_analysis(
16        &self,
17        plan: UnifiedPlan,
18        params: &[SQLParam],
19        _cached: Option<std::sync::Arc<crate::binding::statements::AnalyzedStatement>>,
20    ) -> Result<
21        (
22            UnifiedPlan,
23            Option<std::sync::Arc<crate::binding::statements::AnalyzedStatement>>,
24        ),
25        SQLError,
26    > {
27        self.plan_for_execution(plan, params)
28            .map(|plan| (plan, None))
29    }
30
31    /// Return an executable and whether its analysis inputs permit reuse across
32    /// ordinary messages. Planners without that evidence must analyze again.
33    fn plan_for_statement_cache(
34        &self,
35        plan: UnifiedPlan,
36        params: &[SQLParam],
37    ) -> Result<(UnifiedPlan, bool), SQLError> {
38        self.plan_for_execution(plan, params)
39            .map(|plan| (plan, false))
40    }
41
42    fn plan_for_execution(
43        &self,
44        plan: UnifiedPlan,
45        params: &[SQLParam],
46    ) -> Result<UnifiedPlan, SQLError>;
47
48    /// Analyze and optimize a logical plan, and return what its analysis derives about its result.
49    fn plan_with_result(
50        &self,
51        plan: UnifiedPlan,
52        params: &[SQLParam],
53    ) -> Result<(UnifiedPlan, AnalyzedResult), SQLError>;
54}