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
//! Unit tests for the frontier cost gate's estimate and break-even.

use relate_ir::{Direction, NodePattern, PatternElement, RelPattern};
use relate_names::NamedToken;
use relate_planner::{ContainsCount, DegreeMoments, PatternStats};

use super::frontier_cost::{
    estimate, excess_degree, expand_max_hops, mean_degree, CostEstimate, Expansion,
    MAX_ESTIMATE_HOPS,
};
use crate::exec::test_support::{
    build, exec_cypher, frontier_decline_of, named_node, one_hop_rel, profiled, quantified, served,
};
use crate::frontier_decline::{FrontierDecline, FrontierDeclineReason};
use crate::graph::Graph;
use crate::metrics::KernelRung;
use crate::pattern_engine::PatternEngine;

/// One uniform label: `nodes` nodes whose out-degree moments are fixed.
struct Uniform {
    nodes: u64,
    moments: Option<DegreeMoments>,
    /// The in-degree sum the pair counts keep (`None`: no pair counts).
    in_sum: Option<u128>,
    /// Degree-moments consults made, each a histogram scan on the live graph.
    consults: std::cell::Cell<usize>,
}

impl Uniform {
    /// `nodes` nodes whose degree moments are `sum` and `sum_sq`.
    fn new(nodes: u64, sum: u128, sum_sq: u128) -> Self {
        Self {
            nodes,
            moments: Some(DegreeMoments { sum, sum_sq }),
            // A symmetric label: as many rels arrive as leave.
            in_sum: Some(sum),
            consults: Default::default(),
        }
    }

    /// A label with `out` rels leaving and `inbound` arriving.
    fn directed(nodes: u64, out: u128, inbound: u128) -> Self {
        Self {
            in_sum: Some(inbound),
            ..Self::new(nodes, out, out * 4)
        }
    }

    /// No histogram: nothing to price with.
    fn unpriceable(nodes: u64) -> Self {
        Self {
            nodes,
            moments: None,
            in_sum: None,
            consults: Default::default(),
        }
    }
}

impl PatternStats for Uniform {
    fn label_rows(&self, _: &NamedToken) -> Option<u64> {
        Some(self.nodes)
    }
    fn equality_rows(&self, _: &NamedToken, _: &NamedToken, _: &relate_ir::Expr) -> Option<u64> {
        None
    }
    fn contains_rows(
        &self,
        _: &NamedToken,
        _: &NamedToken,
        _: &relate_ir::Expr,
    ) -> Option<ContainsCount> {
        None
    }
    fn rel_type_rows(&self, _: &NamedToken) -> Option<u64> {
        None
    }
    fn degree_moments(&self, _: &[NamedToken], _: &[NamedToken]) -> Option<DegreeMoments> {
        self.consults.set(self.consults.get() + 1);
        self.moments
    }
    fn in_degree_sum(&self, _: &[NamedToken], _: &[NamedToken]) -> Option<u128> {
        self.in_sum
    }
}

fn rel(direction: Direction) -> RelPattern {
    let mut rel = one_hop_rel(&[]);
    rel.direction = direction;
    rel
}

fn start() -> NodePattern {
    named_node("a")
}

#[test]
fn the_break_even_is_trails_against_pairs_and_probes() {
    // Trails ~ 1.5 x pairs is the measured break-even: well above it the
    // frontier wins, at or below it the walker keeps the clause.
    let wins = CostEstimate {
        trails: 4000.0,
        pairs: 1000.0,
        probes: 1000.0,
    };
    assert!(wins.frontier_wins());
    let loses = CostEstimate {
        trails: 1000.0,
        pairs: 1000.0,
        probes: 1000.0,
    };
    assert!(!loses.frontier_wins());
    let empty = CostEstimate {
        trails: 0.0,
        pairs: 0.0,
        probes: 0.0,
    };
    assert!(!empty.frontier_wins());
}

