relate_executor/effect.rs
1//! The engine's truthful statement write effect (ADR-0402 Decision 2): the
2//! fact the CLI's zero-effect write note is allowed to read, recorded by the
3//! execution itself instead of inferred from the plan's conservative
4//! query-type classification or from matching clause names at a surface.
5//!
6//! A statement's plan-derived query type is deliberately conservative
7//! (ADR-0332): a top-level `CALL { … }` and every `LOAD CSV` count as writes
8//! so the namespace write gate cannot be bypassed, even when the body only
9//! reads. It also cannot see catalog changes — `ALTER DATABASE`, a graph-type
10//! change, `db.ontology.set` — which leave every ADR-0309 mutation counter at
11//! zero while the store did change. Neither fact can answer "did this
12//! statement write and apply nothing", so the executor records the answer
13//! where it is known: the statement's plan holds a mutation operator, and the
14//! statement's write redo took a change.
15
16/// What one statement's execution did to the store: one of exactly three
17/// states, never a pair of flags whose fourth combination has to be explained
18/// away.
19///
20/// Both facts are recorded by the statement boundary itself, never inferred
21/// from the statement's surface spelling:
22///
23/// - the *attempt* fact is the plan walk's answer — the statement's plan holds
24/// a mutation operator (a data-write clause, schema DDL, a catalog write, or
25/// a writes-flagged procedure call) — recorded once at statement entry
26/// through the canonical walk, which descends into `NEXT` stages and every
27/// nested body. It is a fact about the statement, not about rows, so a write
28/// whose pipeline feeds it nothing — the zero-effect write's whole shape —
29/// still counts as an attempt;
30/// - the *applied* fact is the write scratch's own applied-change count, which
31/// the statement entry reset zeroes and every applied change — entity,
32/// schema or catalog — grows: one redo funnel, so a catalog-only change
33/// counts.
34///
35/// The zero state has one spelling, [`Self::None`] — there is no constant
36/// alias and no derived default beside it — and the join
37/// ([`Self::merge`]) is an explicit three-state table, so no variant order is
38/// semantic. There is no "applied but never attempted" state either: every
39/// state that carries the applied fact ([`Self::Applied`]) also answers
40/// [`Self::attempted`].
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum StatementEffect {
43 /// The statement's plan holds no mutation operator, so nothing was
44 /// applied: a read (and every pre-execution construction such as an
45 /// `EXPLAIN` result). The policy stays silent however the planner
46 /// classified the statement.
47 None,
48 /// The statement's plan holds a mutation operator and the engine applied
49 /// nothing: the ADR-0402 "zero-effect write" (the LABS-313 shape, an
50 /// idempotent `MERGE` that matched, a `MATCH … SET` whose `MATCH` bound no
51 /// rows).
52 Attempted,
53 /// The statement's plan holds a mutation operator and at least one change
54 /// reached the write redo — an entity, schema or catalog change alike.
55 Applied,
56}
57
58impl StatementEffect {
59 /// The effect of a statement from its two execution facts: `attempted` —
60 /// the statement's plan holds a mutation operator (the boundary's one plan
61 /// walk) — and `applied` — at least one change reached the write redo since
62 /// the statement's entry reset.
63 ///
64 /// `applied` implies `attempted` for every boundary that takes both facts
65 /// from one execution, and the state model has no fourth state to hold the
66 /// contrary pair. A caller that passes it anyway still gets
67 /// [`Self::Applied`] rather than a silently downgraded [`Self::None`]:
68 /// under-reporting an applied change is the one error the note policy must
69 /// never make.
70 pub fn from_facts(attempted: bool, applied: bool) -> Self {
71 match (attempted, applied) {
72 (false, false) => Self::None,
73 (true, false) => Self::Attempted,
74 (false, true) | (true, true) => Self::Applied,
75 }
76 }
77
78 /// The effect of a structural write boundary (the ADR-0209 bulk batch),
79 /// where the attempt is implied and only the applied fact is observed.
80 pub fn from_applied(applied: bool) -> Self {
81 Self::from_facts(true, applied)
82 }
83
84 /// Whether the statement's plan holds a mutation operator.
85 pub fn attempted(self) -> bool {
86 self != Self::None
87 }
88
89 /// Whether the statement applied at least one change (an entity, schema
90 /// or catalog change — every applied change funnels through the redo).
91 pub fn applied(self) -> bool {
92 self == Self::Applied
93 }
94
95 /// The ADR-0402 "zero-effect write" criterion: the statement's plan holds
96 /// a mutation operator and the engine applied nothing. A read fails the
97 /// first half, an effective write (catalog changes included) the second.
98 pub fn changed_nothing(self) -> bool {
99 self == Self::Attempted
100 }
101
102 /// Fold another statement's effect into this one — the join of the
103 /// three-state lattice, which is how a transaction's ledger accumulates
104 /// its statements: attempted-if-any-attempted, applied-if-any-applied.
105 ///
106 /// The table is spelled out variant by variant, so the declaration order
107 /// of the enum carries no meaning and a fourth state cannot be added
108 /// without deciding every join here.
109 pub fn merge(&mut self, other: Self) {
110 *self = match (*self, other) {
111 (Self::None, Self::None) => Self::None,
112 (Self::None, Self::Attempted)
113 | (Self::Attempted, Self::None)
114 | (Self::Attempted, Self::Attempted) => Self::Attempted,
115 (Self::None, Self::Applied)
116 | (Self::Applied, Self::None)
117 | (Self::Attempted, Self::Applied)
118 | (Self::Applied, Self::Attempted)
119 | (Self::Applied, Self::Applied) => Self::Applied,
120 };
121 }
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127
128 /// The ADR-0402 criterion's truth table, exactly one row per state.
129 #[test]
130 fn changed_nothing_is_the_attempted_without_application_state() {
131 assert!(
132 !StatementEffect::None.changed_nothing(),
133 "a read never reports, however the planner classified it"
134 );
135 assert!(
136 StatementEffect::Attempted.changed_nothing(),
137 "a mutation operator that applied nothing is the note's subject"
138 );
139 assert!(
140 !StatementEffect::Applied.changed_nothing(),
141 "an applied change — entity, schema or catalog — is not zero-effect"
142 );
143 }
144
145 /// The three accessors read the state, and only the applied state
146 /// answers both facts.
147 #[test]
148 fn the_accessors_read_the_state() {
149 for (effect, attempted, applied) in [
150 (StatementEffect::None, false, false),
151 (StatementEffect::Attempted, true, false),
152 (StatementEffect::Applied, true, true),
153 ] {
154 assert_eq!(effect.attempted(), attempted, "{effect:?}");
155 assert_eq!(effect.applied(), applied, "{effect:?}");
156 }
157 }
158
159 /// The join's explicit truth table — all nine pairs, spelled out rather
160 /// than restated through the implementation (a `max` or a merged
161 /// assertion would pass while the table was wrong).
162 #[test]
163 fn merge_is_the_three_state_join() {
164 use StatementEffect::{Applied, Attempted, None};
165 let table = [
166 (None, None, None),
167 (None, Attempted, Attempted),
168 (None, Applied, Applied),
169 (Attempted, None, Attempted),
170 (Attempted, Attempted, Attempted),
171 (Attempted, Applied, Applied),
172 (Applied, None, Applied),
173 (Applied, Attempted, Applied),
174 (Applied, Applied, Applied),
175 ];
176 for (left, right, expected) in table {
177 let mut joined = left;
178 joined.merge(right);
179 assert_eq!(joined, expected, "{left:?} join {right:?}");
180 // The join is commutative, so a ledger folds in any order.
181 let mut reversed = right;
182 reversed.merge(left);
183 assert_eq!(reversed, expected, "{right:?} join {left:?}");
184 }
185 }
186
187 /// The facts constructor joins its two facts: applied implies attempted,
188 /// so the pair the state model forbids cannot be built.
189 #[test]
190 fn from_facts_joins_the_two_facts() {
191 for (attempted, applied, expected) in [
192 (false, false, StatementEffect::None),
193 (true, false, StatementEffect::Attempted),
194 (true, true, StatementEffect::Applied),
195 (false, true, StatementEffect::Applied),
196 ] {
197 assert_eq!(
198 StatementEffect::from_facts(attempted, applied),
199 expected,
200 "attempted={attempted} applied={applied}"
201 );
202 }
203 }
204}