relate-executor 1.3.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
404
405
406
407
408
409
410
411
412
413
//! CIP-135 `WHEN`/`ELSE` through the real execution entry points: a
//! conditional is a per-row operator, so a driving row's `WHEN` sees the
//! state its predecessor's taken branch left (the CIP-213 NEXT-chain shape
//! "WHEN as a NEXT part sees updates made by the branch of a previous row"),
//! while a stage's other clauses keep their whole-stage batch semantics.

use relate_core::Value;

use super::test_support::{
    assert_expression_cap, column, exec_autocommit, exec_cypher, int_cell, string_cell,
    try_exec_cypher,
};
use super::QueryResult;
use crate::graph::Graph;

/// The integer cells of `name`, in result order.
fn integers(result: &QueryResult, name: &str) -> Vec<i64> {
    column(result, name, int_cell)
}

/// The string cells of `name`, in result order.
fn strings(result: &QueryResult, name: &str) -> Vec<String> {
    column(result, name, string_cell)
}

/// A graph seeded by one `setup` statement — the WHEN scenarios' starting
/// graph.
fn seeded(setup: &str) -> Graph {
    let graph = Graph::default();
    exec_autocommit(&graph, setup).unwrap();
    graph
}

/// A graph holding the one `(:State {size: 0})` node the WHEN scenarios
/// start from.
fn state_graph() -> Graph {
    seeded("CREATE (:State {size: 0})")
}

/// A `WHEN` predicate must be a boolean: a numeric or string property is a
/// type error (Neo4j 22G03), never truthy-coerced.
#[test]
fn when_rejects_a_non_boolean_predicate() {
    let graph = seeded("CREATE (:Person {age: 38, name: 'Alice'})");
    for predicate in ["n.age", "n.name"] {
        let query =
            format!("MATCH (n:Person) CALL (n) {{ WHEN {predicate} THEN RETURN 1 AS x }} RETURN x");
        let error = match exec_autocommit(&graph, &query) {
            Err(error) => error,
            Ok(_) => panic!("{predicate}: a non-boolean WHEN predicate must be rejected"),
        };
        assert!(
            error.to_string().contains("requires a boolean"),
            "{predicate}: {error}"
        );
    }
}

/// The cited regression: a `WHEN` in a NEXT chain is evaluated per row
/// against the state the previous row left, so only the first of the three
/// driving rows still sees `size = 0` — one hit instead of three.
#[test]
fn when_in_a_next_chain_sees_the_previous_rows_write() {
    let graph = state_graph();
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i \
         MATCH (b:State) \
         RETURN i, b \
         NEXT \
         WHEN b.size = 0 THEN \
           SET b.size = i \
           RETURN i AS hit",
    )
    .unwrap();
    assert_eq!(result.columns, vec!["hit".to_string()]);
    assert_eq!(integers(&result, "hit"), vec![1]);

    // The write the single hit made stands: row 1's `i`.
    let size = exec_cypher(&graph, "MATCH (b:State) RETURN b.size AS size");
    assert_eq!(integers(&size, "size"), vec![1]);
}

/// The ELSE twin: the first row takes `ELSE` and writes, and the following
/// rows' `WHEN` sees that write.
#[test]
fn when_in_a_next_chain_sees_the_previous_rows_else_write() {
    let graph = state_graph();
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i \
         MATCH (b:State) \
         RETURN i, b \
         NEXT \
         WHEN b.size <> 0 THEN \
           RETURN 0 AS hit \
         ELSE \
           SET b.size = i \
           RETURN i AS hit",
    )
    .unwrap();
    // Grouped by branch in written order: the two `WHEN` hits, then the
    // `ELSE` hit (CIP-213's observed conditional output order).
    assert_eq!(integers(&result, "hit"), vec![0, 0, 1]);
    let size = exec_cypher(&graph, "MATCH (b:State) RETURN b.size AS size");
    assert_eq!(integers(&size, "size"), vec![1]);
}

