relate-parser 1.0.0

Cypher lexer, parser, and AST for the Relate graph database
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use super::*;
use relate_names::SmallName;

#[test]
fn test_parse_call_optional_procedure() {
    let stmt = parse("OPTIONAL CALL db.labels() YIELD label RETURN label").unwrap();
    match &stmt.clauses[0] {
        Clause::CallProcedure(cp) => assert!(cp.optional),
        _ => panic!("expected call procedure"),
    }
}

#[test]
fn test_parse_call_optional_subquery() {
    let stmt = parse("OPTIONAL CALL { RETURN 1 AS x } RETURN x").unwrap();
    match &stmt.clauses[0] {
        Clause::CallSubquery(csq) => assert!(csq.optional),
        _ => panic!("expected call subquery"),
    }
}

#[test]
fn test_parse_optional_before_call_or_match_only() {
    assert!(parse("MATCH (p) OPTIONAL RETURN p").is_err());
    assert!(parse("MATCH (p) CALL OPTIONAL { RETURN 1 AS x } RETURN p, x").is_err());
    assert!(parse("MATCH (p) OPTIONAL OPTIONAL CALL { RETURN 1 AS x } RETURN p, x").is_err());
}

#[test]
fn test_parse_call_where_in_yield() {
    let stmt =
        parse("OPTIONAL CALL db.labels() YIELD label WHERE label = 'X' RETURN label").unwrap();
    match &stmt.clauses[0] {
        Clause::CallProcedure(cp) => assert!(cp.where_expr.is_some()),
        _ => panic!("expected call procedure"),
    }
}

#[test]
fn test_parse_call_subquery() {
    let stmt = parse("CALL { MATCH (n) RETURN n }").unwrap();
    match &stmt.clauses[0] {
        Clause::CallSubquery(c) => {
            assert_eq!(c.statement.clauses.len(), 2);
        }
        _ => panic!("expected call subquery"),
    }
}

#[test]
fn test_parse_call_procedure() {
    let stmt = parse("CALL db.labels() YIELD label").unwrap();
    match &stmt.clauses[0] {
        Clause::CallProcedure(c) => {
            assert_eq!(c.procedure, "db.labels");
            assert_eq!(c.yields.names().collect::<Vec<_>>(), vec!["label"]);
        }
        _ => panic!("expected call procedure"),
    }
}

#[test]
fn test_parse_exists_subquery() {
    let stmt = parse("MATCH (n) WHERE EXISTS { MATCH (n)-[:KNOWS]->() } RETURN n").unwrap();
    match &stmt.clauses[0] {
        Clause::Match(m) => match m.where_expr.as_ref().unwrap() {
            Expr::ExistsSubquery(_) => {}
            _ => panic!("expected exists subquery"),
        },
        _ => panic!("expected match"),
    }
}

#[test]
fn test_parse_exists_subquery_with_union() {
    // Batch-1 regression coverage: `parse_regular_query` must be reused
    // inside EXISTS/COUNT/COLLECT bodies so a UNION chain between the
    // braces parses instead of erroring on "expected RightBrace, got
    // UNION" (CIP-63 union scenarios). Uses UNION ALL with RETURN
    // omitted in both branches -- CIP-63/66's consistency rule (see
    // `test_parse_count_subquery_union_omission_consistency_error`)
    // forbids a plain (distinct) UNION from omitting RETURN at all.
    let stmt = parse(
        "MATCH (a) WHERE EXISTS { MATCH (a)-[:X]->(b) UNION ALL MATCH (a)<-[:Y]-(c) } RETURN a",
    )
    .unwrap();
    match &stmt.clauses[0] {
        Clause::Match(m) => match m.where_expr.as_ref().unwrap() {
            Expr::ExistsSubquery(esq) => {
                assert!(esq.statement.union.is_some());
            }
            _ => panic!("expected exists subquery"),
        },
        _ => panic!("expected match"),
    }
}

