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::UnifiedPlan,
92 ) -> Result<uqa_sql::plan::UnifiedPlan, uqa_sql::SQLError> {
93 super::statement_planning::optimize_engine_plan(self, body.clone())
94 }
95
96 fn physical_explain_plan(
97 &self,
98 body: &uqa_sql::plan::UnifiedPlan,
99 params: &[uqa_sql::SQLParam],
100 costs: bool,
101 control: &uqa_core::memory::ProductionControl<'_>,
102 ) -> Result<uqa_core::memory::Produced<uqa_sql::result::ExplainPhysicalPlan>, uqa_sql::SQLError>
103 {
104 let catalog = self.catalog_read_view();
105 let resolution = self.session_execution_view().relation_name_resolution();
106 uqa_planner::explain::physical_plan_with_control(
107 uqa_planner::explain::PhysicalExplainContext {
108 retrieval: self,
109 statistics: self,
110 validate_source: &|source_resolution, name| {
111 uqa_execution::catalog::foreign::reference::validate_query_source(
112 &catalog,
113 source_resolution,
114 name,
115 )
116 },
117 filters: uqa_planner::filter_pushdown::context::FilterPushdownContext {
118 volatility: self,
119 correlation: uqa_sql::binding::correlation::CorrelationContext {
120 routines: self,
121 catalog: &catalog,
122 resolution: &resolution,
123 scoped_relations: None,
124 },
125 optimizer: &|plan| super::statement_planning::optimize_engine_plan(self, plan),
126 },
127 evaluate: &uqa_execution::eval_constant_scalar_with_control,
128 },
129 body,
130 params,
131 costs,
132 control,
133 )
134 }
135}
136impl context::StatementExecutionInputs<StatementReadSnapshot> for Engine {
137 fn io_context(&self) -> uqa_core::io_usage::IOContext {
138 self.runtime.diagnostics.io_context()
139 }
140
141 fn regex_cache(&self) -> Option<std::sync::Arc<uqa_sql::expr::RegexCache>> {
142 Some(self.session_regex_cache())
143 }
144 fn parser_settings(&self) -> uqa_sql::parser::ParserSettings {
145 Engine::parser_settings(self)
146 }
147
148 fn transaction_timestamp_micros(&self) -> Option<i64> {
149 self.session_transaction_clock_micros()
150 }
151
152 fn temporal_date_order(&self) -> Option<uqa_core::TemporalDateOrder> {
153 Some(uqa_sql::semantics::parameters::datestyle::date_order(
154 &self.session.setting("DateStyle"),
155 ))
156 }
157
158 fn temporal_interval_style(&self) -> Option<uqa_core::TemporalIntervalStyle> {
159 Some(self.session.interval_style())
160 }
161
162 fn diagnostic_search_path(&self) -> Option<Vec<String>> {
163 Some(self.session.state.read().search_path.clone())
164 }
165
166 fn notification_subscriptions_required(&self) -> bool {
167 Engine::notification_subscriptions_required(self)
168 }
169
170 fn statement_timeout(&self) -> Option<std::time::Duration> {
171 let milliseconds = self
172 .session
173 .setting("statement_timeout")
174 .parse::<u64>()
175 .ok()?;
176 (milliseconds != 0).then(|| std::time::Duration::from_millis(milliseconds))
177 }
178
179 fn statement_execution_context(&self) -> StatementExecutionContext<'_, StatementReadSnapshot> {
180 Engine::statement_execution_context(self)
181 }
182}
183
184impl StatementEffects for Engine {
185 fn query_effect_context(&self) -> uqa_sql::semantics::effects::QueryEffectContext<'_> {
186 Engine::query_effect_context(self)
187 }
188}
189mod queries;
190mod routines;
191mod schemas;
192mod session;
193
194impl Engine {
195 pub(crate) fn compiled_statement_context(
196 &self,
197 ) -> uqa_execution::statement::compiled::CompiledStatementContext<
198 '_,
199 crate::session::StatementReadSnapshot,
200 > {
201 uqa_execution::statement::compiled::CompiledStatementContext {
202 aggregates: self,
203 planning: self,
204 statements: self,
205 cancellation: &self.runtime.cancellation,
206 }
207 }
208}
209
210impl context::StatementDiagnostics for Engine {
211 fn diagnostics_control(
212 &self,
213 ) -> Result<uqa_storage::read_control::StorageReadControl, uqa_sql::SQLError> {
214 self.query_retention_control()
215 }
216
217 fn diagnostics_scope(
218 &self,
219 timing: Option<bool>,
220 io: uqa_core::io_usage::IOOptions,
221 ) -> Result<uqa_execution::query::diagnostics::DiagnosticsScope, uqa_sql::SQLError> {
222 self.runtime
223 .diagnostics
224 .enter_with_options(self.query_retention_control()?, timing, io)
225 .map_err(|error| {
226 uqa_execution::storage_errors::storage_error("retain EXPLAIN diagnostics", &error)
227 })
228 }
229}