relate-executor 1.4.0

In-memory graph and Cypher physical executor
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
//! The scalar operators' unit tests, split from `expr.rs` (the same
//! `tests.rs`-beside-the-module precedent as `eval`'s and `workspace`'s
//! own split): the checked-overflow and ADR-0175 slice-bound pins, the
//! regex whole-string-match anchors, the `+` rendering and charging arms,
//! and the equality-tolerant ordering operators.

use super::*;

fn s(x: &str) -> Value {
    Value::String(x.into())
}

fn int(x: i64) -> Value {
    Value::Int(x)
}

fn ty_err(msg: &str) -> String {
    Error::ty(msg).to_string()
}

/// The integer-list value every list-slice test slices over — the one
/// builder, where a per-test closure used to restate it.
fn int_list(xs: &[i64]) -> Value {
    Value::List(xs.iter().map(|&x| int(x)).collect::<Vec<_>>().into())
}

/// Live Neo4j 2026.10.1 raises 22003 for `i64::MIN / -1`; modulo keeps
/// answering 0 (the exact result, ADR-0569).
#[test]
fn integer_division_overflow_is_rejected_and_modulo_wraps() {
    assert_eq!(
        div(int(i64::MIN), int(-1)).unwrap_err().to_string(),
        ty_err("integer out of range")
    );
    assert_eq!(mod_op(int(i64::MIN), int(-1)).unwrap(), int(0));
}

/// Five characters of 1, 2, 4, 1 and 2 bytes: slice bounds count
/// characters, so a byte-indexed slice would split a code point.
const MB: &str = "añ😀bç";

fn mb_slice(start: Option<Value>, end: Option<Value>) -> Value {
    slice(&s(MB), start, end).unwrap()
}

#[test]
fn string_slice_indexes_by_character() {
    assert_eq!(mb_slice(Some(int(1)), Some(int(3))), s("ñ😀"));
    assert_eq!(mb_slice(Some(int(2)), None), s("😀bç"));
    assert_eq!(mb_slice(None, Some(int(2))), s("añ"));
    assert_eq!(mb_slice(None, None), s(MB));
    assert_eq!(mb_slice(Some(int(4)), Some(int(5))), s("ç"));
    // Integral floats are integer bounds.
    assert_eq!(
        mb_slice(Some(Value::Float(1.0)), Some(Value::Float(2.0))),
        s("ñ")
    );
}

#[test]
fn string_slice_negative_bounds_count_from_the_end() {
    assert_eq!(mb_slice(Some(int(-2)), None), s("bç"));
    assert_eq!(mb_slice(Some(int(-4)), Some(int(-1))), s("ñ😀b"));
    assert_eq!(mb_slice(None, Some(int(-3))), s("añ"));
}

#[test]
fn string_slice_out_of_range_bounds_clamp() {
    assert_eq!(mb_slice(Some(int(-10)), Some(int(10))), s(MB));
    assert_eq!(mb_slice(Some(int(3)), Some(int(99))), s("bç"));
    assert_eq!(mb_slice(Some(int(-99)), Some(int(2))), s("añ"));
}

#[test]
fn string_slice_empty_range_is_the_empty_string() {
    assert_eq!(mb_slice(Some(int(2)), Some(int(2))), s(""));
    assert_eq!(mb_slice(Some(int(4)), Some(int(1))), s(""));
    assert_eq!(mb_slice(Some(int(7)), None), s(""));
    assert_eq!(mb_slice(None, Some(int(-9))), s(""));
    assert_eq!(slice(&s(""), Some(int(0)), Some(int(1))).unwrap(), s(""));
}

#[test]
fn string_slice_null_and_non_integer_bounds() {
    assert_eq!(mb_slice(Some(Value::Null), Some(int(2))), Value::Null);
    assert_eq!(mb_slice(Some(int(1)), Some(Value::Null)), Value::Null);
    assert_eq!(mb_slice(Some(Value::Null), None), Value::Null);
    // A null bound wins over a non-integer one.
    assert_eq!(mb_slice(Some(s("x")), Some(Value::Null)), Value::Null);
    let err = |start, end| slice(&s(MB), start, end).unwrap_err().to_string();
    assert_eq!(err(Some(s("x")), None), ty_err("slice start"));
    // ADR-0175: a float bound truncates instead of erroring; a
    // non-numeric bound keeps the bare type error.
    assert_eq!(err(None, Some(Value::Bool(true))), ty_err("slice end"));
    // The start bound is checked first.
    assert_eq!(err(Some(s("x")), Some(s("y"))), ty_err("slice start"));
}