#[test]
fn degree_moments_price_mean_and_excess() {
    let moments = DegreeMoments {
        sum: 600,
        sum_sq: 3000,
    };
    assert_eq!(mean_degree(moments.sum, 100), 6.0);
    assert_eq!(mean_degree(moments.sum, 0), 0.0);
    assert_eq!(excess_degree(moments), 4.0);
    assert_eq!(excess_degree(DegreeMoments::default()), 0.0);
    // Degree-1 nodes have no excess.
    assert_eq!(
        excess_degree(DegreeMoments {
            sum: 10,
            sum_sq: 10
        }),
        0.0
    );
}

/// Undirected hops count every rel twice: the trails of an undirected
/// chain exceed the directed chain's by the doubling on each hop.
#[test]
fn an_undirected_hop_counts_twice() {
    let stats = Uniform::new(100, 300, 1200);
    let directed = rel(Direction::Outgoing);
    let undirected = rel(Direction::Both);
    let one = |rel: &RelPattern| {
        estimate(&stats, 100, 100, 100, &start(), &Expansion::chain(&[rel])).unwrap()
    };
    // First hop: mean 3 per node (x2 undirected) from 100 seeds.
    assert_eq!(one(&directed).trails, 300.0);
    assert_eq!(one(&undirected).trails, 600.0);
    // Second hop rides the excess degree 1200/300 - 1 = 3 (x2).
    let two_directed = estimate(
        &stats,
        100,
        100,
        100,
        &start(),
        &Expansion::chain(&[&directed, &directed]),
    )
    .unwrap();
    assert_eq!(two_directed.trails, 300.0 + 900.0);
    let two_undirected = estimate(
        &stats,
        100,
        100,
        100,
        &start(),
        &Expansion::chain(&[&undirected, &undirected]),
    )
    .unwrap();
    assert_eq!(two_undirected.trails, 600.0 + 3600.0);
}

/// The first hop prices the side it leaves the start label by: a label
/// whose rels all point at it (the co-change `File`: zero out-degree) has
/// no outgoing fanout but a full incoming and undirected one — never the
/// zero a doubled out-sum produced.
#[test]
fn the_first_hop_prices_the_side_it_leaves_by() {
    let sink = Uniform::directed(100, 0, 400);
    let one = |stats: &Uniform, direction| {
        estimate(
            stats,
            100,
            100,
            100,
            &start(),
            &Expansion::chain(&[&rel(direction)]),
        )
        .unwrap()
        .trails
    };
    assert_eq!(one(&sink, Direction::Outgoing), 0.0);
    assert_eq!(one(&sink, Direction::Incoming), 400.0);
    assert_eq!(one(&sink, Direction::Both), 400.0);
    // A source label is the mirror image.
    let source = Uniform::directed(100, 400, 0);
    assert_eq!(one(&source, Direction::Outgoing), 400.0);
    assert_eq!(one(&source, Direction::Incoming), 0.0);
    assert_eq!(one(&source, Direction::Both), 400.0);
    // Mixed: undirected is the sum of both sides, not twice either.
    let mixed = Uniform::directed(100, 100, 300);
    assert_eq!(one(&mixed, Direction::Both), 400.0);
}

/// An undirected or incoming first hop needs the in side: without pair
/// counts there is no estimate, while an outgoing one still prices.
#[test]
fn a_missing_in_degree_side_leaves_only_outgoing_priceable() {
    let stats = Uniform {
        in_sum: None,
        ..Uniform::new(10, 30, 120)
    };
    let one = |direction| {
        estimate(
            &stats,
            10,
            10,
            10,
            &start(),
            &Expansion::chain(&[&rel(direction)]),
        )
    };
    assert!(one(Direction::Outgoing).is_some());
    assert!(one(Direction::Incoming).is_none());
    assert!(one(Direction::Both).is_none());
}

/// Pairs never exceed seeds x endpoint population; probes never exceed
/// the trails.
#[test]
fn pairs_and_probes_are_capped() {
    let stats = Uniform::new(10, 80, 800);
    let both = rel(Direction::Both);
    let hops = Expansion::repeat(&both, 4);
    let cost = estimate(&stats, 10, 10, 10, &start(), &hops).unwrap();
    assert!(cost.trails > 10_000.0);
    assert_eq!(cost.pairs, 100.0);
    assert_eq!(cost.probes, 10.0 * 160.0);
    assert!(cost.frontier_wins());
}

