relate-executor 1.5.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
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
//! The `SET labels(n) = / += / -=` operators — the dynamic label-set
//! write surface. `=` makes the node's label list exactly the operand's
//! names (an omitted name leaves, an empty list strips every label),
//! `+=` adds the names not already carried after the ones kept, and
//! `-=` drops the names the operand names; a no-op spelling leaves both
//! the list and the graph-diff label counters untouched. The label list
//! reads back in write order — the pattern's carried names first, the
//! written ones after them.
//!
//! The one positional exception is ADR-0695: in `MERGE`'s `ON CREATE`
//! arm, a `labels(n) = …` overwrite keeps the merge pattern's own
//! labels for the node it created — the pattern anchors that node's
//! identity, so the overwrite replaces everything else. The anchor is
//! the created node's birth label set (dynamic label atoms resolved at
//! creation), it covers only nodes the merge created, and `+=` / `-=`
//! are unchanged in every position.
//!
//! The operand resolves through the one dynamic-label rule every
//! dynamic label atom shares (`STRING` or `LIST<STRING>`): a constant
//! operand is checked at admission — a zero-row `MATCH` still fails the
//! statement, rendered as a syntax error — while a runtime value is
//! checked per row, rendered as a type error; the rule's detail text is
//! the one authored spelling either way.
//!
//! Every operator test pins the label list the node carries after the
//! write together with the two graph-diff label counters, and the MERGE
//! tests pin the composition with `ON CREATE` / `ON MATCH`, whose arms
//! are ordinary SET item lists — only the taken arm applies.

use super::test_support::{
    column, error_of, exec_autocommit, exec_write, exec_write_uncommitted, list_cell, write_scratch,
};
use crate::graph::Graph;
use relate_core::{DynamicAccessError, DynamicNameToken, Error, Value};

/// The one pin of a `labels(n)` write: `Some(setup)` runs first
/// (committed) when the statement writes against a pre-existing graph,
/// then `cypher`'s single `ls` column — the label list the node carries
/// after the write — with the two graph-diff label counters.
fn assert_label_write(
    setup: Option<&str>,
    cypher: &str,
    expected: &[&str],
    added: i64,
    removed: i64,
) {
    let g = Graph::default();
    if let Some(setup) = setup {
        exec_autocommit(&g, setup).unwrap();
    }
    let result = exec_write_uncommitted(&g, cypher);
    let ls = column(&result, "ls", list_cell);
    let [ls] = ls.as_slice() else {
        panic!("one `ls` row, got {:?}", result.rows);
    };
    assert_eq!(*ls, label_list(expected), "{cypher}");
    assert_eq!(result.stats.labels_added, added, "{cypher}");
    assert_eq!(result.stats.labels_removed, removed, "{cypher}");
}

/// The one rejection pin: `cypher` fails with `detail` — the expected
/// rendering built by [`wrong_label_type`], whichever channel raises it.
fn assert_label_rejection(cypher: &str, detail: &str) {
    let g = Graph::default();
    let result = exec_write(&g, &write_scratch(&g), cypher);
    assert_eq!(error_of(result), detail, "{cypher}");
}

/// The expected label list as one result cell — the value model's own
/// construction, spelled beside the pins that compare against it (the
/// catalog family's `string_list` renders its SHOW/procedure rows; this
/// suite's oracle is not that policy).
fn label_list(names: &[&str]) -> Value {
    Value::List(
        names
            .iter()
            .map(|n| Value::String((*n).into()))
            .collect::<Vec<_>>()
            .into(),
    )
}

/// The dynamic-label rule's wrong-type error, built by the rule's own
/// constructor — the detail is the rule's one authored spelling, never
/// re-stated here.
fn wrong_label_error(type_name: &str) -> Error {
    Error::from(DynamicAccessError::wrong_type(
        DynamicNameToken::Label,
        type_name,
    ))
}

/// The executor channel's rendering: the typed error exactly as the
/// per-row rule raises it — `Error::from` is the same conversion the
/// executor's label evaluation applies.
fn per_row_rejection(type_name: &str) -> String {
    wrong_label_error(type_name).to_string()
}

/// The admission channel's rendering: the same error re-classed into
/// the compile-time syntax family — an `Error::syntax` whose detail
/// carries the kind token, exactly the rendering the front's validation
/// boundary emits for a constant operand's rejection
/// ([`Error::into_syntax`] is the model's own spelling of that
/// re-classing) — so no channel prefix or class token is spelled in
/// this suite.
fn admission_rejection(type_name: &str) -> String {
    wrong_label_error(type_name).into_syntax().to_string()
}

// `=` — the label list becomes exactly the operand's names.

#[test]
fn overwrite_makes_the_label_list_exactly_the_operands_names() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) = ['label1', 'label2'] RETURN labels(n) AS ls",
        &["label1", "label2"],
        2,
        1,
    );
}

#[test]
fn overwrite_with_an_empty_list_strips_every_label() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) = [] RETURN labels(n) AS ls",
        &[],
        0,
        1,
    );
}

