relate-executor 1.0.2

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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Route-versus-GPM parity for `MATCH`-clause shortest paths (ADR-0186).
//! Three fixed cyclic graphs and a seeded corpus of small random graphs,
//! with self-loops and parallel relationships, run the routed selector ×
//! direction × bound × endpoint × type matrix twice: on the BFS route and
//! forced onto `match_gpm_path`. Single-path selectors must agree on
//! (start, end, length) per row and return valid trails; all-path
//! selectors must agree on (start, end, trail).

use std::collections::HashSet;

use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use relate_core::{NodeId, Path, Value};
use relate_ir::Direction;

use crate::exec::test_support::{build, Built};

use super::fixtures::{run, Counts, DirectionExt, Selector, DIRS, SELECTORS};
use super::with_forced_gpm;

/// Corpus seed; a failure names it with the graph index.
const SEED: u64 = 0x0186_0192;
/// Graphs in the corpus.
const GRAPHS: usize = 160;
/// Graphs with more relationships skip unbounded `[*]`: GPM enumerates
/// every trail, which grows exponentially with the relationship count.
const UNBOUNDED_MAX_RELS: usize = 8;
/// Corpus graph `i` runs the matrix combinations whose index is `i` modulo
/// `STRIDE`, so each combination runs on `GRAPHS / STRIDE` graphs. Fixed
/// graphs run every combination.
const STRIDE: usize = 12;

/// A length suffix and its minimum and maximum hops (`None`: unbounded).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct Bound {
    suffix: &'static str,
    min: u32,
    max: Option<u32>,
}

#[rustfmt::skip]
const BOUNDS: [Bound; 4] = [
    Bound { suffix: "*", min: 1, max: None },
    Bound { suffix: "*..2", min: 1, max: Some(2) },
    Bound { suffix: "*1..3", min: 1, max: Some(3) },
    Bound { suffix: "*0..2", min: 0, max: Some(2) },
];

/// A type expression and the types it admits (empty: every type).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct Types {
    expr: &'static str,
    admits: &'static [&'static str],
}

#[rustfmt::skip]
const TYPES: [Types; 3] = [
    Types { expr: "", admits: &[] },
    Types { expr: ":A", admits: &["A"] },
    Types { expr: ":A|B", admits: &["A", "B"] },
];

impl Types {
    fn admit(self, t: &str) -> bool {
        self.admits.is_empty() || self.admits.contains(&t)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum Ends {
    /// `(a:X)…(b:Y)`, neither bound.
    Unbound,
    /// `MATCH (a:X), (b:Y) MATCH … (a)…(b)`.
    BothBound,
    /// `MATCH (a:X) MATCH … (a)…(b:Y)`.
    StartBound,
    /// `MATCH (b:Y) MATCH … (a:X)…(b)`.
    EndBound,
    /// `(a:X)…(a)`.
    Same,
    /// `(a)…(b)`, unbound and unlabeled.
    Plain,
    /// `MATCH (a:X) MATCH … (a)…(a)`.
    SameBound,
}

const ENDS: [Ends; 7] = [
    Ends::Unbound,
    Ends::BothBound,
    Ends::StartBound,
    Ends::EndBound,
    Ends::Same,
    Ends::Plain,
    Ends::SameBound,
];

impl Ends {
    /// Prior `MATCH`, start node pattern, end node pattern, end column.
    fn parts(self) -> (&'static str, &'static str, &'static str, &'static str) {
        match self {
            Ends::Unbound => ("", "(a:X)", "(b:Y)", "b"),
            Ends::BothBound => ("MATCH (a:X), (b:Y) ", "(a)", "(b)", "b"),
            Ends::StartBound => ("MATCH (a:X) ", "(a)", "(b:Y)", "b"),
            Ends::EndBound => ("MATCH (b:Y) ", "(a:X)", "(b)", "b"),
            Ends::Same => ("", "(a:X)", "(a)", "a AS b"),
            Ends::Plain => ("", "(a)", "(b)", "b"),
            Ends::SameBound => ("MATCH (a:X) ", "(a)", "(a)", "a AS b"),
        }
    }

    fn same(self) -> bool {
        matches!(self, Ends::Same | Ends::SameBound)
    }

    fn start_label(self) -> Option<&'static str> {
        (self != Ends::Plain).then_some("X")
    }

    fn end_label(self) -> Option<&'static str> {
        matches!(
            self,
            Ends::Unbound | Ends::BothBound | Ends::StartBound | Ends::EndBound
        )
        .then_some("Y")
    }
}