#[test]
fn list_slice_follows_the_same_bounds_rules() {
    let xs = int_list(&[0, 1, 2, 3]);
    let sl = |start, end| slice(&xs, start, end);
    assert_eq!(sl(Some(int(1)), Some(int(3))).unwrap(), int_list(&[1, 2]));
    assert_eq!(sl(Some(int(-3)), Some(int(-1))).unwrap(), int_list(&[1, 2]));
    assert_eq!(
        sl(Some(int(-9)), Some(int(9))).unwrap(),
        int_list(&[0, 1, 2, 3])
    );
    assert_eq!(sl(Some(int(3)), Some(int(1))).unwrap(), int_list(&[]));
    assert_eq!(sl(None, Some(Value::Null)).unwrap(), Value::Null);
    assert_eq!(
        sl(Some(Value::Bool(true)), None).unwrap_err().to_string(),
        ty_err("slice start")
    );
    assert_eq!(
        sl(None, Some(s("x"))).unwrap_err().to_string(),
        ty_err("slice end")
    );
}

/// The harness pins slice bounds across the whole i64 range: a bound
/// beyond Integer.MAX_VALUE clamps instead of raising NumberOutOfRange
/// (only a literal that does not fit an i64 is a parse-time error).
#[test]
fn list_slice_bounds_clamp_at_and_beyond_the_int32_range() {
    let xs = int_list(&[1, 2, 3, 4, 5]);
    let sl = |start, end| slice(&xs, start, end).unwrap();
    let i32_max_plus_1 = i64::from(i32::MAX) + 1;
    assert_eq!(sl(None, Some(int(i32_max_plus_1))), xs);
    assert_eq!(
        sl(Some(int(0)), Some(int(i32_max_plus_1))),
        int_list(&[1, 2, 3, 4, 5])
    );
    assert_eq!(
        sl(Some(int(i32_max_plus_1)), Some(int(i32_max_plus_1 + 1))),
        int_list(&[])
    );
    assert_eq!(sl(None, Some(int(i64::from(i32::MIN) - 1))), int_list(&[]));
    assert_eq!(sl(Some(int(i64::from(i32::MIN) - 1)), None), xs);
    assert_eq!(sl(None, Some(int(i64::MAX))), xs);
    assert_eq!(sl(Some(int(i64::MAX)), None), int_list(&[]));
    assert_eq!(sl(None, Some(int(i64::MIN))), int_list(&[]));
    assert_eq!(sl(Some(int(i64::MIN)), None), xs);
}

/// ADR-0631: a dynamic FLOAT slice bound saturates into the int32 range
/// instead of raising NumberOutOfRange — the report's three probes plus
/// the negative direction, for the list and the string share of the one
/// helper. The observable contract is "saturates, never errors": after
/// the negative-index and `[0, len]` clamping a slice result cannot
/// distinguish the int32 saturation from a plain i64 clamp (both land on
/// the same sequence window), so the pins assert the answers, with the
/// int32 conversion carried by the ADR and the code's one arm.
#[test]
fn float_slice_bounds_saturate_into_the_int32_range() {
    let xs = int_list(&[1, 2, 3]);
    let sl = |start, end| slice(&xs, start, end).unwrap();
    // The report's probes: an oversized start selects nothing, an
    // oversized end keeps the whole sequence.
    assert_eq!(
        sl(Some(Value::Float(f64::INFINITY)), Some(int(2))),
        int_list(&[])
    );
    assert_eq!(sl(Some(Value::Float(2147483648.0)), None), int_list(&[]));
    assert_eq!(sl(Some(Value::Float(9.3e18)), None), int_list(&[]));
    assert_eq!(sl(None, Some(Value::Float(f64::INFINITY))), xs);
    assert_eq!(sl(None, Some(Value::Float(9.3e18))), xs);
    // The negative spellings saturate to before-the-start: the whole
    // sequence from a -Infinity start, nothing before a -Infinity end.
    assert_eq!(sl(Some(Value::Float(f64::NEG_INFINITY)), None), xs);
    assert_eq!(
        sl(None, Some(Value::Float(f64::NEG_INFINITY))),
        int_list(&[])
    );
    assert_eq!(
        sl(Some(Value::Float(-9.3e18)), Some(int(2))),
        int_list(&[1, 2])
    );
    // The string share of the helper saturates identically (mb_slice is
    // the module's canonical string-slice helper).
    assert_eq!(mb_slice(Some(Value::Float(f64::INFINITY)), None), s(""));
    assert_eq!(mb_slice(None, Some(Value::Float(f64::NEG_INFINITY))), s(""));
    assert_eq!(mb_slice(Some(Value::Float(f64::NEG_INFINITY)), None), s(MB));
}