#[test]
fn overwrite_on_an_unlabeled_node_sets_exactly_the_list() {
    assert_label_write(
        None,
        "CREATE (n) SET labels(n) = ['label1', 'label2'] RETURN labels(n) AS ls",
        &["label1", "label2"],
        2,
        0,
    );
}

#[test]
fn overwrite_from_a_bound_list_sets_the_bound_names() {
    assert_label_write(
        Some("CREATE (n)"),
        "WITH 'Foo' AS label MATCH (n) SET labels(n) = [label] RETURN labels(n) AS ls",
        &["Foo"],
        1,
        0,
    );
}

// `+=` — the carried names stay, the missing ones arrive.

#[test]
fn extend_with_an_empty_list_is_a_no_op() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) += [] RETURN labels(n) AS ls",
        &["label0"],
        0,
        0,
    );
}

#[test]
fn extend_on_an_unlabeled_node_sets_exactly_the_list() {
    assert_label_write(
        None,
        "CREATE (n) SET labels(n) += ['label1', 'label2'] RETURN labels(n) AS ls",
        &["label1", "label2"],
        2,
        0,
    );
}

#[test]
fn extend_keeps_carried_names_and_adds_only_the_missing_ones() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) WITH n, ['label1', 'label2'] AS ls SET labels(n) += ls \
         RETURN labels(n) AS ls",
        &["label0", "label1", "label2"],
        2,
        0,
    );
}

#[test]
fn extend_of_a_concatenated_expression_adds_the_missing_names() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) += ['label0'] + ['label1'] + ['label2'] \
         RETURN labels(n) AS ls",
        &["label0", "label1", "label2"],
        2,
        0,
    );
}

// `-=` — the named names leave; the rest stay.

#[test]
fn remove_drops_the_named_names() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) -= ['label0'] RETURN labels(n) AS ls",
        &[],
        0,
        1,
    );
}

#[test]
fn remove_with_an_empty_list_is_a_no_op() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) -= [] RETURN labels(n) AS ls",
        &["label0"],
        0,
        0,
    );
}

#[test]
fn remove_of_a_name_not_carried_is_silently_ignored() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) -= ['NotThere'] RETURN labels(n) AS ls",
        &["label0"],
        0,
        0,
    );
}

#[test]
fn remove_duplicates_in_the_operand_behave_as_one() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) -= ['label0', 'label0'] RETURN labels(n) AS ls",
        &[],
        0,
        1,
    );
}

#[test]
fn remove_on_an_unlabeled_node_is_a_no_op() {
    assert_label_write(
        None,
        "CREATE (n) SET labels(n) -= ['Anything'] RETURN labels(n) AS ls",
        &[],
        0,
        0,
    );
}

// The shared value rule: `STRING` or `LIST<STRING>`.

#[test]
fn a_scalar_string_operand_names_one_label() {
    assert_label_write(
        Some("CREATE (n:label0)"),
        "MATCH (n:label0) SET labels(n) = 'Foo' RETURN labels(n) AS ls",
        &["Foo"],
        1,
        1,
    );
}

#[test]
fn constant_bad_operands_are_rejected_before_execution() {
    // A zero-row MATCH still fails: the operand is a constant, checked
    // at admission — the write never runs, so no per-row rule can fire.
    for (item, type_name) in [
        ("SET labels(n) = null", "NULL"),
        ("SET labels(n) += null", "NULL"),
        ("SET labels(n) = [1]", "INTEGER"),
        ("SET labels(n) -= [null]", "NULL"),
    ] {
        let query = format!("MATCH (n:NoSuchLabel) {item} RETURN labels(n) AS ls");
        assert_label_rejection(&query, &admission_rejection(type_name));
    }
}

#[test]
fn runtime_bad_operands_are_rejected_per_row() {
    // WITH-bound values are not constants: admission checks only the
    // type class and leaves the value to the per-row rule — a bound
    // null, and a bound list's bad element, fail at execution on a
    // write that actually ran.
    for (query, type_name) in [
        (
            "CREATE (n:label0) WITH n, null AS ns SET labels(n) += ns RETURN labels(n) AS ls",
            "NULL",
        ),
        (
            "CREATE (n:label0) WITH n, [1] AS ls SET labels(n) = ls RETURN labels(n) AS ls",
            "INTEGER",
        ),
        (
            "CREATE (n:label0) WITH n, [null] AS rs SET labels(n) -= rs RETURN labels(n) AS ls",
            "NULL",
        ),
    ] {
        assert_label_rejection(query, &per_row_rejection(type_name));
    }
}

// MERGE composition: the taken arm's SET items apply, the other arm's
// do not.

