relate-executor 1.5.0

In-memory graph and Cypher physical executor
Documentation
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Expression evaluation: the `eval` dispatcher and the `eval/`
//! submodule wiring.

use std::collections::{BTreeMap, HashSet};

use relate_core::{Error, Result, Value};
use relate_ir::{Expr, MapProjectionItem};
use relate_names::SmallName;

use crate::ctx::EvalCtx;
// The pattern-expression arms (PatternPred, ShortestPath)
// and the subquery arms below them delegate UP to engines: the walk
// engine at `crate::eval_walk` (a pattern-walk client like the exec
// walkers, hoisted out of this tree so `eval` stays the expression core
// strictly below `pattern_walk`, importing no walk module) and `exec`
// (the long-established engine-delegation edge). The cross-engine
// pattern policy that walker reads lives at the shared tier
// (`crate::pattern_walk`, LABS-811) — one public path per item, no
// re-export here.
use crate::eval_walk::pattern_exists;
use crate::exec::{eval_shortest, ShortestWant};
use crate::overlay::ExecGraph;
use crate::row::{row_bound, Row};

mod aggregate;
mod arithmetic;
pub(crate) mod bytecode;
mod collection;
mod comprehension;
mod expr;
use comprehension::{eval_list_comp, eval_map_comp};
mod fold;
mod function;
mod items;
mod labels;
mod membership_index;
mod numeric;
mod path_value;
mod predicates;
mod quantifier;
mod regex_cache;
mod string;
mod temporal;
#[cfg(test)]
mod tests;
mod typematch;
mod vector;

pub(crate) use aggregate::{
    fold_aggregate, needs_numbers, rewrite_horizontal_aggregates, FoldInput, Percentile,
    PercentileSample, StreamFold, SumAcc,
};
pub(crate) use fold::{extreme_offer, fold_extreme, fold_quantifier, Extreme};
pub(crate) use items::{into_items, iterated, open_source, Elements};
pub(crate) use labels::{
    dynamic_label_names, dynamic_property_key, label_expr_matches, resolve_write_labels,
};
pub(crate) use membership_index::member_of;
// The expression walkers live in `relate-ir` next to `Expr::children`
// so planner and executor share one traversal.
pub(crate) use relate_ir::{
    all_vars_satisfy, expr_vars, flip_cmp, is_agg_expr, key_of, label_expr_vars, orient,
    var_key_side, var_prop,
};

pub(crate) use expr::{
    binary, binary_pure, cmp_bool, eval_case_with, in_op, in_rhs, is_membership_scalar,
    ordering_operator, unary, OrderingSide,
};
use expr::{eval_case, prop, reject_deleted_access, slice, subscript};
/// Whether every variable `expr` reads is in `allowed`.
pub(crate) fn expr_within(expr: &Expr, allowed: &HashSet<String>) -> bool {
    all_vars_satisfy(expr, |name| allowed.contains(name))
}

/// The one stability fact every evaluate-early policy reads: no
/// nondeterministic call and no scope-bearing form
/// ([`is_row_unstable_node`] is the node classifier).
pub(crate) fn row_stable(expr: &Expr) -> bool {
    !expr.any(&mut is_row_unstable_node)
}

/// Whether `bound` may evaluate once per clause instead of per row: no
/// variables at all (so no row can change it) and [`row_stable`] (no
/// nondeterministic call or scope-bearing form) — defined as [`bound_ready`]
/// against an empty [`Row`].
pub(crate) fn bound_is_row_invariant(bound: &Expr) -> bool {
    bound_ready(bound, &Row::new())
}

/// Whether `expr` may evaluate on `row` as a seek recognizer's bound
/// side: every variable it reads is already bound in `row`
/// ([`all_vars_satisfy`] against the row), and the expression is
/// [`row_stable`] — a nondeterministic call differs per row, and a
/// scope-bearing form such as a subquery reads its own scope. The seek
/// recognizers evaluate the bound side against the *entry* row, before
/// the pattern's own variable and any later pattern's variables are
/// bound; a free-variable eval there does not answer null the way an
/// unbound property read does — a subquery runs its body with the name
/// free (`EXISTS` matches anything, `COUNT` counts the whole side) and
/// hands the recognizer a wrong constant to serve as the seek. A not-ready
/// bound declines the seek; the residual re-applies the predicate per
/// candidate row, which is the same answer, correctly reached.
pub(crate) fn bound_ready(expr: &Expr, row: &Row) -> bool {
    all_vars_satisfy(expr, row_bound(row)) && row_stable(expr)
}