/// One cell of the query matrix.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct Combo {
    sel: Selector,
    dir: Direction,
    bound: Bound,
    ends: Ends,
    types: Types,
}

fn matrix() -> Vec<Combo> {
    let mut out = Vec::new();
    for sel in SELECTORS {
        for dir in DIRS {
            for bound in BOUNDS {
                for ends in ENDS {
                    for types in TYPES {
                        out.push(Combo {
                            sel,
                            dir,
                            bound,
                            ends,
                            types,
                        });
                    }
                }
            }
        }
    }
    out
}

impl Combo {
    /// The query; `bare` drops the `:X` and `:Y` of the shortest-path
    /// pattern's endpoints.
    fn query(self, bare: bool) -> String {
        let Selector { open, close, .. } = self.sel;
        let (l, r) = self.dir.arrows();
        let (prior, start, end, end_col) = self.ends.parts();
        let bare_or = |p: &str, label| if bare { p.replace(label, "") } else { p.into() };
        let (start, end) = (bare_or(start, ":X"), bare_or(end, ":Y"));
        let (types, bound) = (self.types.expr, self.bound.suffix);
        format!(
            "{prior}MATCH p = {open}{start}{l}[r{types}{bound}]{r}{end}{close} \
             RETURN p, r, a, {end_col}"
        )
    }

    /// Input rows of the shortest-path `MATCH`: one per prior-`MATCH` row,
    /// or the single empty row when nothing is bound.
    fn input_rows(self, spec: &Spec) -> usize {
        let (x, y) = (spec.with("X").len(), spec.with("Y").len());
        match self.ends {
            Ends::Unbound | Ends::Same | Ends::Plain => 1,
            Ends::BothBound => x * y,
            Ends::StartBound | Ends::SameBound => x,
            Ends::EndBound => y,
        }
    }

    /// Whether `n` has a relationship this pattern may follow along `dir`.
    fn adj(self, spec: &Spec, n: usize, dir: Direction) -> bool {
        spec.rels
            .iter()
            .any(|&(a, b, t)| self.types.admit(t) && dir.step(n, (a, b)).is_some())
    }

    /// Whether some search ADR-0186 prescribes for this query starts where
    /// a relationship can be followed, so the route must expand one.
    /// Searches whose answer is fixed without looking (a pair whose end
    /// cannot be entered, a zero-length partition) do not count.
    fn expansion_required(self, spec: &Spec) -> bool {
        let (d, back) = (self.dir, self.dir.flip());
        let (xs, ys) = (spec.with("X"), spec.with("Y"));
        let closed =
            |s: usize| self.bound.min == 1 && self.adj(spec, s, d) && self.adj(spec, s, back);
        match self.ends {
            Ends::Unbound | Ends::StartBound => {
                !ys.is_empty() && xs.iter().any(|&s| self.adj(spec, s, d))
            }
            Ends::Plain => (0..spec.labels.len()).any(|s| self.adj(spec, s, d)),
            Ends::EndBound => !xs.is_empty() && ys.iter().any(|&e| self.adj(spec, e, back)),
            Ends::BothBound => xs.iter().any(|&s| {
                ys.iter().any(|&e| {
                    if s == e {
                        closed(s)
                    } else {
                        self.adj(spec, s, d) && self.adj(spec, e, back)
                    }
                })
            }),
            Ends::Same | Ends::SameBound => xs.iter().any(|&s| closed(s)),
        }
    }
}

