Skip to main content

uqa_sql/plan/memory/
query.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use uqa_core::memory::{AllocationObserver, ObserveAllocations};
8use uqa_core::ValueRetentionError;
9
10use crate::plan::{
11    CteCyclePlan, CtePlan, CtePlanBody, CteSearchPlan, ExpressionPlan, OrderPlan, ProjectionPlan,
12    QueryBlockPlan, QueryPlan, RelationalPlan, UnifiedPlan,
13};
14
15impl ObserveAllocations for UnifiedPlan {
16    fn observe_allocations<'a>(
17        &'a self,
18        observer: &mut AllocationObserver<'a, '_>,
19    ) -> Result<(), ValueRetentionError> {
20        match self {
21            Self::Query(value0) => {
22                observer.child(value0)?;
23            }
24            Self::Command(value0) => {
25                observer.child(value0)?;
26            }
27        }
28        Ok(())
29    }
30}
31
32impl ObserveAllocations for QueryPlan {
33    fn observe_allocations<'a>(
34        &'a self,
35        observer: &mut AllocationObserver<'a, '_>,
36    ) -> Result<(), ValueRetentionError> {
37        let Self {
38            analyzed_output: _,
39            analyzed_source: _,
40            analyzed_grouping,
41            relations_bound: _,
42            relation_privilege_subject,
43            ctes,
44            root,
45        } = self;
46        observer.child(analyzed_grouping)?;
47        observer.child(relation_privilege_subject)?;
48        observer.child(ctes)?;
49        observer.child(root)?;
50        Ok(())
51    }
52}
53
54impl ObserveAllocations for CtePlan {
55    fn observe_allocations<'a>(
56        &'a self,
57        observer: &mut AllocationObserver<'a, '_>,
58    ) -> Result<(), ValueRetentionError> {
59        let Self {
60            name,
61            columns,
62            recursive: _,
63            materialization: _,
64            search,
65            cycle,
66            body,
67        } = self;
68        observer.child(name)?;
69        observer.child(columns)?;
70        observer.child(search)?;
71        observer.child(cycle)?;
72        observer.child(body)?;
73        Ok(())
74    }
75}
76
77impl ObserveAllocations for CtePlanBody {
78    fn observe_allocations<'a>(
79        &'a self,
80        observer: &mut AllocationObserver<'a, '_>,
81    ) -> Result<(), ValueRetentionError> {
82        match self {
83            Self::Query(value0) => {
84                observer.child(value0)?;
85            }
86            Self::Command(value0) => {
87                observer.child(value0)?;
88            }
89        }
90        Ok(())
91    }
92}
93
94impl ObserveAllocations for CteSearchPlan {
95    fn observe_allocations<'a>(
96        &'a self,
97        observer: &mut AllocationObserver<'a, '_>,
98    ) -> Result<(), ValueRetentionError> {
99        let Self {
100            columns,
101            breadth_first: _,
102            sequence_column,
103        } = self;
104        observer.child(columns)?;
105        observer.child(sequence_column)?;
106        Ok(())
107    }
108}
109
110impl ObserveAllocations for CteCyclePlan {
111    fn observe_allocations<'a>(
112        &'a self,
113        observer: &mut AllocationObserver<'a, '_>,
114    ) -> Result<(), ValueRetentionError> {
115        let Self {
116            columns,
117            mark_column,
118            mark_value,
119            mark_default,
120            path_column,
121        } = self;
122        observer.child(columns)?;
123        observer.child(mark_column)?;
124        observer.child(mark_value)?;
125        observer.child(mark_default)?;
126        observer.child(path_column)?;
127        Ok(())
128    }
129}
130
131impl ObserveAllocations for RelationalPlan {
132    fn observe_allocations<'a>(
133        &'a self,
134        observer: &mut AllocationObserver<'a, '_>,
135    ) -> Result<(), ValueRetentionError> {
136        match self {
137            Self::QueryBlock(value0) => {
138                observer.child(value0)?;
139            }
140            Self::SetOp {
141                kind: _,
142                all: _,
143                left,
144                right,
145                order_by,
146                limit,
147                with_ties: _,
148                offset,
149                subqueries,
150            } => {
151                observer.child(left)?;
152                observer.child(right)?;
153                observer.child(order_by)?;
154                observer.child(limit)?;
155                observer.child(offset)?;
156                observer.child(subqueries)?;
157            }
158            Self::Values { rows, subqueries } => {
159                observer.child(rows)?;
160                observer.child(subqueries)?;
161            }
162        }
163        Ok(())
164    }
165}
166
167impl ObserveAllocations for QueryBlockPlan {
168    fn observe_allocations<'a>(
169        &'a self,
170        observer: &mut AllocationObserver<'a, '_>,
171    ) -> Result<(), ValueRetentionError> {
172        let Self {
173            deferred_outer_rewrites,
174            windows,
175            projections,
176            privilege_columns,
177            from,
178            r#where,
179            compute: _,
180            group_by,
181            grouping_sets,
182            group_distinct: _,
183            having,
184            order_by,
185            limit,
186            with_ties: _,
187            offset,
188            distinct: _,
189            distinct_on,
190            subqueries,
191            access: _,
192            locking,
193        } = self;
194        observer.child(deferred_outer_rewrites)?;
195        observer.child(windows)?;
196        observer.child(projections)?;
197        observer.child(privilege_columns)?;
198        observer.child(from)?;
199        observer.child(r#where)?;
200        observer.child(group_by)?;
201        observer.child(grouping_sets)?;
202        observer.child(having)?;
203        observer.child(order_by)?;
204        observer.child(limit)?;
205        observer.child(offset)?;
206        observer.child(distinct_on)?;
207        observer.child(subqueries)?;
208        observer.child(locking)?;
209        Ok(())
210    }
211}
212
213impl ObserveAllocations for ProjectionPlan {
214    fn observe_allocations<'a>(
215        &'a self,
216        observer: &mut AllocationObserver<'a, '_>,
217    ) -> Result<(), ValueRetentionError> {
218        let Self { expr, alias } = self;
219        observer.child(expr)?;
220        observer.child(alias)?;
221        Ok(())
222    }
223}
224
225impl ObserveAllocations for OrderPlan {
226    fn observe_allocations<'a>(
227        &'a self,
228        observer: &mut AllocationObserver<'a, '_>,
229    ) -> Result<(), ValueRetentionError> {
230        let Self {
231            expr,
232            descending: _,
233            nulls: _,
234        } = self;
235        observer.child(expr)?;
236        Ok(())
237    }
238}
239
240impl ObserveAllocations for ExpressionPlan {
241    fn observe_allocations<'a>(
242        &'a self,
243        observer: &mut AllocationObserver<'a, '_>,
244    ) -> Result<(), ValueRetentionError> {
245        let Self { scalar, subqueries } = self;
246        observer.child(scalar)?;
247        observer.child(subqueries)?;
248        Ok(())
249    }
250}