uqa_engine/capabilities/
prepared.rs1use crate::{capabilities::ScopedEngineHook, Engine};
10use uqa_execution::query::prepared::{
11 ArgumentBindingContext, PreparedArgumentScopes, ScopedArgumentOperation,
12};
13use uqa_execution::scalar::plan::PhysicalEvalContext;
14use uqa_sql::{
15 plan::{ExpressionPlan, UnifiedPlan},
16 prepared::arguments::ArgumentValidationContext,
17 SQLError, SQLParam,
18};
19
20impl Engine {
21 pub(crate) fn note_prepared_relation_change(&self, relation: &uqa_core::RelationIdentity) {
22 self.mutation_coordinator()
23 .note_prepared_relation_change(relation);
24 }
25
26 pub(crate) fn note_prepared_table_change(&self, table: &crate::TableState) {
27 self.mutation_coordinator()
28 .note_prepared_table_change(table);
29 }
30
31 pub(crate) fn note_prepared_catalog_change(
32 &self,
33 change: uqa_execution::statement::prepared::invalidation::PreparedCatalogChange,
34 ) {
35 self.mutation_coordinator()
36 .note_prepared_catalog_change(change);
37 }
38
39 pub(crate) fn prepared_definition_context(
40 &self,
41 ) -> uqa_sql::prepared::definition::PreparedDefinitionContext<'_> {
42 uqa_sql::prepared::definition::PreparedDefinitionContext {
43 types: self,
44 routines: self,
45 scopes: self,
46 aliases: self,
47 }
48 }
49
50 pub(crate) fn prepared_registration_context(
51 &self,
52 ) -> uqa_execution::statement::prepared::PreparedRegistrationContext<'_> {
53 uqa_execution::statement::prepared::PreparedRegistrationContext {
54 analysis: self.prepared_definition_context(),
55 aggregates: self,
56 registry: self,
57 transaction_timestamp_micros: self.session_transaction_clock_micros(),
58 interval_style: Some(self.session.interval_style()),
59 date_order: Some(uqa_sql::semantics::parameters::datestyle::date_order(
60 &self.session.setting("DateStyle"),
61 )),
62 cancellation: &self.runtime.cancellation,
63 clock: uqa_sql::expr::clock_timestamp_micros,
64 }
65 }
66}
67
68impl PreparedArgumentScopes for Engine {
69 fn with_scope(
70 &self,
71 parameters: &[SQLParam],
72 operation: &mut ScopedArgumentOperation<'_>,
73 ) -> Result<Vec<SQLParam>, SQLError> {
74 let scope = super::query_scope::new_for_current_routine(self);
75 let hook = ScopedEngineHook::new(self, &scope);
76 let evaluation = PhysicalEvalContext::new(None, parameters)
77 .with_function_hook(&hook)
78 .with_subquery_runner(&hook);
79 let cast_type = |name: &str| {
80 uqa_execution::catalog::projection::resolve_catalog_column_type(
81 &self.catalog_execution(),
82 name,
83 )
84 };
85 let mut analyze_type = |argument: &ExpressionPlan| {
86 uqa_execution::query::binding::analyze_expression_plan_type(
87 self, argument, parameters, &scope,
88 )
89 };
90 operation(ArgumentBindingContext {
91 validation: ArgumentValidationContext {
92 aggregates: self,
93 volatility: self,
94 cast_type: &cast_type,
95 routines: self,
96 parameters,
97 },
98 assignment: self,
99 analyze_type: &mut analyze_type,
100 evaluation,
101 })
102 }
103}
104
105struct PreparedDefinitionWrite<'a>(
106 parking_lot::RwLockWriteGuard<
107 'a,
108 std::collections::BTreeMap<String, uqa_sql::prepared::entry::PreparedStatementPlan>,
109 >,
110);
111
112impl uqa_execution::statement::prepared::PreparedDefinitionWrite for PreparedDefinitionWrite<'_> {
113 fn insert(
114 &mut self,
115 name: String,
116 definition: uqa_sql::prepared::entry::PreparedStatementPlan,
117 ) {
118 self.0.insert(name, definition);
119 }
120}
121
122impl uqa_execution::statement::prepared::PreparedDefinitionRegistry for Engine {
123 fn write_definitions(
124 &self,
125 ) -> Box<dyn uqa_execution::statement::prepared::PreparedDefinitionWrite + '_> {
126 Box::new(PreparedDefinitionWrite(self.session.prepared.write()))
127 }
128}
129
130impl uqa_planner::statement_planning::prepared::selection::PreparedPlanSession for Engine {
131 fn prepared_entry(
132 &self,
133 name: &str,
134 ) -> Option<uqa_sql::prepared::entry::PreparedStatementPlan> {
135 self.session.prepared.read().get(name).cloned()
136 }
137 fn plan_cache_mode(&self) -> Result<String, SQLError> {
138 self.show_variable("plan_cache_mode")
139 }
140 fn publish_usage(
141 &self,
142 name: &str,
143 logical_plan: &std::sync::Arc<UnifiedPlan>,
144 update: uqa_sql::prepared::planning::PreparedPlanUpdate,
145 ) {
146 if let Some(current) = self.session.prepared.write().get_mut(name) {
147 if std::sync::Arc::ptr_eq(¤t.logical_plan, logical_plan) {
148 current.record_execution(update);
149 }
150 }
151 }
152}
153
154impl uqa_planner::statement_planning::prepared::selection::PreparedPlanOptimization for Engine {
155 fn optimize_plan(&self, plan: UnifiedPlan) -> Result<UnifiedPlan, SQLError> {
156 super::statement_planning::optimize_engine_plan(self, plan)
157 }
158 fn estimate_plan(
159 &self,
160 plan: &UnifiedPlan,
161 ) -> Result<uqa_planner::plan_cost::PlanCost, SQLError> {
162 super::statement_planning::estimate_engine_plan(self, plan)
163 }
164}
165
166impl uqa_sql::prepared::planning::PreparedPlanProvider for Engine {
167 fn plan_for_execution(
168 &self,
169 name: &str,
170 parameters: &[SQLParam],
171 ) -> Result<Option<UnifiedPlan>, SQLError> {
172 uqa_planner::statement_planning::prepared::selection::select_plan(
173 &uqa_planner::statement_planning::prepared::selection::PreparedPlanningContext {
174 session: self,
175 analysis: self.prepared_definition_context(),
176 optimization: self,
177 },
178 name,
179 parameters,
180 )
181 }
182}