selene-db-gql 1.3.0

ISO/IEC 39075:2024 GQL parser, planner, optimizer, and executor for selene-db.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Shared structural walkers for [`ValueExpr`].
//!
//! Hand-maintained exhaustive `match` arms over [`ValueExpr`] were duplicated
//! across the analyzer, planner, optimizer, and parser — every new variant
//! forced an edit in each copy, and a missed arm was a silent traversal bug.
//! This module centralizes the child- and span-enumeration shape in three
//! inherent methods so adopters express only their per-node work and delegate
//! traversal here:
//!
//! - [`ValueExpr::for_each_child`] / [`ValueExpr::for_each_child_mut`] visit each
//!   **direct** child `ValueExpr` of a node, in source order. They are *shallow*:
//!   the callback receives each immediate child once, and the caller drives
//!   recursion by re-invoking itself inside the callback. Subquery nodes
//!   ([`ValueExpr::Exists`], [`ValueExpr::ValueSubquery`]) carry no direct `ValueExpr` children — their
//!   bodies are [`MatchClause`](crate::MatchClause) /
//!   [`QueryPipeline`](crate::QueryPipeline) — so they yield nothing and the
//!   caller handles any subquery descent explicitly.
//! - [`ValueExpr::for_each_span_mut`] visits the [`SourceSpan`] **owned directly
//!   by this node** (for [`ValueExpr::Literal`] the span stored inside the
//!   [`Literal`](crate::Literal)). It is likewise shallow; spans of child nodes
//!   are reached by recursing with [`for_each_child_mut`](ValueExpr::for_each_child_mut).
//!
//! The shallow contract keeps the methods variant-additive-safe (a new variant
//! is handled in exactly one place) while preserving each adopter's existing
//! recursion order and side-effect timing byte-for-byte.

use crate::ast::{ValueExpr, expr::IsCheckKind, span::SourceSpan};

impl ValueExpr {
    /// Visit each direct child [`ValueExpr`] of this node, in source order.
    ///
    /// Shallow: the callback receives each immediate child exactly once and is
    /// **not** re-applied to grandchildren. Callers that want a full traversal
    /// re-invoke themselves inside `f`. The
    /// [`IsCheckKind::SourceOf`]/[`IsCheckKind::DestinationOf`] operand counts as
    /// a child of the enclosing [`ValueExpr::IsCheck`] and is yielded after the
    /// checked `operand`. Subquery variants yield no children.
    pub fn for_each_child<'a>(&'a self, f: &mut impl FnMut(&'a ValueExpr)) {
        match self {
            Self::Literal(_) | Self::Variable { .. } | Self::Parameter { .. } => {}
            Self::PropertyAccess { target, .. } | Self::PropertyExists { target, .. } => f(target),
            Self::ListLiteral { items, .. }
            | Self::PathConstructor {
                elements: items, ..
            }
            | Self::AllDifferent { items, .. }
            | Self::Same { items, .. } => {
                for item in items {
                    f(item);
                }
            }
            Self::RecordLiteral { fields, .. } => {
                for (_, value) in fields {
                    f(value);
                }
            }
            Self::BinaryOp { lhs, rhs, .. } => {
                f(lhs);
                f(rhs);
            }
            Self::UnaryOp { operand, .. } => f(operand),
            Self::FunctionCall { args, .. } => {
                for arg in args {
                    f(arg);
                }
            }
            Self::DurationBetween { start, end, .. } => {
                f(start);
                f(end);
            }
            Self::Normalize { source, .. } => f(source),
            Self::Trim {
                character, source, ..
            } => {
                if let Some(character) = character {
                    f(character);
                }
                f(source);
            }
            Self::IsCheck { operand, kind, .. } => {
                f(operand);
                if let IsCheckKind::SourceOf(value) | IsCheckKind::DestinationOf(value) = kind {
                    f(value);
                }
            }
            Self::InList { operand, list, .. } => {
                f(operand);
                for item in list {
                    f(item);
                }
            }
            Self::InListExpression { operand, list, .. } => {
                f(operand);
                f(list);
            }
            Self::Case {
                branches,
                else_branch,
                ..
            } => {
                for (condition, value) in branches {
                    f(condition);
                    f(value);
                }
                if let Some(value) = else_branch {
                    f(value);
                }
            }
            Self::Cast { value, .. } => f(value),
            // Subquery bodies are `MatchClause` / `QueryPipeline`, not
            // `ValueExpr`; they carry no direct `ValueExpr` children.
            Self::Exists { .. } | Self::ValueSubquery { .. } => {}
        }
    }