/// No histogram: nothing to price with.
#[test]
fn unpriceable_statistics_have_no_estimate() {
    let stats = Uniform::unpriceable(10);
    let both = rel(Direction::Both);
    assert!(estimate(&stats, 10, 10, 10, &start(), &Expansion::chain(&[&both])).is_none());
}

/// `n` File nodes, each joined to the next `reach` nodes around a ring
/// (`T` rels): degree `2 * reach` undirected.
fn ring(n: usize, reach: usize) -> Graph {
    let labels: Vec<&[&str]> = vec![&["File"]; n];
    let rels: Vec<(usize, usize, &str)> = (0..n)
        .flat_map(|i| (1..=reach).map(move |step| (i, (i + step) % n, "T")))
        .collect();
    build(&labels, &rels).graph
}

/// `commits` Commit nodes each touching `reach` of `files` File nodes
/// (`TOUCHES` rels point at File, so File has no out-edges — the
/// co-change shape).
fn co_change(commits: usize, files: usize, reach: usize) -> Graph {
    let labels: Vec<&[&str]> = (0..files)
        .map(|_| &["File"][..])
        .chain((0..commits).map(|_| &["Commit"][..]))
        .collect();
    let rels: Vec<(usize, usize, &str)> = (0..commits)
        .flat_map(|c| (0..reach).map(move |k| (files + c, (c + k) % files, "TOUCHES")))
        .collect();
    build(&labels, &rels).graph
}

/// Run `cypher` under `engine`, leaving the graph on the default routing.
fn rows_under(g: &mut Graph, engine: PatternEngine, cypher: &str) -> Vec<Vec<crate::exec::Value>> {
    g.configure_pattern_engine(engine);
    let rows = exec_cypher(g, cypher).rows;
    g.configure_pattern_engine(PatternEngine::Auto);
    rows
}

/// The distinct-endpoint count statement over `pattern`.
fn distinct_reach(pattern: &str) -> String {
    format!("MATCH {pattern} WHERE a <> b WITH DISTINCT a, b RETURN count(*) AS c")
}

/// The auto gate end to end: a dense graph's bounded distinct-endpoint
/// recursion rides the frontier (typed and untyped, directed or not),
/// where a short bound over a sparse ring keeps the walker with the typed
/// `Cost` decline in PROFILE — and the answers agree.
#[test]
fn the_auto_gate_engages_dense_distinct_reach_and_holds_short_bounds() {
    let mut dense = ring(40, 4);
    let mut sparse = ring(40, 1);
    let engaged = |g: &Graph, cypher: &str| {
        let rung = served(&profiled(g, cypher)).kernel;
        rung == Some(KernelRung::FrontierNfa)
    };
    // Dense rides the frontier on the long bounds; the short bounds sit
    // near the break-even, so only the sparse side is pinned for them.
    let long_bounds = [
        "(a:File)-[:T*1..4]-(b:File)",
        "(a:File)-[*1..4]-(b)",
        "(a:File)-[*1..4]->(b)",
    ];
    let short_bounds = ["(a:File)-[:T*1..2]-(b:File)", "(a:File)-[*1..2]-(b)"];
    for pattern in long_bounds {
        assert!(engaged(&dense, &distinct_reach(pattern)), "dense {pattern}");
    }
    for pattern in long_bounds.into_iter().chain(short_bounds) {
        let cypher = distinct_reach(pattern);
        assert!(!engaged(&sparse, &cypher), "sparse {cypher}");
        assert_eq!(
            frontier_decline_of(&sparse, &cypher),
            Some(FrontierDeclineReason::Plan(FrontierDecline::Cost)),
            "sparse {cypher}"
        );
        // The routing never changes the answer.
        for g in [&mut dense, &mut sparse] {
            let auto = rows_under(g, PatternEngine::Auto, &cypher);
            let walk = rows_under(g, PatternEngine::Walk, &cypher);
            assert_eq!(auto, walk, "{cypher}");
        }
    }
}

