1use crate::{session::StatementReadSnapshot, Engine};
10use uqa_execution::statement::context::{
11 self, StatementEffects, StatementExecutionContext, StatementValidationContext,
12};
13impl Engine {
14 pub(crate) fn statement_execution_context(
15 &self,
16 ) -> StatementExecutionContext<'_, StatementReadSnapshot> {
17 let runtime = self.query_runtime_view();
18 StatementExecutionContext {
19 diagnostics: self,
20 validation: StatementValidationContext {
21 session: self,
22 rules: self,
23 effects: self,
24 transactions: self,
25 },
26 runtime: context::StatementRuntime {
27 cancellation: runtime.cancellation,
28 notices: runtime.notices,
29 },
30 queries: self,
31 mutations: self,
32 schemas: context::schemas::SchemaStatements {
33 creation: self,
34 removal: self,
35 tables_as: self,
36 views: self,
37 view_alteration: self,
38 foreign_alteration: self,
39 sequence_creation: self,
40 sequence_alteration: self,
41 owners: self,
42 renames: self,
43 inputs: self,
44 },
45 routines: context::routines::RoutineStatements {
46 resolution: self,
47 inputs: self,
48 transactions: self,
49 },
50 prepared: context::session::PreparedStatements {
51 state: self,
52 arguments: self,
53 definitions: self.prepared_registration_context(),
54 plans: self,
55 },
56 settings: self,
57 notifications: self,
58 controls: self,
59 portals: self.portal_execution_context(),
60 roles: self.role_execution_context(),
61 events: self.event_lifecycle_context(),
62 foreign: self,
63 table_privileges: self,
64 explain: uqa_planner::explain::run_explain_with_control,
65 physical_explain: self,
66 }
67 }
68}
69
70impl context::PhysicalExplainPlanning for Engine {
71 fn explained_plan_estimates(
72 &self,
73 body: &uqa_sql::plan::UnifiedPlan,
74 params: &[uqa_sql::SQLParam],
75 control: &uqa_core::memory::ProductionControl<'_>,
76 ) -> Result<
77 uqa_core::memory::Produced<Vec<uqa_sql::result::explain::ExplainNodeEstimate>>,
78 uqa_sql::SQLError,
79 > {
80 uqa_planner::explain::estimate_plan(
81 self.statement_statistics_context(),
82 &self.statement_planning_context().analysis,
83 body,
84 params,
85 control,
86 )
87 }
88
89 fn plan_explained_statement(
90 &self,
91 body: uqa_sql::plan::PlanRef<'_>,
92 ) -> Result<uqa_sql::plan::OwnedExecutablePlan, uqa_sql::SQLError> {
93 let resources =
94 uqa_execution::query::relational::QueryExpressionFactory::production_resources(self)?;
95 let control = resources.control();
96 uqa_planner::statement_planning::executable::optimize_owned_with_control(
97 &self.statement_planning_context(),
98 body.copy_for_rewrite(&control)?,
99 &control,
100 )
101 }
102
103 fn physical_explain_plan(
104 &self,
105 body: &uqa_sql::plan::UnifiedPlan,
106 params: &[uqa_sql::SQLParam],
107 costs: bool,
108 control: &uqa_core::memory::ProductionControl<'_>,
109 ) -> Result<uqa_core::memory::Produced<uqa_sql::result::ExplainPhysicalPlan>, uqa_sql::SQLError>
110 {
111 let catalog = self.catalog_read_view();
112 let resolution = uqa_execution::catalog::services::CatalogSession::relation_name_resolution_with_control(self, control)?;
113 uqa_planner::explain::physical_plan_with_control(
114 uqa_planner::explain::PhysicalExplainContext {
115 retrieval: self,
116 statistics: self,
117 validate_source: &|source_resolution, name| {
118 uqa_execution::catalog::foreign::reference::validate_query_source(
119 &catalog,
120 source_resolution,
121 name,
122 )
123 },
124 filters: uqa_planner::filter_pushdown::context::FilterPushdownContext {
125 volatility: self,
126 correlation: uqa_sql::binding::correlation::CorrelationContext {
127 routines: self,
128 catalog: &catalog,
129 resolution: &resolution,
130 scoped_relations: None,
131 },
132 optimizer: &|plan, control| {
133 uqa_planner::statement_planning::executable::optimize_retained_query(
134 &self.statement_planning_context(),
135 plan,
136 control,
137 )
138 },
139 },
140 evaluate: &uqa_execution::eval_constant_scalar_with_control,
141 },
142 body,
143 params,
144 costs,
145 control,
146 )
147 }
148}
149impl context::StatementExecutionInputs<StatementReadSnapshot> for Engine {
150 fn io_context(&self) -> uqa_core::io_usage::IOContext {
151 self.runtime.diagnostics.io_context()
152 }
153
154 fn regex_cache(&self) -> Option<std::sync::Arc<uqa_sql::expr::RegexCache>> {
155 Some(self.session_regex_cache())
156 }
157 fn parser_settings(&self) -> uqa_sql::parser::ParserSettings {
158 Engine::parser_settings(self)
159 }
160
161 fn transaction_timestamp_micros(&self) -> Option<i64> {
162 self.session_transaction_clock_micros()
163 }
164
165 fn temporal_date_order(&self) -> Option<uqa_core::TemporalDateOrder> {
166 Some(uqa_sql::semantics::parameters::datestyle::date_order(
167 &self.session.setting("DateStyle"),
168 ))
169 }
170
171 fn temporal_interval_style(&self) -> Option<uqa_core::TemporalIntervalStyle> {
172 Some(self.session.interval_style())
173 }
174
175 fn diagnostic_search_path(&self) -> Option<Vec<String>> {
176 Some(self.session.state.read().search_path.clone())
177 }
178
179 fn notification_subscriptions_required(&self) -> bool {
180 Engine::notification_subscriptions_required(self)
181 }
182
183 fn statement_timeout(&self) -> Option<std::time::Duration> {
184 let milliseconds = self
185 .session
186 .setting("statement_timeout")
187 .parse::<u64>()
188 .ok()?;
189 (milliseconds != 0).then(|| std::time::Duration::from_millis(milliseconds))
190 }
191
192 fn statement_execution_context(&self) -> StatementExecutionContext<'_, StatementReadSnapshot> {
193 Engine::statement_execution_context(self)
194 }
195}
196
197impl StatementEffects for Engine {
198 fn query_effect_context(&self) -> uqa_sql::semantics::effects::QueryEffectContext<'_> {
199 Engine::query_effect_context(self)
200 }
201}
202mod queries;
203mod routines;
204mod schemas;
205mod session;
206
207impl Engine {
208 pub(crate) fn compiled_statement_context(
209 &self,
210 ) -> uqa_execution::statement::compiled::CompiledStatementContext<
211 '_,
212 crate::session::StatementReadSnapshot,
213 > {
214 uqa_execution::statement::compiled::CompiledStatementContext {
215 aggregates: self,
216 planning: self,
217 statements: self,
218 cancellation: &self.runtime.cancellation,
219 }
220 }
221}
222
223impl context::StatementDiagnostics for Engine {
224 fn diagnostics_control(
225 &self,
226 ) -> Result<uqa_storage::read_control::StorageReadControl, uqa_sql::SQLError> {
227 self.query_retention_control()
228 }
229
230 fn diagnostics_scope(
231 &self,
232 timing: Option<bool>,
233 io: uqa_core::io_usage::IOOptions,
234 ) -> Result<uqa_execution::query::diagnostics::DiagnosticsScope, uqa_sql::SQLError> {
235 self.runtime
236 .diagnostics
237 .enter_with_options(self.query_retention_control()?, timing, io)
238 .map_err(|error| {
239 uqa_execution::storage_errors::storage_error("retain EXPLAIN diagnostics", &error)
240 })
241 }
242}