    /// Visit each direct child [`ValueExpr`] of this node mutably, in source
    /// order.
    ///
    /// Mutable mirror of [`for_each_child`](Self::for_each_child); the same
    /// shallow contract and child ordering apply.
    pub fn for_each_child_mut(&mut self, f: &mut impl FnMut(&mut ValueExpr)) {
        match self {
            Self::Literal(_) | Self::Variable { .. } | Self::Parameter { .. } => {}
            Self::PropertyAccess { target, .. } | Self::PropertyExists { target, .. } => f(target),
            Self::ListLiteral { items, .. }
            | Self::PathConstructor {
                elements: items, ..
            }
            | Self::AllDifferent { items, .. }
            | Self::Same { items, .. } => {
                for item in items {
                    f(item);
                }
            }
            Self::RecordLiteral { fields, .. } => {
                for (_, value) in fields {
                    f(value);
                }
            }
            Self::BinaryOp { lhs, rhs, .. } => {
                f(lhs);
                f(rhs);
            }
            Self::UnaryOp { operand, .. } => f(operand),
            Self::FunctionCall { args, .. } => {
                for arg in args {
                    f(arg);
                }
            }
            Self::DurationBetween { start, end, .. } => {
                f(start);
                f(end);
            }
            Self::Normalize { source, .. } => f(source),
            Self::Trim {
                character, source, ..
            } => {
                if let Some(character) = character {
                    f(character);
                }
                f(source);
            }
            Self::IsCheck { operand, kind, .. } => {
                f(operand);
                if let IsCheckKind::SourceOf(value) | IsCheckKind::DestinationOf(value) = kind {
                    f(value);
                }
            }
            Self::InList { operand, list, .. } => {
                f(operand);
                for item in list {
                    f(item);
                }
            }
            Self::InListExpression { operand, list, .. } => {
                f(operand);
                f(list);
            }
            Self::Case {
                branches,
                else_branch,
                ..
            } => {
                for (condition, value) in branches {
                    f(condition);
                    f(value);
                }
                if let Some(value) = else_branch {
                    f(value);
                }
            }
            Self::Cast { value, .. } => f(value),
            // Subquery bodies are `MatchClause` / `QueryPipeline`, not
            // `ValueExpr`; they carry no direct `ValueExpr` children.
            Self::Exists { .. } | Self::ValueSubquery { .. } => {}
        }
    }