/// A label predicate in the `WHEN` reads the graph, not the row's label
/// snapshot: the predecessor's `SET b:Done` hides the label from the
/// following rows — one hit, not three.
#[test]
fn when_in_a_next_chain_sees_a_label_its_predecessor_set() {
    let graph = seeded("CREATE (:State)");
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i \
         MATCH (b:State) \
         RETURN i, b \
         NEXT \
         WHEN NOT b:Done THEN \
           SET b:Done \
           RETURN i AS hit",
    )
    .unwrap();
    assert_eq!(integers(&result, "hit"), vec![1]);

    let done = exec_cypher(&graph, "MATCH (b:State:Done) RETURN count(*) AS c");
    assert_eq!(integers(&done, "c"), vec![1]);
}

/// The removal twin: `REMOVE b:Todo` by the first row's branch takes the
/// label away from the following rows' `WHEN`.
#[test]
fn when_in_a_next_chain_sees_a_label_its_predecessor_removed() {
    let graph = seeded("CREATE (:State:Todo)");
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i \
         MATCH (b:State) \
         RETURN i, b \
         NEXT \
         WHEN b:Todo THEN \
           REMOVE b:Todo \
           RETURN i AS hit",
    )
    .unwrap();
    assert_eq!(integers(&result, "hit"), vec![1]);

    let todo = exec_cypher(&graph, "MATCH (b:State:Todo) RETURN count(*) AS c");
    assert_eq!(integers(&todo, "c"), vec![0]);
}

/// A property read in the `WHEN` does not fall back to the row's stale
/// property map once a predecessor's branch removed the property — one
/// hit, not three.
#[test]
fn when_in_a_next_chain_sees_a_property_its_predecessor_removed() {
    let graph = state_graph();
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i \
         MATCH (b:State) \
         RETURN i, b \
         NEXT \
         WHEN b.size IS NOT NULL THEN \
           REMOVE b.size \
           RETURN i AS hit",
    )
    .unwrap();
    assert_eq!(integers(&result, "hit"), vec![1]);

    let sized = exec_cypher(
        &graph,
        "MATCH (b:State) WHERE b.size IS NOT NULL RETURN count(*) AS c",
    );
    assert_eq!(integers(&sized, "c"), vec![0]);
}

/// The conditional's output is grouped by branch in written order, not in
/// driving-row order (Neo4j's observed order).
#[test]
fn when_in_a_next_chain_groups_its_output_by_branch() {
    let graph = seeded("CREATE (:L1 {x: 1}), (:L1 {x: 2}), (:L2 {x: 3})");
    let result = exec_autocommit(
        &graph,
        "MATCH (n) RETURN n ORDER BY n.x \
         NEXT \
         WHEN n.x > 2 THEN RETURN \"large number\" AS msg \
         WHEN n.x > 1 THEN RETURN \"small number\" AS msg \
         ELSE RETURN \"tiny number\" AS msg",
    )
    .unwrap();
    // Branch-major in written order, not the ascending driving-row order
    // (Neo4j's observed conditional output order).
    assert_eq!(
        strings(&result, "msg"),
        vec!["large number", "small number", "tiny number"]
    );
}

/// A taken branch body runs once per incoming row, so an aggregate inside
/// it sees one row, not the whole stage's stream.
#[test]
fn when_in_a_next_chain_aggregates_per_incoming_row() {
    let graph = Graph::default();
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i RETURN i NEXT WHEN true THEN RETURN count(*) AS c",
    )
    .unwrap();
    assert_eq!(integers(&result, "c"), vec![1, 1, 1]);
}