#[test]
fn test_parse_count_subquery_with_union() {
    let stmt = parse("RETURN COUNT { MATCH (a) RETURN a UNION ALL MATCH (b) RETURN b AS a } AS c")
        .unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::CountSubquery(csq) => {
                let union = csq.statement.union.as_ref().expect("expected union");
                assert!(union.all);
            }
            _ => panic!("expected count subquery"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_count_empty_braces_is_error() {
    assert!(parse("RETURN COUNT { } AS n").is_err());
}

#[test]
fn test_parse_count_subquery_union_omission_consistency_error() {
    // CIP-66: mixing an explicit RETURN with an omitted one across UNION
    // ALL branches must fail to compile "for consistency".
    assert!(parse("RETURN COUNT { MATCH (m) RETURN 1 UNION ALL MATCH (p) } > 0 AS r").is_err());
}

#[test]
fn test_parse_collect_subquery_basic() {
    let stmt =
        parse("MATCH (p) RETURN COLLECT { MATCH (p)-[:F]->(f) RETURN f.name } AS xs").unwrap();
    match &stmt.clauses[1] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::CollectSubquery(csq) => {
                assert_eq!(csq.statement.clauses.len(), 2);
            }
            _ => panic!("expected collect subquery"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_collect_subquery_with_union() {
    let stmt = parse(
        "RETURN COLLECT { MATCH (a) RETURN a.name AS name UNION MATCH (b) RETURN b.name AS name } AS xs",
    )
    .unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::CollectSubquery(csq) => {
                assert!(csq.statement.union.is_some());
            }
            _ => panic!("expected collect subquery"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_collect_empty_braces_is_error() {
    assert!(parse("RETURN COLLECT { } AS xs").is_err());
}

#[test]
fn test_parse_collect_requires_single_return_item() {
    assert!(parse("RETURN COLLECT { RETURN 1, 2 } AS xs").is_err());
}

#[test]
fn test_parse_collect_rejects_return_star() {
    assert!(parse("MATCH (p) RETURN COLLECT { MATCH (p)-->(f) RETURN * } AS xs").is_err());
}

#[test]
fn test_parse_collect_requires_trailing_return() {
    assert!(parse("MATCH (p) RETURN COLLECT { MATCH (p)-->(f) } AS xs").is_err());
}

#[test]
fn test_parse_collect_union_branch_must_have_single_item_too() {
    assert!(parse("RETURN COLLECT { RETURN 1 AS a UNION RETURN 1 AS a, 2 AS b } AS xs").is_err());
}

#[test]
fn test_parse_collect_as_a_function_call_still_works() {
    // `collect` must keep working as the ordinary aggregation function
    // now that COLLECT is a keyword token.
    let stmt = parse("MATCH (n) RETURN collect(n.name) AS names").unwrap();
    match &stmt.clauses[1] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::FunctionCall(fc) => assert_eq!(fc.name.with_str(str::to_lowercase), "collect"),
            _ => panic!("expected function call"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_exists_simple_pattern() {
    let stmt = parse("MATCH (n) WHERE EXISTS { (n)-->() } RETURN n").unwrap();
    match &stmt.clauses[0] {
        Clause::Match(m) => match m.where_expr.as_ref().unwrap() {
            Expr::ExistsSubquery(e) => {
                assert_eq!(e.statement.clauses.len(), 1);
            }
            _ => panic!("expected exists subquery"),
        },
        _ => panic!("expected match"),
    }
}

// ---- CIP-141 CALL variable scope clause ----

#[test]
fn test_call_scope_clause_forms() {
    let cases: &[(&str, CallScope)] = &[
        (
            "CALL () { RETURN 1 AS x } RETURN x",
            CallScope::Vars(vec![]),
        ),
        (
            "WITH 1 AS a CALL (a) { RETURN a AS b } RETURN b",
            CallScope::Vars(vec!["a".into()]),
        ),
        (
            "WITH 1 AS a, 2 AS b CALL (a, b) { RETURN a + b AS c } RETURN c",
            CallScope::Vars(vec!["a".into(), "b".into()]),
        ),
        (
            "WITH 1 AS a CALL (*) { RETURN a AS b } RETURN b",
            CallScope::Wildcard,
        ),
        // Naked CALL still parses with `scope: None`.
        ("CALL { RETURN 1 AS x } RETURN x", CallScope::None),
    ];
    for (q, want) in cases {
        assert_eq!(call_subquery_of(&parse_ok(q)).scope, *want);
    }
}

#[test]
fn test_call_scope_clause_syntax_errors() {
    // Wildcard mixed with references, in either order.
    assert!(parse("CALL (*, a) { RETURN 1 AS x } RETURN x").is_err());
    assert!(parse("CALL (a, *) { RETURN 1 AS x } RETURN x").is_err());
    // A non-variable token in the scope clause.
    assert!(parse("CALL (1) { RETURN 1 AS x } RETURN x").is_err());
    // A variable declaration is forbidden in a scope clause.
    assert!(parse("CALL (1 AS a) { RETURN 1 AS x } RETURN x").is_err());
}

// ---- CIP-32/CIP-34/CIP-198/CIP-30 CALL ... IN TRANSACTIONS ----

#[test]
fn test_in_transactions_absent_by_default() {
    assert!(
        call_subquery_of(&parse_ok("CALL { RETURN 1 AS x } RETURN x"))
            .in_transactions
            .is_none()
    );
}

#[test]
fn test_in_transactions_basic_forms() {
    // Bare `IN TRANSACTIONS`, no batching modifier.
    let stmt = parse_ok("CALL { RETURN 1 AS x } IN TRANSACTIONS RETURN x");
    let cit = call_subquery_of(&stmt)
        .in_transactions
        .as_ref()
        .expect("IN TRANSACTIONS");
    assert_eq!(cit.concurrency, Concurrency::Sequential);
    assert!(cit.batch_size.is_none());
    assert_eq!(cit.on_error, OnErrorMode::Fail);
    assert!(cit.report_status.is_none());

    // With a variable scope clause too.
    assert!(call_subquery_of(&parse_ok(
        "CALL () { RETURN 1 AS x } IN TRANSACTIONS RETURN x"
    ))
    .in_transactions
    .is_some());

    // `OF n ROWS` and the singular `OF 1 ROW` form.
    let stmt = parse_ok("CALL { RETURN 1 AS x } IN TRANSACTIONS OF 5 ROWS RETURN x");
    let cit = call_subquery_of(&stmt)
        .in_transactions
        .as_ref()
        .expect("IN TRANSACTIONS");
    assert_eq!(
        cit.batch_size,
        Some(Expr::Literal(LiteralValue::Integer(5)))
    );
    assert!(parse("CALL { RETURN 1 AS x } IN TRANSACTIONS OF 1 ROW RETURN x").is_ok());
    // A parameter batch size.
    assert!(parse("CALL { RETURN 1 AS x } IN TRANSACTIONS OF $n ROWS RETURN x").is_ok());
}

#[test]
fn test_in_transactions_concurrency_forms() {
    let cases: &[(&str, Concurrency)] = &[
        (
            "CALL { RETURN 1 AS x } IN CONCURRENT TRANSACTIONS RETURN x",
            Concurrency::DefaultUnspecified,
        ),
        (
            "CALL { RETURN 1 AS x } IN 2 CONCURRENT TRANSACTIONS RETURN x",
            Concurrency::Value(Expr::Literal(LiteralValue::Integer(2))),
        ),
    ];
    for (q, want) in cases {
        assert_eq!(
            call_subquery_of(&parse_ok(q))
                .in_transactions
                .as_ref()
                .expect("IN TRANSACTIONS")
                .concurrency,
            *want
        );
    }

    // An arbitrary expression is allowed as the concurrency value.
    assert!(
        parse("CALL { RETURN 1 AS x } IN 1 + 2 CONCURRENT TRANSACTIONS OF 2 ROWS RETURN x").is_ok()
    );
}

#[test]
fn test_in_transactions_disjoint_by_forms() {
    // CIP-260: the hint only exists on the CONCURRENT form; `AUTO`/`NONE`
    // are keyword operands and a parenthesized list partitions by keys.
    let expr_form = parse_ok(
        "UNWIND range(0, 9) AS i CALL (i) { CREATE (:A {i: i}) } IN CONCURRENT TRANSACTIONS OF 5 ROWS DISJOINT BY (i, i + 1)",
    );
    assert_eq!(
        call_subquery_of(&expr_form)
            .in_transactions
            .as_ref()
            .expect("IN TRANSACTIONS")
            .disjoint,
        Some(DisjointBy::Expressions(vec![
            Expr::Variable("i".into()),
            Expr::Binary(Box::new(BinaryExpr {
                left: Expr::Variable("i".into()),
                op: PLUS,
                right: Expr::Literal(LiteralValue::Integer(1)),
            })),
        ]))
    );
    for (q, want) in [
        (
            "CALL (i) { RETURN i } IN CONCURRENT TRANSACTIONS DISJOINT BY AUTO RETURN i",
            DisjointBy::Auto,
        ),
        (
            "CALL (i) { RETURN i } IN CONCURRENT TRANSACTIONS DISJOINT BY NONE RETURN i",
            DisjointBy::None,
        ),
    ] {
        assert_eq!(
            call_subquery_of(&parse_ok(q))
                .in_transactions
                .as_ref()
                .expect("IN TRANSACTIONS")
                .disjoint,
            Some(want)
        );
    }
    // Sequential IN TRANSACTIONS has no disjointness to control.
    assert!(
        parse("CALL (i) { RETURN i } IN TRANSACTIONS OF 5 ROWS DISJOINT BY (i) RETURN i").is_err()
    );
    // An empty key list is invalid input.
    assert!(parse(
        "CALL (i) { RETURN i } IN CONCURRENT TRANSACTIONS OF 5 ROWS DISJOINT BY () RETURN i"
    )
    .is_err());
}

#[test]
fn test_in_transactions_on_error_forms() {
    for (query, expected) in [
        (
            "CALL { RETURN 1 AS x } IN TRANSACTIONS ON ERROR CONTINUE RETURN x",
            OnErrorMode::Continue,
        ),
        (
            "CALL { RETURN 1 AS x } IN TRANSACTIONS ON ERROR BREAK RETURN x",
            OnErrorMode::Break,
        ),
        (
            "CALL { RETURN 1 AS x } IN TRANSACTIONS ON ERROR FAIL RETURN x",
            OnErrorMode::Fail,
        ),
        // Bare RETRY: no duration, default (FAIL) fallback.
        (
            "CALL { RETURN 1 AS x } IN TRANSACTIONS ON ERROR RETRY RETURN x",
            OnErrorMode::Retry {
                duration: None,
                fallback: RetryFallback::Fail,
            },
        ),
        // `RETRY FOR <dur> SECONDS THEN BREAK`.
        (
            "CALL { RETURN 1 AS x } IN TRANSACTIONS ON ERROR RETRY FOR 2 SECONDS THEN BREAK RETURN x",
            OnErrorMode::Retry {
                duration: Some(Expr::Literal(LiteralValue::Integer(2))),
                fallback: RetryFallback::Break,
            },
        ),
        // `RETRY <dur> SECOND` with no `FOR` and no `THEN`.
        (
            "CALL { RETURN 1 AS x } IN TRANSACTIONS ON ERROR RETRY 1 SECOND RETURN x",
            OnErrorMode::Retry {
                duration: Some(Expr::Literal(LiteralValue::Integer(1))),
                fallback: RetryFallback::Fail,
            },
        ),
    ] {
        assert_eq!(
            call_subquery_of(&parse_ok(query))
                .in_transactions
                .as_ref()
                .expect("IN TRANSACTIONS")
                .on_error,
            expected
        );
    }
}

#[test]
fn test_in_transactions_report_status_and_ordering() {
    for q in [
        "CALL { RETURN 1 AS x } IN TRANSACTIONS ON ERROR CONTINUE REPORT STATUS AS s RETURN x",
        // REPORT STATUS may precede ON ERROR (CIP-34 concurrent-error-handling[7]).
        "CALL { RETURN 1 AS x } IN TRANSACTIONS REPORT STATUS AS s ON ERROR CONTINUE RETURN x",
    ] {
        let stmt = parse_ok(q);
        let cit = call_subquery_of(&stmt)
            .in_transactions
            .as_ref()
            .expect("IN TRANSACTIONS");
        assert_eq!(cit.on_error, OnErrorMode::Continue);
        assert_eq!(cit.report_status.as_ref(), Some(&SmallName::from_name("s")));
    }
}

#[test]
fn test_in_transactions_all_clauses_together() {
    let stmt = parse_ok(
        "CALL (x) { RETURN x } IN 2 CONCURRENT TRANSACTIONS OF 3 ROWS ON ERROR RETRY FOR 1 SECOND THEN CONTINUE REPORT STATUS AS s RETURN x, s",
    );
    let cit = call_subquery_of(&stmt)
        .in_transactions
        .as_ref()
        .expect("IN TRANSACTIONS");
    assert_eq!(
        cit.concurrency,
        Concurrency::Value(Expr::Literal(LiteralValue::Integer(2)))
    );
    assert_eq!(
        cit.batch_size,
        Some(Expr::Literal(LiteralValue::Integer(3)))
    );
    assert_eq!(
        cit.on_error,
        OnErrorMode::Retry {
            duration: Some(Expr::Literal(LiteralValue::Integer(1))),
            fallback: RetryFallback::Continue,
        }
    );
    assert_eq!(cit.report_status.as_ref(), Some(&SmallName::from_name("s")));
}