relate-executor 1.2.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
//! `UNWIND` row carry.
//!
//! Expanding a list copies the input row once per element. When that row
//! still holds the list (`UNWIND file.symbols AS row` while `file` contains
//! `symbols`), each copy is another full list and a file of N symbols costs
//! N². The list is removed from the carried row when the rest of the clause
//! list does not read it. Sibling fields (`file.path`, `file.language`) stay.
//! A later read of the list keeps the copy, so `RETURN file.symbols` is
//! unchanged.

use std::cell::Cell;
use std::collections::BTreeMap;
use std::rc::Rc;
use std::sync::Arc;

use relate_core::{Result, Value};
use relate_ir::{var_prop_owned, walk_clauses, Clause, Expr, MapProjectionItem};
use relate_names::SmallName;
// `web_time` on wasm32: `std::time::Instant::now()` traps there (see
// `dispatch.rs`).
use web_time::Instant;

use crate::ctx::ExecCtx;
use crate::eval::{eval, into_items, lazy_range, rows_logical_bytes, Items, RangeValues};
use crate::overlay::ExecGraph;
use crate::row::Row;

use super::scope::call_scope_reads;

pub(super) enum Source {
    /// `UNWIND xs AS x`: drop the `xs` column.
    Column(String),
    /// `UNWIND file.symbols AS row`: drop that key from the map, node, or
    /// relationship bound to `var`.
    Key { var: String, key: String },
}

/// Reads of `source` in one clause suffix. A property base (`file` in
/// `file.path`) is one variable occurrence and one base, so it does not by
/// itself keep the list. A bare `file` is a variable occurrence with no
/// matching base.
struct Hits {
    reads: bool,
    vars: u32,
    bases: u32,
}

impl Source {
    /// Strip decision for this `UNWIND`. `None` means every carried row keeps
    /// the source. The suffix walk runs once; applying it does not.
    pub(super) fn plan(expression: &Expr, suffix: &[Clause]) -> Option<Self> {
        let source = source(expression)?;
        if suffix_keeps(&source, suffix) {
            None
        } else {
            Some(source)
        }
    }

    pub(super) fn strip(&self, row: &mut Row) {
        match self {
            Source::Column(name) => {
                row.shift_remove(name);
            }
            Source::Key { var, key } => {
                if let Some(value) = row.get_mut(var) {
                    clear_key(value, key);
                }
            }
        }
    }

    fn var(&self) -> &str {
        match self {
            Source::Column(name) => name,
            Source::Key { var, .. } => var,
        }
    }

    fn reads_property(&self, key: &str) -> bool {
        match self {
            Source::Column(_) => true,
            Source::Key { key: want, .. } => key == want,
        }
    }

    fn reads_projection(&self, items: &[MapProjectionItem]) -> bool {
        match self {
            Source::Column(_) => true,
            Source::Key { key, .. } => items.iter().any(|item| match item {
                MapProjectionItem::AllProperties => true,
                MapProjectionItem::Property { key: item_key } => item_key == key.as_str(),
                // A variable selector reads a row variable. `note` counts
                // that whether or not this projection's base is the source.
                MapProjectionItem::Variable { .. } | MapProjectionItem::KeyValue { .. } => false,
            }),
        }
    }
}

fn source(expression: &Expr) -> Option<Source> {
    match expression {
        Expr::Variable(name) => Some(Source::Column(name.to_string())),
        other => var_prop_owned(other).map(|(var, key)| Source::Key { var, key }),
    }
}

fn clear_key(value: &mut Value, key: &str) {
    match value {
        Value::Map(map) => {
            // Shared with a parameter or another row (ADR-0170). Copy the
            // map only then, so the removal stays on this row.
            Arc::make_mut(map).remove(key);
        }
        // Shared with another row (the value is an `Arc`). Copy the entity
        // only when this removal is not the sole owner.
        Value::Node(node) => {
            Arc::make_mut(&mut Arc::make_mut(node).properties).remove(key);
        }
        Value::Relationship(rel) => {
            Arc::make_mut(&mut Arc::make_mut(rel).properties).remove(key);
        }
        _ => {}
    }
}

