1use crate::{capabilities::query_scope::CteScope, session::StatementReadSnapshot, Engine};
10use uqa_execution::query::{
11 cte::context::{CteBodyExecutor, CteExecutionContext, QueryOutputRewriter},
12 output::QueryOutput,
13 statement::consumer::QueryOutputMode,
14};
15use uqa_sql::{plan::QueryPlan, SQLError, SQLParam, ScalarExpr};
16
17impl Engine {
18 pub(crate) fn cte_execution_context(&self) -> CteExecutionContext<'_, StatementReadSnapshot> {
19 CteExecutionContext {
20 queries: self,
21 rewrites: self,
22 routines: self,
23 functions: self,
24 runtime: self.query_runtime_view(),
25 }
26 }
27}
28impl CteBodyExecutor<StatementReadSnapshot> for Engine {
29 fn read_control(&self) -> Result<uqa_storage::read_control::StorageReadControl, SQLError> {
30 self.query_retention_control()
31 }
32 fn retained_query(
33 &self,
34 query: &QueryPlan,
35 params: &[SQLParam],
36 ctes: &CteScope,
37 ) -> Result<Option<Box<dyn uqa_execution::PhysicalOperator>>, SQLError> {
38 self.retained_query_operator(query.clone(), params.to_vec(), ctes.clone())
39 }
40 fn execute_query(
41 &self,
42 query: &QueryPlan,
43 params: &[SQLParam],
44 ctes: &mut CteScope,
45 ) -> Result<QueryOutput, SQLError> {
46 uqa_execution::query::statement::execute_query_plan_output(
47 &self.query_execution_context(),
48 query,
49 params,
50 ctes,
51 QueryOutputMode::SharedSpill,
52 )
53 }
54 fn execute_lateral_query(
55 &self,
56 query: &QueryPlan,
57 outer: &uqa_execution::OwnedPhysicalRow,
58 params: &[SQLParam],
59 ctes: &CteScope,
60 ) -> Result<QueryOutput, SQLError> {
61 uqa_execution::query::sources::lateral_query::execute_lateral_subquery_output(
62 &self.source_execution_context(),
63 query,
64 outer,
65 params,
66 ctes,
67 )
68 }
69 fn execute_command(
70 &self,
71 command: &uqa_planner::CommandPlan,
72 params: &[SQLParam],
73 ctes: &CteScope,
74 ) -> Result<uqa_execution::mutation::output::MutationOutput, SQLError> {
75 uqa_execution::mutation::entry::execute_cte_command(
76 &self.mutation_entry_context(),
77 command,
78 params,
79 ctes,
80 )
81 }
82 fn fire_after_triggers(
83 &self,
84 queue: &uqa_execution::mutation::triggers::queue::AfterTriggerQueue,
85 ) -> Result<(), SQLError> {
86 queue.fire(&self.trigger_execution_context())
87 }
88}
89impl QueryOutputRewriter for Engine {
90 fn prune_query_outputs(
91 &self,
92 query: &mut QueryPlan,
93 aliases: &[String],
94 projection: &uqa_sql::plan::source_projection::SourceProjection,
95 schema: &uqa_sql::RowSchema,
96 visible_ctes: &dyn Fn(&str) -> bool,
97 ) -> Result<(), SQLError> {
98 uqa_planner::optimizer::prune_query_outputs(
99 query,
100 aliases,
101 &|name| projection.contains(name),
102 Some(schema),
103 uqa_planner::optimizer::OutputPruningContext {
104 aggregates: self,
105 routines: Some(self),
106 volatility: Some(self),
107 visible_ctes,
108 },
109 )
110 }
111
112 fn optimize_retained_query(&self, query: &QueryPlan) -> Result<QueryPlan, SQLError> {
113 super::statement_planning::optimize_engine_query(self, query)
114 }
115
116 fn push_output_filter(
117 &self,
118 query: &QueryPlan,
119 qualifier: &str,
120 filter: &ScalarExpr,
121 columns: Option<&[String]>,
122 ) -> Result<Option<QueryPlan>, SQLError> {
123 super::query_planning::push_output_filter_into_query_plan(
124 self, query, qualifier, filter, columns,
125 )
126 }
127
128 fn push_derived_output_filter(
129 &self,
130 query: &QueryPlan,
131 qualifier: &str,
132 filter: &ScalarExpr,
133 columns: Option<&[String]>,
134 ) -> Result<Option<QueryPlan>, SQLError> {
135 super::query_planning::push_derived_output_filter(self, query, qualifier, filter, columns)
136 }
137}