use crate::wire::plan_keys;
use crate::CypherValue;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PlanFusedMetricsWire {
pub seek_ns: i64,
pub node_reads: i64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FusedChildWire {
pub parent: i64,
pub metrics: Option<PlanFusedMetricsWire>,
}
pub(crate) fn fused_child(args: &crate::IndexMap<String, CypherValue>) -> Option<FusedChildWire> {
let parent = super::plan_args::optional_int(args, plan_keys::FUSED_INTO)?;
for key in plan_keys::FUSED_CHILD_NEVER
.iter()
.chain(plan_keys::METRIC_BLOCK_KEYS.iter())
{
if args.contains_key(*key) {
panic!(
"plan wire {key} never rides a fused child — the walk and the \
PROFILE metric block stay the fused parent's (virtual plan operators)"
);
}
}
let metrics =
super::plan_args::metric_group_ints(args, [plan_keys::SEEK_TIME_NS, plan_keys::NODE_READS])
.map(|[seek_ns, node_reads]| PlanFusedMetricsWire {
seek_ns,
node_reads,
});
Some(FusedChildWire { parent, metrics })
}
#[cfg(test)]
mod tests {
use super::*;
use crate::wire::plan_node_wire;
use crate::wire::plan_test_fixtures::{args_mut, fused_seek_wire_node, plan_wire_node};
#[test]
fn a_fused_seek_child_carries_its_parent_link_and_seek_pair() {
let child = fused_seek_wire_node(2, 1, 40, 3);
let root = plan_wire_node("NodeIndexSeek", 1, None, vec![child]);
let wire = plan_node_wire(&root);
assert_eq!(wire.fused, None);
let child = plan_node_wire(&wire.children[0]);
assert_eq!(
child.fused,
Some(FusedChildWire {
parent: 1,
metrics: Some(PlanFusedMetricsWire {
seek_ns: 40,
node_reads: 3
})
})
);
assert!(
child.metrics.is_none(),
"a fused child never carries the metric block"
);
}
#[test]
fn a_fused_hop_child_carries_no_metrics() {
let mut hop = plan_wire_node("Expand", 3, None, vec![]);
args_mut(&mut hop).insert(plan_keys::FUSED_INTO.to_string(), CypherValue::Int(1));
let wire = plan_node_wire(&hop);
assert_eq!(
wire.fused,
Some(FusedChildWire {
parent: 1,
metrics: None
})
);
}
#[test]
#[should_panic(expected = "plan wire Hops never rides a fused child")]
fn a_fused_child_never_carries_the_walk_kinds() {
let mut hop = plan_wire_node("Expand", 3, None, vec![]);
let args = args_mut(&mut hop);
args.insert(plan_keys::FUSED_INTO.to_string(), CypherValue::Int(1));
args.insert(plan_keys::HOPS.to_string(), CypherValue::Int(7));
let _ = plan_node_wire(&hop);
}
#[test]
#[should_panic(expected = "plan wire Rows never rides a fused child")]
fn a_fused_child_never_carries_the_metric_block() {
let mut child = fused_seek_wire_node(2, 1, 40, 3);
let args = args_mut(&mut child);
for key in plan_keys::METRIC_BLOCK_KEYS {
args.insert(key.to_string(), CypherValue::Int(1));
}
let _ = plan_node_wire(&child);
}
}