#[test]
fn add_concatenates_a_string_with_temporal_and_point_scalars() {
    let g = crate::graph::Graph::default();
    let graph = ExecGraph::read(&g);
    let point = Value::Point(relate_core::PointValue::cartesian_2d(1.0, 0.0));
    let point_text = "point({x: 1.0, y: 0.0, crs: 'cartesian'})";
    let cases: Vec<(Value, Value, String)> = vec![
        (s("hi"), duration(0, 1), "hiP1D".to_string()),
        (duration(0, 1), s("hi"), "P1Dhi".to_string()),
        (s("hi"), point.clone(), format!("hi{point_text}")),
        (point, s("hi"), format!("{point_text}hi")),
        // The scalar arms keep their old rendering.
        (s("hi"), int(2), "hi2".to_string()),
        (int(2), s("hi"), "2hi".to_string()),
    ];
    for (l, r, expected) in cases {
        assert_eq!(add(&graph, l, r).unwrap(), s(&expected), "{expected}");
    }
}

/// A UUID has a display form but is not a `+` operand: live rejects both
/// spellings, so neither mirror arm may concatenate it.
#[test]
fn add_rejects_a_uuid_operand_beside_a_string() {
    let g = crate::graph::Graph::default();
    let graph = ExecGraph::read(&g);
    for (l, r) in [(s("id="), Value::Uuid(1)), (Value::Uuid(1), s("!"))] {
        assert_eq!(
            add(&graph, l, r).unwrap_err().to_string(),
            ty_err("+ type mismatch")
        );
    }
}

#[test]
fn slice_of_null_or_a_non_sequence() {
    assert_eq!(
        slice(&Value::Null, Some(s("x")), None).unwrap(),
        Value::Null
    );
    assert_eq!(
        slice(&int(5), Some(s("x")), None).unwrap_err().to_string(),
        ty_err("slice needs a list")
    );
}

/// A non-null non-string regex operand is a type error (22NB1), and an
/// invalid pattern is an argument error (22N11) — neither is a null
/// result.
#[test]
fn regex_rejections_carry_the_neo4j_classes() {
    let g = crate::graph::Graph::default();
    let graph = ExecGraph::read(&g);
    let run = |l: Value, r: Value| binary(&graph, BinaryOp::Regex, l, r);
    // 22NB1: a non-string operand is a type mismatch, not null.
    assert!(run(s("a"), int(42)).is_err());
    // 22N11: an invalid pattern is an argument error, not a syntax error.
    assert_eq!(
        run(s("a"), s("[")).unwrap_err(),
        Error::argument_error("invalid regular expression")
    );
    // Null still propagates.
    assert_eq!(run(Value::Null, int(42)).unwrap(), Value::Null);
}

// `=~` is a whole-string match, not a substring search.
#[test]
fn regex_anchors_at_both_ends() {
    assert!(!regex_is_match("21_val", "1_.*").unwrap());
    assert!(regex_is_match("1_val", "1_.*").unwrap());
    // An alternation stays inside the anchors.
    assert!(!regex_is_match("ab", "a|b").unwrap());
    assert!(regex_is_match("a", "a|b").unwrap());
    assert!(regex_is_match("", "x*").unwrap());
    assert!(regex_is_match("x1_val", ".*1_.*").unwrap());
    assert!(regex_is_match("abc", "abc").unwrap());
    assert!(regex_is_match("abc", "b").is_ok_and(|m| !m));
    assert!(regex_is_match("abc", "[").is_err());
}

fn duration(months: i64, days: i64) -> Value {
    Value::Temporal(relate_core::Temporal::Duration {
        months,
        days,
        nanos: 0,
    })
}

/// A CASE WHEN arm's condition is a predicate: a runtime non-boolean
/// (a property read the plan cannot type) raises the typed
/// `requires a boolean` rejection — the same one every other predicate
/// position raises (Neo4j 22G03) — never a silent fall-through to the
/// next arm or ELSE.
#[test]
fn case_when_rejects_a_runtime_non_boolean_condition() {
    use crate::exec::test_support::{exec_autocommit, try_exec_cypher};
    use crate::graph::Graph;
    let graph = Graph::default();
    exec_autocommit(&graph, "CREATE (:Person {age: 38, name: 'Alice'})").unwrap();
    for predicate in ["n.age", "n.name"] {
        let query =
            format!("MATCH (n:Person) RETURN CASE WHEN {predicate} THEN 'yes' ELSE 'no' END AS a");
        let error = try_exec_cypher(&graph, &query)
            .err()
            .unwrap_or_else(|| panic!("{predicate}: a non-boolean WHEN must be rejected"));
        assert!(
            error.to_string().contains("requires a boolean"),
            "{predicate}: {error}"
        );
    }
}

