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: Some(self.transaction_timestamp_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 clock: uqa_sql::expr::clock_timestamp_micros,
63 }
64 }
65}
66
67impl PreparedArgumentScopes for Engine {
68 fn with_scope(
69 &self,
70 parameters: &[SQLParam],
71 operation: &mut ScopedArgumentOperation<'_>,
72 ) -> Result<Vec<SQLParam>, SQLError> {
73 let scope = super::query_scope::new_for_current_routine(self);
74 let hook = ScopedEngineHook::new(self, &scope);
75 let evaluation = PhysicalEvalContext::new(None, parameters)
76 .with_function_hook(&hook)
77 .with_subquery_runner(&hook);
78 let cast_type = |name: &str| {
79 uqa_execution::catalog::projection::resolve_catalog_column_type(
80 &self.catalog_execution(),
81 name,
82 )
83 };
84 let mut analyze_type = |argument: &ExpressionPlan| {
85 uqa_execution::query::binding::analyze_expression_plan_type(
86 self, argument, parameters, &scope,
87 )
88 };
89 operation(ArgumentBindingContext {
90 validation: ArgumentValidationContext {
91 aggregates: self,
92 volatility: self,
93 cast_type: &cast_type,
94 },
95 assignment: self,
96 analyze_type: &mut analyze_type,
97 evaluation,
98 })
99 }
100}
101
102struct PreparedDefinitionWrite<'a>(
103 parking_lot::RwLockWriteGuard<
104 'a,
105 std::collections::BTreeMap<String, uqa_sql::prepared::entry::PreparedStatementPlan>,
106 >,
107);
108
109impl uqa_execution::statement::prepared::PreparedDefinitionWrite for PreparedDefinitionWrite<'_> {
110 fn insert(
111 &mut self,
112 name: String,
113 definition: uqa_sql::prepared::entry::PreparedStatementPlan,
114 ) {
115 self.0.insert(name, definition);
116 }
117}
118
119impl uqa_execution::statement::prepared::PreparedDefinitionRegistry for Engine {
120 fn write_definitions(
121 &self,
122 ) -> Box<dyn uqa_execution::statement::prepared::PreparedDefinitionWrite + '_> {
123 Box::new(PreparedDefinitionWrite(self.session.prepared.write()))
124 }
125}
126
127impl uqa_planner::statement_planning::prepared::selection::PreparedPlanSession for Engine {
128 fn prepared_entry(
129 &self,
130 name: &str,
131 ) -> Option<uqa_sql::prepared::entry::PreparedStatementPlan> {
132 self.session.prepared.read().get(name).cloned()
133 }
134 fn plan_cache_mode(&self) -> Result<String, SQLError> {
135 self.show_variable("plan_cache_mode")
136 }
137 fn publish_usage(
138 &self,
139 name: &str,
140 logical_plan: &std::sync::Arc<UnifiedPlan>,
141 update: uqa_sql::prepared::planning::PreparedPlanUpdate,
142 ) {
143 if let Some(current) = self.session.prepared.write().get_mut(name) {
144 if std::sync::Arc::ptr_eq(¤t.logical_plan, logical_plan) {
145 current.record_execution(update);
146 }
147 }
148 }
149}
150
151impl uqa_planner::statement_planning::prepared::selection::PreparedPlanOptimization for Engine {
152 fn optimize_plan(&self, plan: UnifiedPlan) -> Result<UnifiedPlan, SQLError> {
153 super::statement_planning::optimize_engine_plan(self, plan)
154 }
155 fn estimate_plan(
156 &self,
157 plan: &UnifiedPlan,
158 ) -> Result<uqa_planner::plan_cost::PlanCost, SQLError> {
159 super::statement_planning::estimate_engine_plan(self, plan)
160 }
161}
162
163impl uqa_sql::prepared::planning::PreparedPlanProvider for Engine {
164 fn plan_for_execution(
165 &self,
166 name: &str,
167 parameters: &[SQLParam],
168 ) -> Result<Option<UnifiedPlan>, SQLError> {
169 uqa_planner::statement_planning::prepared::selection::select_plan(
170 &uqa_planner::statement_planning::prepared::selection::PreparedPlanningContext {
171 session: self,
172 analysis: self.prepared_definition_context(),
173 optimization: self,
174 },
175 name,
176 parameters,
177 )
178 }
179}