relate_model/plan_metrics_wire.rs
1//! The PROFILE metric-wire family (ADR-0344 phase 2 + metric fields,
2//! split from `plan_wire.rs` when the IN-TRANSACTIONS batch facts made it
3//! the family's seventh shape): the traversal rung a node reports, the
4//! metric set as named fields, and the shapes the set composes — the
5//! store-read mix, the phase times, the walk start, the frontier
6//! family's run counters, the `CALL { … } IN TRANSACTIONS` batch facts
7//! (REL8-2047) — plus the profiled-facts presentation bundle. The node
8//! wire form and its decoder stay in `plan_wire.rs`; this module is the
9//! family's one home, re-exported through `wire` the way the memo and
10//! warning shapes are.
11
12/// The traversal rung a PROFILE node reports (LABS-1117), typed from the
13/// wire vocabulary: the chain carries its hop count, so a chain without
14/// one — the half-state the parser rejects — is unrepresentable. [`Self::as_str`]
15/// is the vocabulary's one spelling; `plan_keys::KERNEL_RUNGS` is derived
16/// from it.
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum KernelRungWire {
19 /// The unrolled fixed-k chain kernel, `hops` hops.
20 Chain { hops: i64 },
21 /// The hub-star candidate-intersection kernel.
22 HubStar,
23 /// The frontier-NFA rung over the BFS.
24 FrontierNfa,
25 /// The per-row walker — every gate's decline.
26 Walker,
27}
28
29impl KernelRungWire {
30 /// The chain rung's wire name — a name, never a variant value, so no
31 /// hop count is fabricated to reach it.
32 pub const CHAIN: &'static str = "chain";
33 /// The hub-star rung's wire name.
34 pub const HUB_STAR: &'static str = "hub-star";
35 /// The frontier-NFA rung's wire name.
36 pub const FRONTIER_NFA: &'static str = "frontier-nfa";
37 /// The per-row walker's wire name.
38 pub const WALKER: &'static str = "walker";
39 /// Every rung name, in vocabulary order — the wire table's one
40 /// spelling. `plan_keys::KERNEL_RUNGS` is this list; the executor's
41 /// `KernelRung::as_str` reads the same constants.
42 pub const NAMES: [&'static str; 4] = [
43 Self::CHAIN,
44 Self::HUB_STAR,
45 Self::FRONTIER_NFA,
46 Self::WALKER,
47 ];
48
49 /// The rung's wire name (`const` so the table derives from this match
50 /// instead of repeating it).
51 pub const fn as_str(self) -> &'static str {
52 match self {
53 KernelRungWire::Chain { .. } => Self::CHAIN,
54 KernelRungWire::HubStar => Self::HUB_STAR,
55 KernelRungWire::FrontierNfa => Self::FRONTIER_NFA,
56 KernelRungWire::Walker => Self::WALKER,
57 }
58 }
59
60 /// The chain rung's hop count — `None` for every other rung.
61 pub const fn hops(self) -> Option<i64> {
62 match self {
63 KernelRungWire::Chain { hops } => Some(hops),
64 KernelRungWire::HubStar | KernelRungWire::FrontierNfa | KernelRungWire::Walker => None,
65 }
66 }
67}
68
69/// The PROFILE metric set (ADR-0344 phase 2 + metric fields) as named
70/// fields: which number is rows, nanoseconds, store accesses, or
71/// row-materialization bytes is stated by the names, not by a position
72/// convention.
73#[derive(Clone, Copy, Debug, Default, PartialEq)]
74pub struct PlanMetricsWire {
75 pub rows: i64,
76 pub time_ns: i64,
77 pub db_hits: i64,
78 pub memory_bytes: i64,
79 /// Expression allocation bytes charged by the operator's clause
80 /// (ADR-0463), beside the row-materialization `memory_bytes`.
81 pub expression_built_bytes: i64,
82 /// The store-read mix behind `db_hits` (LABS-369).
83 pub reads: PlanReadsWire,
84 /// The phase times behind the read kinds (LABS-448): the seek's and
85 /// the walk's elapsed nanoseconds — emitted with the metric block,
86 /// like the kinds, so a half-present pair is a wire-contract break.
87 pub phases: PlanPhasesWire,
88 /// The walk's chosen start, when the clause walked one (LABS-369).
89 pub start: Option<PlanStartWire>,
90 /// Which rung served the clause's `MATCH` walk (LABS-1117), typed: the
91 /// chain carries its hop count, so the half-state the wire rejects (a
92 /// chain rung without one) is unrepresentable here. `None` when the
93 /// producer emitted no key (a non-`MATCH` clause, a folded or `SEARCH`
94 /// `MATCH`, or a batch-chain `MATCH`).
95 pub kernel: Option<KernelRungWire>,
96 /// Whether the clause ran ADR-0468's batch scan/filter/project chain
97 /// (LABS-359) — `false` on the wire means absent, the producer emits
98 /// the key only when true.
99 pub batched: bool,
100 /// The batch filter's zone skips (LABS-645): conjunct-batch
101 /// evaluations the chunk's derived min/max zone metadata provably
102 /// replaced with the correct empty survivor set — `0` on the wire
103 /// means absent, the producer emits the key only when nonzero.
104 pub zone_skips: i64,
105 /// The frontier family's run counters (ADR-0478), `Default` = the
106 /// family declined the clause: the producer emits the whole set —
107 /// the run counters, the NFA rung's facts, and the budget fraction —
108 /// only when it ran (`levels > 0`), all together like the start
109 /// trio.
110 pub frontier: PlanFrontierWire,
111 /// Why the frontier rung did not serve the clause: a run-time decline
112 /// (ADR-0516) or a plan-time gate (ADR-0478), one of the
113 /// `plan_keys::DECLINE_REASONS` vocabulary — `None` on every clause
114 /// the rung served or never considered, present only on a PROFILE node.
115 pub frontier_decline: Option<&'static str>,
116 /// The expression-memo counters (ADR-0654): `None` = no memo was
117 /// probed on this node, the producer emitting the group only when one
118 /// was (the `batched` / `zone_skips` / `frontier` presence rule).
119 pub memo: Option<super::plan_memo::PlanMemoWire>,
120 /// The `CALL { … } IN TRANSACTIONS` batch facts (REL8-2047):
121 /// `Default` = the arm ran no batch, the producer emitting the seven
122 /// integers together only when one did (the `frontier` presence
123 /// rule's).
124 pub intx: PlanInTxWire,
125}
126
127/// The frontier family's run counters a PROFILE node reports (ADR-0478):
128/// the BFS's edge visits (the work its walk budget caps), its completed
129/// levels, and its accepted endpoints — the `[frontier …]` operator
130/// marker's own trio — beside the NFA rung's own facts: the
131/// (node, state) cells the runs created, the re-arrival rule memo's
132/// hits and misses, the admission asks by verdict, and the budget
133/// fraction the rule's work spent.
134#[derive(Clone, Copy, Debug, Default, PartialEq)]
135pub struct PlanFrontierWire {
136 pub edge_visits: i64,
137 pub levels: i64,
138 pub endpoints: i64,
139 /// (node, state) cells the runs created — the pairs that occupied
140 /// the span×states tables.
141 pub cells: i64,
142 /// Re-arrival rule memo hits: asks the verdict table answered
143 /// without a search.
144 pub memo_hits: i64,
145 /// Re-arrival rule memo misses: asks that ran the bounded search.
146 pub memo_misses: i64,
147 /// Admission asks the representative-chain proof settled.
148 pub representative_proofs: i64,
149 /// Admission asks the bounded search proved.
150 pub search_proven: i64,
151 /// Admission asks the bounded search disproved within its bounds.
152 pub search_disproven: i64,
153 /// Admission asks the shared work budget cut (ADR-0516).
154 pub declined_exhausted: i64,
155 /// The re-arrival work fraction the runs spent — one run's budget as
156 /// the denominator, so `1.0` is a drained (declined) run and a
157 /// multi-seed clause reads as run-equivalents.
158 pub budget_fraction: f64,
159}
160
161/// The `CALL { … } IN TRANSACTIONS` batch facts a PROFILE node reports
162/// (REL8-2047): the batch counters — run, committed, rolled back — and
163/// the per-batch maxima: the input batch's and the installed output's
164/// retained row bytes, the scratch the batch created, and the redo
165/// log's reserved bytes at commit (the WAL payload it wrote). `Default`
166/// = the arm ran no batch (or the node is not the batched call): the
167/// producer emits the seven integers together only when one did.
168#[derive(Clone, Copy, Debug, Default, PartialEq)]
169pub struct PlanInTxWire {
170 /// Batches that started a transaction.
171 pub batches: i64,
172 /// Batches that committed.
173 pub committed: i64,
174 /// Batches that rolled back under the `ON ERROR` policy.
175 pub rolled_back: i64,
176 /// The largest input batch's retained row bytes.
177 pub peak_input_rows_bytes: i64,
178 /// The largest installed output's retained row bytes.
179 pub peak_output_rows_bytes: i64,
180 /// The most scratch bytes one batch created (redo excluded).
181 pub peak_scratch_bytes: i64,
182 /// The most redo-log bytes one batch carried at its commit — the WAL
183 /// payload it wrote.
184 pub peak_wal_bytes: i64,
185}
186
187impl PlanInTxWire {
188 /// Whether the arm ran any batch — the group's one presence rule (the
189 /// executor twin is `relate_executor::InTxBatches::ran`): the wire
190 /// emits the seven keys only when it holds, so their absence is the
191 /// whole "no batches" fact.
192 pub fn ran(&self) -> bool {
193 self.batches > 0
194 }
195}
196
197/// A metrics-carrying node's presentation bundle (cumulative vs self):
198/// the PROFILE metric set and its subtree total — the derived total
199/// travels with the metrics it sums, present together or not at all, so
200/// no presentation can render a total beside unavailable metrics or a
201/// metric set without one.
202#[derive(Clone, Copy, Debug, PartialEq)]
203pub struct PlanProfiledFacts {
204 /// The node's PROFILE metric set.
205 pub metrics: PlanMetricsWire,
206 /// The subtree total of the node's elapsed time: its own `time_ns`
207 /// plus every descendant's — the one fact the `Total (ms)` cell and
208 /// the document's `total_time_ms` render.
209 pub total_time_ns: i64,
210}
211
212/// The store-read mix a PROFILE node reports (LABS-369): the kinds the
213/// one `Db Hits` total blends — relationship hops, neighbor walks, and
214/// node-store reads.
215#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
216pub struct PlanReadsWire {
217 pub hops: i64,
218 pub neighbor_walks: i64,
219 pub node_reads: i64,
220}
221
222/// The phase times a PROFILE node reports (LABS-448): the elapsed time
223/// the read kinds' store work spent — the seek (candidate resolution and
224/// admission, the `Nodes` kind's work) and the walk (relationship
225/// traversal, the `Rels`/`Degree` kinds' work), inclusive of a
226/// multi-element pattern's nested phases.
227#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
228pub struct PlanPhasesWire {
229 /// The seek phase's nanoseconds.
230 pub seek_ns: i64,
231 /// The walk phase's nanoseconds.
232 pub walk_ns: i64,
233}
234
235/// How a PROFILE node's walk chose its start (LABS-369): the pattern
236/// element index, the direction, and the reason class the executor
237/// recorded (`bound-pivot`, `cheapest`, `seek-anchored`, `index-0`).
238#[derive(Clone, Copy, Debug, PartialEq, Eq)]
239pub struct PlanStartWire {
240 pub element: i64,
241 pub backward: bool,
242 /// The reason class, one of the `plan_keys::REASONS` vocabulary — a
243 /// borrowed static, like the warning names.
244 pub reason: &'static str,
245}