fn suffix_keeps(source: &Source, suffix: &[Clause]) -> bool {
    // A `CALL { … }` clause reads the row without any expression naming
    // it — tracked beside the expression accumulator (the two visitors
    // capture different locals, so both are plain `&mut` closures).
    let mut row_read = false;
    let mut hits = Hits {
        reads: false,
        vars: 0,
        bases: 0,
    };
    walk_clauses(
        suffix,
        &mut |clause| row_read |= call_scope_reads_row(clause, source),
        &mut |expr| note(expr, source, &mut hits),
    );
    row_read || hits.reads || hits.vars > hits.bases
}

/// Whether a clause reads the row without an expression naming it: the
/// naked and `(*)` `CALL { … }` scopes take the whole row, and an import
/// list must find every name it names — the one [`call_scope_reads`] rule
/// [`super::pushdown::live_from`] applies to the same clause.
fn call_scope_reads_row(clause: &Clause, source: &Source) -> bool {
    let Clause::CallSubquery { scope, .. } = clause else {
        return false;
    };
    call_scope_reads(scope).is_none_or(|vars| vars.iter().any(|name| name == source.var()))
}

fn note(expr: &Expr, source: &Source, hits: &mut Hits) {
    let var = source.var();
    match expr {
        Expr::Star => hits.reads = true,
        Expr::Variable(name) if name == var => hits.vars += 1,
        Expr::Property { expr, key, .. } => {
            if note_base(expr, var, hits) && key.with_str(|text| source.reads_property(text)) {
                hits.reads = true;
            }
        }
        Expr::DynamicProperty { expr, .. } => {
            if note_base(expr, var, hits) {
                hits.reads = true;
            }
        }
        Expr::MapProjection { expr, items } => {
            // A variable selector reads that name from the row. It is not a
            // child expression, and the projection base is often a different
            // variable (`mm { file }`).
            if items
                .iter()
                .any(|item| matches!(item, MapProjectionItem::Variable { name } if name == var))
            {
                hits.reads = true;
            }
            if note_base(expr, var, hits) && source.reads_projection(items) {
                hits.reads = true;
            }
        }
        _ => {}
    }
}

/// Count one base when `expr` is exactly the source variable.
fn note_base(expr: &Expr, var: &str, hits: &mut Hits) -> bool {
    match expr {
        Expr::Variable(name) if name == var => {
            hits.bases += 1;
            true
        }
        _ => false,
    }
}

/// The elements one input row's `UNWIND` source yields, walked one at a
/// time: a `range(...)` call is a counter (no list is ever built), an
/// evaluated list moves or clones its elements as the walk reaches them, and
/// a non-list scalar is its own single element.
pub(super) enum Elements {
    Range(RangeValues),
    List(Items),
    One(Option<Value>),
}

impl Elements {
    /// Elements not yet yielded.
    pub(super) fn remaining(&self) -> Result<usize> {
        match self {
            Elements::Range(range) => range.remaining(),
            Elements::List(items) => Ok(items.len()),
            Elements::One(value) => Ok(usize::from(value.is_some())),
        }
    }
}

impl Iterator for Elements {
    type Item = Result<Value>;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Elements::Range(range) => range.next(),
            Elements::List(items) => items.next().map(Ok),
            Elements::One(value) => value.take().map(Ok),
        }
    }
}

/// Evaluate `expression` for one input row without materializing a
/// generator source: `range(...)` becomes a counter, everything else is
/// evaluated once (a list is then walked, never copied wholesale).
pub(super) fn open_source(
    graph: &ExecGraph<'_>,
    row: &Row,
    expression: &Expr,
    params: &BTreeMap<String, Value>,
) -> Result<Elements> {
    if let Expr::Function { name, args, .. } = expression {
        let plan = name.with_str(|text| lazy_range(graph, row, params, text, args))?;
        if let Some(plan) = plan {
            return Ok(Elements::Range(plan.values()));
        }
    }
    Ok(match eval(graph, row, expression, params)? {
        Value::List(xs) => Elements::List(into_items(xs)),
        // A null source yields no rows; any other scalar is its own
        // single element.
        Value::Null => Elements::One(None),
        other => Elements::One(Some(other)),
    })
}

