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
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! Unit and differential tests for composable morsel aggregate sink (ADR-0730).

use relate_core::Value;

use super::agg_fold::take_path_fold_bindings;
use super::test_support::{exec_cypher, Builder};
use crate::graph::Graph;

fn test_linear_graph() -> Graph {
    let b = Builder::new();
    // Build 5-hop paths: a -> b -> c -> d -> e -> f
    // Multiple paths with varying property values and nulls.
    for i in 0..10 {
        let a = b.node(&format!("a{i}"), &["A"]);
        let val_b = if i % 2 == 0 {
            Some(i as i64 * 10)
        } else {
            None
        };
        let b_node = b.prop_node(
            &format!("b{i}"),
            &["B"],
            &[
                ("val", val_b.map(Value::Int).unwrap_or(Value::Null)),
                ("grp", Value::String(if i < 5 { "g1" } else { "g2" }.into())),
            ],
        );
        let c = b.prop_node(
            &format!("c{i}"),
            &["C"],
            &[("score", Value::Int((i as i64 + 1) * 5))],
        );
        let d = b.node(&format!("d{i}"), &["D"]);
        let e = b.node(&format!("e{i}"), &["E"]);
        let f = b.prop_node(
            &format!("f{i}"),
            &["F"],
            &[("weight", Value::Int(i as i64 * 2))],
        );

        b.rel(a, b_node, "R", &[]);
        b.rel(b_node, c, "R", &[]);
        b.rel(c, d, "R", &[]);
        b.rel(d, e, "R", &[]);
        b.rel(e, f, "R", &[]);
    }
    b.done()
}

#[test]
fn morsel_agg_ungrouped_1_to_5_hops() {
    let g = test_linear_graph();

    // 1-hop
    let _ = take_path_fold_bindings();
    let r1 = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->(b:B) RETURN count(*) AS c, sum(b.val) AS s, min(b.val) AS mi, max(b.val) AS ma",
    );
    assert_eq!(
        r1.rows,
        vec![vec![
            Value::Int(10),
            Value::Int(200), // 0 + 20 + 40 + 60 + 80
            Value::Int(0),
            Value::Int(80),
        ]]
    );
    assert!(take_path_fold_bindings() >= 10);

    // 2-hop
    let r2 = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->()-[:R]->(c:C) RETURN count(*) AS c, sum(c.score) AS s, min(c.score) AS mi, max(c.score) AS ma",
    );
    assert_eq!(
        r2.rows,
        vec![vec![
            Value::Int(10),
            Value::Int(275), // sum of 5..50
            Value::Int(5),
            Value::Int(50),
        ]]
    );

    // 3-hop
    let r3 = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->()-[:R]->()-[:R]->(d:D) RETURN count(*) AS c",
    );
    assert_eq!(r3.rows, vec![vec![Value::Int(10)]]);

    // 5-hop
    let r5 = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->()-[:R]->()-[:R]->()-[:R]->()-[:R]->(f:F) \
         RETURN count(*) AS c, sum(f.weight) AS s, min(f.weight) AS mi, max(f.weight) AS ma",
    );
    assert_eq!(
        r5.rows,
        vec![vec![
            Value::Int(10),
            Value::Int(90), // sum of 0, 2, ..., 18 = 90
            Value::Int(0),
            Value::Int(18),
        ]]
    );
}

#[test]
fn morsel_agg_grouped_by_node_id_and_props() {
    let g = test_linear_graph();

    // Grouped by single NodeId slot
    let r_node = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->(b:B) RETURN a, count(*) AS c ORDER BY c DESC LIMIT 3",
    );
    assert_eq!(r_node.rows.len(), 3);
    for row in &r_node.rows {
        assert_eq!(row[1], Value::Int(1));
    }

    // Grouped by node property
    let r_prop = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->(b:B) RETURN b.grp AS grp, count(*) AS c, sum(b.val) AS s ORDER BY grp",
    );
    assert_eq!(
        r_prop.rows,
        vec![
            vec![Value::String("g1".into()), Value::Int(5), Value::Int(60)], // 0 + 20 + 40
            vec![Value::String("g2".into()), Value::Int(5), Value::Int(140)], // 60 + 80
        ]
    );
}