/// The seek recognizers' one bound-side evaluation: [`bound_ready`] first,
/// then the eval — the single site that pairs the proof with the eval, so
/// a recognizer cannot evaluate a bound the proof has not cleared (the
/// wrong-constant-seek regression this pairing exists to prevent has no
/// compile-time signal — only this one spelling to review). A not-ready
/// bound, or an eval that errors or yields nothing, answers `None`: the
/// recognizer declines, and the residual re-applies the predicate per
/// candidate row.
pub(crate) fn eval_bound(
    expr: &Expr,
    graph: &ExecGraph<'_>,
    row: &Row,
    params: &BTreeMap<String, Value>,
) -> Option<Value> {
    if !bound_ready(expr, row) {
        return None;
    }
    eval(graph, row, expr, params).ok()
}

pub(crate) use function::is_row_unstable_node;
use function::{entity_backing_map, function};
pub(crate) use predicates::plain_where_holds;
#[cfg(test)]
pub(crate) use predicates::take_candidate_passes;
pub(crate) use predicates::{
    candidate_passes, filter_rows_where, filter_rows_where_clause, inline_predicates_ok,
    predicate_holds, where_clause_holds,
};
use quantifier::{eval_all_reduce, eval_quantifier, eval_reduce};
pub(crate) use quantifier::{AllReduceFold, AllReduceStep};
use string::is_normalized;
pub(crate) use typematch::value_matches_type;

/// The boolean-or-null reading shared by every requiring site: a boolean
/// yields its truthiness, null yields the unknown, anything else is an
/// `InvalidArgumentType` type error naming the requiring context. This is
/// the *whole* policy of the list-quantifier WHERE predicate
/// (`quantifier.rs`); the boolean operators layer the deprecated
/// list/path conversion on top through [`bool_operand`] (ADR-0458 §7).
pub(crate) fn bool_or_null(v: &Value, ctx: &str) -> Result<Option<bool>> {
    match v {
        Value::Bool(b) => Ok(Some(*b)),
        Value::Null => Ok(None),
        // ADR-0511: the non-boolean arm is the typed
        // `InvalidArgumentType` variant, so this site and the planner's
        // boolean-requiring checks classify as the TypeError class Neo4j
        // surfaces for them (LABS-400, superseding ADR-0003's SyntaxError
        // pin for this family). The one `requires_boolean` constructor
        // keeps the wording identical across those sites.
        _ => Err(Error::requires_boolean(ctx)),
    }
}

/// The one boolean-operand policy of the boolean operators (`expr.rs`'s
/// NOT/AND/OR/XOR) and, through [`predicate_value_holds`], the predicate
/// positions (MATCH/WHERE, WITH/FILTER, HAVING, the GPM route filter, the
/// pattern engines' per-trial WHERE, the comprehension WHEREs):
/// [`bool_or_null`] plus the deprecated list/path conversion ADR-0458 §7 —
/// an empty list is false, a non-empty one (and any path, which always
/// holds at least one node) is true (ReturnAcceptance "RETURN true AND
/// list"). Every other requiring site reads [`bool_or_null`] directly, so
/// a list or path can never stand in for a quantifier predicate.
pub(crate) fn bool_operand(v: &Value, ctx: &str) -> Result<Option<bool>> {
    match v {
        Value::List(values) => Ok(Some(!values.is_empty())),
        Value::Path(_) => Ok(Some(true)),
        _ => bool_or_null(v, ctx),
    }
}

/// Whether an evaluated boolean-context predicate holds: the same runtime
/// list/path conversion the boolean operators apply (an empty list is false,
/// a non-empty list and any path true), a null never holds, and any other
/// non-boolean is the typed `InvalidArgumentType` a statically known one is
/// rejected with. The MATCH/WHERE, WITH/FILTER, HAVING, GPM route-filter,
/// pattern-engine and comprehension predicate positions read this one
/// helper; the list-quantifier's inner WHERE keeps [`bool_or_null`].
/// Named apart from [`predicate_holds`] (the expression-level form).
pub(crate) fn predicate_value_holds(v: &Value) -> Result<bool> {
    Ok(bool_operand(v, "predicate")? == Some(true))
}

