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
//! The chain kernel's cost gate on hops the walk model cannot price
//! (LABS-991): an untyped rel, or a hop leaving an unlabeled node, rides
//! the any-label out-degree tail instead of passing the row estimate
//! through — so a graph with a high-degree node declines the chain to
//! the streaming walker (with the walker's own answer), while a graph
//! without one keeps the kernel.
use relate_core::Value;
use crate::exec::test_support::{build, exec_cypher, Built};
use super::{take_kernel_engagements, with_kernels_disabled};
/// A five-node `:N` chain (`0 -> 1 -> 2 -> 3 -> 4`, rel type `R`), plus —
/// when `hub_leaves > 0` — a separate hub node fanning out to that many
/// leaves. The chain is the only 4-hop path, so the answer is one row
/// either way; the hub only moves the any-label out-degree tail (a
/// hub of 64+ leaves puts four unlabeled hops past the gate's 2^20
/// stage ceiling).
fn chain_with_hub(hub_leaves: usize) -> Built {
let total = 5 + usize::from(hub_leaves > 0) + hub_leaves;
let labels: Vec<&[&str]> = (0..total).map(|_| &["N"][..]).collect();
let mut rels: Vec<(usize, usize, &str)> = (0..4).map(|i| (i, i + 1, "R")).collect();
rels.extend((0..hub_leaves).map(|leaf| (5, 6 + leaf, "R")));
build(&labels, &rels)
}
/// Every pattern the walk model leaves unpriced: untyped, typed through
/// unlabeled interior nodes, and untyped through labeled ones.
const SHAPES: [&str; 3] = [
"(s:N {id: 0})-->()-->()-->()-->(n:N)",
"(s:N {id: 0})-[:R]->()-[:R]->()-[:R]->()-[:R]->(n:N)",
"(s:N {id: 0})-->(:N)-->(:N)-->(:N)-->(n:N)",
];
/// The rows-kernel statement over `pattern`: a `LIMIT` keeps the count
/// fold (LABS-1293) out — a plain `RETURN count` rides the fold, which
/// never lands the last stage and so prices one hop fewer — so the rows
/// kernel's gate is what the two tests pin.
fn rows_statement(pattern: &str) -> String {
format!("MATCH {pattern} RETURN count(n) AS c LIMIT 5")
}
#[test]
fn a_high_degree_node_declines_the_unpriced_chain_to_the_walker() {
let built = chain_with_hub(64);
for pattern in SHAPES {
let cypher = rows_statement(pattern);
let _ = take_kernel_engagements();
let answer = exec_cypher(&built.graph, &cypher).rows;
assert_eq!(take_kernel_engagements(), 0, "declined: {cypher}");
assert_eq!(answer, vec![vec![Value::Int(1)]], "{cypher}");
let walker = with_kernels_disabled(|| exec_cypher(&built.graph, &cypher).rows);
assert_eq!(answer, walker, "{cypher}");
}
}
#[test]
fn a_thin_graph_keeps_the_kernel_on_the_unpriced_chain() {
// No node out-degree above 1: the any-label tail is 1, the bound
// never grows, and the kernel stays engaged.
let built = chain_with_hub(0);
for pattern in SHAPES {
let cypher = rows_statement(pattern);
let _ = take_kernel_engagements();
let answer = exec_cypher(&built.graph, &cypher).rows;
assert_eq!(take_kernel_engagements(), 1, "engaged: {cypher}");
assert_eq!(answer, vec![vec![Value::Int(1)]], "{cypher}");
}
}