#[test]
fn merge_on_a_matching_node_takes_the_on_match_arm() {
    assert_label_write(
        Some("CREATE (:Person {name: 'Gem'})"),
        "WITH 'NewLabel' AS label, 'OldLabel' AS label2 \
         MERGE (gem:Person {name: 'Gem'}) \
         ON CREATE SET labels(gem) = [label] \
         ON MATCH SET labels(gem) += [label2] \
         RETURN labels(gem) AS ls",
        &["Person", "OldLabel"],
        1,
        0,
    );
}

#[test]
fn merge_on_a_missing_node_takes_the_on_create_arm() {
    assert_label_write(
        None,
        "WITH 'Fresh' AS label \
         MERGE (gem:Person {name: 'Gem'}) \
         ON CREATE SET labels(gem) += [label] \
         ON MATCH SET labels(gem) += ['Never'] \
         RETURN labels(gem) AS ls",
        &["Person", "Fresh"],
        2,
        0,
    );
}

#[test]
fn merge_on_create_overwrite_on_a_binding_a_same_named_rel_clobbers_is_typed_rejected() {
    // The dynamic rel type reuse leaves the rel pattern's shape
    // undeclared, so `create_rel` rebinds `r` to the relationship — the
    // anchor scan must skip it and let the arm's target resolution raise
    // the typed non-node error (this pinned a panic before the fix).
    assert_label_rejection(
        "WITH 'T' AS t \
         MERGE (r:Person)-[r:$(t)]->(m) \
         ON CREATE SET labels(r) = ['X'] \
         RETURN labels(m) AS ls",
        "syntax error: SET labels() requires a node",
    );
}

// ADR-0695 — the ON CREATE overwrite keeps the merge pattern's own
// labels for the node it created; the anchor is that node's birth label
// set, and every other overwrite position stays a true overwrite.

#[test]
fn merge_on_create_overwrite_keeps_the_patterns_own_labels() {
    assert_label_write(
        None,
        "WITH 'NewLabel' AS label, 'OldLabel' AS label2 \
         MERGE (gem:Person {name: 'Gem'}) \
         ON CREATE SET labels(gem) = [label] \
         ON MATCH SET labels(gem) += [label2] \
         RETURN labels(gem) AS ls",
        &["Person", "NewLabel"],
        2,
        0,
    );
}

#[test]
fn merge_on_create_overwrite_reads_pattern_names_first_after_a_removal() {
    assert_label_write(
        None,
        "WITH 'New' AS name \
         MERGE (gem:Person {name: 'Gem'}) \
         ON CREATE SET labels(gem) -= ['Person'], labels(gem) = [name] \
         RETURN labels(gem) AS ls",
        &["Person", "New"],
        2,
        0,
    );
}

#[test]
fn merge_on_create_overwrite_on_a_no_label_pattern_behaves_like_plain_overwrite() {
    assert_label_write(
        None,
        "WITH 'Fresh' AS label \
         MERGE (gem {name: 'Gem'}) \
         ON CREATE SET labels(gem) = [label] \
         RETURN labels(gem) AS ls",
        &["Fresh"],
        1,
        0,
    );
}

#[test]
fn merge_on_create_overwrite_anchors_a_dynamic_pattern_label() {
    assert_label_write(
        None,
        "WITH 'Born' AS born \
         MERGE (n:$(born)) \
         ON CREATE SET labels(n) = ['Fresh'] \
         RETURN labels(n) AS ls",
        &["Born", "Fresh"],
        2,
        0,
    );
}

#[test]
fn merge_on_create_overwrite_replaces_an_earlier_items_label() {
    assert_label_write(
        None,
        "WITH 'New' AS name \
         MERGE (gem:Person {name: 'Gem'}) \
         ON CREATE SET gem:Extra, labels(gem) = [name] \
         RETURN labels(gem) AS ls",
        &["Person", "New"],
        2,
        0,
    );
}

#[test]
fn merge_on_create_overwrite_on_a_node_the_merge_did_not_create_is_plain() {
    assert_label_write(
        Some("CREATE (:Keep:Drop {name: 'Other'})"),
        "MATCH (other {name: 'Other'}) \
         WITH other, 'X' AS x \
         MERGE (gem:Person {name: 'Gem'}) \
         ON CREATE SET labels(other) = [x] \
         RETURN labels(other) AS ls",
        &["X"],
        2,
        2,
    );
}

#[test]
fn merge_on_match_overwrite_stays_a_true_overwrite() {
    assert_label_write(
        Some("CREATE (:Person {name: 'Gem'})"),
        "WITH 'Fresh' AS label \
         MERGE (gem:Person {name: 'Gem'}) \
         ON CREATE SET labels(gem) = ['Never'] \
         ON MATCH SET labels(gem) = [label] \
         RETURN labels(gem) AS ls",
        &["Fresh"],
        1,
        1,
    );
}

#[test]
fn merge_on_create_remove_still_drops_a_pattern_label() {
    assert_label_write(
        None,
        "MERGE (gem:Person {name: 'Gem'}) \
         ON CREATE SET labels(gem) -= ['Person'] \
         RETURN labels(gem) AS ls",
        &[],
        0,
        0,
    );
}