use super::*;
use relate_test_support::run_with_big_stack;
#[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:?}"
);
});
}
#[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]);
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(_)));
}
#[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",
"MATCH ()-[:A|:B*..2]->() RETURN 1",
"MATCH (a WHERE a:X|:Y)-[r:A|:B]->(b) RETURN 1",
] {
assert!(parse(query).is_err(), "{query} must be rejected");
}
}
#[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");
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");
}
#[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() {
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) => {
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"),
}
}
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"),
}
}
#[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"));
}
#[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, "=~");
}
#[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 == "="));
}
#[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(_)));
}
#[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);
}
#[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() {
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() {
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"),
}
}
#[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]
);
assert!(parse("RETURN count(all(x IN [1, 2] WHERE x > 1)) AS c").is_ok());
assert!(parse("RETURN count(ALL 1) AS c").is_ok());
}
#[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:?}"
);
}
}
#[test]
fn test_pattern_in_quantifier_without_pipe_still_parses() {
assert!(parse("RETURN any((a)-[:R]->(b))").is_ok());
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() {
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"),
}
}
#[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() {
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());
}