    /// Visit the [`SourceSpan`] owned directly by this node.
    ///
    /// Shallow: only the span(s) belonging to *this* node are visited (for
    /// [`ValueExpr::Literal`] the span stored inside the
    /// [`Literal`](crate::Literal)). Spans of child expressions are reached by
    /// recursing with [`for_each_child_mut`](Self::for_each_child_mut). Every
    /// `ValueExpr` variant owns exactly one span, so the callback fires once per
    /// node.
    pub fn for_each_span_mut(&mut self, f: &mut impl FnMut(&mut SourceSpan)) {
        match self {
            Self::Literal(literal) => f(literal.span_mut()),
            Self::Variable { span, .. }
            | Self::Parameter { span, .. }
            | Self::PropertyAccess { span, .. }
            | Self::ListLiteral { span, .. }
            | Self::RecordLiteral { span, .. }
            | Self::PathConstructor { span, .. }
            | Self::BinaryOp { span, .. }
            | Self::UnaryOp { span, .. }
            | Self::FunctionCall { span, .. }
            | Self::DurationBetween { span, .. }
            | Self::IsCheck { span, .. }
            | Self::InList { span, .. }
            | Self::InListExpression { span, .. }
            | Self::AllDifferent { span, .. }
            | Self::Same { span, .. }
            | Self::PropertyExists { span, .. }
            | Self::Case { span, .. }
            | Self::Exists { span, .. }
            | Self::ValueSubquery { span, .. }
            | Self::Normalize { span, .. }
            | Self::Trim { span, .. }
            | Self::Cast { span, .. } => f(span),
        }
    }
}

#[cfg(test)]
mod tests {
    use selene_core::DbString;

    use crate::ast::ValueExpr;
    use crate::ast::expr::{BinaryOp, IsCheckKind, Literal};
    use crate::ast::span::SourceSpan;

    fn span(offset: u32) -> SourceSpan {
        SourceSpan::new(offset, 1)
    }

    fn db_string(value: &str) -> DbString {
        selene_core::db_string(value).expect("test string fits DB string cap")
    }

    fn var(name: &str, offset: u32) -> ValueExpr {
        ValueExpr::Variable {
            name: db_string(name),
            span: span(offset),
        }
    }

    fn int(value: i64, offset: u32) -> ValueExpr {
        ValueExpr::Literal(Literal::Integer(value, span(offset)))
    }

    #[test]
    fn for_each_child_binary_op_yields_both_operands_in_order() {
        let expr = ValueExpr::BinaryOp {
            op: BinaryOp::Add,
            lhs: Box::new(int(1, 10)),
            rhs: Box::new(int(2, 20)),
            span: span(0),
        };
        let mut seen = Vec::new();
        expr.for_each_child(&mut |child| seen.push(child.span().byte_offset));
        assert_eq!(seen, vec![10, 20]);
    }

    #[test]
    fn for_each_child_list_literal_yields_every_item() {
        let expr = ValueExpr::ListLiteral {
            items: vec![int(1, 1), int(2, 2), int(3, 3)],
            span: span(0),
        };
        let mut seen = Vec::new();
        expr.for_each_child(&mut |child| seen.push(child.span().byte_offset));
        assert_eq!(seen, vec![1, 2, 3]);
    }

