Skip to main content

uqa_sql/plan/memory/
scalar.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::ir::{ScalarExpr, ScalarFrameBound, ScalarOrder, ScalarWindowFrame, ScalarWindowSpec};
11
12impl ObserveAllocations for ScalarExpr {
13    #[expect(
14        clippy::too_many_lines,
15        reason = "exhaustive observation follows every scalar allocation without evaluation"
16    )]
17    fn observe_allocations<'a>(
18        &'a self,
19        observer: &mut AllocationObserver<'a, '_>,
20    ) -> Result<(), ValueRetentionError> {
21        match self {
22            Self::Star => {}
23            Self::QualifiedStar(value0) => {
24                observer.child(value0)?;
25            }
26            Self::Default => {}
27            Self::Column(value0) => {
28                observer.child(value0)?;
29            }
30            Self::Position(_) => {}
31            Self::InternalColumn(_) => {}
32            Self::QualifiedColumn { qualifier, column } => {
33                observer.child(qualifier)?;
34                observer.child(column)?;
35            }
36            Self::Literal(value0) => {
37                observer.child(value0)?;
38            }
39            Self::TypedLiteral {
40                value,
41                ty,
42                collation_oid: _,
43                composite_source,
44                bound_type,
45                parameter_index: _,
46            } => {
47                observer.child(value)?;
48                observer.child(ty)?;
49                observer.child(composite_source)?;
50                observer.child(bound_type)?;
51            }
52            Self::Collated {
53                expr,
54                ty,
55                collation: _,
56            } => {
57                observer.child(expr)?;
58                observer.child(ty)?;
59            }
60            Self::Param(_) => {}
61            Self::Func {
62                name,
63                binding,
64                args,
65                distinct: _,
66                order_by,
67                order_syntax: _,
68                filter,
69            } => {
70                observer.child(name)?;
71                observer.child(binding)?;
72                observer.child(args)?;
73                observer.child(order_by)?;
74                observer.child(filter)?;
75            }
76            Self::Array(value0) => {
77                observer.child(value0)?;
78            }
79            Self::Row(value0) => {
80                observer.child(value0)?;
81            }
82            Self::CompositeRow {
83                items,
84                binding,
85                bound_type,
86            } => {
87                observer.child(items)?;
88                observer.child(binding)?;
89                observer.child(bound_type)?;
90            }
91            Self::Binary { op: _, lhs, rhs } => {
92                observer.child(lhs)?;
93                observer.child(rhs)?;
94            }
95            Self::UnaryMinus(value0) => {
96                observer.child(value0)?;
97            }
98            Self::Not(value0) => {
99                observer.child(value0)?;
100            }
101            Self::And(value0) => {
102                observer.child(value0)?;
103            }
104            Self::Or(value0) => {
105                observer.child(value0)?;
106            }
107            Self::IsNull { expr, negated: _ } => {
108                observer.child(expr)?;
109            }
110            Self::Between { expr, low, high } => {
111                observer.child(expr)?;
112                observer.child(low)?;
113                observer.child(high)?;
114            }
115            Self::InList {
116                expr,
117                list,
118                negated: _,
119            } => {
120                observer.child(expr)?;
121                observer.child(list)?;
122            }
123            Self::WindowCall {
124                name,
125                args,
126                spec,
127                filter,
128                modifiers: _,
129            } => {
130                observer.child(name)?;
131                observer.child(args)?;
132                observer.child(spec)?;
133                observer.child(filter)?;
134            }
135            Self::Case {
136                base,
137                when,
138                else_branch,
139            } => {
140                observer.child(base)?;
141                observer.child(when)?;
142                observer.child(else_branch)?;
143            }
144            Self::Cast {
145                implicit: _,
146                expr,
147                ty,
148            } => {
149                observer.child(expr)?;
150                observer.child(ty)?;
151            }
152            Self::ScalarSubquery(_) => {}
153            Self::Exists {
154                subquery: _,
155                negated: _,
156            } => {}
157            Self::InSubquery {
158                expr,
159                subquery: _,
160                negated: _,
161            } => {
162                observer.child(expr)?;
163            }
164            Self::RetainedLiteral(value0) => {
165                observer.child(value0)?;
166            }
167        }
168        Ok(())
169    }
170}
171
172impl ObserveAllocations for ScalarOrder {
173    fn observe_allocations<'a>(
174        &'a self,
175        observer: &mut AllocationObserver<'a, '_>,
176    ) -> Result<(), ValueRetentionError> {
177        let Self {
178            expr,
179            descending: _,
180            nulls: _,
181        } = self;
182        observer.child(expr)?;
183        Ok(())
184    }
185}
186
187impl ObserveAllocations for ScalarWindowSpec {
188    fn observe_allocations<'a>(
189        &'a self,
190        observer: &mut AllocationObserver<'a, '_>,
191    ) -> Result<(), ValueRetentionError> {
192        let Self {
193            definition: _,
194            partition_by,
195            order_by,
196            frame,
197        } = self;
198        observer.child(partition_by)?;
199        observer.child(order_by)?;
200        observer.child(frame)?;
201        Ok(())
202    }
203}
204
205impl ObserveAllocations for ScalarWindowFrame {
206    fn observe_allocations<'a>(
207        &'a self,
208        observer: &mut AllocationObserver<'a, '_>,
209    ) -> Result<(), ValueRetentionError> {
210        let Self {
211            mode: _,
212            start,
213            end,
214            between: _,
215            exclusion: _,
216        } = self;
217        observer.child(start)?;
218        observer.child(end)?;
219        Ok(())
220    }
221}
222
223impl ObserveAllocations for ScalarFrameBound {
224    fn observe_allocations<'a>(
225        &'a self,
226        observer: &mut AllocationObserver<'a, '_>,
227    ) -> Result<(), ValueRetentionError> {
228        match self {
229            Self::UnboundedPreceding => {}
230            Self::UnboundedFollowing => {}
231            Self::CurrentRow => {}
232            Self::Preceding(value0) => {
233                observer.child(value0)?;
234            }
235            Self::Following(value0) => {
236                observer.child(value0)?;
237            }
238        }
239        Ok(())
240    }
241}