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
//! Composable `ExpandInto` for bound-both-ends 1-hop relationship expansion.
//!
//! When both ends of a fixed 1-hop relationship are row-bound, `expand_into_hop`
//! probes the smaller-degree side (flipping the scan direction when the target has
//! smaller typed degree) and verifies connectivity in O(1) via relationship endpoints
//! before property evaluation or row cloning.
use relate_core::{Error, NodeId, Result};
use relate_ir::Direction;
use crate::pattern_walk::{commit_trail, RelHop, UsedRels};
use crate::row::Row;
use super::expand::{admit_candidate, PathMatch};
/// Expand a fixed 1-hop relationship between two pre-bound nodes (`from` and `target`).
///
/// Probes the smaller-degree side, checks relationship endpoints in O(1), and runs
/// the canonical `admit_candidate` with `from` and `dir` so relationship properties,
/// filters, `used` rel tracking, and `WalkTallies` behave identically to standard
/// expansion.
#[allow(clippy::too_many_arguments)]
pub(super) fn expand_into_hop(
pm: &PathMatch<'_, '_>,
rel_idx: usize,
from: NodeId,
target: NodeId,
dir: Direction,
row: Row,
used: UsedRels,
descend: &mut dyn FnMut(Row, UsedRels) -> Result<bool>,
) -> Result<()> {
let PathMatch {
graph,
path,
params,
..
} = *pm;
let rp = &path.rels[rel_idx];
let filter = &pm.rel_filters[rel_idx];
let types = &filter.types;
let from_deg = graph.typed_adj_len_resolved(from, dir, types);
let to_deg = graph.typed_adj_len_resolved(target, dir.flip(), types);
let (anchor, scan_dir, reversed) = if to_deg < from_deg {
(target, dir.flip(), true)
} else {
(from, dir, false)
};
pm.tallies.expand_into(reversed);
let rel_rung = &pm.ladder.rel[rel_idx];
let hop = RelHop {
pattern: rp,
filter,
};
graph.try_find_neighbor_resolved::<Error>(anchor, scan_dir, types, |rid| {
graph.cancel_tick()?;
// Fast O(1) endpoint check before property evaluation or Row/UsedRels cloning.
if graph.hop(from, rid, dir) != Some(target) {
return Ok(false);
}
// Canonical per-hop admission: evaluates edge predicates (type, properties),
// checks used set, and increments walk tallies. Always called with `from` and
// the forward traversal `dir`.
let Some(_next) = admit_candidate(
graph,
&row,
params,
hop,
rel_idx,
dir,
&used,
&[],
from,
rid,
pm.tallies,
)?
else {
return Ok(false);
};
let mut row2 = row.clone();
let mut used2 = used.clone();
if !commit_trail(
&mut row2,
&mut used2,
rp,
std::slice::from_ref(&rid),
graph,
rel_idx,
path.has_path_name(),
rel_rung,
) {
return Ok(false);
}
descend(row2, used2)
})?;
Ok(())
}