#[test]
fn morsel_agg_distinct_variants() {
    let b = Builder::new();
    let n1 = b.node("n1", &["User"]);
    let n2 = b.node("n2", &["User"]);
    let i1 = b.prop_node("i1", &["Item"], &[("score", Value::Int(10))]);
    let i2 = b.prop_node("i2", &["Item"], &[("score", Value::Int(10))]);
    let i3 = b.prop_node("i3", &["Item"], &[("score", Value::Int(20))]);

    b.rel(n1, i1, "BUYS", &[]);
    b.rel(n1, i2, "BUYS", &[]);
    b.rel(n2, i2, "BUYS", &[]);
    b.rel(n2, i3, "BUYS", &[]);

    let g = b.done();

    let res = exec_cypher(
        &g,
        "MATCH (u:User)-[:BUYS]->(i:Item) \
         RETURN count(i) AS ci, count(DISTINCT i) AS cdi, \
                count(i.score) AS cs, count(DISTINCT i.score) AS cds, \
                sum(i.score) AS sum_all, sum(DISTINCT i.score) AS sum_dist, \
                collect(DISTINCT i.score) AS col_dist",
    );
    assert_eq!(
        res.rows,
        vec![vec![
            Value::Int(4),
            Value::Int(3),
            Value::Int(4),
            Value::Int(2),
            Value::Int(50), // 10 + 10 + 10 + 20
            Value::Int(30), // 10 + 20
            Value::List(vec![Value::Int(10), Value::Int(20)].into()),
        ]]
    );
}

#[test]
fn morsel_agg_predicate_perturbations() {
    let g = test_linear_graph();

    // Base query
    let base = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->(b:B) WHERE b.val = 20 RETURN count(*) AS c",
    );
    assert_eq!(base.rows, vec![vec![Value::Int(1)]]);

    // OR predicate
    let or_res = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->(b:B) WHERE b.val = 20 OR b.val = 40 RETURN count(*) AS c, sum(b.val) AS s",
    );
    assert_eq!(or_res.rows, vec![vec![Value::Int(2), Value::Int(60)]]);

    // NOT predicate
    let not_res = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->(b:B) WHERE NOT (b.grp = \"g1\") RETURN count(*) AS c",
    );
    assert_eq!(not_res.rows, vec![vec![Value::Int(5)]]);

    // Reversed direction
    let rev_res = exec_cypher(
        &g,
        "MATCH (b:B)<-[:R]-(a:A) WHERE b.grp = \"g1\" RETURN count(*) AS c",
    );
    assert_eq!(rev_res.rows, vec![vec![Value::Int(5)]]);
}

#[test]
fn morsel_agg_empty_and_null_semantics() {
    let g = Graph::default();

    // Empty graph ungrouped
    let empty_ungrouped = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->(b:B) \
         RETURN count(*) AS c, count(b.val) AS cb, sum(b.val) AS s, \
                avg(b.val) AS a, min(b.val) AS mi, max(b.val) AS ma, collect(b.val) AS col",
    );
    assert_eq!(
        empty_ungrouped.rows,
        vec![vec![
            Value::Int(0),
            Value::Int(0),
            Value::Int(0), // Cypher sum() over empty set is 0
            Value::Null,
            Value::Null,
            Value::Null,
            Value::List(vec![].into()),
        ]]
    );

    // Empty graph grouped
    let empty_grouped = exec_cypher(
        &g,
        "MATCH (a:A)-[:R]->(b:B) RETURN a.id AS id, count(*) AS c",
    );
    assert!(empty_grouped.rows.is_empty());

    // Non-empty graph with all-null property values
    let b = Builder::new();
    let a1 = b.node("a1", &["A"]);
    let b1 = b.prop_node("b1", &["B"], &[("val", Value::Null)]);
    b.rel(a1, b1, "R", &[]);
    let g2 = b.done();

    let all_null_res = exec_cypher(
        &g2,
        "MATCH (a:A)-[:R]->(b:B) RETURN count(b.val) AS c, sum(b.val) AS s",
    );
    assert_eq!(all_null_res.rows, vec![vec![Value::Int(0), Value::Int(0)]]);
}