/// A write-only branch body projects no statement result: the row it ran
/// for contributes no result row, so a unit WHEN's result is empty even
/// though its write stands.
#[test]
fn a_unit_when_branch_contributes_no_rows() {
    let graph = state_graph();
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i \
         MATCH (b:State) \
         RETURN i, b \
         NEXT \
         WHEN COUNT { (:Made) } = 0 THEN \
           CREATE (:Made)",
    )
    .unwrap();
    assert!(result.rows.is_empty(), "{:?}", result.rows);

    // Only the first row's branch ran: one created node.
    let made = exec_cypher(&graph, "MATCH (m:Made) RETURN count(*) AS c");
    assert_eq!(integers(&made, "c"), vec![1]);
}

/// A unit `WHEN` stage is still one unit of output for the NEXT seeding
/// policy (ADR-0287): the following stage runs once, over one row.
#[test]
fn a_unit_when_next_part_seeds_one_row() {
    let graph = Graph::default();
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i \
         RETURN i \
         NEXT \
         WHEN i > 0 THEN \
           CREATE (:B) \
         NEXT \
         RETURN count(*) AS c",
    )
    .unwrap();
    assert_eq!(integers(&result, "c"), vec![1]);

    // Every driving row's branch still ran.
    let created = exec_cypher(&graph, "MATCH (b:B) RETURN count(*) AS c");
    assert_eq!(integers(&created, "c"), vec![3]);
}

/// The per-row scope is the conditional, not the stage: a filtering MATCH
/// still evaluates its predicate over the whole incoming batch before any
/// later update, so every row matches the pre-stage state.
#[test]
fn a_filtering_match_in_a_next_part_still_evaluates_before_the_update() {
    let graph = state_graph();
    let result = exec_autocommit(
        &graph,
        "UNWIND range(1, 3) AS i \
         RETURN i \
         NEXT \
         MATCH (b:State) WHERE b.size = 0 \
         SET b.size = i \
         RETURN i AS hit",
    )
    .unwrap();
    assert_eq!(integers(&result, "hit"), vec![1, 2, 3]);
    let size = exec_cypher(&graph, "MATCH (b:State) RETURN b.size AS size");
    assert_eq!(integers(&size, "size"), vec![3]);
}

/// A `CALL (vars) { WHEN … / ELSE … }` whose branch bodies are all unit
/// (write-only) output is itself a unit body: the outer rows pass through
/// with the branch writes applied, exactly like a plain unit subquery,
/// instead of being dropped by the inner-join merge (gap 3 — the labels
/// were written while `c` came back 0).
#[test]
fn a_unit_conditional_call_body_passes_the_outer_rows_through() {
    let graph = seeded("CREATE (:L {m: 'S'}), (:L {m: '.'})");
    let result = exec_autocommit(
        &graph,
        "MATCH (l:L) \
         CALL (l) { \
           WHEN l.m = 'S' THEN { SET l:A } \
           ELSE { SET l:B } \
         } \
         RETURN count(l) AS c",
    )
    .unwrap();
    assert_eq!(integers(&result, "c"), vec![2]);

    // Both branches ran on their own driving row: `S` gained `A`, `.` `B`.
    let a = exec_cypher(&graph, "MATCH (l:L:A) RETURN l.m AS m ORDER BY m");
    assert_eq!(strings(&a, "m"), vec!["S"]);
    let b = exec_cypher(&graph, "MATCH (l:L:B) RETURN l.m AS m ORDER BY m");
    assert_eq!(strings(&b, "m"), vec!["."]);
}

/// The gap report's baseline: a plain unit subquery body (a bare `SET`, no
/// conditional) keeps its pass-through — the unit-conditional refinement
/// changed no other body shape.
#[test]
fn a_plain_unit_subquery_call_body_still_passes_the_outer_rows_through() {
    let graph = seeded("CREATE (:L {m: 'S'}), (:L {m: '.'})");
    let result = exec_autocommit(
        &graph,
        "MATCH (l:L) CALL (l) { SET l:C } RETURN count(l) AS c",
    )
    .unwrap();
    assert_eq!(integers(&result, "c"), vec![2]);

    let labeled = exec_cypher(&graph, "MATCH (l:L:C) RETURN count(*) AS c");
    assert_eq!(integers(&labeled, "c"), vec![2]);
}