/// What a streamed `UNWIND` reports to PROFILE once its consumer drained it.
/// The feed yields one row at a time, so the memory it holds is one row: the
/// row's size is recorded (batch of one × row size), not a retained total.
/// The generation time is measured only when PROFILE asked for it, so the
/// plain path never reads a clock per row.
#[derive(Default)]
pub(crate) struct UnwindStats {
    rows: Cell<u64>,
    /// Logical bytes of one generated row: the in-flight working set.
    row_bytes: Cell<u64>,
    time_ns: Cell<u128>,
    timed: bool,
}

impl UnwindStats {
    pub(super) fn new(timed: bool) -> Self {
        Self {
            timed,
            ..Self::default()
        }
    }

    pub(super) fn rows(&self) -> u64 {
        self.rows.get()
    }

    pub(super) fn row_bytes(&self) -> u64 {
        self.row_bytes.get()
    }

    /// Nanoseconds spent generating rows (zero unless timed).
    pub(super) fn time_ns(&self) -> u128 {
        self.time_ns.get()
    }
}

/// The rows of an `UNWIND`, produced one at a time and never retained: the
/// generator form of the clause. A consumer that folds rows as they arrive
/// (an aggregation) holds constant state however long the source is, and a
/// consumer that stops early never generates the rest.
pub(crate) struct UnwindFeed<'a> {
    ctx: ExecCtx<'a>,
    inputs: std::vec::IntoIter<Row>,
    expression: &'a Expr,
    alias: &'a SmallName,
    strip: Option<Source>,
    /// The input row being expanded, with its remaining elements.
    current: Option<(Row, Elements)>,
    /// Shared with the dispatcher so the PROFILE record of this clause can
    /// report after a consumer drained the feed.
    stats: Rc<UnwindStats>,
}

impl<'a> UnwindFeed<'a> {
    pub(super) fn new(
        ctx: ExecCtx<'a>,
        tail: &[Clause],
        inputs: Vec<Row>,
        expression: &'a Expr,
        alias: &'a SmallName,
        stats: Rc<UnwindStats>,
    ) -> Self {
        Self {
            ctx,
            inputs: inputs.into_iter(),
            expression,
            alias,
            strip: Source::plan(expression, tail),
            current: None,
            stats,
        }
    }

    /// Open the next input row's source as the current expansion, and
    /// return the charge facts of the rows this source will add — its
    /// element count and the carried width — from this seam, so the
    /// materializing form bills without re-reading the opener's state.
    /// `Ok(None)` when the inputs are exhausted. A list source sheds the
    /// source column from the carried row; a scalar keeps its row whole.
    fn open_next_input(&mut self) -> Result<Option<(usize, usize)>> {
        let Some(row) = self.inputs.next() else {
            return Ok(None);
        };
        let elements = open_source(self.ctx.graph, &row, self.expression, self.ctx.params)?;
        let mut template = row;
        if !matches!(elements, Elements::One(_)) {
            if let Some(source) = &self.strip {
                source.strip(&mut template);
            }
        }
        let charge = (elements.remaining()?, template.len().saturating_add(1));
        self.current = Some((template, elements));
        Ok(Some(charge))
    }

    /// The next row of the source being expanded, `None` once it is spent.
    fn pull_current(&mut self) -> Option<Result<Row>> {
        let (template, elements) = self.current.as_mut()?;
        match elements.next() {
            Some(Ok(value)) => {
                if let Err(error) = self.ctx.graph.cancel_tick() {
                    return Some(Err(error));
                }
                let mut row = template.clone();
                row.insert(self.alias.to_string(), value);
                self.stats.rows.set(self.stats.rows.get() + 1);
                if self.stats.timed && self.stats.rows.get() == 1 {
                    self.stats
                        .row_bytes
                        .set(rows_logical_bytes(std::slice::from_ref(&row)));
                }
                Some(Ok(row))
            }
            Some(Err(error)) => {
                self.current = None;
                Some(Err(error))
            }
            None => {
                self.current = None;
                None
            }
        }
    }