/// An operand of a container access (`Subscript`/`Property`/
/// `DynamicProperty`/`Slice`): a plain variable is borrowed from the row —
/// evaluating it would deep-clone the whole bound container, and the access
/// only needs the element — while any other expression is evaluated and
/// owned. `Cow` carries the borrowed-or-owned value.
fn eval_operand<'a>(
    graph: &ExecGraph<'a>,
    row: &'a Row,
    expr: &Expr,
    params: &BTreeMap<String, Value>,
) -> Result<std::borrow::Cow<'a, Value>> {
    if let Expr::Variable(name) = expr {
        // Free read bridge: the String-keyed row schema is probed through
        // the name's text (the inline arm decodes on the stack).
        if let Some(v) = row.get_name(name) {
            return Ok(std::borrow::Cow::Borrowed(v));
        }
    }
    let v = eval(graph, row, expr, params)?;
    Ok(std::borrow::Cow::Owned(v))
}

/// The summed logical bytes of a row set — the row-materialization metric
/// (ADR-0344 metric fields): each binding counts its key's string bytes
/// (ADR-0042 string size) plus its value's [`Value::logical_bytes`]. The
/// one canonical spelling of "deep size of these rows", beside the `Row`
/// type it measures; the PROFILE dispatch loop is the caller. A row's
/// map header itself is not counted — only what the row carries, so a
/// column rename shows up as the value's size, not a constant offset.
pub(crate) fn rows_logical_bytes(rows: &[Row]) -> u64 {
    rows.iter()
        .flat_map(|row| row.iter())
        .map(|(key, value)| relate_core::str_logical_bytes(key) + value.logical_bytes())
        .sum()
}

/// The one retaining value collector (ADR-0463): charge the output
/// container's slot buffer, then land each value with its owned payload.
/// Every charged list-building site — the two comprehension projections
/// and the list literal — routes through it, so the charge protocol has
/// one spelling; the aggregate evaluator reaches it through the rebuilt
/// literal's scalar `eval` arm.
pub(crate) fn retain_values(
    graph: &ExecGraph<'_>,
    bound: usize,
    values: impl Iterator<Item = Result<Value>>,
) -> Result<Value> {
    let mut retained = graph.retain_slots(bound)?;
    let mut out = Vec::with_capacity(bound);
    for value in values {
        let value = value?;
        retained.add_owned(graph, &value)?;
        out.push(value);
    }
    Value::checked_list(out)
}

/// The one retaining map collector — the map twin of [`retain_values`]:
/// charge the map entry-storage figure, then land each entry's key bytes
/// and value payload. The map literal and the map comprehension's
/// projections route through it; the aggregate evaluator reaches it
/// through the rebuilt literal's scalar `eval` arm.
pub(crate) fn retain_map(
    graph: &ExecGraph<'_>,
    bound: usize,
    entries: impl Iterator<Item = Result<(String, Value)>>,
) -> Result<Value> {
    let mut retained = graph.retain_map_entries(bound)?;
    let mut out: BTreeMap<String, Value> = BTreeMap::new();
    for entry in entries {
        let (key, value) = entry?;
        retained.add_map_entry(graph, &key, &value)?;
        out.insert(key, value);
    }
    Value::checked_map(out)
}

/// The one call-shape test: `expr` is a call to `name` (case-insensitive)
/// with exactly `arity` arguments, returning them. Every statement-shape
/// recognizer (the seek shapes, the id-conversion reader, the label-set
/// group keys of ADR-0589) consults it instead of re-spelling the
/// name/arity conjunction.
pub(crate) fn named_call<'a>(expr: &'a Expr, name: &str, arity: usize) -> Option<&'a [Expr]> {
    match expr {
        Expr::Function {
            name: actual, args, ..
        } if actual.with_str(|text| text.eq_ignore_ascii_case(name)) && args.len() == arity => {
            Some(args)
        }
        _ => None,
    }
}

