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