    /// Drain the feed into a held vector, charging each source's output to
    /// the running total before its first row is built: the clause's
    /// materializing form, for a consumer that cannot fold as rows arrive.
    /// The charge facts come from the opener itself, so no landing is
    /// billed from a re-read of the opener's state.
    pub(super) fn materialize(mut self) -> Result<Vec<Row>> {
        // ADR-0463: the output vector is one retaining container; its
        // running total is the guard's.
        let mut retained = self.ctx.graph.retain_bytes(0)?;
        let mut rows = Vec::new();
        while let Some((count, width)) = self.open_next_input()? {
            let added = crate::workspace::retained_rows_bytes(count, width);
            retained.add_allocation(self.ctx.graph, added)?;
            while let Some(row) = self.pull_current() {
                rows.push(row?);
            }
        }
        Ok(rows)
    }
}

impl Iterator for UnwindFeed<'_> {
    type Item = Result<Row>;

    fn next(&mut self) -> Option<Self::Item> {
        let generated = if !self.stats.timed {
            self.generate()
        } else {
            let started = Instant::now();
            let generated = self.generate();
            self.stats
                .time_ns
                .set(self.stats.time_ns.get() + started.elapsed().as_nanos());
            generated
        };
        // The Iterator protocol's one reshape of the generator's answer:
        // `Ok(None)` is exhaustion, every error an item.
        match generated {
            Ok(Some(row)) => Some(Ok(row)),
            Ok(None) => None,
            Err(error) => Some(Err(error)),
        }
    }
}

impl UnwindFeed<'_> {
    /// The next generated row: the current expansion's next element, or —
    /// once it is spent — the next input row's expansion. `Ok(None)` once
    /// the inputs are exhausted. The opener's charge facts are discarded
    /// here: a generated row is never retained, so they serve the
    /// materializing form alone.
    fn generate(&mut self) -> Result<Option<Row>> {
        loop {
            if let Some(row) = self.pull_current() {
                return Ok(Some(row?));
            }
            if self.open_next_input()?.is_none() {
                return Ok(None);
            }
        }
    }
}

#[cfg(test)]
mod strip_plan_tests {
    use super::*;
    use relate_ir::{CallScope, Query, SingleQuery};

    fn call(scope: CallScope) -> Clause {
        Clause::CallSubquery {
            query: Box::new(Query {
                parts: vec![SingleQuery { clauses: vec![] }],
                union_all: vec![],
                next: None,
            }),
            optional: false,
            scope,
            in_transactions: None,
        }
    }

    #[test]
    fn plan_strips_when_nothing_reads_the_source() {
        let plan = Source::plan(&Expr::Variable("xs".into()), &[]);
        assert!(matches!(plan, Some(Source::Column(name)) if name == "xs"));
    }

    #[test]
    fn plan_keeps_for_the_three_call_scope_shapes() {
        // The shapes this module grew to keep: the naked and `(*)` scopes
        // take the whole row, and an import list naming the source reads
        // it without an expression.
        let shapes = [
            CallScope::None,
            CallScope::Wildcard,
            CallScope::Vars(vec!["xs".into()]),
        ];
        for scope in shapes {
            let suffix = [call(scope)];
            assert!(Source::plan(&Expr::Variable("xs".into()), &suffix).is_none());
        }
    }

    #[test]
    fn plan_strips_when_an_import_list_names_another_var() {
        let suffix = [call(CallScope::Vars(vec!["other".into()]))];
        let plan = Source::plan(&Expr::Variable("xs".into()), &suffix);
        assert!(matches!(plan, Some(Source::Column(name)) if name == "xs"));
    }
}