relate-executor 1.2.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
//! The commit-loop admission probe: the per-slot overlay, its rejections,
//! the final-slot short-circuit, and the pre-preflight classification
//! boundary.

use relate_core::{Error, Value};

use crate::graph::Graph;
use crate::redo::RedoOp;
use crate::SlotPosition;

use super::{create_node, create_rel, seeded, seeded_constrained, tuple, unique, unique_rel};

/// The create-then-X fixture shared by the in-slot hole tests: one committed
/// `:Person {email: 1}`, then the slot builder run twice — value 1 conflicts
/// with the committed holder, value 2 (the same shape, a distinct value)
/// admits.
fn assert_conflict_then_distinct_admits(build: impl Fn(i64, Value) -> Vec<RedoOp>) {
    let graph = Graph::default();
    graph
        .apply_redo(
            vec![
                create_node(1, "Person", &[("email", Value::Int(1))]),
                unique("c", "Person", &["email"]),
            ],
            1,
        )
        .unwrap();
    let mut admission = graph.begin_constraint_admission();
    let err = admission.admit(&build(2, Value::Int(1))).unwrap_err();
    assert!(matches!(err, Error::Conflict));
    // A distinct value in the same shape admits through the same admission
    // (a leaked hold on the rejected tuple would be structural, not caught
    // here: `admit` is check-then-accept and `accept` only runs on `Ok`).
    admission.admit(&build(3, Value::Int(2))).unwrap();
}

#[test]
fn admission_rejects_a_duplicate_and_accepts_distinct_values() {
    let graph = Graph::default();
    graph
        .apply_redo(vec![unique("c", "P", &["email"])], 1)
        .unwrap();
    let mut admission = graph.begin_constraint_admission();
    admission
        .admit(&[create_node(0, "P", &[("email", Value::Int(1))])])
        .unwrap();
    let err = admission
        .admit(&[create_node(1, "P", &[("email", Value::Int(1))])])
        .unwrap_err();
    assert!(matches!(err, Error::Conflict));
    admission
        .admit(&[create_node(1, "P", &[("email", Value::Int(2))])])
        .unwrap();
}

/// ADR-0439's create-registration rule (the Decision's One scope: a create
/// always registers its scope-filtered view): a create outside the
/// constrained label is pulled into scope by a later `AddLabels` in the same
/// slot. The create registers its entity unconditionally, so the label add
/// finds the state the create established instead of seeding the committed
/// (absent) view and no-opping; the introduction then conflicts with the
/// committed holder.
#[test]
fn admission_create_then_label_in_slot_conflicts() {
    assert_conflict_then_distinct_admits(|id, value| {
        vec![
            create_node(id, "Temp", &[("email", value)]),
            RedoOp::AddLabels {
                id,
                labels: vec!["Person".into()],
            },
        ]
    });
}

/// The create-then-label hole with the key set after the label: the create
/// registers an empty constrained view, `AddLabels` pulls it under the
/// constraint, and the `SetNodeProp` completes the conflicting tuple.
#[test]
fn admission_create_then_label_then_set_in_slot_conflicts() {
    assert_conflict_then_distinct_admits(|id, value| {
        vec![
            create_node(id, "Temp", &[]),
            RedoOp::AddLabels {
                id,
                labels: vec!["Person".into()],
            },
            RedoOp::SetNodeProp {
                id,
                key: "email".into(),
                value,
            },
        ]
    });
}

/// The mirror ordering — key before label. `SetNodeProp` mutates the view
/// the create registered even though the entity carries no constrained
/// label yet; the later `AddLabels` completes the tuple.
#[test]
fn admission_create_then_set_then_label_in_slot_conflicts() {
    assert_conflict_then_distinct_admits(|id, value| {
        vec![
            create_node(id, "Temp", &[]),
            RedoOp::SetNodeProp {
                id,
                key: "email".into(),
                value,
            },
            RedoOp::AddLabels {
                id,
                labels: vec!["Person".into()],
            },
        ]
    });
}