#[test]
fn morsel_agg_cyclic_patterns() {
    let b = Builder::new();
    let n0 = b.node("n0", &["N"]);
    let n1 = b.node("n1", &["N"]);
    let n2 = b.node("n2", &["N"]);

    b.rel(n0, n0, "LOOP", &[]); // self-loop
    b.rel(n1, n2, "E", &[]);
    b.rel(n2, n1, "E", &[]); // cycle n1 <-> n2

    let g = b.done();

    // Self loop
    let self_loop = exec_cypher(&g, "MATCH (x:N)-[:LOOP]->(x) RETURN count(*) AS c");
    assert_eq!(self_loop.rows, vec![vec![Value::Int(1)]]);

    // 2-hop cycle
    let cycle = exec_cypher(&g, "MATCH (a:N)-[:E]->(b:N)-[:E]->(a) RETURN count(*) AS c");
    assert_eq!(cycle.rows, vec![vec![Value::Int(2)]]);
}

#[test]
fn morsel_agg_comma_patterns_joined() {
    let b = Builder::new();
    let c1 = b.prop_node("c1", &["Comment"], &[("id", Value::Int(1))]);
    let c2 = b.prop_node("c2", &["Comment"], &[("id", Value::Int(2))]);
    let t1 = b.prop_node(
        "t1",
        &["Tag"],
        &[("name", Value::String("Copernicus".into()))],
    );
    let p1 = b.prop_node("p1", &["Place"], &[("name", Value::String("India".into()))]);

    b.rel(c1, t1, "HAS_TAG", &[]);
    b.rel(c1, p1, "IN_PLACE", &[]);
    b.rel(c2, t1, "HAS_TAG", &[]);
    b.rel(c2, p1, "IN_PLACE", &[]);

    let g = b.done();

    let res = exec_cypher(
        &g,
        "MATCH (c:Comment)-[:HAS_TAG]->(t:Tag), (c)-[:IN_PLACE]->(l:Place) \
         WHERE t.name = \"Copernicus\" \
         RETURN l.name AS name, count(c.id) AS count \
         ORDER BY count DESC",
    );
    assert_eq!(
        res.rows,
        vec![vec![Value::String("India".into()), Value::Int(2)]]
    );
}

#[test]
fn morsel_agg_comma_patterns_merges_labels_on_shared_node() {
    let b = Builder::new();
    let a = b.node("a", &["A"]);
    let b_node = b.node("b", &["B"]);
    // Hub node has BOTH H1 and H2 labels
    let h_both = b.node("h1", &["H1", "H2"]);
    // Another hub node has only H1
    let h_only1 = b.node("h2", &["H1"]);

    b.rel(a, h_both, "R1", &[]);
    b.rel(b_node, h_both, "R2", &[]);

    b.rel(a, h_only1, "R1", &[]);
    b.rel(b_node, h_only1, "R2", &[]);

    let g = b.done();

    // Query requires h to satisfy both :H1 and :H2
    let res = exec_cypher(
        &g,
        "MATCH (a:A)-[:R1]->(h:H1), (b:B)-[:R2]->(h:H2) RETURN count(*) AS c",
    );
    // Only h_both matches both labels, so exactly 1 path
    assert_eq!(res.rows, vec![vec![Value::Int(1)]]);
}

