relate-parser 1.0.2

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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
use super::*;
use relate_test_support::run_with_big_stack;

// CIP-272 round-3 review: a `{` literal with no top-level `|` is a map
// literal, and the comprehension speculation must not full-parse it first.
// Before the lookahead in front of `finish_map_comprehension`, nesting like
// this doubled the parse work per level (measured on the pre-fix parser in
// a debug build: depth 12 took 6.5 ms, depth 16 took 105 ms, depth 20 took
// 1.7 s -- four times the work per two levels, so depth 50 never finishes).
// Depth 50 pins the shape as a bounded-time parse, and the counter staying
// at zero pins it as a single non-speculative pass. Parsing recurses once per
// nesting level, which overflows a default-sized (2 MiB) test thread before
// depth 50, so the parse runs on an explicitly sized stack -- for the same
// reason the retired in-repo CIP-eval gate set RUST_MIN_STACK for its
// binary (ADR-0520).
#[test]
fn test_pipe_less_nesting_is_not_speculatively_parsed() {
    run_with_big_stack(|| {
        let depth = 50;
        let mut source = String::from("m");
        for _ in 0..depth {
            source = format!("{{a: b IN {source}}}");
        }
        let count = || super::SPECULATIVE_COMPREHENSION_PARSES.with(std::cell::Cell::get);
        let before = count();
        let expr =
            parse_expression_str(&source).expect("pipe-less nesting has to parse as a map literal");
        let speculative = count() - before;
        assert_eq!(
            speculative, 0,
            "no speculative comprehension parse may start for a pipe-less brace"
        );
        assert!(
            matches!(expr, Expr::Map(_)),
            "expected a map literal, got {expr:?}"
        );
    });
}

// `[true IN [true], false]` is a two-element list literal whose first
// element is a boolean `IN` expression, not a malformed comprehension:
// a comma can never appear inside a comprehension, so the parser restores
// the literal reading when it meets one (Neo4j's preference).
#[test]
fn test_comma_after_in_source_parses_as_a_list_literal() {
    let stmt = parse("RETURN [true IN [true], false]").unwrap();
    let Clause::Return(ret) = &stmt.clauses[0] else {
        panic!("expected RETURN");
    };
    let Expr::List(items) = &ret.items[0].expr else {
        panic!("expected a list literal, got {:?}", ret.items[0].expr);
    };
    assert_eq!(items.len(), 2);
    assert!(matches!(items[0], Expr::In(_)), "got {:?}", items[0]);

    // The nested shape from the source: the outer `[v IN ...]` is still a
    // comprehension, while the inner list is a literal.
    let stmt = parse("RETURN null IN [var0 IN [true IN [true], false]]").unwrap();
    let Clause::Return(ret) = &stmt.clauses[0] else {
        panic!("expected RETURN");
    };
    let Expr::In(outer) = &ret.items[0].expr else {
        panic!("expected an IN expression, got {:?}", ret.items[0].expr);
    };
    let Expr::ListComprehension(inner) = &outer.list else {
        panic!("expected a list comprehension, got {:?}", outer.list);
    };
    assert!(matches!(inner.list_expr, Expr::List(_)));
}

// The legacy repeated-colon relationship type separator is legal for a
// bare type list and rejected beside a variable, inline properties or a
// variable length (Neo4j HelpfulErrorMessages).
#[test]
fn test_legacy_pipe_colon_relationship_types() {
    parse("MATCH ()-[:A|:B]->() RETURN 1").expect("a bare legacy type list parses");
    for query in [
        "MATCH ()-[x:A|:B]->() RETURN 1",
        "MATCH ()-[:A|:B {p: 1}]->() RETURN 1",
        "MATCH ()-[:A|:B*1..2]->() RETURN 1",
        // Regression (fail-open): the legacy-pipe-colon fact is returned
        // from the type-spec parse, not carried in a clause-scoped flag.
        // The earlier `a:X|:Y` predicate used to leave that flag set (its
        // `parse_label_spec(true)` caller never restored it), which made
        // the relationship rejection below silently not fire.
        "MATCH (a WHERE a:X|:Y)-[r:A|:B]->(b) RETURN 1",
    ] {
        assert!(parse(query).is_err(), "{query} must be rejected");
    }
}