/// The row-independent leaves — the literals, folded run-time values and `$param` — evaluated
/// without a row: the one mapping scalar [`eval`] and the aggregate
/// evaluator share, so a new row-independent variant lands here once.
/// `None` for every expression that needs a row.
pub(crate) fn row_independent_leaf(
    expr: &Expr,
    params: &BTreeMap<String, Value>,
) -> Option<Result<Value>> {
    Some(match expr {
        Expr::Null => Ok(Value::Null),
        Expr::Bool(b) => Ok(Value::Bool(*b)),
        Expr::Int(i) => Ok(Value::Int(*i)),
        Expr::Float(f) => Ok(Value::Float(*f)),
        Expr::String(s) => Ok(Value::String(s.as_str().into())),
        // A folded run-time literal carries its value; it needs no row.
        Expr::RuntimeValue(value) => Ok(value.clone()),
        // ADR-0308: the facade rejects a planned statement whose parameter
        // references are absent before dispatch, so this fires only for
        // expressions the plan walk cannot see (catalog commands lower
        // theirs at run time) — with the same typed error, never `null`.
        // A parameter needs no row, so an empty aggregate group still
        // resolves it (ADR-0503).
        Expr::Parameter(n) => params
            .get(n)
            .cloned()
            .ok_or_else(|| Error::parameters_missing([n.as_str()])),
        _ => return None,
    })
}

