Skip to main content

relate_model/
plan_fused.rs

1//! The fused virtual child's wire shape (virtual plan operators): a
2//! display-only breakdown step under its fused parent operator — the
3//! seek step of an index-seek-started MATCH and one `Expand` step per
4//! hop of the logical pattern. Execution is untouched (ADR-0344: the
5//! clause stays the executed unit), so a fused child never carries the
6//! PROFILE metric block — rows, time and memory stay the fused parent's
7//! clause totals. A child carries a metric only where a real counter
8//! measured the step: the seek step's seek-phase time (LABS-448) and
9//! node reads (LABS-375), which for a seek-started MATCH are the start
10//! element's candidate resolution. A hop step carries none — no real
11//! per-hop counter exists (splitting the walk tallies per hop is a
12//! follow-up), so `Hops` on a fused child is a contract break, kept
13//! loud like every other half-state here.
14
15use crate::wire::plan_keys;
16use crate::CypherValue;
17
18/// The metrics a fused virtual child carries: the seek step's seek-phase
19/// time and node reads — the same candidate-funnel counter family, so
20/// they ride together or not at all. An `Expand` hop step carries none.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub struct PlanFusedMetricsWire {
23    /// The seek step's phase time, nanoseconds (LABS-448's seek clock).
24    pub seek_ns: i64,
25    /// The seek step's node-store reads (LABS-375's candidate funnel).
26    pub node_reads: i64,
27}
28
29/// One wire node's parsed fused facts (virtual plan operators): the
30/// fused parent operator's id and the step's own measured pair. The one
31/// fused fact — the `FusedInto` key's presence IS "this node is a fused
32/// child", so a flag beside it (or a parent link without one) is
33/// unrepresentable past the parse.
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35pub struct FusedChildWire {
36    /// The fused parent operator's id.
37    pub parent: i64,
38    /// The seek step's metrics; `None` on a hop step (no real per-hop
39    /// counter exists).
40    pub metrics: Option<PlanFusedMetricsWire>,
41}
42
43/// Read one node's fused facts, `None` on every non-fused node. The
44/// contract, kept loud like the accessor's other groups: `FusedInto` is
45/// the parent operator's non-negative id, and its presence is the whole
46/// fact; a fused child never carries the walk-side keys
47/// ([`plan_keys::FUSED_CHILD_NEVER`]) nor the PROFILE metric block
48/// ([`plan_keys::METRIC_BLOCK_KEYS`]) — the walk and the totals stay the
49/// fused parent's — and carries the seek pair (`SeekTimeNs`,
50/// `NodeReads`) through the one all-or-none group policy every metric
51/// family shares, together or not at all.
52pub(crate) fn fused_child(args: &crate::IndexMap<String, CypherValue>) -> Option<FusedChildWire> {
53    let parent = super::plan_args::optional_int(args, plan_keys::FUSED_INTO)?;
54    for key in plan_keys::FUSED_CHILD_NEVER
55        .iter()
56        .chain(plan_keys::METRIC_BLOCK_KEYS.iter())
57    {
58        if args.contains_key(*key) {
59            panic!(
60                "plan wire {key} never rides a fused child — the walk and the \
61                 PROFILE metric block stay the fused parent's (virtual plan operators)"
62            );
63        }
64    }
65    let metrics =
66        super::plan_args::metric_group_ints(args, [plan_keys::SEEK_TIME_NS, plan_keys::NODE_READS])
67            .map(|[seek_ns, node_reads]| PlanFusedMetricsWire {
68                seek_ns,
69                node_reads,
70            });
71    Some(FusedChildWire { parent, metrics })
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use crate::wire::plan_node_wire;
78    use crate::wire::plan_test_fixtures::{args_mut, plan_wire_node};
79
80    /// Compose a fused wire child: the seek step's shape — `FusedInto`
81    /// beside the seek pair, no metric block.
82    fn fused_seek_wire_node(id: i64, parent: i64, seek_ns: i64, node_reads: i64) -> CypherValue {
83        let mut node = plan_wire_node("NodeIndexSeek", id, None, vec![]);
84        let args = args_mut(&mut node);
85        args.insert(plan_keys::FUSED_INTO.to_string(), CypherValue::Int(parent));
86        args.insert(
87            plan_keys::SEEK_TIME_NS.to_string(),
88            CypherValue::Int(seek_ns),
89        );
90        args.insert(
91            plan_keys::NODE_READS.to_string(),
92            CypherValue::Int(node_reads),
93        );
94        node
95    }
96
97    /// The fused seek child reads its parent link and its own measured
98    /// pair — never the parent's metric block. The `FusedInto` key's
99    /// presence is the whole fused fact.
100    #[test]
101    fn a_fused_seek_child_carries_its_parent_link_and_seek_pair() {
102        let child = fused_seek_wire_node(2, 1, 40, 3);
103        let root = plan_wire_node("NodeIndexSeek", 1, None, vec![child]);
104        let wire = plan_node_wire(&root);
105        assert_eq!(wire.fused, None);
106        let child = plan_node_wire(&wire.children[0]);
107        assert_eq!(
108            child.fused,
109            Some(FusedChildWire {
110                parent: 1,
111                metrics: Some(PlanFusedMetricsWire {
112                    seek_ns: 40,
113                    node_reads: 3
114                })
115            })
116        );
117        assert!(
118            child.metrics.is_none(),
119            "a fused child never carries the metric block"
120        );
121    }
122
123    /// A fused hop child carries no metrics — no real per-hop counter
124    /// exists.
125    #[test]
126    fn a_fused_hop_child_carries_no_metrics() {
127        let mut hop = plan_wire_node("Expand", 3, None, vec![]);
128        args_mut(&mut hop).insert(plan_keys::FUSED_INTO.to_string(), CypherValue::Int(1));
129        let wire = plan_node_wire(&hop);
130        assert_eq!(
131            wire.fused,
132            Some(FusedChildWire {
133                parent: 1,
134                metrics: None
135            })
136        );
137    }
138
139    /// The walk-side keys stay the fused parent's: `Hops` on a fused
140    /// child is a wire-contract break, kept loud.
141    #[test]
142    #[should_panic(expected = "plan wire Hops never rides a fused child")]
143    fn a_fused_child_never_carries_the_walk_kinds() {
144        let mut hop = plan_wire_node("Expand", 3, None, vec![]);
145        let args = args_mut(&mut hop);
146        args.insert(plan_keys::FUSED_INTO.to_string(), CypherValue::Int(1));
147        args.insert(plan_keys::HOPS.to_string(), CypherValue::Int(7));
148        let _ = plan_node_wire(&hop);
149    }
150
151    /// The parent's rows, time and memory stay the parent's: the metric
152    /// block on a fused child is a wire-contract break. The block's keys
153    /// are the one table the accessor and the fixtures read, never a
154    /// second list here.
155    #[test]
156    #[should_panic(expected = "plan wire Rows never rides a fused child")]
157    fn a_fused_child_never_carries_the_metric_block() {
158        let mut child = fused_seek_wire_node(2, 1, 40, 3);
159        let args = args_mut(&mut child);
160        for key in plan_keys::METRIC_BLOCK_KEYS {
161            args.insert(key.to_string(), CypherValue::Int(1));
162        }
163        let _ = plan_node_wire(&child);
164    }
165}