#[test]
fn morsel_agg_snw_q1_differential() {
    let b = Builder::new();
    let p1 = b.prop_node(
        "p1",
        &["Person"],
        &[
            ("personID", Value::Int(101)),
            ("name", Value::String("Alice".into())),
        ],
    );
    let p2 = b.prop_node(
        "p2",
        &["Person"],
        &[
            ("personID", Value::Int(102)),
            ("name", Value::String("Bob".into())),
        ],
    );
    let p3 = b.prop_node(
        "p3",
        &["Person"],
        &[
            ("personID", Value::Int(103)),
            ("name", Value::String("Charlie".into())),
        ],
    );
    let _p4 = b.prop_node(
        "p4",
        &["Person"],
        &[
            ("personID", Value::Int(104)),
            ("name", Value::String("Dave".into())),
        ],
    );

    let f1 = b.node("f1", &["Person"]);
    let f2 = b.node("f2", &["Person"]);
    let f3 = b.node("f3", &["Person"]);
    let f4 = b.node("f4", &["Person"]);

    // p1 has 3 followers (f1, f2, f3)
    b.rel(f1, p1, "FOLLOWS", &[]);
    b.rel(f2, p1, "FOLLOWS", &[]);
    b.rel(f3, p1, "FOLLOWS", &[]);

    // p2 has 2 followers (f1, f4)
    b.rel(f1, p2, "FOLLOWS", &[]);
    b.rel(f4, p2, "FOLLOWS", &[]);

    // p3 has 1 follower (f2)
    b.rel(f2, p3, "FOLLOWS", &[]);

    let g = b.done();

    let _ = take_path_fold_bindings();
    let query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
                 RETURN p.personID, p.name, count(f) AS c \
                 ORDER BY c DESC LIMIT 3";
    let res = exec_cypher(&g, query);

    assert!(take_path_fold_bindings() > 0, "morsel agg must be engaged");

    let oracle_query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
                        WITH f, p \
                        RETURN p.personID, p.name, count(f) AS c \
                        ORDER BY c DESC LIMIT 3";
    let oracle = exec_cypher(&g, oracle_query);

    assert_eq!(res.rows, oracle.rows);
    assert_eq!(
        res.rows,
        vec![
            vec![
                Value::Int(101),
                Value::String("Alice".into()),
                Value::Int(3),
            ],
            vec![Value::Int(102), Value::String("Bob".into()), Value::Int(2),],
            vec![
                Value::Int(103),
                Value::String("Charlie".into()),
                Value::Int(1),
            ],
        ]
    );
}

#[test]
fn morsel_agg_colliding_property_tuples_merge() {
    let b = Builder::new();
    // Two distinct Person nodes sharing (personID=101, name="Alice")
    let p1a = b.prop_node(
        "p1a",
        &["Person"],
        &[
            ("personID", Value::Int(101)),
            ("name", Value::String("Alice".into())),
            ("weight", Value::Int(10)),
            ("score", Value::Float(2.5)),
        ],
    );
    let p1b = b.prop_node(
        "p1b",
        &["Person"],
        &[
            ("personID", Value::Int(101)),
            ("name", Value::String("Alice".into())),
            ("weight", Value::Int(20)),
            ("score", Value::Float(7.5)),
        ],
    );

    // Two distinct Person nodes sharing (personID=Null, name=Null)
    let p2a = b.prop_node(
        "p2a",
        &["Person"],
        &[
            ("personID", Value::Null),
            ("name", Value::Null),
            ("weight", Value::Int(5)),
            ("score", Value::Float(1.0)),
        ],
    );
    let p2b = b.prop_node(
        "p2b",
        &["Person"],
        &[
            ("personID", Value::Null),
            ("name", Value::Null),
            ("weight", Value::Null),
            ("score", Value::Null),
        ],
    );

    // Distinct Person node with unique properties
    let p3 = b.prop_node(
        "p3",
        &["Person"],
        &[
            ("personID", Value::Int(103)),
            ("name", Value::String("Bob".into())),
            ("weight", Value::Int(100)),
            ("score", Value::Float(50.0)),
        ],
    );

    let f1 = b.node("f1", &["Person"]);
    let f2 = b.node("f2", &["Person"]);
    let f3 = b.node("f3", &["Person"]);
    let f4 = b.node("f4", &["Person"]);

    b.rel(f1, p1a, "FOLLOWS", &[]);
    b.rel(f2, p1a, "FOLLOWS", &[]);
    b.rel(f3, p1b, "FOLLOWS", &[]);

    b.rel(f1, p2a, "FOLLOWS", &[]);
    b.rel(f4, p2b, "FOLLOWS", &[]);

    b.rel(f2, p3, "FOLLOWS", &[]);

    let g = b.done();

    let _ = take_path_fold_bindings();
    let query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
                 RETURN p.personID AS pid, p.name AS name, \
                        count(*) AS c_star, \
                        count(f) AS c_f, \
                        sum(p.weight) AS sum_w, \
                        avg(p.score) AS avg_s, \
                        min(p.weight) AS min_w, \
                        max(p.weight) AS max_w, \
                        collect(p.weight) AS col_w \
                 ORDER BY c_star DESC, pid, name";
    let res = exec_cypher(&g, query);
    assert!(take_path_fold_bindings() > 0, "morsel agg must be engaged");

    let oracle_query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
                        WITH f, p \
                        RETURN p.personID AS pid, p.name AS name, \
                               count(*) AS c_star, \
                               count(f) AS c_f, \
                               sum(p.weight) AS sum_w, \
                               avg(p.score) AS avg_s, \
                               min(p.weight) AS min_w, \
                               max(p.weight) AS max_w, \
                               collect(p.weight) AS col_w \
                        ORDER BY c_star DESC, pid, name";
    let oracle = exec_cypher(&g, oracle_query);

    assert_eq!(res.rows, oracle.rows);
}