/// One graph: per-node labels and `(from, to, type)` relationships, both by
/// index.
struct Spec {
    labels: Vec<&'static [&'static str]>,
    rels: Vec<(usize, usize, &'static str)>,
}

impl Spec {
    /// `n` nodes, all `:X:Y` so bound endpoints range over every node, for
    /// queries with bare endpoints.
    fn fixed(n: usize, rels: &[(usize, usize, &'static str)]) -> Spec {
        let (labels, rels) = (vec![&["X", "Y"][..]; n], rels.to_vec());
        Spec { labels, rels }
    }

    fn generate(rng: &mut StdRng) -> Spec {
        let n = rng.random_range(4..=7);
        let labels = (0..n)
            .map(|_| -> &'static [&'static str] {
                match rng.random_range(0..4) {
                    0 => &[],
                    1 => &["X"],
                    2 => &["Y"],
                    _ => &["X", "Y"],
                }
            })
            .collect();
        // Mostly small enough for unbounded GPM enumeration; the rest run
        // bounded patterns only.
        let m = if rng.random_bool(0.85) {
            rng.random_range(0..=UNBOUNDED_MAX_RELS)
        } else {
            rng.random_range(UNBOUNDED_MAX_RELS + 1..=12)
        };
        let mut rels: Vec<(usize, usize, &'static str)> = Vec::with_capacity(m);
        for _ in 0..m {
            let t = if rng.random_bool(0.5) { "A" } else { "B" };
            // Reuse an earlier relationship's endpoints now and then, for
            // parallel and antiparallel pairs.
            let (a, b) = if !rels.is_empty() && rng.random_bool(0.2) {
                let (a, b, _) = rels[rng.random_range(0..rels.len())];
                if rng.random_bool(0.5) {
                    (a, b)
                } else {
                    (b, a)
                }
            } else {
                (rng.random_range(0..n), rng.random_range(0..n))
            };
            rels.push((a, b, t));
        }
        Spec { labels, rels }
    }

    fn with(&self, label: &str) -> Vec<usize> {
        (0..self.labels.len())
            .filter(|&i| self.labels[i].contains(&label))
            .collect()
    }

    fn has_self_loop(&self) -> bool {
        self.rels.iter().any(|&(a, b, _)| a == b)
    }

    fn has_parallel_pair(&self) -> bool {
        let mut seen = HashSet::new();
        self.rels
            .iter()
            .any(|&(a, b, _)| a != b && !seen.insert((a, b)))
    }

    /// The graph as Rust literals, ready for a named regression test.
    fn describe(&self) -> String {
        format!("labels: {:?}\n  rels: {:?}", self.labels, self.rels)
    }
}

/// Cyclic graphs with parallel relationships and self-loops.
#[rustfmt::skip]
fn fixed_specs() -> [Spec; 3] {
    [
        Spec::fixed(4, &[(0, 1, "A"), (1, 0, "A"), (0, 1, "B"), (1, 2, "A"),
                         (2, 2, "A"), (2, 3, "B"), (3, 1, "A")]),
        Spec::fixed(5, &[(0, 1, "A"), (1, 2, "A"), (2, 0, "B"), (2, 3, "A"),
                         (3, 4, "A"), (4, 2, "A"), (0, 0, "B")]),
        Spec::fixed(5, &[(0, 1, "A"), (0, 2, "A"), (1, 3, "B"), (2, 3, "A"),
                         (3, 4, "A"), (4, 0, "A"), (1, 2, "B")]),
    ]
}

/// A returned row by spec index.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Trail {
    start: usize,
    end: usize,
    rels: Vec<usize>,
}

/// What route and GPM rows are compared by: the whole trail for all-path
/// selectors; for single-path ones its length, since which trail among
/// ties they return is unspecified.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
enum Key {
    Trail(Trail),
    Length { start: usize, end: usize, len: u32 },
}

/// Validate one `RETURN p, r, a, b` row against the graph and the pattern
/// and return its trail.
fn check_row(spec: &Spec, built: &Built, c: Combo, row: &[Value]) -> Result<Trail, String> {
    let [Value::Path(p), Value::List(r), Value::Node(a), Value::Node(b)] = row else {
        return Err(format!("unexpected row shape {row:?}"));
    };
    let Path { nodes, rels } = &**p;
    if nodes.len() != rels.len() + 1 {
        return Err(format!(
            "{} nodes for {} relationships",
            nodes.len(),
            rels.len()
        ));
    }
    let node = |id: NodeId| {
        built
            .nodes
            .iter()
            .position(|&n| n == id)
            .ok_or_else(|| format!("unknown node id {id}"))
    };
    let (start, end) = (node(nodes[0].id)?, node(nodes[nodes.len() - 1].id)?);
    let (a, b) = (node(a.id)?, node(b.id)?);
    if (start, end) != (a, b) {
        return Err(format!("path runs {start}..{end}, a/b are {a}/{b}"));
    }
    let len = rels.len() as u32;
    if len < c.bound.min || c.bound.max.is_some_and(|m| len > m) {
        return Err(format!("length {len} outside the bounds"));
    }
    let mut trail = Vec::new();
    for (i, rel) in rels.iter().enumerate() {
        let k = built
            .rels
            .iter()
            .position(|&r| r == rel.id)
            .ok_or_else(|| format!("unknown relationship id {}", rel.id))?;
        let (from, to, t) = spec.rels[k];
        if trail.contains(&k) {
            return Err(format!("relationship #{k} repeats"));
        }
        if !c.types.admit(t) {
            return Err(format!("relationship #{k} has type {t}"));
        }
        let (at, next) = (node(nodes[i].id)?, node(nodes[i + 1].id)?);
        if c.dir.step(at, (from, to)) != Some(next) {
            return Err(format!(
                "relationship #{k} ({from})-[:{t}]->({to}) does not lead {at} to {next} {:?}",
                c.dir
            ));
        }
        trail.push(k);
    }
    for (n, label) in [(start, c.ends.start_label()), (end, c.ends.end_label())] {
        if let Some(label) = label.filter(|l| !spec.labels[n].contains(l)) {
            return Err(format!("node {n} lacks :{label}"));
        }
    }
    if c.ends.same() && start != end {
        return Err(format!("(a)…(a) path runs {start}..{end}"));
    }
    let r_rels: Vec<_> = r
        .iter()
        .map(|v| match v {
            Value::Relationship(x) => Some(x),
            _ => None,
        })
        .collect();
    if r_rels.len() != rels.len()
        || r_rels
            .iter()
            .zip(rels)
            .any(|(x, y)| x.map(|arc| arc.as_ref()) != Some(y))
    {
        return Err(format!("r = {r:?} is not relationships(p)"));
    }
    Ok(Trail {
        start,
        end,
        rels: trail,
    })
}

/// The sorted comparison keys of `trails`.
fn keys(c: Combo, trails: &[Trail]) -> Vec<Key> {
    let mut out: Vec<Key> = trails
        .iter()
        .map(|t| {
            if c.sel.all {
                Key::Trail(t.clone())
            } else {
                let (start, end, len) = (t.start, t.end, t.rels.len() as u32);
                Key::Length { start, end, len }
            }
        })
        .collect();
    out.sort();
    out
}

#[test]
fn route_matches_gpm_on_fixed_and_random_graphs() {
    let mut rng = StdRng::seed_from_u64(SEED);
    let specs: Vec<Spec> = (0..GRAPHS).map(|_| Spec::generate(&mut rng)).collect();
    assert!(specs.len() >= 150);
    assert!(specs
        .iter()
        .all(|s| (4..=7).contains(&s.labels.len()) && s.rels.len() <= 12));
    assert!(specs.iter().any(Spec::has_self_loop), "no self-loop");
    assert!(
        specs.iter().any(Spec::has_parallel_pair),
        "no parallel pair"
    );
    // Fixed graphs run every combination with bare endpoints, which corpus
    // queries never have; corpus graphs run their stride and feed the
    // coverage sets.
    let fixed = fixed_specs();
    let fixed_graphs = fixed.iter().enumerate().map(|(i, s)| (s, i, false));
    let graphs = fixed_graphs.chain(specs.iter().enumerate().map(|(i, s)| (s, i, true)));

    let matrix = matrix();
    let mut ran = HashSet::new();
    let mut routed = HashSet::new();
    let mut expanded = HashSet::new();
    let mut failures = Vec::new();
    let mut cases_run = 0usize;
    for (spec, i, corpus) in graphs {
        let name = if corpus {
            format!("seed {SEED:#x}, graph {i}")
        } else {
            format!("fixed graph {i}")
        };
        let built = build(&spec.labels, &spec.rels);
        let g = &built.graph;
        for (ci, &c) in matrix.iter().enumerate() {
            if (corpus && ci % STRIDE != i % STRIDE)
                || (c.bound.max.is_none() && spec.rels.len() > UNBOUNDED_MAX_RELS)
            {
                continue;
            }
            let q = c.query(!corpus);
            let fail = |detail: String| {
                format!("{name}:\n  {}\n  query: {q}\n  {detail}", spec.describe())
            };
            let (route_rows, counts) = run(g, &q);
            let (gpm_rows, gpm_counts) = with_forced_gpm(|| run(g, &q));
            cases_run += 1;
            assert_eq!(
                gpm_counts,
                Counts::default(),
                "forced GPM still took the route: {q}"
            );
            let checked = |rows: &[Vec<Value>], side: &str| {
                rows.iter()
                    .map(|row| check_row(spec, &built, c, row))
                    .collect::<Result<Vec<_>, _>>()
                    .map_err(|e| fail(format!("{side} row invalid: {e}")))
            };
            let (route, gpm) = match (checked(&route_rows, "route"), checked(&gpm_rows, "GPM")) {
                (Ok(route), Ok(gpm)) => (route, gpm),
                (Err(e), _) | (_, Err(e)) => {
                    failures.push(e);
                    continue;
                }
            };
            let (route_keys, gpm_keys) = (keys(c, &route), keys(c, &gpm));
            if route_keys != gpm_keys {
                failures.push(fail(format!("route {route_keys:?}\n  GPM   {gpm_keys:?}")));
            }
            let want_runs = c.input_rows(spec) as u64;
            if counts.route_runs != want_runs {
                failures.push(fail(format!(
                    "route ran {} times for {want_runs} input rows",
                    counts.route_runs
                )));
            }
            let must_expand =
                c.expansion_required(spec) || route.iter().any(|t| !t.rels.is_empty());
            if must_expand && counts.expands == 0 {
                failures.push(fail("the route expanded nothing".into()));
            }
            if corpus {
                ran.insert(c);
                if counts.route_runs > 0 {
                    routed.insert(c);
                }
                if counts.expands > 0 {
                    expanded.insert(c);
                }
            }
        }
    }
    eprintln!(
        "shortest_parity: cases_run = {cases_run} on {} fixed and {GRAPHS} corpus graphs \
         (seed {SEED:#x})",
        fixed.len()
    );
    assert!(
        failures.is_empty(),
        "{} divergences, first ones:\n{}",
        failures.len(),
        failures[..failures.len().min(8)].join("\n\n")
    );
    for c in &matrix {
        assert!(ran.contains(c), "{c:?} never ran");
        assert!(routed.contains(c), "{c:?} never took the route");
        // `(a)-[*0..2]-(a)` only ever yields the zero-length path.
        if !(c.ends.same() && c.bound.min == 0) {
            assert!(expanded.contains(c), "{c:?} never expanded on the route");
        }
    }
}