Skip to main content

uqa_engine/capabilities/
cte.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind child CTE execution to the active session and query scopes.
8
9use 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, params, ctes)
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 optimize_retained_query(
91        &self,
92        query: uqa_sql::plan::MutableQueryPlan,
93        control: &uqa_core::memory::ProductionControl<'_>,
94    ) -> Result<uqa_sql::plan::OwnedQueryPlan, SQLError> {
95        uqa_planner::statement_planning::executable::optimize_retained_query(
96            &self.statement_planning_context(),
97            query,
98            control,
99        )
100    }
101
102    fn prune_owned_query_outputs(
103        &self,
104        query: uqa_sql::plan::MutableQueryPlan,
105        aliases: &[String],
106        projection: &uqa_sql::plan::source_projection::SourceProjection,
107        schema: &uqa_sql::RowSchema,
108        visible_ctes: &dyn Fn(&str) -> bool,
109        control: &uqa_core::memory::ProductionControl<'_>,
110    ) -> Result<uqa_sql::plan::MutableQueryPlan, SQLError> {
111        uqa_planner::optimizer::prune_owned_query_outputs(
112            query,
113            aliases,
114            &|name| projection.contains(name),
115            Some(schema),
116            uqa_planner::optimizer::OutputPruningContext {
117                production: *control,
118                aggregates: self,
119                routines: Some(self),
120                volatility: Some(self),
121                visible_ctes,
122            },
123        )
124    }
125
126    fn push_output_filter(
127        &self,
128        query: &QueryPlan,
129        qualifier: &str,
130        filter: &ScalarExpr,
131        columns: Option<&[String]>,
132    ) -> Result<Option<uqa_sql::plan::OwnedQueryPlan>, SQLError> {
133        super::query_planning::push_output_filter_into_query_plan(
134            self, query, qualifier, filter, columns,
135        )
136    }
137
138    fn push_derived_output_filter(
139        &self,
140        query: &QueryPlan,
141        qualifier: &str,
142        filter: &ScalarExpr,
143        columns: Option<&[String]>,
144    ) -> Result<Option<uqa_sql::plan::OwnedQueryPlan>, SQLError> {
145        super::query_planning::push_derived_output_filter(self, query, qualifier, filter, columns)
146    }
147}