#[test]
fn morsel_agg_distinct_with_node_prop_fallback_to_general() {
    let b = Builder::new();
    // Two distinct Person nodes sharing name="Shared"
    let p1 = b.prop_node(
        "p1",
        &["Person"],
        &[("name", Value::String("Shared".into()))],
    );
    let p2 = b.prop_node(
        "p2",
        &["Person"],
        &[("name", Value::String("Shared".into()))],
    );

    let f1 = b.node("f1", &["Person"]);
    let f2 = b.node("f2", &["Person"]);

    // f1 follows BOTH p1 and p2!
    b.rel(f1, p1, "FOLLOWS", &[]);
    b.rel(f1, p2, "FOLLOWS", &[]);
    // f2 follows p2
    b.rel(f2, p2, "FOLLOWS", &[]);

    let g = b.done();

    // With DISTINCT f, the distinct set must span across both p1 and p2 (count = 2, f1 and f2)
    let query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
                 RETURN p.name, count(DISTINCT f) AS c";
    let res = exec_cypher(&g, query);

    let oracle_query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
                        WITH f, p \
                        RETURN p.name, count(DISTINCT f) AS c";
    let oracle = exec_cypher(&g, oracle_query);

    assert_eq!(res.rows, oracle.rows);
    assert_eq!(
        res.rows,
        vec![vec![Value::String("Shared".into()), Value::Int(2)]]
    );
}

#[test]
fn morsel_agg_mixed_node_and_node_prop_keys() {
    let b = Builder::new();
    let p1 = b.prop_node(
        "p1",
        &["Person"],
        &[("name", Value::String("SameName".into()))],
    );
    let p2 = b.prop_node(
        "p2",
        &["Person"],
        &[("name", Value::String("SameName".into()))],
    );

    let f1 = b.node("f1", &["Person"]);
    let f2 = b.node("f2", &["Person"]);
    let f3 = b.node("f3", &["Person"]);

    b.rel(f1, p1, "FOLLOWS", &[]);
    b.rel(f2, p1, "FOLLOWS", &[]);
    b.rel(f3, p2, "FOLLOWS", &[]);

    let g = b.done();

    let _ = take_path_fold_bindings();
    let query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
                 RETURN p, p.name, count(f) AS c \
                 ORDER BY c DESC";
    let res = exec_cypher(&g, query);
    assert!(take_path_fold_bindings() > 0, "morsel agg must be engaged");

    let oracle_query = "MATCH (f:Person)-[:FOLLOWS]->(p:Person) \
                        WITH f, p \
                        RETURN p, p.name, count(f) AS c \
                        ORDER BY c DESC";
    let oracle = exec_cypher(&g, oracle_query);

    assert_eq!(res.rows, oracle.rows);
    assert_eq!(res.rows.len(), 2);
    assert_eq!(res.rows[0][2], Value::Int(2));
    assert_eq!(res.rows[1][2], Value::Int(1));
}