    #[test]
    fn for_each_child_case_yields_branches_then_else() {
        let expr = ValueExpr::Case {
            branches: vec![(int(1, 1), int(2, 2)), (int(3, 3), int(4, 4))],
            else_branch: Some(Box::new(int(5, 5))),
            span: span(0),
        };
        let mut seen = Vec::new();
        expr.for_each_child(&mut |child| seen.push(child.span().byte_offset));
        assert_eq!(seen, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn for_each_child_is_check_yields_operand_then_kind_value() {
        let expr = ValueExpr::IsCheck {
            operand: Box::new(var("n", 1)),
            kind: IsCheckKind::SourceOf(Box::new(var("e", 2))),
            negated: false,
            span: span(0),
        };
        let mut seen = Vec::new();
        expr.for_each_child(&mut |child| seen.push(child.span().byte_offset));
        assert_eq!(seen, vec![1, 2]);
    }

    #[test]
    fn for_each_child_trim_skips_absent_character() {
        let expr = ValueExpr::Trim {
            spec: crate::ast::expr::TrimSpec::Both,
            character: None,
            source: Box::new(var("s", 7)),
            span: span(0),
        };
        let mut seen = Vec::new();
        expr.for_each_child(&mut |child| seen.push(child.span().byte_offset));
        assert_eq!(seen, vec![7]);
    }

    #[test]
    fn for_each_child_subquery_yields_nothing() {
        // VALUE { ... } carries a QueryPipeline body, not a ValueExpr child.
        let pipeline = crate::parse("RETURN 1")
            .ok()
            .and_then(|statement| match statement {
                crate::Statement::Query(pipeline) => Some(pipeline),
                _ => None,
            })
            .expect("RETURN 1 parses to a query pipeline");
        let expr = ValueExpr::ValueSubquery {
            body: Box::new(pipeline),
            span: span(0),
        };
        let mut count = 0;
        expr.for_each_child(&mut |_| count += 1);
        assert_eq!(count, 0);
    }

    #[test]
    fn for_each_child_mut_can_rewrite_children() {
        let mut expr = ValueExpr::BinaryOp {
            op: BinaryOp::Add,
            lhs: Box::new(var("a", 1)),
            rhs: Box::new(var("b", 2)),
            span: span(0),
        };
        // Replace every direct child variable with a literal.
        expr.for_each_child_mut(&mut |child| {
            if matches!(child, ValueExpr::Variable { .. }) {
                *child = int(0, 99);
            }
        });
        let mut kinds = Vec::new();
        expr.for_each_child(&mut |child| kinds.push(matches!(child, ValueExpr::Literal(_))));
        assert_eq!(kinds, vec![true, true]);
    }

    #[test]
    fn for_each_span_mut_visits_only_the_owning_node_span() {
        // The node span is offset 0; the child spans (10, 20) must be untouched
        // because the walker is shallow.
        let mut expr = ValueExpr::BinaryOp {
            op: BinaryOp::Add,
            lhs: Box::new(int(1, 10)),
            rhs: Box::new(int(2, 20)),
            span: span(0),
        };
        let mut visited = Vec::new();
        expr.for_each_span_mut(&mut |s| {
            visited.push(s.byte_offset);
            s.byte_offset = s.byte_offset.saturating_add(100);
        });
        assert_eq!(visited, vec![0]);
        let ValueExpr::BinaryOp { span, lhs, rhs, .. } = &expr else {
            unreachable!("constructed a BinaryOp");
        };
        assert_eq!(span.byte_offset, 100);
        assert_eq!(lhs.span().byte_offset, 10);
        assert_eq!(rhs.span().byte_offset, 20);
    }

    #[test]
    fn for_each_span_mut_reaches_literal_inner_span() {
        let mut expr = int(7, 42);
        let mut visited = Vec::new();
        expr.for_each_span_mut(&mut |s| visited.push(s.byte_offset));
        assert_eq!(visited, vec![42]);
    }

    #[test]
    fn recursive_span_walk_offsets_every_span() {
        // Composing for_each_span_mut with for_each_child_mut recursion mirrors
        // the parser's rebase / the eq scrub: every span in the tree is visited
        // exactly once.
        fn offset_all(expr: &mut ValueExpr, by: u32) {
            expr.for_each_span_mut(&mut |s| s.byte_offset = s.byte_offset.saturating_add(by));
            expr.for_each_child_mut(&mut |child| offset_all(child, by));
        }
        let mut expr = ValueExpr::BinaryOp {
            op: BinaryOp::Add,
            lhs: Box::new(int(1, 10)),
            rhs: Box::new(ValueExpr::UnaryOp {
                op: crate::ast::expr::UnaryOp::Negate,
                operand: Box::new(int(2, 30)),
                span: span(20),
            }),
            span: span(0),
        };
        offset_all(&mut expr, 1000);
        let mut all = Vec::new();
        fn gather(expr: &ValueExpr, out: &mut Vec<u32>) {
            let mut e = expr.clone();
            e.for_each_span_mut(&mut |s| out.push(s.byte_offset));
            expr.for_each_child(&mut |child| gather(child, out));
        }
        gather(&expr, &mut all);
        all.sort_unstable();
        assert_eq!(all, vec![1000, 1010, 1020, 1030]);
    }
}