/// A CASE without an aggregate keeps its lazy branches: only the taken
/// arm evaluates, so the plain form's unselected `1/0` never runs — the
/// behavior the aggregating projection's CASE arm must match (the shared
/// [`eval_case_with`] policy is what holds the two together).
#[test]
fn plain_case_evaluates_only_the_taken_branch() {
    use crate::exec::test_support::try_exec_cypher;
    use crate::graph::Graph;
    let result = try_exec_cypher(
        &Graph::default(),
        "RETURN CASE 1 WHEN 1 THEN 1 ELSE 1/0 END AS x",
    )
    .expect("the unselected ELSE arm must not evaluate");
    assert_eq!(result.rows, vec![vec![Value::Int(1)]]);
}

/// ADR-0463 regression (rust review round 1): a mixed list+string
/// operand pair builds a **list** (`other + 1` slots), not a string, so
/// its charge must be the list buffer. The former estimator's wildcard
/// string arms matched first and charged ~len+32 bytes, letting
/// `bigList + 'x'` bypass the budget.
#[test]
fn mixed_list_and_string_charges_the_list_buffer() {
    let g = crate::graph::Graph::default();
    let graph = ExecGraph::read(&g);
    let list = Value::List(vec![int(1), int(2), int(3)].into());
    let out = add(&graph, list, s("x")).unwrap();
    assert!(matches!(out, Value::List(_)), "{out:?}");
    assert_eq!(
        graph.expression_built_bytes(),
        crate::workspace::buffer_bytes(4, std::mem::size_of::<Value>() as u64,)
    );
}

/// The same pair under a one-byte cap: the list buffer allocation (one
/// slot per element plus the header) is refused, where the old
/// string-shaped estimate would have slipped through.
#[test]
fn mixed_list_and_string_is_refused_when_the_buffer_exceeds_the_cap() {
    let mut g = crate::graph::Graph::default();
    g.configure_expression_memory_limit(Some(1));
    let graph = ExecGraph::read(&g);
    let list = Value::List(vec![int(1), int(2), int(3)].into());
    let error = add(&graph, list, s("x")).unwrap_err();
    assert!(
        matches!(
            error,
            Error::Capacity {
                limit: 1,
                kind: relate_core::CapacityKind::Expression,
                ..
            }
        ),
        "{error}"
    );
}

fn cmp(op: BinaryOp, l: Value, r: Value) -> Value {
    let g = crate::graph::Graph::default();
    let graph = ExecGraph::read(&g);
    binary(&graph, op, l, r).unwrap()
}

// Durations have `=` but no ordering: `<`/`>` are null, `<=`/`>=` still
// hold for equal values (TCK TernaryComparisonAcceptance).
#[test]
fn equal_durations_satisfy_the_equality_tolerant_operators() {
    let d = duration(0, 1);
    assert_eq!(cmp(BinaryOp::Lt, d.clone(), d.clone()), Value::Null);
    assert_eq!(cmp(BinaryOp::Gt, d.clone(), d.clone()), Value::Null);
    assert_eq!(cmp(BinaryOp::Lte, d.clone(), d.clone()), Value::Bool(true));
    assert_eq!(cmp(BinaryOp::Gte, d.clone(), d.clone()), Value::Bool(true));
    // Different durations are incomparable in every direction.
    let other = duration(0, 2);
    for op in [BinaryOp::Lt, BinaryOp::Gt, BinaryOp::Lte, BinaryOp::Gte] {
        assert_eq!(cmp(op, d.clone(), other.clone()), Value::Null);
    }
}

/// Neo4j's `-` takes a duration right operand only: subtracting one
/// temporal from another is the 22N01 type mismatch, not a duration
/// between (`duration.between` answers that).
#[test]
fn subtracting_two_temporals_is_a_type_mismatch() {
    // One 2020-01-01 fixture serves both assertions.
    let date = Value::Temporal(temporal::parse_iso("date", "2020-01-01").unwrap());
    let error = sub(date.clone(), date.clone()).unwrap_err();
    assert_eq!(error.to_string(), ty_err("- type mismatch"));

    // A temporal minus a duration is still arithmetic.
    assert_eq!(
        sub(date, duration(0, 1)).unwrap(),
        Value::Temporal(temporal::parse_iso("date", "2019-12-31").unwrap())
    );
}

// UUIDs have `=` but no ordering either.
#[test]
fn equal_uuids_satisfy_the_equality_tolerant_operators() {
    let a = Value::Uuid(1);
    let b = Value::Uuid(2);
    assert_eq!(cmp(BinaryOp::Gt, a.clone(), b.clone()), Value::Null);
    assert_eq!(cmp(BinaryOp::Lt, b.clone(), a.clone()), Value::Null);
    assert_eq!(cmp(BinaryOp::Gte, a.clone(), a.clone()), Value::Bool(true));
    assert_eq!(cmp(BinaryOp::Lte, a.clone(), a.clone()), Value::Bool(true));
}