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
//! `UNWIND` row carry: the strip decision and the feed wiring. The
//! element sources themselves (the `range()` counter, the walked list,
//! the per-element comprehension pull) live in the eval layer
//! (`crate::eval::items`, REL8-1372).
//!
//! 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::sync::Arc;
use relate_core::{Result, Value};
use relate_ir::{var_prop_owned, walk_clauses, Clause, Expr, MapProjectionItem};
use relate_names::SmallName;
use crate::ctx::ExecCtx;
use crate::eval::{open_source, Elements};
use crate::row::Row;
use super::feed::StreamedFeed;
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 `UNWIND` producer: the [`FeedSource`](super::feed::FeedSource) the
/// shared spine drives — the generator form of the clause, its rows
/// produced one at a time and never retained, so 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. Owns the expanded expression, the alias, the strip decision (the
/// row carry this module's head documents), and the input row being
/// expanded with its remaining elements.
pub(crate) struct UnwindSource<'a> {
expression: &'a Expr,
alias: &'a SmallName,
strip: Option<Source>,
/// The input row being expanded, with its remaining elements. The
/// elements borrow the comprehension's fields out of `expression`'s
/// pointee (`'a`), never out of `self`.
current: Option<(Row, Elements<'a>)>,
}
impl<'a> UnwindSource<'a> {
pub(super) fn new(tail: &[Clause], expression: &'a Expr, alias: &'a SmallName) -> Self {
Self {
expression,
alias,
strip: Source::plan(expression, tail),
current: None,
}
}
}
impl super::feed::FeedSource for UnwindSource<'_> {
fn open(&mut self, ctx: ExecCtx<'_>, row: Row) -> Result<bool> {
let elements = open_source(ctx.graph, &row, self.expression, ctx.params)?;
let mut template = row;
// A list source sheds the source column from the carried row; a
// scalar keeps its row whole.
if !matches!(elements, Elements::One(_)) {
if let Some(source) = &self.strip {
source.strip(&mut template);
}
}
self.current = Some((template, elements));
Ok(true)
}
fn charge_facts(&self) -> Result<(usize, usize)> {
let (template, elements) = self
.current
.as_ref()
.expect("charge_facts is read on an open source");
Ok((elements.pending_bound()?, template.len().saturating_add(1)))
}
fn pull(&mut self, ctx: ExecCtx<'_>) -> Result<Option<Row>> {
let Some((template, elements)) = self.current.as_mut() else {
return Ok(None);
};
let value = match elements.next(ctx, template) {
Ok(Some(value)) => value,
// Exhausted or failed: the source is spent either way, so a
// later pull answers `None` rather than re-driving it.
spent => {
self.current = None;
return spent.map(|_| None);
}
};
ctx.graph.cancel_tick()?;
let mut row = template.clone();
row.insert(self.alias.to_string(), value);
Ok(Some(row))
}
}
/// The clause's feed over its source: the constructor both the parked
/// (fused) and the materializing form build (`load_csv_feed`'s shape).
pub(super) fn unwind_feed<'a>(
ctx: ExecCtx<'a>,
tail: &'a [Clause],
inputs: Vec<Row>,
expression: &'a Expr,
alias: &'a SmallName,
profiled: bool,
) -> StreamedFeed<'a> {
StreamedFeed::new(
ctx,
inputs,
Box::new(UnwindSource::new(tail, expression, alias)),
profiled,
)
}
#[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"));
}
}