/// Evaluate a scalar expression against a row.
pub fn eval(
    graph: &ExecGraph<'_>,
    row: &Row,
    expr: &Expr,
    params: &BTreeMap<String, Value>,
) -> Result<Value> {
    // The row-independent leaves answer first, from their one mapping;
    // the match below never evaluates them.
    if let Some(leaf) = row_independent_leaf(expr, params) {
        return leaf;
    }
    let ctx = EvalCtx { graph, row, params };
    match expr {
        // Answered above; the arm exists only so this match stays
        // exhaustive over `Expr` (a new variant must be routed here), and
        // a leaf the mapping does not answer is a typed internal error.
        Expr::Null
        | Expr::Bool(_)
        | Expr::Int(_)
        | Expr::Float(_)
        | Expr::String(_)
        | Expr::RuntimeValue(_)
        | Expr::Parameter(_) => Err(Error::internal(
            "a row-independent leaf reached the row evaluator unanswered",
        )),
        // ADR-0335: the plan-time binding walks reject an undefined
        // variable before execution, so a row miss here is either a
        // planner/executor binding bug or an expression the plan walk
        // cannot see (run-time-lowered catalog predicates) — never `null`.
        Expr::Variable(n) => row
            .get_name(n)
            .cloned()
            .ok_or_else(|| n.with_str(Error::undefined_variable)),
        Expr::Star => Err(Error::semantic("* is not a scalar expression")),
        Expr::Property { expr, key, key_id } => {
            // Variable operands are read by reference (see Subscript): a
            // property read must not clone the whole map it reads from.
            let v = eval_operand(graph, row, expr, params)?;
            prop(graph, &v, &(key.clone(), *key_id))
        }
        Expr::DynamicProperty { expr, key } => {
            let v = eval_operand(graph, row, expr, params)?;
            if v.is_null() {
                return Ok(Value::Null);
            }
            let key = dynamic_property_key(eval(graph, row, key, params)?)?;
            prop(
                graph,
                &v,
                &relate_names::unresolved(SmallName::from_name(&key)),
            )
        }
        Expr::LabelPredicate {
            expr,
            label_expr,
            strict,
        } => {
            let v = eval(graph, row, expr, params)?;
            Ok(match v {
                Value::Node(n) => Value::Bool(label_expr_matches(
                    graph, row, params, label_expr, &n.labels, false,
                )?),
                Value::Relationship(r) => Value::Bool(label_expr_matches(
                    graph,
                    row,
                    params,
                    label_expr,
                    std::slice::from_ref(&r.rel_type),
                    true,
                )?),
                Value::Null => Value::Null,
                _ if *strict => {
                    return Err(Error::invalid_argument_type(
                        "IS LABELED requires a node or relationship",
                    ))
                }
                _ => Value::Bool(false),
            })
        }
        Expr::Map(entries) => {
            // The map literal is the list literal's twin (REL8-1372): the
            // same query-text-bounded container, collected — charged —
            // through the one map collector its comprehension forms use.
            retain_map(
                graph,
                entries.len(),
                entries
                    .iter()
                    .map(|(k, e)| eval(graph, row, e, params).map(|v| (k.clone(), v))),
            )
        }
        Expr::List(xs) => {
            // REL8-1372: the literal's own materialization is charged —
            // its length is bounded by the query-text cap, so this is a
            // bound check rather than a length guard, but an uncharged
            // build let a query-sized literal allocate hundreds of MB
            // under a small expression cap.
            retain_values(
                graph,
                xs.len(),
                xs.iter().map(|e| eval(graph, row, e, params)),
            )
        }
        Expr::Unary { op, expr } => {
            let v = eval(graph, row, expr, params)?;
            unary(*op, v)
        }
        Expr::Binary { op, left, right } => {
            let l = eval(graph, row, left, params)?;
            let r = eval(graph, row, right, params)?;
            binary(graph, *op, l, r)
        }
        Expr::Function {
            name,
            distinct,
            args,
        } => name.with_str(|text| function(graph, row, params, text, *distinct, args)),
        // 42I24 (see `function.rs`): the scalar-eval backstop for a `count(*)`
        // that reached evaluation outside an aggregation context is a
        // compile-time-class `SyntaxError` in Neo4j.
        Expr::CountStar => Err(Error::invalid_aggregation(
            "count(*) only valid in aggregation",
        )),
        Expr::Case {
            when_then,
            else_expr,
        } => eval_case(graph, row, params, when_then, else_expr),
        Expr::Subscript { expr, index } => {
            // The container is evaluated before the index (the pre-by-reference
            // order, so error precedence is unchanged), and a variable
            // operand is read by reference: evaluating it would deep-clone
            // the whole container, and the subscript only needs the element.
            let v = eval_operand(graph, row, expr, params)?;
            let i = eval(graph, row, index, params)?;
            subscript(graph, &v, i)
        }
        Expr::Slice { expr, start, end } => {
            let v = eval_operand(graph, row, expr, params)?;
            let s = match start {
                Some(e) => Some(eval(graph, row, e, params)?),
                None => None,
            };
            let e = match end {
                Some(e) => Some(eval(graph, row, e, params)?),
                None => None,
            };
            slice(&v, s, e)
        }
        // CIP-272 map comprehension: the source-shape triage and the
        // bind/filter/project loop live together in `eval_map_comp`.
        Expr::MapComp {
            key_ident,
            value_ident,
            source,
            predicate,
            key_expr,
            value_expr,
        } => eval_map_comp(
            ctx,
            key_ident.as_ref(),
            value_ident,
            source,
            predicate.as_deref(),
            key_expr,
            value_expr,
        ),
        Expr::ListComp {
            ident,
            list,
            predicate,
            projection,
        } => eval_list_comp(ctx, ident, list, predicate.as_deref(), projection),
        Expr::Quantifier {
            kind,
            ident,
            list,
            predicate,
        } => eval_quantifier(ctx, *kind, ident, list, predicate),
        Expr::Reduce {
            accumulator,
            initial,
            ident,
            list,
            expression,
        } => eval_reduce(ctx, accumulator, initial, ident, list, expression),
        Expr::AllReduce {
            accumulator,
            initial,
            ident,
            list,
            expression,
            predicate,
        } => eval_all_reduce(
            ctx,
            accumulator,
            initial,
            ident,
            list,
            expression,
            predicate,
        ),
        Expr::IsType {
            expr,
            type_expr,
            negated,
        } => {
            let value = eval(graph, row, expr, params)?;
            Ok(Value::Bool(
                value_matches_type(&value, type_expr) ^ *negated,
            ))
        }
        Expr::IsNormalized {
            expr,
            form,
            negated,
        } => {
            let value = eval(graph, row, expr, params)?;
            let Value::String(s) = value else {
                return Ok(Value::Null);
            };
            // ADR-0463: the normalization walk allocates a full copy that can
            // expand several-fold; charge the conservative bound first.
            crate::eval::string::charge_unicode_expansion(graph, &s)?;
            let result = is_normalized(&s, form.as_deref().unwrap_or("NFC"));
            Ok(Value::Bool(result ^ *negated))
        }
        Expr::ChronoCompare {
            left,
            op,
            right,
            negated,
        } => {
            let l = eval(graph, row, left, params)?;
            let r = eval(graph, row, right, params)?;
            Ok(temporal::chrono_compare(*op, &l, &r, *negated))
        }
        Expr::MapProjection { expr, items } => eval_map_projection(ctx, expr, items),
        Expr::PathConstruct { elements } => path_value::construct(
            graph,
            elements
                .iter()
                .map(|e| eval(graph, row, e, params))
                .collect::<Result<Vec<_>>>()?,
        ),
        Expr::DistanceMetric(_) => Err(Error::semantic(
            "distance metrics are only valid as a vector_distance/vector_norm argument",
        )),
        Expr::PatternPred { path } => {
            // LABS-193/LABS-246: existence only — the anchored probe, then
            // a limit-1 walk; never a full match collection.
            Ok(Value::Bool(pattern_exists(graph, row, path, params)?))
        }
        Expr::ShortestPath { all, path } => {
            eval_shortest(graph, row, params, *all, path, ShortestWant::Path)
        }
        Expr::ExistsSubquery { query } => crate::exec::eval_exists_query(graph, row, params, query),
        Expr::CountSubquery { query } => crate::exec::eval_count_query(graph, row, params, query),
        Expr::CollectSubquery { query, path_shadow } => {
            crate::exec::eval_collect_query(graph, row, params, query, path_shadow.as_ref())
        }
    }
}

