Skip to main content

uqa_sql/plan/memory/
metadata.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::ast::{
11    AssignmentBinding, AssignmentPathStep, ColumnType, CompositeFieldBinding, CompositeRowBinding,
12    CompositeTypeReference, EnumTypeReference, FunctionBinding, FunctionResolutionError,
13    FunctionValueBinding, InternalRelationId, IntervalFields, JoinUsing, LockWait, LockingClause,
14    OperatorJoinRelations, OperatorResolutionError, RangeSubtype, RangeTypeReference,
15    ReturningAliases, RoutineInvocationBinding,
16};
17use crate::expr::composites::{
18    CompositeAttribute, CompositeConstantSource, CompositeTypeDescriptor, DroppedCompositeAttribute,
19};
20
21// Copy bounds make these inline-only observations fail to compile if a carrier acquires owned storage.
22macro_rules! inline {
23    ($($ty:ty),* $(,)?) => {$ (
24        impl ObserveAllocations for $ty {
25            fn observe_allocations<'a>(&'a self, _: &mut AllocationObserver<'a, '_>) -> Result<(), ValueRetentionError> {
26                fn require_copy<T: Copy>(_: &T) {}
27                require_copy(self);
28                Ok(())
29            }
30        }
31    )*};
32}
33inline!(
34    AssignmentPathStep,
35    IntervalFields,
36    RangeSubtype,
37    DroppedCompositeAttribute,
38    LockWait,
39    InternalRelationId
40);
41
42impl ObserveAllocations for RangeTypeReference {
43    fn observe_allocations<'a>(
44        &'a self,
45        observer: &mut AllocationObserver<'a, '_>,
46    ) -> Result<(), ValueRetentionError> {
47        let Self {
48            schema,
49            name,
50            oid: _,
51            array_oid: _,
52            alignment: _,
53        } = self;
54        observer.child(schema)?;
55        observer.child(name)
56    }
57}
58
59impl ObserveAllocations for FunctionBinding {
60    fn observe_allocations<'a>(
61        &'a self,
62        observer: &mut AllocationObserver<'a, '_>,
63    ) -> Result<(), ValueRetentionError> {
64        let Self {
65            object_id: _,
66            name,
67            argument_types,
68            builtin: _,
69            dispatch: _,
70            invocation,
71            value_binding,
72            resolution_error,
73            input_collation: _,
74        } = self;
75        observer.child(name)?;
76        observer.child(argument_types)?;
77        observer.child(invocation)?;
78        observer.child(value_binding)?;
79        observer.child(resolution_error)?;
80        Ok(())
81    }
82}
83
84impl ObserveAllocations for FunctionValueBinding {
85    fn observe_allocations<'a>(
86        &'a self,
87        observer: &mut AllocationObserver<'a, '_>,
88    ) -> Result<(), ValueRetentionError> {
89        match self {
90            Self::CompositeField(value0) => {
91                observer.child(value0)?;
92            }
93            Self::Assignment(value0) => {
94                observer.child(value0)?;
95            }
96        }
97        Ok(())
98    }
99}
100
101impl ObserveAllocations for CompositeFieldBinding {
102    fn observe_allocations<'a>(
103        &'a self,
104        observer: &mut AllocationObserver<'a, '_>,
105    ) -> Result<(), ValueRetentionError> {
106        let Self {
107            type_oid: _,
108            number: _,
109            result_type,
110            collation_oid: _,
111            dropped: _,
112            changed_type,
113        } = self;
114        observer.child(result_type)?;
115        observer.child(changed_type)?;
116        Ok(())
117    }
118}
119
120impl ObserveAllocations for FunctionResolutionError {
121    fn observe_allocations<'a>(
122        &'a self,
123        observer: &mut AllocationObserver<'a, '_>,
124    ) -> Result<(), ValueRetentionError> {
125        match self {
126            Self::UndefinedFunction { signature } => {
127                observer.child(signature)?;
128            }
129            Self::Operator(value0) => {
130                observer.child(value0)?;
131            }
132        }
133        Ok(())
134    }
135}
136
137impl ObserveAllocations for OperatorResolutionError {
138    fn observe_allocations<'a>(
139        &'a self,
140        observer: &mut AllocationObserver<'a, '_>,
141    ) -> Result<(), ValueRetentionError> {
142        let Self { sqlstate, message } = self;
143        observer.child(sqlstate)?;
144        observer.child(message)?;
145        Ok(())
146    }
147}
148
149impl ObserveAllocations for RoutineInvocationBinding {
150    fn observe_allocations<'a>(
151        &'a self,
152        observer: &mut AllocationObserver<'a, '_>,
153    ) -> Result<(), ValueRetentionError> {
154        let Self {
155            argument_positions,
156            argument_targets,
157            argument_sources,
158            parameter_types,
159            return_type,
160            variadic_mode: _,
161        } = self;
162        observer.child(argument_positions)?;
163        observer.child(argument_targets)?;
164        observer.child(argument_sources)?;
165        observer.child(parameter_types)?;
166        observer.child(return_type)?;
167        Ok(())
168    }
169}
170
171impl ObserveAllocations for AssignmentBinding {
172    fn observe_allocations<'a>(
173        &'a self,
174        observer: &mut AllocationObserver<'a, '_>,
175    ) -> Result<(), ValueRetentionError> {
176        let Self {
177            path,
178            value_argument: _,
179        } = self;
180        observer.child(path)?;
181        Ok(())
182    }
183}
184
185impl ObserveAllocations for ColumnType {
186    fn observe_allocations<'a>(
187        &'a self,
188        observer: &mut AllocationObserver<'a, '_>,
189    ) -> Result<(), ValueRetentionError> {
190        match self {
191            Self::Named(value0) => {
192                observer.child(value0)?;
193            }
194            Self::SmallInteger => {}
195            Self::Integer => {}
196            Self::BigInteger => {}
197            Self::Oid => {}
198            Self::Xid => {}
199            Self::Boolean => {}
200            Self::Void => {}
201            Self::Text => {}
202            Self::RefCursor => {}
203            Self::Name => {}
204            Self::Uuid => {}
205            Self::Varchar(_) => {}
206            Self::Bpchar => {}
207            Self::Character(_) => {}
208            Self::Real => {}
209            Self::DoublePrecision => {}
210            Self::Numeric {
211                precision: _,
212                scale: _,
213            } => {}
214            Self::Json => {}
215            Self::Xml => {}
216            Self::JsonB => {}
217            Self::Bytea => {}
218            Self::Bit(_) => {}
219            Self::VarBit(_) => {}
220            Self::InternalChar => {}
221            Self::Regproc => {}
222            Self::Regprocedure => {}
223            Self::Regclass => {}
224            Self::Regnamespace => {}
225            Self::Regcollation => {}
226            Self::Regrole => {}
227            Self::Regtype => {}
228            Self::PgNodeTree => {}
229            Self::AclItem => {}
230            Self::Int2Vector => {}
231            Self::OidVector => {}
232            Self::AnyArray => {}
233            Self::Record => {}
234            Self::Array(value0) => {
235                observer.child(value0)?;
236            }
237            Self::Date => {}
238            Self::Time => {}
239            Self::TimePrecision(_) => {}
240            Self::TimeTz => {}
241            Self::TimeTzPrecision(_) => {}
242            Self::Timestamp => {}
243            Self::TimestampPrecision(_) => {}
244            Self::TimestampTz => {}
245            Self::TimestampTzPrecision(_) => {}
246            Self::Interval => {}
247            Self::IntervalWithFields {
248                fields,
249                precision: _,
250            } => {
251                observer.child(fields)?;
252            }
253            Self::Range(value0) => {
254                observer.child(value0)?;
255            }
256            Self::Multirange(value0) => {
257                observer.child(value0)?;
258            }
259            Self::DeclaredRange(value0) => {
260                observer.child(value0)?;
261            }
262            Self::DeclaredMultirange(value0) => {
263                observer.child(value0)?;
264            }
265            Self::Vector(_) => {}
266            Self::Tensor(_) => {}
267            Self::Domain {
268                schema,
269                name,
270                oid: _,
271                array_oid: _,
272                collation_oid: _,
273                base,
274            } => {
275                observer.child(schema)?;
276                observer.child(name)?;
277                observer.child(base)?;
278            }
279            Self::Enum(value0) => {
280                observer.child(value0)?;
281            }
282            Self::Composite(value0) => {
283                observer.child(value0)?;
284            }
285        }
286        Ok(())
287    }
288}
289
290impl ObserveAllocations for EnumTypeReference {
291    fn observe_allocations<'a>(
292        &'a self,
293        observer: &mut AllocationObserver<'a, '_>,
294    ) -> Result<(), ValueRetentionError> {
295        let Self {
296            schema,
297            name,
298            oid: _,
299            array_oid: _,
300        } = self;
301        observer.child(schema)?;
302        observer.child(name)?;
303        Ok(())
304    }
305}
306
307impl ObserveAllocations for CompositeTypeReference {
308    fn observe_allocations<'a>(
309        &'a self,
310        observer: &mut AllocationObserver<'a, '_>,
311    ) -> Result<(), ValueRetentionError> {
312        let Self {
313            schema,
314            name,
315            oid: _,
316            array_oid: _,
317            relation_oid: _,
318        } = self;
319        observer.child(schema)?;
320        observer.child(name)?;
321        Ok(())
322    }
323}
324
325impl ObserveAllocations for CompositeRowBinding {
326    fn observe_allocations<'a>(
327        &'a self,
328        observer: &mut AllocationObserver<'a, '_>,
329    ) -> Result<(), ValueRetentionError> {
330        let Self {
331            ty,
332            attributes,
333            layout: _,
334            argument_types,
335        } = self;
336        observer.child(ty)?;
337        observer.child(attributes)?;
338        observer.child(argument_types)?;
339        Ok(())
340    }
341}
342
343impl ObserveAllocations for CompositeConstantSource {
344    fn observe_allocations<'a>(
345        &'a self,
346        observer: &mut AllocationObserver<'a, '_>,
347    ) -> Result<(), ValueRetentionError> {
348        let Self { value, descriptors } = self;
349        observer.child(value)?;
350        observer.child(descriptors)?;
351        Ok(())
352    }
353}
354
355impl ObserveAllocations for CompositeTypeDescriptor {
356    fn observe_allocations<'a>(
357        &'a self,
358        observer: &mut AllocationObserver<'a, '_>,
359    ) -> Result<(), ValueRetentionError> {
360        let Self {
361            dropped,
362            type_oid: _,
363            relation_oid: _,
364            attributes,
365        } = self;
366        observer.child(dropped)?;
367        observer.child(attributes)?;
368        Ok(())
369    }
370}
371
372impl ObserveAllocations for CompositeAttribute {
373    fn observe_allocations<'a>(
374        &'a self,
375        observer: &mut AllocationObserver<'a, '_>,
376    ) -> Result<(), ValueRetentionError> {
377        let Self {
378            name,
379            ty,
380            collation_oid: _,
381            number: _,
382        } = self;
383        observer.child(name)?;
384        observer.child(ty)?;
385        Ok(())
386    }
387}
388
389impl ObserveAllocations for JoinUsing {
390    fn observe_allocations<'a>(
391        &'a self,
392        observer: &mut AllocationObserver<'a, '_>,
393    ) -> Result<(), ValueRetentionError> {
394        let Self { columns, alias } = self;
395        observer.child(columns)?;
396        observer.child(alias)?;
397        Ok(())
398    }
399}
400
401impl ObserveAllocations for OperatorJoinRelations {
402    fn observe_allocations<'a>(
403        &'a self,
404        observer: &mut AllocationObserver<'a, '_>,
405    ) -> Result<(), ValueRetentionError> {
406        let Self { left, right } = self;
407        observer.child(left)?;
408        observer.child(right)?;
409        Ok(())
410    }
411}
412
413impl ObserveAllocations for ReturningAliases {
414    fn observe_allocations<'a>(
415        &'a self,
416        observer: &mut AllocationObserver<'a, '_>,
417    ) -> Result<(), ValueRetentionError> {
418        let Self {
419            old,
420            new,
421            old_explicit: _,
422            new_explicit: _,
423        } = self;
424        observer.child(old)?;
425        observer.child(new)?;
426        Ok(())
427    }
428}
429
430impl ObserveAllocations for LockingClause {
431    fn observe_allocations<'a>(
432        &'a self,
433        observer: &mut AllocationObserver<'a, '_>,
434    ) -> Result<(), ValueRetentionError> {
435        let Self {
436            strength: _,
437            wait,
438            relations,
439        } = self;
440        observer.child(wait)?;
441        observer.child(relations)?;
442        Ok(())
443    }
444}