1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! The non-trivial clause arms the dispatcher delegates to: CREATE, MERGE,
//! DELETE, SET and REMOVE. The dispatcher keeps the loop and the shared
//! clause-exit reporting; the MATCH and CALL arms delegate beside their
//! executors (`match_clause.rs`, `procedures.rs`), and UNWIND beside its
//! generator (`unwind.rs`).
use std::collections::BTreeMap;
use std::sync::Arc;
use relate_core::{NodeId, Result, Value};
use relate_ir::{Expr, LabelsFunctionOp, MatchMode};
use crate::ctx::ExecCtx;
use crate::eval::eval;
use crate::graph::EntityId;
use crate::metrics::ParkedRecord;
use crate::overlay::ExecGraph;
use crate::row::Row;
use super::create::{
create_patterns, merge_match_pattern, patterns_have_graph_sensitive_expr,
reject_null_merge_props, resolve_create_patterns,
};
use super::feed::RowStream;
use super::match_clause::match_paths;
use super::one_path::ResolvedPatterns;
use super::pushdown::Residual;
use super::scope::refresh_rows;
use super::write::{
apply_resolved_removes, apply_resolved_sets, apply_sets, delete_values, resolve_removes,
resolve_sets, ResolvedSet,
};
/// The CREATE clause's input policy — the one owner of the graph-sensitive
/// decision: a graph-sensitive pattern set must resolve every row's
/// patterns against the pre-write graph before the first write lands, so
/// the whole input is held in one [`exec_create`] call (charged there);
/// every other form is ADR-0660's `pass` disposition — one bounded batch
/// written at a time through the barrier. The fact is resolved once here
/// and handed down; `exec_create` never re-asks it.
pub(super) fn create_from_stream<'a, 'b>(
graph: &'b ExecGraph<'_>,
mut stream: RowStream<'a>,
patterns: &'b [relate_ir::PathPattern],
params: &'b BTreeMap<String, Value>,
record: Option<ParkedRecord<'a>>,
) -> Result<RowStream<'b>>
where
'a: 'b,
{
let graph_sensitive = patterns_have_graph_sensitive_expr(patterns);
if graph_sensitive {
// The whole-input hold is the held form it is — `probe()` and
// `is_held()` stay meaningful (the consumer's eager page and the
// projection's input charge read them) — carrying the clause's
// PROFILE record so the dispatcher's deferral is honest on both
// returns. The arm that picked the policy owns its derived form:
// the prepared patterns are resolved here, before any write lands.
let rows = stream.drain()?;
let prepared = Some(
rows.iter()
.map(|row| resolve_create_patterns(graph, row, patterns, params))
.collect::<Result<Vec<_>>>()?,
);
let rows = exec_create(graph, rows, patterns, params, prepared)?;
Ok(RowStream::held_profiled(rows, record))
} else {
stream.write_barrier(
move |rows| exec_create(graph, rows, patterns, params, None),
record,
)
}
}
/// The CREATE clause arm: charge the driving rows, create, and refresh the
/// bindings. `prepared` is the caller's one resolution of the input policy
/// — the per-row pattern sets a graph-sensitive arm resolved before any
/// write landed, `None` for the barrier's batch-at-a-time pass (the
/// patterns are re-resolved per batch against the pre-batch graph).
pub(super) fn exec_create(
graph: &ExecGraph<'_>,
rows: Vec<Row>,
patterns: &[relate_ir::PathPattern],
params: &BTreeMap<String, Value>,
prepared: Option<Vec<Vec<relate_ir::PathPattern>>>,
) -> Result<Vec<Row>> {
let mut rows = rows;
// MATCH (and any earlier clause) drops its row charge on return, but
// this clause keeps two containers until it finishes: the driving
// rows and — when the patterns are graph-sensitive — the prepared
// per-row pattern buffer. The prepared buffer is loop-invariant
// (built once above, never mutated), so its retained figure
// ([`crate::workspace::prepared_pattern_bytes`]) is computed once
// and both containers bill as the rows' live walk plus that
// constant — at creation and at each resize — never re-walking the
// pattern buffer (stellar round 2, F-2-18, closing the rust-review
// Medium: the re-walk made the clause's accounting quadratic in
// driving rows).
let prepared_bytes = prepared
.as_deref()
.map_or(0, crate::workspace::prepared_pattern_bytes);
// The clause's one live figure — the rows' buffer walk plus the
// loop-invariant prepared-pattern bytes — so the four re-measures
// below cannot disagree.
let live = |rows: &[Row]| crate::workspace::row_buffer_bytes(rows) + prepared_bytes;
// The caller's one pairing, made structural: each row's own resolved
// pattern set when the arm prepared them, the shared patterns
// otherwise — consumed as one iterator zipped against the row
// positions, so a shorter prepared set fails the assert up front,
// never a positional `p[n]` index the caller keeps true by convention.
let per_row: Box<dyn Iterator<Item = &[relate_ir::PathPattern]> + '_> = match &prepared {
Some(sets) => {
assert_eq!(
sets.len(),
rows.len(),
"the prepared pattern sets pair with the driving rows"
);
Box::new(sets.iter().map(Vec::as_slice))
}
None => Box::new(std::iter::repeat(patterns)),
};
let mut driving = relate_core::tokens::WorkspaceCharge::retain_with(|| live(&rows))?;
for (n, row_patterns) in (0..rows.len()).zip(per_row) {
create_patterns(graph, &mut rows[n], row_patterns, params)?;
if n % 64 == 63 {
driving.resize_with(|| live(&rows))?;
}
}
driving.resize_with(|| live(&rows))?;
refresh_rows(graph, &mut rows);
driving.resize_with(|| live(&rows))?;
Ok(rows)
}
/// The MERGE clause arm: match the pattern per row, create-and-set on a
/// miss (the `ON CREATE` arm's `labels(n) = …` overwrites keep the
/// pattern's own labels for the node it created, ADR-0695), set on a
/// hit, then refresh the bindings.
pub(super) fn exec_merge(
graph: &ExecGraph<'_>,
rows: Vec<Row>,
pattern: &relate_ir::PathPattern,
on_create: &[relate_ir::SetItem],
on_match: &[relate_ir::SetItem],
params: &BTreeMap<String, Value>,
) -> Result<Vec<Row>> {
let mut rows = rows;
// The match/null-check constraints drop MERGE's own self-referencing
// tautologies; the create path keeps the original pattern (see
// `merge_match_pattern`).
let match_pattern = merge_match_pattern(pattern);
let mut next = Vec::new();
for row in rows {
reject_null_merge_props(graph, &row, &match_pattern, params)?;
let none = Residual::empty();
// Resolved per row: MERGE creates between rows, so no table (and
// no reach cache in it) may outlive the graph state it read.
let resolved = ResolvedPatterns::resolve(
graph,
std::slice::from_ref(&match_pattern),
MatchMode::DifferentRelationships,
);
let matched = match_paths(
ExecCtx::new(graph, params),
&resolved,
MatchMode::DifferentRelationships,
row.clone(),
&none,
None,
None,
)?;
if matched.is_empty() {
let mut created = row;
create_patterns(graph, &mut created, std::slice::from_ref(pattern), params)?;
// ADR-0695: the ON CREATE arm's SET application — the merge
// arm owns the anchored-overwrite policy whole (the generic
// SET path stays merge-free).
apply_sets_on_create(graph, &mut created, on_create, params, pattern)?;
next.push(created);
} else {
for mut m in matched {
apply_sets(graph, &mut m, on_match, params)?;
next.push(m);
}
}
}
rows = next;
refresh_rows(graph, &mut rows);
Ok(rows)
}
/// ADR-0695: the birth label sets of the nodes this merge created, read
/// from the row's just-created bindings before the `ON CREATE` items
/// apply — an `ON CREATE` `labels(n) = …` overwrite unions them back
/// into its written set, because the pattern anchors the identity of the
/// node it created. Only fresh, named node patterns carry an anchor: a
/// bare node beside a relationship reuses an earlier clause's binding
/// (it was not created here), and an anonymous node cannot be an
/// `ON CREATE SET` target. The row's just-created bindings are the one
/// source of the fact — `create_patterns` inserts each carrying the
/// created node's labels — read before the arm's items apply. The anchor
/// holds the binding's own `Arc` (a refcount bump, never a copy); the
/// names are cloned once, at the union, only for the node an overwrite
/// actually targets.
fn merge_create_label_anchors(
row: &Row,
pattern: &relate_ir::PathPattern,
) -> Vec<(NodeId, Arc<Vec<String>>)> {
pattern
.nodes
.iter()
.filter(|np| !pattern.node_reuses_binding(np))
.filter_map(|np| {
let name = np.variable.as_ref()?;
// The binding always exists — `create_patterns` binds every
// fresh, named pattern to the node it created — so a miss is
// an invariant violation, not an anchor decision.
let binding = row
.get_name(name)
.expect("create_patterns binds every fresh, named node pattern");
// A binding that is no longer a node is not an anchor: a
// same-named relationship can clobber it, because the dynamic
// rel type reuse `MERGE (r:Person)-[r:$(t)]->(m)` leaves the
// rel pattern's shape undeclared and `create_rel` rebinds `r`
// to the relationship it just created. The arm's own target
// resolution owns that rejection (the typed
// `SET labels() requires a node` error); the anchor scan
// skips what is not a node.
match binding {
Value::Node(node) => Some((node.id, Arc::clone(&node.labels))),
_ => None,
}
})
.collect()
}
/// ADR-0695: the `ON CREATE` arm's SET application — [`apply_sets`]'s own
/// halves with the one merge-anchored overwrite patched between them: a
/// `labels(n) = …` item whose target is a node this merge created gets
/// that node's birth labels prepended to its written set, because the
/// pattern anchors the identity of the node it created, so the overwrite
/// replaces everything else. Every other item, and an overwrite whose
/// target the merge did not create, applies exactly as [`apply_sets`]
/// does. The seam is the removes' own resolve/apply pair, so this arm
/// and [`merge_create_label_anchors`] hold the whole policy and the
/// generic SET path stays merge-free.
fn apply_sets_on_create(
graph: &ExecGraph<'_>,
row: &mut Row,
items: &[relate_ir::SetItem],
params: &BTreeMap<String, Value>,
pattern: &relate_ir::PathPattern,
) -> Result<()> {
let mut resolved = resolve_sets(graph, row, items, params)?;
// The anchors are built only when the resolved arm holds a
// `labels(n) = …` overwrite — the one item that can read them
// (REL8-2243's perf note). The gate reads the same resolved
// vocabulary the loop below consumes, so the fact is discriminated
// once; a plain ON CREATE SET (properties, static labels, `+=` /
// `-=`) pays nothing.
let anchors = if resolved.iter().any(|item| {
matches!(
item,
ResolvedSet::LabelsFunction(_, _, LabelsFunctionOp::Overwrite)
)
}) {
merge_create_label_anchors(row, pattern)
} else {
Vec::new()
};
for item in &mut resolved {
if let ResolvedSet::LabelsFunction(
EntityId::Node(id),
labels,
LabelsFunctionOp::Overwrite,
) = item
{
let Some((_, anchor)) = anchors.iter().find(|(birth, _)| *birth == *id) else {
continue;
};
// The anchor's names come first: the pattern's carried names
// read back before the written ones — the write-order
// contract — even when an earlier item removed a name the
// overwrite re-adds. The extend is unconditional: the
// overwrite primitive's add half owns membership (a repeat,
// or a name the node already carries, is added once, in
// first-occurrence order).
let mut unioned = Vec::with_capacity(anchor.len() + labels.len());
unioned.extend(anchor.iter().cloned());
unioned.append(labels);
*labels = unioned;
}
}
apply_resolved_sets(graph, resolved)
}
/// The DELETE clause arm: resolve the expressions, delete the values, and
/// refresh the surviving bindings as tombstones.
pub(super) fn exec_delete(
graph: &ExecGraph<'_>,
rows: Vec<Row>,
detach: bool,
expressions: &[Expr],
params: &BTreeMap<String, Value>,
) -> Result<Vec<Row>> {
let mut rows = rows;
let resolved = rows
.iter()
.flat_map(|row| {
expressions
.iter()
.map(|expr| eval(graph, row, expr, params))
})
.collect::<Result<Vec<_>>>()?;
delete_values(graph, &resolved, detach)?;
// The deleted binding stays addressable as an empty
// tombstone (`refresh_rows`), which a later `MATCH` on the
// same variable still matches (`node_ok`'s snapshot path)
// while the store no longer holds the node.
refresh_rows(graph, &mut rows);
Ok(rows)
}
/// The SET clause arm: apply each item per row and refresh the bindings.
pub(super) fn exec_set(
graph: &ExecGraph<'_>,
rows: Vec<Row>,
items: &[relate_ir::SetItem],
params: &BTreeMap<String, Value>,
) -> Result<Vec<Row>> {
let mut rows = rows;
for row in rows.iter_mut() {
apply_sets(graph, row, items, params)?;
}
refresh_rows(graph, &mut rows);
Ok(rows)
}
/// The REMOVE clause arm: resolve and apply each removal, then refresh.
pub(super) fn exec_remove(
graph: &ExecGraph<'_>,
rows: Vec<Row>,
items: &[relate_ir::RemoveItem],
params: &BTreeMap<String, Value>,
) -> Result<Vec<Row>> {
let mut rows = rows;
let resolved = rows
.iter()
.map(|row| resolve_removes(graph, row, items, params))
.collect::<Result<Vec<_>>>()?;
for items in resolved {
apply_resolved_removes(graph, items)?;
}
refresh_rows(graph, &mut rows);
Ok(rows)
}
#[cfg(test)]
mod tests {
use crate::exec::test_support::try_exec_cypher;
use crate::graph::Graph;
use relate_core::{CapacityKind, Error, Value};
/// ADR-0660's write barrier: a write whose driving input is longer
/// than one batch (`BATCH_ROWS`) applies batch by batch and still
/// answers the whole input — `SET`, `REMOVE`, `MERGE` and `DELETE`
/// each route their rows through one barrier.
#[test]
fn a_write_over_more_than_one_batch_answers_the_whole_input() {
use crate::exec::test_support::exec_autocommit;
let g = Graph::default();
// 1500 driving rows: two batches (BATCH_ROWS = 1024).
exec_autocommit(&g, "UNWIND range(0, 1499) AS i CREATE (:Barrier {id: i})").unwrap();
let count = |cypher: &str| exec_autocommit(&g, cypher).unwrap().rows;
assert_eq!(
count("MATCH (n:Barrier) SET n.touched = n.id RETURN count(*) AS c"),
vec![vec![Value::Int(1500)]]
);
assert_eq!(
count("MATCH (n:Barrier) WHERE n.touched = n.id RETURN count(*) AS c"),
vec![vec![Value::Int(1500)]]
);
assert_eq!(
count("MATCH (n:Barrier) REMOVE n.touched RETURN count(*) AS c"),
vec![vec![Value::Int(1500)]]
);
assert_eq!(
count("MATCH (n:Barrier) WHERE n.touched IS NULL RETURN count(*) AS c"),
vec![vec![Value::Int(1500)]]
);
assert_eq!(
count("UNWIND range(1500, 2999) AS i MERGE (:Barrier {id: i}) RETURN count(*) AS c"),
vec![vec![Value::Int(1500)]]
);
assert_eq!(
count("MATCH (n:Barrier) DELETE n RETURN count(*) AS c"),
vec![vec![Value::Int(3000)]]
);
assert_eq!(
count("MATCH (n:Barrier) RETURN count(*) AS c"),
vec![vec![Value::Int(0)]]
);
}
/// ADR-0463 round 3: when UNWIND materializes (its follower is a
/// state-holder, here an unlimited `ORDER BY`), it materializes one
/// clone of the carried row per list element, so its charge is
/// `elements × (row width + 1)` slots, not one slot per element. A wide
/// row (200 bindings) over a 30k list must hit the typed per-allocation
/// refusal; the old element-only charge stayed far under the 256 MiB cap
/// while the row clones allocated hundreds of MiB. (A pass-through
/// follower streams the same generator instead — ADR-0660 — so it is the
/// state-holder below that pins the materializing charge.)
///
/// The 200-term `+` chain is a 200-deep expression tree: the evaluator
/// recurses once per level, and how much stack that takes depends on
/// the frame sizes of the day (it overflowed the default 2 MiB test
/// stack until the pattern-comprehension lowering shrank a frame), so
/// the body runs on the shared big-stack harness (REL8-2276).
#[test]
fn unwinding_a_wide_row_charges_the_row_clone() {
relate_test_support::run_with_big_stack(unwinding_a_wide_row_body);
}
fn unwinding_a_wide_row_body() {
let width = 200usize;
let mut query = String::from("UNWIND [1] AS seed WITH ");
for i in 0..width {
if i > 0 {
query.push_str(", ");
}
query.push_str(&format!("seed + {i} AS a{i}"));
}
query.push_str(" UNWIND range(1, 30000) AS x RETURN ");
query.push_str(
&(0..width)
.map(|i| format!("a{i}"))
.collect::<Vec<_>>()
.join(" + "),
);
query.push_str(" + x AS s ORDER BY s");
let error = try_exec_cypher(&Graph::default(), &query)
.err()
.expect("a wide-row UNWIND must be refused");
assert!(
matches!(
error,
Error::Capacity {
limit,
kind: CapacityKind::Expression,
..
} if limit == crate::memory::EXPRESSION_BUDGET_BYTES
),
"{error}"
);
}
/// LABS-1246: an aggregation fed by `UNWIND range(...)` folds the rows as
/// they are generated, so its state does not depend on the range length.
/// Two million elements under a 1 MiB expression cap refuse when the list
/// and the rows are materialized and pass when streamed.
#[test]
fn a_range_unwind_into_an_aggregation_holds_no_rows() {
use crate::exec::test_support::exec_cypher;
let mut g = Graph::default();
g.configure_expression_memory_limit(Some(1 << 20));
let result = exec_cypher(
&g,
"UNWIND range(0, 1999999) AS i RETURN i % 4 AS b, count(*) AS n ORDER BY b",
);
assert_eq!(
result.rows,
(0..4)
.map(|b| vec![Value::Int(b), Value::Int(500_000)])
.collect::<Vec<_>>()
);
}
/// The streamed path reads the same answers as the materialized one: the
/// same `range()` argument contract (negative and empty ranges, a step
/// that does not land on the end, the extreme-bound size arithmetic) and
/// the same scalar and null sources.
#[test]
fn a_streamed_unwind_matches_the_materialized_source() {
use crate::exec::test_support::{exec_cypher, try_exec_cypher};
let g = Graph::default();
let same = |streamed: &str, listed: &str| {
assert_eq!(
exec_cypher(&g, streamed).rows,
exec_cypher(&g, listed).rows,
"{streamed}"
);
};
same(
"UNWIND range(5, 1, -2) AS i RETURN count(*) AS n, sum(i) AS s",
"UNWIND [5, 3, 1] AS i RETURN count(*) AS n, sum(i) AS s",
);
same(
"UNWIND range(1, 0) AS i RETURN count(*) AS n, sum(i) AS s",
"UNWIND [] AS i RETURN count(*) AS n, sum(i) AS s",
);
same(
"UNWIND range(1, 10, 4) AS i RETURN count(*) AS n, max(i) AS m",
"UNWIND [1, 5, 9] AS i RETURN count(*) AS n, max(i) AS m",
);
same(
"UNWIND range(9223372036854775800, 9223372036854775807, 3) AS i RETURN count(*) AS n",
"UNWIND [9223372036854775800, 9223372036854775803, 9223372036854775806] AS i RETURN count(*) AS n",
);
same(
"UNWIND null AS i RETURN count(*) AS n",
"UNWIND [] AS i RETURN count(*) AS n",
);
same(
"UNWIND 7 AS i RETURN count(*) AS n, sum(i) AS s",
"UNWIND [7] AS i RETURN count(*) AS n, sum(i) AS s",
);
assert!(try_exec_cypher(&g, "UNWIND range(1, 3, 0) AS i RETURN count(*)").is_err());
assert!(try_exec_cypher(
&g,
"UNWIND range(1, -9223372036854775808) AS i RETURN count(*)"
)
.is_err());
}
/// A retaining aggregate keeps its refusal: `collect` over the same
/// streamed range still charges what it holds.
#[test]
fn a_streamed_collect_is_still_charged() {
use crate::exec::test_support::try_exec_cypher;
let mut g = Graph::default();
g.configure_expression_memory_limit(Some(1 << 20));
let error = try_exec_cypher(&g, "UNWIND range(0, 1999999) AS i RETURN collect(i) AS xs")
.err()
.expect("a retained collect over two million values must be refused");
assert!(
matches!(
error,
Error::Capacity {
kind: CapacityKind::Expression,
..
}
),
"{error}"
);
}
/// Two fused UNWINDs in one statement each drive their own generator:
/// the second one's range comes from the first one's aggregate.
#[test]
fn two_streamed_unwinds_in_one_statement_do_not_share_state() {
use crate::exec::test_support::exec_cypher;
let g = Graph::default();
let result = exec_cypher(
&g,
"UNWIND range(1, 4) AS a WITH count(*) AS c \
UNWIND range(1, c * 10) AS b RETURN count(*) AS n, sum(b) AS s",
);
assert_eq!(result.rows, vec![vec![Value::Int(40), Value::Int(820)]]);
}
}