Skip to main content

uqa_sql/plan/memory/
command.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    AssignmentPlan, ConflictActionPlan, ConflictPlan, DeletePlan, InsertPlan, MergePlan,
12    MergeWhenPlan, RuleValuesInput, UpdatePlan, ViewCheckPlan, ViewRuleInsertPlan,
13    ViewRuleReturningPlan, ViewRuleUpdatePlan,
14};
15
16impl ObserveAllocations for InsertPlan {
17    fn observe_allocations<'a>(
18        &'a self,
19        observer: &mut AllocationObserver<'a, '_>,
20    ) -> Result<(), ValueRetentionError> {
21        let Self {
22            table,
23            target_relation_bound: _,
24            relations_bound: _,
25            statement_privilege_subject,
26            target_privilege_subject,
27            target_qualifier,
28            target_alias,
29            include_descendants: _,
30            columns,
31            overriding: _,
32            ctes,
33            rows,
34            source,
35            rule_values_input,
36            on_conflict,
37            returning,
38            returning_aliases,
39            subqueries,
40            view_checks,
41            view_rule_relations,
42            view_rule_insert_plans,
43            view_rule_returning,
44        } = self;
45        observer.child(table)?;
46        observer.child(statement_privilege_subject)?;
47        observer.child(target_privilege_subject)?;
48        observer.child(target_qualifier)?;
49        observer.child(target_alias)?;
50        observer.child(columns)?;
51        observer.child(ctes)?;
52        observer.child(rows)?;
53        observer.child(source)?;
54        observer.child(rule_values_input)?;
55        observer.child(on_conflict)?;
56        observer.child(returning)?;
57        observer.child(returning_aliases)?;
58        observer.child(subqueries)?;
59        observer.child(view_checks)?;
60        observer.child(view_rule_relations)?;
61        observer.child(view_rule_insert_plans)?;
62        observer.child(view_rule_returning)?;
63        Ok(())
64    }
65}
66
67impl ObserveAllocations for UpdatePlan {
68    fn observe_allocations<'a>(
69        &'a self,
70        observer: &mut AllocationObserver<'a, '_>,
71    ) -> Result<(), ValueRetentionError> {
72        let Self {
73            table,
74            target_relation_bound: _,
75            relations_bound: _,
76            statement_privilege_subject,
77            target_privilege_subject,
78            target_qualifier,
79            target_alias,
80            include_descendants: _,
81            assignments,
82            predicate,
83            ctes,
84            source,
85            returning,
86            returning_aliases,
87            subqueries,
88            view_checks,
89            view_rule_relations,
90            view_rule_update_plans,
91            view_rule_returning,
92        } = self;
93        observer.child(table)?;
94        observer.child(statement_privilege_subject)?;
95        observer.child(target_privilege_subject)?;
96        observer.child(target_qualifier)?;
97        observer.child(target_alias)?;
98        observer.child(assignments)?;
99        observer.child(predicate)?;
100        observer.child(ctes)?;
101        observer.child(source)?;
102        observer.child(returning)?;
103        observer.child(returning_aliases)?;
104        observer.child(subqueries)?;
105        observer.child(view_checks)?;
106        observer.child(view_rule_relations)?;
107        observer.child(view_rule_update_plans)?;
108        observer.child(view_rule_returning)?;
109        Ok(())
110    }
111}
112
113impl ObserveAllocations for DeletePlan {
114    fn observe_allocations<'a>(
115        &'a self,
116        observer: &mut AllocationObserver<'a, '_>,
117    ) -> Result<(), ValueRetentionError> {
118        let Self {
119            table,
120            target_relation_bound: _,
121            relations_bound: _,
122            statement_privilege_subject,
123            target_privilege_subject,
124            target_qualifier,
125            target_alias,
126            include_descendants: _,
127            predicate,
128            ctes,
129            source,
130            returning,
131            returning_aliases,
132            subqueries,
133            view_rule_relations,
134            view_rule_returning,
135        } = self;
136        observer.child(table)?;
137        observer.child(statement_privilege_subject)?;
138        observer.child(target_privilege_subject)?;
139        observer.child(target_qualifier)?;
140        observer.child(target_alias)?;
141        observer.child(predicate)?;
142        observer.child(ctes)?;
143        observer.child(source)?;
144        observer.child(returning)?;
145        observer.child(returning_aliases)?;
146        observer.child(subqueries)?;
147        observer.child(view_rule_relations)?;
148        observer.child(view_rule_returning)?;
149        Ok(())
150    }
151}
152
153impl ObserveAllocations for MergePlan {
154    fn observe_allocations<'a>(
155        &'a self,
156        observer: &mut AllocationObserver<'a, '_>,
157    ) -> Result<(), ValueRetentionError> {
158        let Self {
159            ctes,
160            target,
161            statement_privilege_subject,
162            target_privilege_subject,
163            target_qualifier,
164            target_alias,
165            include_descendants: _,
166            target_predicate,
167            source,
168            join_condition,
169            when_clauses,
170            returning,
171            returning_aliases,
172            subqueries,
173            view_checks,
174        } = self;
175        observer.child(ctes)?;
176        observer.child(target)?;
177        observer.child(statement_privilege_subject)?;
178        observer.child(target_privilege_subject)?;
179        observer.child(target_qualifier)?;
180        observer.child(target_alias)?;
181        observer.child(target_predicate)?;
182        observer.child(source)?;
183        observer.child(join_condition)?;
184        observer.child(when_clauses)?;
185        observer.child(returning)?;
186        observer.child(returning_aliases)?;
187        observer.child(subqueries)?;
188        observer.child(view_checks)?;
189        Ok(())
190    }
191}
192
193impl ObserveAllocations for AssignmentPlan {
194    fn observe_allocations<'a>(
195        &'a self,
196        observer: &mut AllocationObserver<'a, '_>,
197    ) -> Result<(), ValueRetentionError> {
198        let Self { target, value } = self;
199        observer.child(target)?;
200        observer.child(value)?;
201        Ok(())
202    }
203}
204
205impl ObserveAllocations for ConflictPlan {
206    fn observe_allocations<'a>(
207        &'a self,
208        observer: &mut AllocationObserver<'a, '_>,
209    ) -> Result<(), ValueRetentionError> {
210        let Self {
211            predicate,
212            constraint,
213            conflict_columns,
214            expressions,
215            action,
216        } = self;
217        observer.child(predicate)?;
218        observer.child(constraint)?;
219        observer.child(conflict_columns)?;
220        observer.child(expressions)?;
221        observer.child(action)?;
222        Ok(())
223    }
224}
225
226impl ObserveAllocations for ConflictActionPlan {
227    fn observe_allocations<'a>(
228        &'a self,
229        observer: &mut AllocationObserver<'a, '_>,
230    ) -> Result<(), ValueRetentionError> {
231        match self {
232            Self::Nothing => {}
233            Self::Update {
234                assignments,
235                predicate,
236            } => {
237                observer.child(assignments)?;
238                observer.child(predicate)?;
239            }
240        }
241        Ok(())
242    }
243}
244
245impl ObserveAllocations for MergeWhenPlan {
246    fn observe_allocations<'a>(
247        &'a self,
248        observer: &mut AllocationObserver<'a, '_>,
249    ) -> Result<(), ValueRetentionError> {
250        match self {
251            Self::UpdateMatched {
252                condition,
253                assignments,
254            } => {
255                observer.child(condition)?;
256                observer.child(assignments)?;
257            }
258            Self::DeleteMatched { condition } => {
259                observer.child(condition)?;
260            }
261            Self::UpdateNotMatchedBySource {
262                condition,
263                assignments,
264            } => {
265                observer.child(condition)?;
266                observer.child(assignments)?;
267            }
268            Self::DeleteNotMatchedBySource { condition } => {
269                observer.child(condition)?;
270            }
271            Self::InsertNotMatched {
272                condition,
273                columns,
274                overriding: _,
275                values,
276            } => {
277                observer.child(condition)?;
278                observer.child(columns)?;
279                observer.child(values)?;
280            }
281            Self::NothingMatched { condition } => {
282                observer.child(condition)?;
283            }
284            Self::NothingNotMatched { condition } => {
285                observer.child(condition)?;
286            }
287            Self::NothingNotMatchedBySource { condition } => {
288                observer.child(condition)?;
289            }
290        }
291        Ok(())
292    }
293}
294
295impl ObserveAllocations for RuleValuesInput {
296    fn observe_allocations<'a>(
297        &'a self,
298        observer: &mut AllocationObserver<'a, '_>,
299    ) -> Result<(), ValueRetentionError> {
300        let Self {
301            width: _,
302            inline: _,
303            defaults,
304            unresolved,
305            written,
306        } = self;
307        observer.child(defaults)?;
308        observer.child(unresolved)?;
309        observer.child(written)?;
310        Ok(())
311    }
312}
313
314impl ObserveAllocations for ViewCheckPlan {
315    fn observe_allocations<'a>(
316        &'a self,
317        observer: &mut AllocationObserver<'a, '_>,
318    ) -> Result<(), ValueRetentionError> {
319        let Self { view, predicate } = self;
320        observer.child(view)?;
321        observer.child(predicate)?;
322        Ok(())
323    }
324}
325
326impl ObserveAllocations for ViewRuleInsertPlan {
327    fn observe_allocations<'a>(
328        &'a self,
329        observer: &mut AllocationObserver<'a, '_>,
330    ) -> Result<(), ValueRetentionError> {
331        let Self {
332            relation,
333            supplied_columns,
334            input_columns,
335        } = self;
336        observer.child(relation)?;
337        observer.child(supplied_columns)?;
338        observer.child(input_columns)?;
339        Ok(())
340    }
341}
342
343impl ObserveAllocations for ViewRuleUpdatePlan {
344    fn observe_allocations<'a>(
345        &'a self,
346        observer: &mut AllocationObserver<'a, '_>,
347    ) -> Result<(), ValueRetentionError> {
348        let Self {
349            relation,
350            assigned_columns,
351            input_columns,
352        } = self;
353        observer.child(relation)?;
354        observer.child(assigned_columns)?;
355        observer.child(input_columns)?;
356        Ok(())
357    }
358}
359
360impl ObserveAllocations for ViewRuleReturningPlan {
361    fn observe_allocations<'a>(
362        &'a self,
363        observer: &mut AllocationObserver<'a, '_>,
364    ) -> Result<(), ValueRetentionError> {
365        let Self {
366            relation,
367            target_qualifier,
368            returning,
369            aliases,
370            subqueries,
371        } = self;
372        observer.child(relation)?;
373        observer.child(target_qualifier)?;
374        observer.child(returning)?;
375        observer.child(aliases)?;
376        observer.child(subqueries)?;
377        Ok(())
378    }
379}