// SET/REMOVE property targets accept the same general expression bases
// Neo4j does: a parameter, a null/literal, and a CASE expression.
#[test]
fn test_set_and_remove_accept_expression_targets() {
    parse("MATCH (n) SET $node['k'] = 1").expect("a parameter base parses");
    parse("REMOVE null.p").expect("a null base parses");
    parse("REMOVE CASE WHEN true THEN 1 ELSE 2 END.p").expect("a CASE base parses");
    // SET reads the same CIP-131 production as REMOVE, so the literal bases
    // parse there too (the two used to disagree: `SET null.p = 1` fell
    // through to a variable read).
    parse("SET null.p = 1").expect("SET accepts a null base");
    parse("SET true.p = 1").expect("SET accepts a boolean-literal base");
    parse("SET CASE WHEN true THEN 1 ELSE 2 END.p = 1").expect("SET accepts a CASE base");
}

// A map projection with no selectors (`x {}`) is the empty map projection,
// not an error: the brace lookahead must recognize `{` + `}`.
#[test]
fn test_empty_map_projection_parses() {
    let stmt = parse("RETURN x {}").unwrap();
    let Clause::Return(ret) = &stmt.clauses[0] else {
        panic!("expected RETURN");
    };
    let Expr::MapProjection(projection) = &ret.items[0].expr else {
        panic!("expected a map projection, got {:?}", ret.items[0].expr);
    };
    assert!(projection.items.is_empty());
}