/// A unit conditional body under `IN TRANSACTIONS` still yields the outer
/// row per iteration, so every committed batch reports its status row and
/// the failing iteration reports its own, with the live error message
/// (CIP-198 pins `'/ by zero'`) — the committed rows used to disappear
/// with the body's empty result (gap 3's status variant).
#[test]
fn a_unit_conditional_call_in_transactions_reports_every_iteration() {
    let graph = Graph::default();
    let result = exec_autocommit(
        &graph,
        "UNWIND [1, 2, 3] AS i \
         CALL (i) { \
           WHEN i = 3 THEN { CREATE (:X {e: 1/0}) } \
           ELSE { MERGE (z:Z) SET z.i = i } \
         } IN TRANSACTIONS OF 1 ROW ON ERROR BREAK REPORT STATUS AS s \
         RETURN i, s.committed AS committed, s.errorMessage AS err",
    )
    .unwrap();
    assert_eq!(
        result.columns,
        vec!["i".to_string(), "committed".to_string(), "err".to_string()]
    );
    assert_eq!(
        result.rows,
        vec![
            vec![Value::Int(1), Value::Bool(true), Value::Null],
            vec![Value::Int(2), Value::Bool(true), Value::Null],
            vec![
                Value::Int(3),
                Value::Bool(false),
                Value::String("/ by zero".into())
            ],
        ]
    );

    // The committed iterations' writes stand (one `Z`, its `i` the second
    // iteration's); the failed iteration's node never landed.
    let z = exec_cypher(&graph, "MATCH (z:Z) RETURN count(*) AS c, z.i AS i");
    assert_eq!(integers(&z, "c"), vec![1]);
    assert_eq!(integers(&z, "i"), vec![2]);
    let x = exec_cypher(&graph, "MATCH (x:X) RETURN count(*) AS c");
    assert_eq!(integers(&x, "c"), vec![0]);
}

/// REL8-1845 (ADR-0463): the conditional's output buckets are a retaining
/// container — each driving row's branch production is charged to the
/// running total before its rows land, where the pre-conversion form held
/// the whole branch output unbilled. A 5_000-row NEXT chain whose branch
/// bodies each produce 200 rows (a million bucketed rows) refuses at the
/// lowered cap instead of building them.
#[test]
fn a_conditional_output_charges_each_driving_rows_branch_production() {
    let mut graph = Graph::default();
    graph.configure_expression_memory_limit(Some(1 << 20));
    let error = try_exec_cypher(
        &graph,
        "UNWIND range(1, 5_000) AS i \
         RETURN i \
         NEXT \
         WHEN i > 0 THEN UNWIND range(1, 200) AS j RETURN i * 1000 + j AS v",
    )
    .err()
    .expect("a million bucketed conditional rows must be refused at the lowered cap");
    assert_expression_cap(&error, 1 << 20);
}

/// REL8-1845: the same million-row conditional output under a cap it fits
/// in — the charge is honest, not a blanket refusal: the buckets build and
/// the branch-major order (the two `WHEN` rows of row 1, then row 2's, …)
/// holds for the statement result.
#[test]
fn a_charged_conditional_output_still_builds_under_a_cap_it_fits() {
    let mut graph = Graph::default();
    graph.configure_expression_memory_limit(Some(128 << 20));
    let result = exec_cypher(
        &graph,
        "UNWIND range(1, 5_000) AS i \
         RETURN i \
         NEXT \
         WHEN i > 0 THEN UNWIND range(1, 200) AS j RETURN i * 1000 + j AS v",
    );
    assert_eq!(result.rows.len(), 1_000_000);
    // Row-major within each driving row's branch output, branch order the
    // written order — the pinned conditional output order at scale.
    assert_eq!(result.rows[0][0], Value::Int(1001));
    assert_eq!(result.rows[199][0], Value::Int(1200));
    assert_eq!(result.rows[200][0], Value::Int(2001));
}