/// The relationship form cannot reproduce the node hole: a relationship's
/// type is fixed at creation and cannot be added later, so an out-of-scope
/// create can never be pulled under a constrained type in-slot. What this
/// pins is the `rel_view` Option path unconditional create registration
/// made reachable: a create whose type is not interned yet yields no view,
/// and a later `SetRelProp` on a constrained key name is a no-op — never a
/// panic (the pre-fix `expect`) and never a false conflict with a holder of
/// the constrained type. The constrained type's own create-then-set shape
/// still conflicts.
#[test]
fn admission_uninterned_rel_create_never_panics_or_false_conflicts() {
    let graph = Graph::default();
    graph
        .apply_redo(
            vec![
                create_node(0, "N", &[]),
                create_node(1, "N", &[]),
                create_rel(7, 0, 1, "R", &[("k", Value::Int(1))]),
                unique_rel("r", "R", &["k"]),
            ],
            1,
        )
        .unwrap();
    assert_eq!(
        graph.constraint_key_holder_count("r", &tuple([Value::Int(1)])),
        1
    );
    let mut admission = graph.begin_constraint_admission();
    // `Unknown` was never interned: no constraint can cover it, so setting
    // its constrained key name does not duplicate `r`'s tuple.
    admission
        .admit(&[
            create_rel(8, 0, 1, "Unknown", &[]),
            RedoOp::SetRelProp {
                id: 8,
                key: "k".into(),
                value: Value::Int(1),
            },
        ])
        .unwrap();
    // The interned, constrained type still conflicts on the same shape.
    let err = admission
        .admit(&[
            create_rel(9, 0, 1, "R", &[]),
            RedoOp::SetRelProp {
                id: 9,
                key: "k".into(),
                value: Value::Int(1),
            },
        ])
        .unwrap_err();
    assert!(matches!(err, Error::Conflict));
}

#[test]
fn admission_allows_an_earlier_free_in_the_same_group() {
    let graph = seeded_constrained();
    let mut admission = graph.begin_constraint_admission();
    // Slot A frees the committed tuple, slot B may reuse it.
    admission.admit(&[RedoOp::DeleteNode { id: 0 }]).unwrap();
    admission
        .admit(&[create_node(3, "P", &[("email", Value::Int(1))])])
        .unwrap();
}

/// The final-slot short-circuit: a last slot whose ops can only free has no
/// later reader, so admission records no overlay hold for it. The committed
/// holder therefore stays visible — an introduction checked next (the
/// observation seam; a real group has no later slot) still conflicts.
/// `check_with_earlier` is the seam the commit loop calls with
/// [`SlotPosition::of`], so the position is pinned here.
#[test]
fn final_slot_delete_only_records_no_overlay_free() {
    let graph = seeded_constrained();
    let mut admission = graph.begin_constraint_admission();
    let pending = admission
        .check_with_earlier(
            &[RedoOp::DeleteNode { id: 0 }],
            &mut std::iter::empty(),
            SlotPosition::Final,
        )
        .unwrap()
        .0;
    admission.accept(pending);
    // No free was recorded (the graph is not mutated here), so the committed
    // holder of `email = 1` still blocks the value.
    let err = admission
        .check_with_earlier(
            &[create_node(3, "P", &[("email", Value::Int(1))])],
            &mut std::iter::empty(),
            SlotPosition::Followed,
        )
        .unwrap_err();
    assert!(matches!(err, Error::Conflict));
}

/// The other direction of the final-slot short-circuit: the same delete-only
/// slot before the last one records its free, so a later slot may reuse the
/// value.
#[test]
fn non_final_slot_delete_only_records_its_free() {
    let graph = seeded_constrained();
    let mut admission = graph.begin_constraint_admission();
    let pending = admission
        .check_with_earlier(
            &[RedoOp::DeleteNode { id: 0 }],
            &mut std::iter::empty(),
            SlotPosition::Followed,
        )
        .unwrap()
        .0;
    admission.accept(pending);
    admission
        .check_with_earlier(
            &[create_node(3, "P", &[("email", Value::Int(1))])],
            &mut std::iter::empty(),
            SlotPosition::Followed,
        )
        .unwrap();
}

