Skip to main content

uqa_engine/capabilities/
statements.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Compose statement contexts over the current Engine state without owning plan dispatch.
8
9use 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                aliases: self,
26            },
27            runtime: context::StatementRuntime {
28                cancellation: runtime.cancellation,
29                notices: runtime.notices,
30            },
31            queries: self,
32            mutations: self,
33            schemas: context::schemas::SchemaStatements {
34                creation: self,
35                removal: self,
36                tables_as: self,
37                views: self,
38                view_alteration: self,
39                foreign_alteration: self,
40                sequence_creation: self,
41                sequence_alteration: self,
42                owners: self,
43                renames: self,
44                inputs: self,
45            },
46            routines: context::routines::RoutineStatements {
47                resolution: self,
48                inputs: self,
49                transactions: self,
50            },
51            prepared: context::session::PreparedStatements {
52                state: self,
53                arguments: self,
54                definitions: self.prepared_registration_context(),
55                plans: self,
56            },
57            settings: self,
58            notifications: self,
59            controls: self,
60            portals: self.portal_execution_context(),
61            roles: self.role_execution_context(),
62            events: self.event_lifecycle_context(),
63            foreign: self,
64            table_privileges: self,
65            explain: uqa_planner::explain::run_explain_with_physical,
66            physical_explain: self,
67        }
68    }
69}
70
71impl context::PhysicalExplainPlanning for Engine {
72    fn physical_explain_plan(
73        &self,
74        body: &uqa_sql::plan::UnifiedPlan,
75        params: &[uqa_sql::SQLParam],
76    ) -> Result<uqa_sql::result::ExplainPhysicalPlan, uqa_sql::SQLError> {
77        let catalog = self.catalog_read_view();
78        let resolution = self.session_execution_view().relation_name_resolution();
79        uqa_planner::explain::physical_plan(
80            uqa_planner::explain::PhysicalExplainContext {
81                retrieval: self,
82                statistics: self,
83                validate_source: &|source_resolution, name| {
84                    uqa_execution::catalog::foreign::reference::validate_query_source(
85                        &catalog,
86                        source_resolution,
87                        name,
88                    )
89                },
90                filters: uqa_planner::filter_pushdown::context::FilterPushdownContext {
91                    volatility: self,
92                    correlation: uqa_sql::binding::correlation::CorrelationContext {
93                        catalog: &catalog,
94                        resolution: &resolution,
95                    },
96                    optimizer: &|plan| super::statement_planning::optimize_engine_plan(self, plan),
97                },
98                evaluate: &|expression, params| {
99                    uqa_execution::eval_scalar(
100                        expression,
101                        &uqa_execution::ScalarEvalContext::new(None, params),
102                    )
103                },
104            },
105            body,
106            params,
107        )
108    }
109}
110impl context::StatementExecutionInputs<StatementReadSnapshot> for Engine {
111    fn parser_settings(&self) -> uqa_sql::parser::ParserSettings {
112        Engine::parser_settings(self)
113    }
114
115    fn transaction_timestamp_micros(&self) -> Option<i64> {
116        Some(Engine::transaction_timestamp_micros(self))
117    }
118
119    fn temporal_date_order(&self) -> Option<uqa_core::TemporalDateOrder> {
120        Some(uqa_sql::semantics::parameters::datestyle::date_order(
121            &self.session.setting("DateStyle"),
122        ))
123    }
124
125    fn temporal_interval_style(&self) -> Option<uqa_core::TemporalIntervalStyle> {
126        Some(self.session.interval_style())
127    }
128
129    fn diagnostic_search_path(&self) -> Option<Vec<String>> {
130        Some(self.session.state.read().search_path.clone())
131    }
132
133    fn notification_subscriptions_required(&self) -> bool {
134        Engine::notification_subscriptions_required(self)
135    }
136
137    fn statement_timeout(&self) -> Option<std::time::Duration> {
138        let milliseconds = self
139            .session
140            .setting("statement_timeout")
141            .parse::<u64>()
142            .ok()?;
143        (milliseconds != 0).then(|| std::time::Duration::from_millis(milliseconds))
144    }
145
146    fn statement_execution_context(&self) -> StatementExecutionContext<'_, StatementReadSnapshot> {
147        Engine::statement_execution_context(self)
148    }
149}
150
151impl StatementEffects for Engine {
152    fn query_effect_context(&self) -> uqa_sql::semantics::effects::QueryEffectContext<'_> {
153        Engine::query_effect_context(self)
154    }
155}
156mod queries;
157mod routines;
158mod schemas;
159mod session;
160
161impl Engine {
162    pub(crate) fn compiled_statement_context(
163        &self,
164    ) -> uqa_execution::statement::compiled::CompiledStatementContext<
165        '_,
166        crate::session::StatementReadSnapshot,
167    > {
168        uqa_execution::statement::compiled::CompiledStatementContext {
169            aggregates: self,
170            planning: self,
171            statements: self,
172        }
173    }
174}
175
176impl context::StatementDiagnostics for Engine {
177    fn diagnostics_scope(
178        &self,
179    ) -> Result<uqa_execution::query::diagnostics::DiagnosticsScope, uqa_sql::SQLError> {
180        self.runtime
181            .diagnostics
182            .enter(self.query_retention_control()?)
183            .map_err(|error| {
184                uqa_execution::storage_errors::storage_error("retain EXPLAIN diagnostics", &error)
185            })
186    }
187}