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 execute_query(
30        &self,
31        query: &QueryPlan,
32        params: &[SQLParam],
33        ctes: &mut CteScope,
34    ) -> Result<QueryOutput, SQLError> {
35        uqa_execution::query::statement::execute_query_plan_output(
36            &self.query_execution_context(),
37            query,
38            params,
39            ctes,
40            QueryOutputMode::SharedSpill,
41        )
42    }
43    fn execute_lateral_query(
44        &self,
45        query: &QueryPlan,
46        outer: &uqa_execution::OwnedPhysicalRow,
47        params: &[SQLParam],
48        ctes: &CteScope,
49    ) -> Result<QueryOutput, SQLError> {
50        uqa_execution::query::sources::lateral_query::execute_lateral_subquery_output(
51            &self.source_execution_context(),
52            query,
53            outer,
54            params,
55            ctes,
56        )
57    }
58    fn execute_command(
59        &self,
60        command: &uqa_planner::CommandPlan,
61        params: &[SQLParam],
62        ctes: &CteScope,
63    ) -> Result<uqa_execution::mutation::output::MutationOutput, SQLError> {
64        uqa_execution::mutation::entry::execute_cte_command(
65            &self.mutation_entry_context(),
66            command,
67            params,
68            ctes,
69        )
70    }
71    fn fire_after_triggers(
72        &self,
73        queue: &uqa_execution::mutation::triggers::queue::AfterTriggerQueue,
74    ) -> Result<(), SQLError> {
75        queue.fire(&self.trigger_execution_context())
76    }
77}
78impl QueryOutputRewriter for Engine {
79    fn prune_query_outputs(
80        &self,
81        query: &mut QueryPlan,
82        aliases: &[String],
83        projection: &uqa_sql::plan::source_projection::SourceProjection,
84        schema: &uqa_sql::RowSchema,
85    ) {
86        uqa_planner::optimizer::prune_query_outputs(
87            query,
88            aliases,
89            &|name| projection.contains(name),
90            Some(schema),
91            self,
92        );
93    }
94
95    fn optimize_retained_query(&self, query: &QueryPlan) -> Result<QueryPlan, SQLError> {
96        super::statement_planning::optimize_engine_query(self, query)
97    }
98
99    fn push_output_filter(
100        &self,
101        query: &QueryPlan,
102        qualifier: &str,
103        filter: &ScalarExpr,
104        columns: Option<&[String]>,
105    ) -> Result<Option<QueryPlan>, SQLError> {
106        super::query_planning::push_output_filter_into_query_plan(
107            self, query, qualifier, filter, columns,
108        )
109    }
110}