Skip to main content

uqa_sql/plan/
visit.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Read-only traversal of every scalar expression a plan owns: the counterpart of the plan rewrite for analyses that check a plan without changing it.
8
9use super::{
10    CommandPlan, CtePlan, CtePlanBody, QueryPlan, RelationalPlan, SourcePlan, UnifiedPlan,
11};
12use crate::ir::ScalarExpr;
13
14impl UnifiedPlan {
15    /// Visit every scalar expression this plan owns, each root once, including those of its CTEs, relational sources, scalar subqueries and command inputs. The nested nodes of a root are reached through [`ScalarExpr::visit`].
16    pub fn visit_scalar_expressions(&self, visit: &mut dyn FnMut(&ScalarExpr)) {
17        match self {
18            Self::Query(query) => query.visit_scalar_expressions(visit),
19            Self::Command(command) => command.visit_scalar_expressions(visit),
20        }
21    }
22}
23
24fn visit_ctes(ctes: &[CtePlan], visit: &mut dyn FnMut(&ScalarExpr)) {
25    for cte in ctes {
26        match &cte.body {
27            CtePlanBody::Query(query) => query.visit_scalar_expressions(visit),
28            CtePlanBody::Command(command) => command.visit_scalar_expressions(visit),
29        }
30        if let Some(cycle) = &cte.cycle {
31            visit(&cycle.mark_value);
32            visit(&cycle.mark_default);
33        }
34    }
35}
36
37impl QueryPlan {
38    /// Visit every scalar expression this query owns, each root once, including those of its CTEs, relational sources and scalar subqueries.
39    pub fn visit_scalar_expressions(&self, visit: &mut dyn FnMut(&ScalarExpr)) {
40        visit_ctes(&self.ctes, visit);
41        match &self.root {
42            RelationalPlan::QueryBlock(block) => {
43                for rewrite in &block.deferred_outer_rewrites {
44                    rewrite.visit_scalar_expressions(visit);
45                }
46                if let Some(source) = &block.from {
47                    source.visit_scalar_expressions(visit);
48                }
49                for expression in block
50                    .r#where
51                    .iter()
52                    .chain(block.projections.iter().map(|projection| &projection.expr))
53                    .chain(&block.group_by)
54                    .chain(block.grouping_sets.iter().flatten())
55                    .chain(block.having.iter())
56                    .chain(block.order_by.iter().map(|order| &order.expr))
57                    .chain(block.limit.iter())
58                    .chain(block.offset.iter())
59                    .chain(&block.distinct_on)
60                {
61                    visit(expression);
62                }
63                for window in &block.windows {
64                    for expression in window.spec.expressions() {
65                        visit(expression);
66                    }
67                }
68                for subquery in &block.subqueries {
69                    subquery.visit_scalar_expressions(visit);
70                }
71            }
72            RelationalPlan::SetOp {
73                left,
74                right,
75                order_by,
76                limit,
77                offset,
78                subqueries,
79                ..
80            } => {
81                left.visit_scalar_expressions(visit);
82                right.visit_scalar_expressions(visit);
83                for expression in order_by
84                    .iter()
85                    .map(|order| &order.expr)
86                    .chain(limit.as_deref())
87                    .chain(offset.as_deref())
88                {
89                    visit(expression);
90                }
91                for subquery in subqueries {
92                    subquery.visit_scalar_expressions(visit);
93                }
94            }
95            RelationalPlan::Values { rows, subqueries } => {
96                for expression in rows.iter().flatten() {
97                    visit(expression);
98                }
99                for subquery in subqueries {
100                    subquery.visit_scalar_expressions(visit);
101                }
102            }
103        }
104    }
105}
106
107impl SourcePlan {
108    /// Visit every scalar expression this source owns, each root once: join conditions, `VALUES` rows, function arguments and the expressions of the query bodies nested in derived tables.
109    pub fn visit_scalar_expressions(&self, visit: &mut dyn FnMut(&ScalarExpr)) {
110        match self {
111            crate::plan::SourcePlan::RetainedRows(_) => {}
112            Self::Table { .. } => {}
113            Self::Subquery { body, .. } => body.visit_scalar_expressions(visit),
114            Self::Join {
115                left, right, on, ..
116            } => {
117                left.visit_scalar_expressions(visit);
118                right.visit_scalar_expressions(visit);
119                if let Some(on) = on {
120                    visit(on);
121                }
122            }
123            Self::Values { rows, .. } => {
124                for expression in rows.iter().flatten() {
125                    visit(expression);
126                }
127            }
128            Self::Function { args, .. } => {
129                for argument in args {
130                    visit(argument);
131                }
132            }
133            Self::FunctionGroup { functions, .. } => {
134                for argument in functions.iter().flat_map(|function| &function.args) {
135                    visit(argument);
136                }
137            }
138        }
139    }
140}
141
142mod command;