#[test]
fn test_parse_expressions() {
    // Arithmetic
    let stmt = parse("RETURN 1 + 2 * 3").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Binary(b) => {
                assert_eq!(b.op, "+");
                match &b.right {
                    Expr::Binary(b2) => assert_eq!(b2.op, "*"),
                    _ => panic!("expected binary"),
                }
            }
            _ => panic!("expected binary"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_comparison() {
    let stmt = parse("RETURN 1 = 1").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Comparison(c) => assert_eq!(c.op, "="),
            _ => panic!("expected comparison"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_boolean_ops() {
    let stmt = parse("RETURN true AND false OR NOT true").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Binary(b) => assert_eq!(b.op, "OR"),
            _ => panic!("expected binary"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_is_null() {
    let stmt = parse("MATCH (n) WHERE n.name IS NULL RETURN n").unwrap();
    match &stmt.clauses[0] {
        Clause::Match(m) => match m.where_expr.as_ref().unwrap() {
            Expr::IsNull(e) => assert!(!e.negated),
            _ => panic!("expected is null"),
        },
        _ => panic!("expected match"),
    }
}

#[test]
fn test_parse_is_not_null() {
    let stmt = parse("MATCH (n) WHERE n.name IS NOT NULL RETURN n").unwrap();
    match &stmt.clauses[0] {
        Clause::Match(m) => match m.where_expr.as_ref().unwrap() {
            Expr::IsNull(e) => assert!(e.negated),
            _ => panic!("expected is not null"),
        },
        _ => panic!("expected match"),
    }
}

#[test]
fn test_parse_in_expr() {
    let stmt = parse("RETURN 1 IN [1, 2, 3]").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::In(_) => {}
            _ => panic!("expected in expr"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_function_call() {
    let stmt = parse("RETURN toUpper('hello')").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::FunctionCall(f) => {
                assert_eq!(f.name, "toUpper");
                assert_eq!(f.args.len(), 1);
            }
            _ => panic!("expected function call"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_count_star() {
    let stmt = parse("RETURN count(*)").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::CountStar => {}
            _ => panic!("expected count star"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_case_expr() {
    let stmt = parse("RETURN CASE WHEN true THEN 1 ELSE 2 END").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Case(c) => {
                assert!(matches!(
                    &c.whens[0].condition,
                    Expr::Literal(LiteralValue::Bool(true))
                ));
                assert_eq!(c.whens.len(), 1);
                assert!(c.else_expr.is_some());
            }
            _ => panic!("expected case"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_simple_case() {
    let stmt = parse("RETURN CASE n.type WHEN 'A' THEN 1 WHEN 'B' THEN 2 END").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Case(c) => {
                // The simple form's test operand is lowered into each WHEN
                // as the equality comparison's left side.
                assert_eq!(c.whens.len(), 2);
                for (when, value) in c.whens.iter().zip(["A", "B"]) {
                    let Expr::Comparison(cmp) = &when.condition else {
                        panic!("expected equality comparison, got {:?}", when.condition);
                    };
                    assert_eq!(cmp.op, "=");
                    assert!(matches!(cmp.left, Expr::Property(_)));
                    assert!(matches!(
                        &cmp.right,
                        Expr::Literal(LiteralValue::String(s)) if s == value
                    ));
                }
            }
            _ => panic!("expected case"),
        },
        _ => panic!("expected return"),
    }
}

/// The RETURN item's expression as a simple-CASE `Expr::Case`.
fn case_return(cypher: &str) -> crate::ast::CaseExpr {
    let stmt = parse(cypher).unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Case(c) => (**c).clone(),
            other => panic!("expected case, got {other:?}"),
        },
        _ => panic!("expected return"),
    }
}

/// Generalized simple-CASE `WHEN <comparison operator> <expr>`
/// (CIP-143/CIP-190): the CASE operand is the implicit left-hand side.
#[test]
fn test_parse_simple_case_comparison_when_operand() {
    let c = case_return("RETURN CASE x WHEN < 1 THEN 1 ELSE 2 END");
    assert_eq!(c.whens.len(), 1);
    let Expr::Comparison(cmp) = &c.whens[0].condition else {
        panic!("expected comparison predicate");
    };
    assert_eq!(cmp.op, "<");
    assert!(matches!(&cmp.left, Expr::Variable(v) if v == "x"));
}

/// Generalized simple-CASE `WHEN =~ <regex>` operand.
#[test]
fn test_parse_simple_case_regex_when_operand() {
    let c = case_return("RETURN CASE n.name WHEN =~ 'C.*t' THEN 10 ELSE 15 END");
    let Expr::Comparison(cmp) = &c.whens[0].condition else {
        panic!("expected comparison predicate");
    };
    assert_eq!(cmp.op, "=~");
}

/// Comma-separated `<when operand list>` (CIP-143/CIP-190): the elements
/// fold into one OR-ed predicate over the CASE operand.
#[test]
fn test_parse_simple_case_when_operand_list() {
    let c = case_return("RETURN CASE n.salary WHEN 1000, 1500 THEN 'low' END");
    assert_eq!(c.whens.len(), 1);
    let Expr::Binary(or) = &c.whens[0].condition else {
        panic!("expected OR of equality predicates");
    };
    assert_eq!(or.op, "OR");
    assert!(matches!(&or.left, Expr::Comparison(cmp) if cmp.op == "="));
    assert!(matches!(&or.right, Expr::Comparison(cmp) if cmp.op == "="));
}

/// A list mixing a comparison predicate and an `IS NULL` predicate.
#[test]
fn test_parse_simple_case_mixed_when_operand_list() {
    let c = case_return("RETURN CASE x WHEN < 1500, IS NULL THEN 'low' END");
    let Expr::Binary(or) = &c.whens[0].condition else {
        panic!("expected OR of predicates");
    };
    assert_eq!(or.op, "OR");
    assert!(matches!(&or.left, Expr::Comparison(cmp) if cmp.op == "<"));
    assert!(matches!(&or.right, Expr::IsNull(_)));
}

/// Simple-CASE `WHEN IS [NOT] [<normal form>] NORMALIZED` (CIP-133).
#[test]
fn test_parse_simple_case_normalized_when_operand() {
    let c = case_return("RETURN CASE n.name WHEN IS NOT NFKD NORMALIZED THEN 3 ELSE 15 END");
    let Expr::IsNormalized(norm) = &c.whens[0].condition else {
        panic!("expected normalized predicate");
    };
    assert_eq!(norm.form.as_deref(), Some("NFKD"));
    assert!(norm.negated);
}

/// Simple-CASE bare `WHEN :: <value type>` (CIP-87).
#[test]
fn test_parse_simple_case_bare_type_predicate_when_operand() {
    let c = case_return("RETURN CASE n WHEN :: POINT THEN 6 ELSE 15 END");
    assert!(matches!(&c.whens[0].condition, Expr::IsType(_)));
}

#[test]
fn test_parse_parameter() {
    let stmt = parse("MATCH (n) WHERE n.name = $name RETURN n").unwrap();
    match &stmt.clauses[0] {
        Clause::Match(m) => match m.where_expr.as_ref().unwrap() {
            Expr::Comparison(c) => match &c.right {
                Expr::Parameter(name) => assert_eq!(name, "name"),
                _ => panic!("expected parameter"),
            },
            _ => panic!("expected comparison"),
        },
        _ => panic!("expected match"),
    }
}

#[test]
fn test_parse_property_access() {
    let stmt = parse("RETURN n.name").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Property(p) => {
                assert_eq!(p.property, "name");
            }
            _ => panic!("expected property access"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_list_literal() {
    let stmt = parse("RETURN [1, 2, 3]").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::List(l) => assert_eq!(l.len(), 3),
            _ => panic!("expected list"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_map_literal() {
    let stmt = parse("RETURN {a: 1, b: 2}").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Map(pairs) => {
                assert_eq!(pairs.len(), 2);
                assert_eq!(pairs[0].key, "a");
                assert_eq!(pairs[1].key, "b");
            }
            _ => panic!("expected map"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_subscript() {
    let stmt = parse("RETURN list[0]").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Subscript(s) => {
                assert!(!s.slice);
                assert!(s.index.is_some());
            }
            _ => panic!("expected subscript"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_slice() {
    let stmt = parse("RETURN list[1..3]").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Subscript(s) => {
                assert!(s.slice);
                assert!(s.from.is_some());
                assert!(s.to.is_some());
            }
            _ => panic!("expected subscript"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_list_comprehension() {
    let stmt = parse("RETURN [x IN [1,2,3] WHERE x > 1 | x * 2]").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::ListComprehension(lc) => {
                assert_eq!(lc.variable, "x");
                assert!(lc.where_expr.is_some());
                assert!(lc.map_expr.is_some());
            }
            _ => panic!("expected list comprehension"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_reduce() {
    let stmt = parse("RETURN reduce(acc = 0, x IN [1,2,3] | acc + x)").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Reduce(re) => {
                assert_eq!(re.accumulator, "acc");
                assert_eq!(re.variable, "x");
            }
            _ => panic!("expected reduce"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_allreduce() {
    let stmt = parse("RETURN allReduce(acc = 0, x IN [1,2,3] | acc + x, acc < 10)").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::AllReduce(ar) => {
                assert_eq!(ar.accumulator, "acc");
                assert_eq!(ar.variable, "x");
            }
            _ => panic!("expected allReduce"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_allreduce_requires_the_third_predicate_argument() {
    // allReduce is the three-argument Cypher 25 form (accumulator, update,
    // keep-going predicate); the two-argument `reduce()` spelling inside
    // an allReduce call is a parse error, not a silent fallback to
    // `reduce` semantics.
    assert!(parse("RETURN allReduce(acc = 0, x IN [1,2,3] | acc + x)").is_err());
}

#[test]
fn test_parse_concat_operator_is_left_associative_like_plus() {
    // `||` shares precedence with `+`/`-` and is parsed left-associatively,
    // producing the same nested-Binary shape `+` would.
    let stmt = parse("RETURN [1] || [2] || [3]").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Binary(outer) => {
                assert_eq!(outer.op, "||");
                match &outer.left {
                    Expr::Binary(inner) => assert_eq!(inner.op, "||"),
                    _ => panic!("expected nested binary || on the left"),
                }
            }
            _ => panic!("expected binary ||"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_quantifier() {
    let stmt = parse("RETURN all(x IN [1,2,3] WHERE x > 0)").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Quantifier(q) => {
                assert_eq!(q.quantifier, "all");
                assert_eq!(q.variable, "x");
                assert!(q.predicate.is_some());
            }
            _ => panic!("expected quantifier"),
        },
        _ => panic!("expected return"),
    }
}

// A list predicate used as a *function argument* (`toBoolean(all(...))`)
// must keep its quantifier reading: the enclosing call's explicit
// `f(ALL x)` set-quantifier spelling had been swallowing the leading ALL,
// so the argument parsed as a plain `all(...)` call and died on WHERE.
#[test]
fn test_quantifier_as_function_argument_is_not_a_set_quantifier() {
    let stmt = parse("RETURN toBoolean(all(n0 IN [1, 2] WHERE n0 > 0)) AS r").unwrap();
    let Clause::Return(r) = &stmt.clauses[0] else {
        panic!("expected return");
    };
    let Expr::FunctionCall(call) = &r.items[0].expr else {
        panic!("expected function call, got {:?}", r.items[0].expr);
    };
    assert_eq!(call.name, "toBoolean");
    assert!(
        matches!(call.args[0], Expr::Quantifier(_)),
        "the argument must stay a list predicate, got {:?}",
        call.args[0]
    );
    // The nested reading composes with the aggregate quantifier spelling:
    // `count(all(x IN … WHERE …))` aggregates the predicate's value.
    assert!(parse("RETURN count(all(x IN [1, 2] WHERE x > 1)) AS c").is_ok());
    // The plain set-quantifier spelling still reads as one.
    assert!(parse("RETURN count(ALL 1) AS c").is_ok());
}

// LABS-201: agents repeatedly use patterns inside list predicates without
// the required EXISTS {}, and spell the predicate with the GQL pipe instead
// of WHERE. Both used to fail as a bare `expected RightParen, got |`; the
// parser now names the fix in each case.
#[test]
fn test_quantifier_pipe_instead_of_where_names_the_fix() {
    for q in ["any", "all", "none", "single"] {
        let err = parse(&format!("MATCH (n) WHERE {q}(x IN [1,2] | x > 1) RETURN n")).unwrap_err();
        assert!(
            err.message.contains("WHERE"),
            "{q}: expected the error to point at WHERE, got {err:?}"
        );
    }
}

#[test]
fn test_pattern_in_list_predicate_points_at_exists() {
    for q in ["any", "all", "none", "single"] {
        let err = parse(&format!(
            "MATCH (a) WHERE {q}((a)-[:R]->(b) | b.name = 'x') RETURN a"
        ))
        .unwrap_err();
        assert!(
            err.message.contains("EXISTS"),
            "{q}: expected the error to point at EXISTS, got {err:?}"
        );
    }
}

// The pipe-less pattern form keeps its existing parse: the EXISTS-targeted
// lookahead must not hijack it (the validator reports it as a pattern
// predicate outside WHERE).
#[test]
fn test_pattern_in_quantifier_without_pipe_still_parses() {
    assert!(parse("RETURN any((a)-[:R]->(b))").is_ok());
    // An arithmetic argument is not a pattern and keeps the generic
    // right-paren error path.
    assert!(parse("RETURN any((1 + 2) > 3)").is_ok());
}

#[test]
fn test_parse_has_label() {
    let stmt = parse("MATCH (n) WHERE n:Person RETURN n").unwrap();
    match &stmt.clauses[0] {
        Clause::Match(m) => match m.where_expr.as_ref().unwrap() {
            Expr::HasLabel(h) => {
                assert_eq!(h.label_expr.as_label_list(), Some(vec!["Person".into()]));
            }
            _ => panic!("expected has label"),
        },
        _ => panic!("expected match"),
    }
}

#[test]
fn test_parse_shortest_path_expr() {
    // shortestPath is parsed as an expression, not in MATCH pattern context
    let stmt = parse("RETURN shortestPath((a)-[*]->(b))").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::ShortestPath(sp) => {
                assert!(!sp.all);
                assert_eq!(sp.pattern.elements.len(), 3);
            }
            _ => panic!("expected shortest path expr"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_unary_minus() {
    let stmt = parse("RETURN -42").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Unary(u) => {
                assert_eq!(u.op, "-");
                match &u.expr {
                    Expr::Literal(LiteralValue::Integer(v)) => assert_eq!(*v, 42),
                    _ => panic!("expected integer"),
                }
            }
            _ => panic!("expected unary"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_namespaced_function() {
    let stmt = parse("RETURN point.distance(p1, p2)").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::FunctionCall(f) => {
                assert_eq!(f.name, "point.distance");
                assert_eq!(f.args.len(), 2);
            }
            _ => panic!("expected function call"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_exponentiation() {
    let stmt = parse("RETURN 2 ^ 3").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Binary(b) => assert_eq!(b.op, "^"),
            _ => panic!("expected binary"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_xor() {
    let stmt = parse("RETURN true XOR false").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Binary(b) => assert_eq!(b.op, "XOR"),
            _ => panic!("expected binary"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_count_distinct() {
    let stmt = parse("RETURN count(DISTINCT n)").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::FunctionCall(f) => {
                assert_eq!(f.name, "count");
                assert!(f.distinct);
            }
            _ => panic!("expected function call"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_modulo() {
    let stmt = parse("RETURN 10 % 3").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Binary(b) => assert_eq!(b.op, "%"),
            _ => panic!("expected binary"),
        },
        _ => panic!("expected return"),
    }
}

#[test]
fn test_parse_chained_comparison() {
    let stmt = parse("RETURN 1 < 2 < 3").unwrap();
    match &stmt.clauses[0] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::Binary(b) => {
                assert_eq!(b.op, "AND");
            }
            _ => panic!("expected binary AND for chained comparison"),
        },
        _ => panic!("expected return"),
    }
}

// -- PROPERTY_EXISTS (CIP-247) --

#[test]
fn test_parse_property_exists_basic() {
    let stmt = parse("MATCH (p) RETURN PROPERTY_EXISTS(p, email) AS e").unwrap();
    match &stmt.clauses[1] {
        Clause::Return(r) => match &r.items[0].expr {
            Expr::FunctionCall(fc) => {
                assert_eq!(fc.name, "property_exists");
                assert_eq!(fc.args.len(), 2);
                assert_eq!(fc.args[0], Expr::Variable("p".into()));
                assert_eq!(
                    fc.args[1],
                    Expr::Literal(LiteralValue::String("email".into()))
                );
            }
            other => panic!("expected function call, got {other:?}"),
        },
        other => panic!("expected return, got {other:?}"),
    }
}

#[test]
fn test_parse_property_exists_literal_property_name_is_error() {
    // A string literal in property-name position is a compile-time error.
    assert!(parse("RETURN PROPERTY_EXISTS(p, \"email\")").is_err());
}

#[test]
fn test_parse_property_exists_non_variable_first_arg_is_error() {
    assert!(parse("RETURN PROPERTY_EXISTS(1, name)").is_err());
}

#[test]
fn test_parse_property_exists_expression_property_name_is_error() {
    assert!(parse("RETURN PROPERTY_EXISTS(p, a + b)").is_err());
    assert!(parse("RETURN PROPERTY_EXISTS(p, toString(x))").is_err());
}