1use super::{ScalarExpr, ScalarFrameBound, ScalarOrder, ScalarWindowSpec};
10use crate::SQLError;
11use uqa_core::memory::ProductionControl;
12
13#[cfg(test)]
14mod tests;
15
16impl ScalarExpr {
17 #[expect(
19 clippy::too_many_lines,
20 reason = "exhaustive syntax comparison preserves the derived field comparison order"
21 )]
22 pub fn has_same_syntax_with_control(
23 &self,
24 other: &Self,
25 control: &ProductionControl<'_>,
26 ) -> Result<bool, SQLError> {
27 control.check()?;
28 Ok(match (self, other) {
29 (Self::RetainedLiteral(left), Self::RetainedLiteral(right)) => {
30 left.has_same_representation_with_control(right, control)?
31 }
32 (Self::Array(left), Self::Array(right))
33 | (Self::Row(left), Self::Row(right))
34 | (Self::And(left), Self::And(right))
35 | (Self::Or(left), Self::Or(right)) => sequence(left, right, control)?,
36 (
37 Self::CompositeRow {
38 items: left,
39 binding: lb,
40 bound_type: lt,
41 },
42 Self::CompositeRow {
43 items: right,
44 binding: rb,
45 bound_type: rt,
46 },
47 ) => sequence(left, right, control)? && lb == rb && lt == rt,
48 (Self::UnaryMinus(left), Self::UnaryMinus(right))
49 | (Self::Not(left), Self::Not(right)) => {
50 left.has_same_syntax_with_control(right, control)?
51 }
52 (
53 Self::Binary {
54 op: lo,
55 lhs: ll,
56 rhs: lr,
57 },
58 Self::Binary {
59 op: ro,
60 lhs: rl,
61 rhs: rr,
62 },
63 ) => {
64 lo == ro
65 && ll.has_same_syntax_with_control(rl, control)?
66 && lr.has_same_syntax_with_control(rr, control)?
67 }
68 (
69 Self::Collated {
70 expr: left,
71 ty: lt,
72 collation: lc,
73 },
74 Self::Collated {
75 expr: right,
76 ty: rt,
77 collation: rc,
78 },
79 ) => left.has_same_syntax_with_control(right, control)? && lt == rt && lc == rc,
80 (
81 Self::Cast {
82 implicit: li,
83 expr: left,
84 ty: lt,
85 },
86 Self::Cast {
87 implicit: ri,
88 expr: right,
89 ty: rt,
90 },
91 ) => li == ri && left.has_same_syntax_with_control(right, control)? && lt == rt,
92 (
93 Self::IsNull {
94 expr: left,
95 negated: ln,
96 },
97 Self::IsNull {
98 expr: right,
99 negated: rn,
100 },
101 ) => left.has_same_syntax_with_control(right, control)? && ln == rn,
102 (
103 Self::Between {
104 expr: left,
105 low: ll,
106 high: lh,
107 },
108 Self::Between {
109 expr: right,
110 low: rl,
111 high: rh,
112 },
113 ) => {
114 left.has_same_syntax_with_control(right, control)?
115 && ll.has_same_syntax_with_control(rl, control)?
116 && lh.has_same_syntax_with_control(rh, control)?
117 }
118 (
119 Self::InList {
120 expr: left,
121 list: ll,
122 negated: ln,
123 },
124 Self::InList {
125 expr: right,
126 list: rl,
127 negated: rn,
128 },
129 ) => {
130 left.has_same_syntax_with_control(right, control)?
131 && sequence(ll, rl, control)?
132 && ln == rn
133 }
134 (
135 Self::InSubquery {
136 expr: left,
137 subquery: ls,
138 negated: ln,
139 },
140 Self::InSubquery {
141 expr: right,
142 subquery: rs,
143 negated: rn,
144 },
145 ) => left.has_same_syntax_with_control(right, control)? && ls == rs && ln == rn,
146 (
147 Self::Func {
148 name: ln,
149 binding: lb,
150 args: la,
151 distinct: ld,
152 order_by: lo,
153 order_syntax: ls,
154 filter: lf,
155 },
156 Self::Func {
157 name: rn,
158 binding: rb,
159 args: ra,
160 distinct: rd,
161 order_by: ro,
162 order_syntax: rs,
163 filter: rf,
164 },
165 ) => {
166 ln == rn
167 && lb == rb
168 && sequence(la, ra, control)?
169 && ld == rd
170 && orders(lo, ro, control)?
171 && ls == rs
172 && optional(lf.as_deref(), rf.as_deref(), control)?
173 }
174 (
175 Self::WindowCall {
176 name: ln,
177 args: la,
178 spec: ls,
179 filter: lf,
180 modifiers: lm,
181 },
182 Self::WindowCall {
183 name: rn,
184 args: ra,
185 spec: rs,
186 filter: rf,
187 modifiers: rm,
188 },
189 ) => {
190 ln == rn
191 && sequence(la, ra, control)?
192 && window(ls, rs, control)?
193 && optional(lf.as_deref(), rf.as_deref(), control)?
194 && lm == rm
195 }
196 (
197 Self::Case {
198 base: lb,
199 when: lw,
200 else_branch: le,
201 },
202 Self::Case {
203 base: rb,
204 when: rw,
205 else_branch: re,
206 },
207 ) => {
208 if !optional(lb.as_deref(), rb.as_deref(), control)? || lw.len() != rw.len() {
209 return Ok(false);
210 }
211 for ((lc, lv), (rc, rv)) in lw.iter().zip(rw) {
212 if !lc.has_same_syntax_with_control(rc, control)?
213 || !lv.has_same_syntax_with_control(rv, control)?
214 {
215 return Ok(false);
216 }
217 }
218 optional(le.as_deref(), re.as_deref(), control)?
219 }
220 (
221 Self::Star
222 | Self::QualifiedStar(_)
223 | Self::Default
224 | Self::Column(_)
225 | Self::Position(_)
226 | Self::InternalColumn(_)
227 | Self::QualifiedColumn { .. }
228 | Self::Literal(_)
229 | Self::TypedLiteral { .. }
230 | Self::Param(_)
231 | Self::ScalarSubquery(_)
232 | Self::Exists { .. },
233 _,
234 ) => self == other,
235 _ => false,
236 })
237 }
238}
239
240fn sequence(
241 left: &[ScalarExpr],
242 right: &[ScalarExpr],
243 control: &ProductionControl<'_>,
244) -> Result<bool, SQLError> {
245 if left.len() != right.len() {
246 return Ok(false);
247 }
248 for (left, right) in left.iter().zip(right) {
249 if !left.has_same_syntax_with_control(right, control)? {
250 return Ok(false);
251 }
252 }
253 Ok(true)
254}
255
256fn optional(
257 left: Option<&ScalarExpr>,
258 right: Option<&ScalarExpr>,
259 control: &ProductionControl<'_>,
260) -> Result<bool, SQLError> {
261 match (left, right) {
262 (Some(left), Some(right)) => left.has_same_syntax_with_control(right, control),
263 (None, None) => Ok(true),
264 _ => Ok(false),
265 }
266}
267
268fn orders(
269 left: &[ScalarOrder],
270 right: &[ScalarOrder],
271 control: &ProductionControl<'_>,
272) -> Result<bool, SQLError> {
273 if left.len() != right.len() {
274 return Ok(false);
275 }
276 for (left, right) in left.iter().zip(right) {
277 if !left
278 .expr
279 .has_same_syntax_with_control(&right.expr, control)?
280 || left.descending != right.descending
281 || left.nulls != right.nulls
282 {
283 return Ok(false);
284 }
285 }
286 Ok(true)
287}
288
289fn window(
290 left: &ScalarWindowSpec,
291 right: &ScalarWindowSpec,
292 control: &ProductionControl<'_>,
293) -> Result<bool, SQLError> {
294 if left.definition != right.definition
295 || !sequence(&left.partition_by, &right.partition_by, control)?
296 || !orders(&left.order_by, &right.order_by, control)?
297 {
298 return Ok(false);
299 }
300 match (&left.frame, &right.frame) {
301 (Some(left), Some(right)) => Ok(left.mode == right.mode
302 && bound(&left.start, &right.start, control)?
303 && bound(&left.end, &right.end, control)?
304 && left.between == right.between
305 && left.exclusion == right.exclusion),
306 (None, None) => Ok(true),
307 _ => Ok(false),
308 }
309}
310
311fn bound(
312 left: &ScalarFrameBound,
313 right: &ScalarFrameBound,
314 control: &ProductionControl<'_>,
315) -> Result<bool, SQLError> {
316 match (left, right) {
317 (ScalarFrameBound::Preceding(left), ScalarFrameBound::Preceding(right))
318 | (ScalarFrameBound::Following(left), ScalarFrameBound::Following(right)) => {
319 left.has_same_syntax_with_control(right, control)
320 }
321 (
322 ScalarFrameBound::UnboundedPreceding
323 | ScalarFrameBound::UnboundedFollowing
324 | ScalarFrameBound::CurrentRow,
325 _,
326 ) => Ok(left == right),
327 _ => Ok(false),
328 }
329}