#[test]
fn admission_allows_a_free_earlier_in_the_same_slot() {
    let graph = seeded_constrained();
    let mut admission = graph.begin_constraint_admission();
    admission
        .admit(&[
            RedoOp::DeleteNode { id: 0 },
            create_node(3, "P", &[("email", Value::Int(1))]),
        ])
        .unwrap();
}

#[test]
fn admission_does_not_flag_a_reassert_of_the_same_value() {
    let graph = seeded_constrained();
    let mut admission = graph.begin_constraint_admission();
    admission
        .admit(&[RedoOp::SetNodeProp {
            id: 0,
            key: "email".into(),
            value: Value::Int(1),
        }])
        .unwrap();
}

#[test]
fn admission_checks_the_full_composite_tuple() {
    let graph = Graph::default();
    graph
        .apply_redo(vec![unique("c", "P", &["email", "tenant"])], 1)
        .unwrap();
    let mut admission = graph.begin_constraint_admission();
    admission
        .admit(&[create_node(
            0,
            "P",
            &[("email", Value::Int(1)), ("tenant", Value::Int(7))],
        )])
        .unwrap();
    // Same first component, different full tuple.
    admission
        .admit(&[create_node(
            1,
            "P",
            &[("email", Value::Int(1)), ("tenant", Value::Int(8))],
        )])
        .unwrap();
    let err = admission
        .admit(&[create_node(
            2,
            "P",
            &[("email", Value::Int(1)), ("tenant", Value::Int(7))],
        )])
        .unwrap_err();
    assert!(matches!(err, Error::Conflict));
}

#[test]
fn admission_rejects_a_constraint_install_over_an_accepted_introduction() {
    let graph = Graph::default();
    let mut admission = graph.begin_constraint_admission();
    let first_slot = [create_node(0, "P", &[("email", Value::Int(1))])];
    admission.admit(&first_slot).unwrap();
    let mut earlier = std::iter::once(first_slot.as_slice());
    let err = admission
        .check_with_earlier(
            &[unique("c", "P", &["email"])],
            &mut earlier,
            SlotPosition::Followed,
        )
        .unwrap_err();
    assert!(matches!(err, Error::Conflict));
}

#[test]
fn admission_rejects_an_introduction_into_a_constraint_installed_in_the_group() {
    let graph = Graph::default();
    let mut admission = graph.begin_constraint_admission();
    admission.admit(&[unique("c", "P", &["email"])]).unwrap();
    let err = admission
        .admit(&[create_node(0, "P", &[("email", Value::Int(1))])])
        .unwrap_err();
    assert!(matches!(err, Error::Conflict));
}

#[test]
fn a_rejected_slot_leaves_no_tuple_hold() {
    let graph = Graph::default();
    graph
        .apply_redo(vec![unique("c", "P", &["email"])], 1)
        .unwrap();
    let mut admission = graph.begin_constraint_admission();
    // The slot is checked but never accepted (the capacity/timestamp arm
    // rejects it after the check): its introduction must leave no hold.
    let pending = admission
        .check(&[create_node(0, "P", &[("email", Value::Int(1))])])
        .unwrap();
    drop(pending);
    admission
        .admit(&[create_node(1, "P", &[("email", Value::Int(1))])])
        .unwrap();
}

#[test]
fn a_rejected_drop_constraint_does_not_free() {
    let graph = seeded_constrained();
    let mut admission = graph.begin_constraint_admission();
    // An accepted slot introduces a value.
    admission
        .admit(&[create_node(3, "P", &[("email", Value::Int(9))])])
        .unwrap();
    // A slot that drops the constraint is checked but rejected afterwards:
    // the drop must not take effect for later slots.
    let pending = admission
        .check(&[RedoOp::DropConstraint { name: "c".into() }])
        .unwrap();
    drop(pending);
    let err = admission
        .admit(&[create_node(4, "P", &[("email", Value::Int(9))])])
        .unwrap_err();
    assert!(matches!(err, Error::Conflict));
}