/// A map projection `n { .k, v: expr, name, .* }`: evaluate the target,
/// then build the output map item by item. A null target is null (CIP-284),
/// a deleted entity is rejected once for the whole projection, and an
/// `.*` reads a bound entity's properties from its store entry (CIP-257)
/// — the same policies the per-item reads apply, stated here once so the
/// projection answers as one unit.
fn eval_map_projection(
    ctx: EvalCtx<'_>,
    expr: &Expr,
    items: &[MapProjectionItem],
) -> Result<Value> {
    let EvalCtx { graph, row, params } = ctx;
    let target = eval(graph, row, expr, params)?;
    // A map projection over a null entity is null (CIP-284): the
    // projection has no properties to project, and returning the
    // `meta`-only map instead made `collect(n {.*, meta: …})` over
    // an `OPTIONAL MATCH` miss collect a row instead of nothing.
    if target.is_null() {
        return Ok(Value::Null);
    }
    // The whole-projection admission: `n{.*}` and a `{k: v}`-only
    // projection read no property off the target, so without this call they
    // would silently project `{}` while `n.p` raises DeletedEntityAccess
    // (one projection, two answers). The `Property` arm's read goes through
    // `prop`, which applies the same policy idempotently.
    reject_deleted_access(graph, &target)?;
    // The projection's output is a runtime value map: its keys are
    // the value domain (String), rendered once per entry here.
    let mut map: BTreeMap<String, Value> = BTreeMap::new();
    for item in items {
        match item {
            MapProjectionItem::KeyValue { key, value } => {
                map.insert(key.to_string(), eval(graph, row, value, params)?);
            }
            MapProjectionItem::Property { key } => {
                let value = prop(graph, &target, &relate_names::unresolved(key.clone()))?;
                map.insert(key.to_string(), value);
            }
            MapProjectionItem::Variable { name } => {
                // ADR-0335: same policy as `Expr::Variable` — an
                // unbound name is a typed error, never `null`.
                let value = row
                    .get_name(name)
                    .cloned()
                    .ok_or_else(|| name.with_str(Error::undefined_variable))?;
                map.insert(name.to_string(), value);
            }
            MapProjectionItem::AllProperties => match &target {
                Value::Map(properties) => {
                    map.extend(properties.iter().map(|(k, v)| (k.clone(), v.clone())))
                }
                // A bound entity carries its labels but not its
                // properties: the store entry is the read's source
                // (CIP-257) — the one store-backed resolver
                // `properties()` and `keys()` ride (the caller's
                // earlier deleted-entity rejection is idempotent
                // with the resolver's own).
                value @ (Value::Node(_) | Value::Relationship(_)) => {
                    let properties = entity_backing_map(graph, value, "`{.*}` map projection")?;
                    map.extend(
                        properties
                            .iter()
                            .map(|(key, value)| (key.clone(), value.clone())),
                    );
                }
                _ => {}
            },
        }
    }
    Value::checked_map(map)
}