uqa_sql/binding/statements/
procedural.rs1mod owned;
10pub use owned::{analyze_procedural_plan_owned, OwnedProceduralPlanAnalysis};
11
12use super::{AnalyzedResult, BindingContext, StatementAnalysisContext};
13use crate::{
14 catalog::resolution::EffectiveSearchPath,
15 plan::{CommandPlan, UnifiedPlan},
16 prepared::dependencies::{PreparedAnalysisDependencies, PreparedDependencySnapshot},
17 routines::{declaration::RoutineTypeCatalog, resolution::RoutineOverloadContext},
18 SQLError, SQLParam,
19};
20
21#[derive(Debug)]
22pub struct ProceduralPlanAnalysis {
23 pub composite_inputs: crate::prepared::composites::CompositeInputs,
24 pub result: AnalyzedResult,
25 pub dependencies: PreparedAnalysisDependencies,
26 pub effective_search_path: Option<EffectiveSearchPath>,
27 pub dependency_snapshot: Option<PreparedDependencySnapshot>,
28}
29
30impl ProceduralPlanAnalysis {
31 pub fn prepared_definition(
33 &self,
34 plan: &UnifiedPlan,
35 params: &[SQLParam],
36 ) -> Result<crate::prepared::definition::PreparedDefinition, SQLError> {
37 let parameter_types = (1..=params.len())
38 .map(|index| {
39 crate::type_resolution::scalar_type(
40 &crate::ScalarExpr::Param(index),
41 &crate::RowSchema::new(Vec::new()),
42 params,
43 )
44 })
45 .collect::<Result<Vec<_>, _>>()?;
46 let result_schema = match &self.result {
47 AnalyzedResult::Command => None,
48 AnalyzedResult::Schema(schema) => Some(schema.clone()),
49 AnalyzedResult::Rows(types) => Some(crate::RowSchema::with_types(
50 vec![String::new(); types.len()],
51 types.clone(),
52 )),
53 };
54 Ok(crate::prepared::definition::PreparedDefinition {
55 composite_inputs: self.composite_inputs.clone(),
56 logical_plan: plan.clone(),
57 parameter_types: parameter_types.into(),
58 result_schema,
59 effective_search_path: self.effective_search_path.clone().into(),
60 dependencies: self.dependencies.clone().into(),
61 dependency_snapshot: self.dependency_snapshot.clone().into(),
62 })
63 }
64}
65
66pub fn analyze_procedural_plan(
69 context: &StatementAnalysisContext<'_>,
70 overloads: &RoutineOverloadContext<'_>,
71 types: &dyn RoutineTypeCatalog,
72 plan: &mut UnifiedPlan,
73 params: &[SQLParam],
74) -> Result<ProceduralPlanAnalysis, SQLError> {
75 let mut analyzed = None;
76 context.scopes.with_scope(&mut |scope| {
77 let binding = scope.binding_context()?;
78 let (result, dependencies) =
79 analyze_inputs(context, overloads, types, plan, params, &binding)?;
80 crate::routines::compilation::bind_analyzed_sql_body_types(types, plan)?;
81 super::super::composite_inputs::retain_composite_inputs(
82 context.routines,
83 plan,
84 params,
85 &binding,
86 )?;
87 let composite_inputs =
88 crate::prepared::composites::CompositeInputs::capture(plan, context.routines)?;
89 let effective_search_path = binding.catalog.effective_search_path(&binding.resolution)?;
90 let dependency_snapshot = binding
91 .catalog
92 .prepared_dependency_snapshot(&dependencies)?;
93 analyzed = Some(ProceduralPlanAnalysis {
94 composite_inputs,
95 result,
96 dependencies,
97 effective_search_path,
98 dependency_snapshot,
99 });
100 Ok(())
101 })?;
102 analyzed.ok_or_else(|| SQLError::Internal("procedural analysis scope did not run".into()))
103}
104
105fn analyze_inputs(
106 context: &StatementAnalysisContext<'_>,
107 overloads: &RoutineOverloadContext<'_>,
108 types: &dyn RoutineTypeCatalog,
109 plan: &mut UnifiedPlan,
110 params: &[SQLParam],
111 binding: &BindingContext<'_>,
112) -> Result<(AnalyzedResult, PreparedAnalysisDependencies), SQLError> {
113 if let UnifiedPlan::Command(command) = plan {
114 match command.as_mut() {
115 CommandPlan::Call { .. } => {
116 return crate::binding::preparation::prepare_procedure_call(
117 context, overloads, types, plan, params, binding,
118 );
119 }
120 CommandPlan::Explain { body, .. } => {
121 let (_, dependencies) =
122 analyze_inputs(context, overloads, types, body, params, binding)?;
123 return Ok((AnalyzedResult::Command, dependencies));
124 }
125 _ => {}
126 }
127 }
128 let analysis = crate::binding::preparation::read_procedural_inputs(
129 context.routines,
130 plan,
131 params,
132 binding,
133 context.aliases,
134 )?;
135 Ok((analysis.result, analysis.dependencies))
136}