/// Untyped and typed, directed and undirected, bounded and unbounded
/// distinct-endpoint statements answer the walker's rows under every
/// routing (the walker, the cost-gated default, the forced rung).
#[test]
fn every_routing_agrees_on_untyped_and_typed_reach() {
    let mut g = ring(7, 2);
    for pattern in [
        "(a:File)-[*]-(b)",
        "(a:File)-[*2..]->(b:File)",
        "(a:File)<-[*..3]-(b)",
        "(a:File)-[:T*]-(b:File)",
        "(a:File)-[*1..3]-(b)",
        "(a:File)-[:T*1..3]->(b)",
        "(a:File)-[:T|U*1..3]-(b)",
    ] {
        let cypher = distinct_reach(pattern);
        let walk = rows_under(&mut g, PatternEngine::Walk, &cypher);
        let auto = rows_under(&mut g, PatternEngine::Auto, &cypher);
        let nfa = rows_under(&mut g, PatternEngine::Nfa, &cypher);
        assert_eq!(walk, auto, "auto: {cypher}");
        assert_eq!(walk, nfa, "nfa: {cypher}");
    }
}

/// A path whose one hop is a quantified `a-[T]->b` body, `min..max`.
fn quantified_spine(min: u32, max: u32) -> [PatternElement; 2] {
    [
        PatternElement::Node(named_node("a")),
        PatternElement::Quantified(quantified(
            vec![
                PatternElement::Relationship(one_hop_rel(&["T"])),
                PatternElement::Node(named_node("b")),
            ],
            min,
            Some(max),
        )),
    ]
}

/// A quantifier expands to its maximum count: `*0..0` contributes no
/// hops, `*1..3` three.
#[test]
fn the_quantifier_expands_to_its_maximum() {
    let priced =
        |min: u32, max: u32| expand_max_hops(&quantified_spine(min, max)).map(|e| e.hops());
    assert_eq!(priced(0, 0), Some(0));
    assert_eq!(priced(1, 3), Some(3));
    assert_eq!(priced(2, 2), Some(2));
}

/// The hop ceiling has one spelling: a quantifier expanded to exactly
/// [`MAX_ESTIMATE_HOPS`] hops prices, one hop more declines (the caller
/// engages rather than underestimates).
#[test]
fn the_hop_ceiling_is_one_boundary() {
    let prices = |max: u32| expand_max_hops(&quantified_spine(1, max)).is_some();
    assert!(prices(MAX_ESTIMATE_HOPS as u32));
    assert!(!prices(MAX_ESTIMATE_HOPS as u32 + 1));
}

/// A quantifier repeats one rel up to the hop ceiling; the estimate
/// consults the histograms once per distinct rel, never per expanded
/// hop, so a crafted bounded pattern cannot inflate plan-time scans.
#[test]
fn the_estimate_consults_per_distinct_rel_not_per_hop() {
    let stats = Uniform::new(100, 300, 1200);
    let both = rel(Direction::Both);
    let hops = Expansion::repeat(&both, 1000);
    let cost = estimate(&stats, 100, 100, 100, &start(), &hops).unwrap();
    assert!(!cost.trails.is_nan());
    // First hop, one landing price for every later hop, the adjacency.
    assert_eq!(stats.consults.get(), 3);
}

/// LABS-1661: the co-change shape (undirected over rels that all point at
/// the start label) priced zero trails and declined on every bound. The
/// dense long bound now rides the frontier and the sparse short bound
/// keeps the walker, and the routings agree on the answer.
#[test]
fn the_auto_gate_prices_a_sink_label_from_its_in_degree() {
    let mut dense = co_change(24, 6, 4);
    let pattern = "(a:File)-[:TOUCHES*1..4]-(b:File)";
    let cypher = distinct_reach(pattern);
    assert_eq!(
        served(&profiled(&dense, &cypher)).kernel,
        Some(KernelRung::FrontierNfa)
    );
    let sparse = co_change(8, 8, 1);
    let short = distinct_reach("(a:File)-[:TOUCHES*1..2]-(b:File)");
    assert_eq!(
        frontier_decline_of(&sparse, &short),
        Some(FrontierDeclineReason::Plan(FrontierDecline::Cost))
    );
    let walk = rows_under(&mut dense, PatternEngine::Walk, &cypher);
    for engine in [PatternEngine::Auto, PatternEngine::Nfa] {
        assert_eq!(rows_under(&mut dense, engine, &cypher), walk, "{engine:?}");
    }
}