#[test]
fn admission_allows_a_value_after_an_earlier_constraint_drop() {
    let graph = seeded_constrained();
    let mut admission = graph.begin_constraint_admission();
    admission
        .admit(&[RedoOp::DropConstraint { name: "c".into() }])
        .unwrap();
    admission
        .admit(&[create_node(3, "P", &[("email", Value::Int(1))])])
        .unwrap();
}

#[test]
fn two_constraints_in_one_group_keep_distinct_overlay_ids() {
    let graph = Graph::default();
    graph
        .apply_redo(
            vec![unique("c1", "P", &["email"]), unique("c2", "P", &["ssn"])],
            1,
        )
        .unwrap();
    let mut admission = graph.begin_constraint_admission();
    // Two constraints over the same label: the group overlay's TupleId must
    // not collide across them, so an email hold must not block an ssn write.
    admission
        .admit(&[create_node(0, "P", &[("email", Value::Int(1))])])
        .unwrap();
    admission
        .admit(&[create_node(1, "P", &[("ssn", Value::Int(1))])])
        .unwrap();
    // A node carrying both keys under both constraints, with distinct
    // values, admits cleanly.
    admission
        .admit(&[create_node(
            2,
            "P",
            &[("ssn", Value::Int(2)), ("email", Value::Int(2))],
        )])
        .unwrap();
    // A genuine duplicate under c1 still conflicts.
    assert!(admission
        .admit(&[create_node(3, "P", &[("email", Value::Int(1))])])
        .is_err());
}

/// The two pre-preflight seams — the entry-growth estimate (replicated
/// path) and the admission probe (`flush_group`) — classify a `Derived`
/// record before the WAL preflight runs. An `entity_type` naming neither
/// kind is corruption the preflight rejects, but neither seam may panic on
/// the way there: the estimate is unpriceable (no charge) and the probe
/// installs no key, while the preflight returns the typed storage error.
#[test]
fn corrupt_derived_entity_type_is_unpriceable_before_preflight_and_rejected_by_it() {
    let corrupt = RedoOp::CatalogDerivedConstraint {
        constraint: relate_model::Constraint {
            name: "c".into(),
            label: "P".into(),
            properties: vec!["email".into()],
            constraint_type: relate_model::ConstraintType::Unique,
            property_type: None,
            entity_type: "FOO".into(),
            enforced_label: None,
        },
    };

    // Seam 1: the capacity estimate (`commit_replicated` →
    // `estimate_redo_delta`, before the preflight). The seeded graph has
    // live `P` entities, so a classifiable uniqueness record would charge a
    // backfill here; an unclassifiable one is skipped instead — the
    // contrast pins that the arm skips only the unclassifiable record.
    let graph = seeded();
    let classifiable = RedoOp::CatalogDerivedConstraint {
        constraint: relate_model::Constraint {
            name: "c".into(),
            label: "P".into(),
            properties: vec!["email".into()],
            constraint_type: relate_model::ConstraintType::Unique,
            property_type: None,
            entity_type: "NODE".into(),
            enforced_label: None,
        },
    };
    assert!(graph.estimate_redo_delta(&[classifiable]) > 0);
    assert_eq!(graph.estimate_redo_delta(&[corrupt.clone()]), 0);
    assert!(graph.constraint_key_spec("c").is_none());

    // Seam 2: the admission probe. It checks the slot, finds no key to
    // install, and does not panic.
    let mut admission = graph.begin_constraint_admission();
    admission.admit(&[corrupt.clone()]).unwrap();

    // The preflight rejects the same record with its typed storage error
    // before apply can reach the classification.
    let record = [corrupt];
    let err = graph
        .validate_redo_group_applicability([(&record[..], 1)])
        .unwrap_err();
    assert!(matches!(err, Error::Storage(_)));
}