relate-executor 1.3.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! The `LOAD CSV` clause: read the source named by each row's URL and bind
//! one row per CSV record.
//!
//! The generator form (ADR-0555, the shared feed spine in `feed.rs`): the
//! source's records are walked one at a time through the record cursor and
//! the rows are handed to the consumer as they are built — never
//! collected — so a consumer that folds rows as they arrive (an
//! aggregation) holds constant state however long the file is, and a
//! consumer that stops early never reads the rest of it. The source's
//! *text* is the reader's memoized `Arc<str>` (one physical read per
//! canonical source, ADR-0081): the clause streams records and rows, not
//! the file read.
//!
//! [`exec_load_csv`] is the materializing form for a consumer that cannot
//! fold as rows arrive, charging each source's output to the running total
//! before its first row is built (ADR-0463).

use std::sync::Arc;

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

use crate::csv::{field_terminator_char, record_value, split_csv_line, RecordCursor};
use crate::ctx::ExecCtx;
use crate::eval::eval;
use crate::row::Row;

use super::feed::{FeedSource, StreamedFeed};

/// The read opened on one input row: the row being read for (the template
/// the bound record column joins), its source's remaining records, and —
/// under `WITH HEADERS`, one fact, not the flag re-encoded — the header
/// names.
struct OpenRead {
    template: Row,
    cursor: RecordCursor<Arc<str>>,
    headers: Option<Vec<String>>,
}

/// The blank-record policy, spelled once: a blank physical record yields
/// no row, so the walk skips it and the charge does not bill it.
fn yields_row(record: &str) -> bool {
    !record.is_empty()
}

/// One `LOAD CSV` clause's fields, bundled once (the way `SubquerySpec`
/// bundles `CALL { … }`): the dispatcher's arm, the batch-major driver's
/// head and every entry below take the clause by this one value, so a new
/// clause field widens one struct, not three signatures.
#[derive(Clone, Copy)]
pub(super) struct LoadCsvSpec<'a> {
    pub(super) with_headers: bool,
    pub(super) url: &'a Expr,
    pub(super) variable: &'a SmallName,
    pub(super) field_terminator: Option<&'a str>,
}

/// The `LOAD CSV` producer: the [`FeedSource`] the shared spine drives.
/// Owns the field terminator (resolved once at construction — an invalid
/// one refuses the clause before any file is read), the URL expression,
/// the bound variable, and the read opened on the current input row.
pub(super) struct LoadCsvSource<'a> {
    delim: char,
    with_headers: bool,
    url: &'a Expr,
    variable: &'a SmallName,
    current: Option<OpenRead>,
}

impl<'a> LoadCsvSource<'a> {
    /// A bad terminator is a clause-level refusal: it fails here, before
    /// the first source is opened.
    pub(super) fn new(spec: LoadCsvSpec<'a>) -> Result<Self> {
        let LoadCsvSpec {
            with_headers,
            url,
            variable,
            field_terminator,
        } = spec;
        Ok(Self {
            delim: field_terminator_char(field_terminator)?,
            with_headers,
            url,
            variable,
            current: None,
        })
    }
}

impl FeedSource for LoadCsvSource<'_> {
    fn open(&mut self, ctx: ExecCtx<'_>, row: Row) -> Result<bool> {
        let src = eval(ctx.graph, &row, self.url, ctx.params)?;
        let path = match src {
            Value::String(s) => s,
            // A null URL contributes no rows at all: the input row is
            // skipped, not padded. The skip still samples the cancel
            // tick, so a source of only null URLs stays cancellable.
            Value::Null => {
                ctx.graph.cancel_tick()?;
                return Ok(false);
            }
            _ => return Err(Error::ty("LOAD CSV FROM requires a string URL")),
        };
        // The source boundary (`CsvReader::load`) resolves the CIP-202
        // canned fixture URL; everything else goes through the
        // local-file policy (ADR-0081).
        let text: Arc<str> = ctx.graph.csv.load(&path)?;
        let mut cursor = RecordCursor::new(text);
        // The header record is extracted first, so a leading blank line
        // under WITH HEADERS cannot strip the header and flip map rows
        // to list rows.
        let headers: Option<Vec<String>> = if self.with_headers {
            cursor
                .next_record(self.delim)
                .map(|h| split_csv_line(h, self.delim))
        } else {
            None
        };
        self.current = Some(OpenRead {
            template: row,
            cursor,
            headers,
        });
        Ok(true)
    }

    fn charge_facts(&self) -> Result<(usize, usize)> {
        // Read off the open read alone — the materializing form's own
        // fact: the streamed path never counts the records it will not
        // bill (the count re-walks the remaining text). The count is the
        // rows `pull` will build, so a blank record the walk skips is
        // never billed either.
        let current = self
            .current
            .as_ref()
            .expect("charge_facts is read on an open source");
        Ok((
            current.cursor.remaining_records(self.delim, yields_row),
            current.template.len().saturating_add(1),
        ))
    }

    fn pull(&mut self, ctx: ExecCtx<'_>) -> Result<Option<Row>> {
        let Some(current) = self.current.as_mut() else {
            return Ok(None);
        };
        loop {
            let Some(record) = current.cursor.next_record(self.delim) else {
                return Ok(None);
            };
            // The cancel sample sits at the loop head — before the skip —
            // so a run of blank physical lines stays cancellable.
            ctx.graph.cancel_tick()?;
            // Blank-line policy for the data records: a blank record (an
            // empty physical line between record breaks) yields no row.
            if !yields_row(record) {
                continue;
            }
            // Ragged-row policy (docs/api/cli/spec.md): under WITH HEADERS a
            // record narrower than the header maps its missing names to
            // null — Neo4j's `LOAD CSV WITH HEADERS` semantics, applied at
            // the parse boundary by `record_value`'s named rule
            // (`zip_with_header_nulls`); extra cells are dropped. List rows
            // keep their natural width.
            let val = record_value(
                current.headers.as_deref(),
                split_csv_line(record, self.delim),
            );
            let mut row = current.template.clone();
            row.insert(self.variable.to_string(), val);
            return Ok(Some(row));
        }
    }
}

/// The clause's materializing form: the feed spine's `materialize` for a
/// consumer that cannot fold rows as they arrive (the dispatcher's
/// non-streaming clause path).
pub(super) fn exec_load_csv(
    ctx: ExecCtx<'_>,
    rows: Vec<Row>,
    spec: LoadCsvSpec<'_>,
) -> Result<Vec<Row>> {
    load_csv_feed(ctx, rows, spec, false)?.materialize()
}

/// The clause's feed over its source: the constructor both the parked
/// (fused) and the materializing form build, so the two share the source
/// construction and its terminator refusal.
pub(super) fn load_csv_feed<'a>(
    ctx: ExecCtx<'a>,
    rows: Vec<Row>,
    spec: LoadCsvSpec<'a>,
    profiled: bool,
) -> Result<StreamedFeed<'a>> {
    Ok(StreamedFeed::new(
        ctx,
        rows,
        Box::new(LoadCsvSource::new(spec)?),
        profiled,
    ))
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::fs;

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

    use super::super::test_support::{exec_cypher_on, profiled_write_routed, try_exec_cypher_on};
    use super::{exec_load_csv, load_csv_feed, LoadCsvSource, LoadCsvSpec};
    use crate::csv::{local_file_url, test_root, CsvPolicy};
    use crate::ctx::ExecCtx;
    use crate::exec::feed::FeedSource;
    use crate::row::Row;
    use crate::{ExecGraph, Graph};

    fn csv_rooted_graph(root: &std::path::Path) -> Graph {
        let mut graph = Graph::default();
        graph.configure_load_csv(CsvPolicy::new(Some(root.to_path_buf()), None).unwrap());
        graph
    }

    fn csv_row(value: &str) -> Vec<Value> {
        vec![Value::List(vec![Value::String(value.into())].into())]
    }

    #[test]
    fn load_csv_rejects_multi_char_field_terminator() {
        let graph = Graph::default();
        let view = ExecGraph::read(&graph);
        let params = BTreeMap::new();
        let url = Expr::String("rows.csv".into());
        let err = exec_load_csv(
            ExecCtx::new(&view, &params),
            vec![Row::new()],
            LoadCsvSpec {
                with_headers: false,
                url: &url,
                variable: &SmallName::from_name("row"),
                field_terminator: Some("||"),
            },
        )
        .unwrap_err();
        assert_eq!(
            err,
            Error::ty("LOAD CSV FIELDTERMINATOR must be exactly one character")
        );
    }

    #[test]
    fn one_physical_read_per_statement() {
        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("rows.csv"), "a\nb\n").unwrap();
        #[cfg(unix)]
        std::os::unix::fs::symlink("rows.csv", root.join("alias.csv")).unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let result = exec_cypher_on(
            &view,
            "UNWIND [1,2,3] AS n LOAD CSV FROM 'rows.csv' AS row RETURN n, row",
        );
        assert_eq!(result.rows.len(), 6);
        assert_eq!(
            result.rows[0],
            vec![
                Value::Int(1),
                Value::List(vec![Value::String("a".into())].into())
            ]
        );
        assert_eq!(view.csv.read_count(), 1);
        #[cfg(unix)]
        assert_eq!(view.csv.load("alias.csv").unwrap().as_ref(), "a\nb\n");
        assert_eq!(view.csv.read_count(), 1);
        assert_eq!(
            ExecGraph::read(&graph)
                .csv
                .load("rows.csv")
                .unwrap()
                .as_ref(),
            "a\nb\n"
        );
        assert_eq!(view.csv.read_count(), 1);
        // The CIP-202 fixture resolves from canned data, never a physical
        // read, even with a local root configured.
        assert_eq!(
            view.csv
                .load("https://data.neo4j.com/bands/artists.csv")
                .unwrap()
                .as_ref(),
            "1,ABBA,1992\n2,Roxette,1986\n3,Europe,1979\n"
        );
        assert_eq!(view.csv.read_count(), 1);
    }

    #[test]
    fn load_csv_honors_rfc4180_quoting_across_lines() {
        let dir = test_root();
        let root = dir.path();
        fs::write(
            root.join("rows.csv"),
            "id,title,owner\n1,\"Neo4j, graph\nand \"\"Cypher\"\"\",42\n2,plain,7\n",
        )
        .unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let result = exec_cypher_on(
            &view,
            "LOAD CSV WITH HEADERS FROM 'rows.csv' AS r RETURN r.id, r.title, r.owner",
        );
        let row = |id: &str, title: &str, owner: &str| {
            vec![id, title, owner]
                .into_iter()
                .map(|s| Value::String(s.into()))
                .collect::<Vec<_>>()
        };
        assert_eq!(
            result.rows,
            vec![
                row("1", "Neo4j, graph\nand \"Cypher\"", "42"),
                row("2", "plain", "7"),
            ]
        );
    }

    /// Neo4j-parity ragged-row rule: under WITH HEADERS a short record maps
    /// the missing names to null (an absent field is distinguishable from
    /// an empty one); extra cells past the header width are dropped; list
    /// rows keep their natural width.
    #[test]
    fn load_csv_with_headers_maps_missing_cells_to_null() {
        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("ragged.csv"), "a,b,c\n1,2\n3,4,5,6\n9,\n").unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let result = exec_cypher_on(
            &view,
            "LOAD CSV WITH HEADERS FROM 'ragged.csv' AS r RETURN r.a, r.b, r.c",
        );
        let s = |v: &str| Value::String(v.into());
        assert_eq!(
            result.rows,
            vec![
                vec![s("1"), s("2"), Value::Null],
                vec![s("3"), s("4"), s("5")],
                vec![s("9"), s(""), Value::Null],
            ]
        );
        let lists = exec_cypher_on(&view, "LOAD CSV FROM 'ragged.csv' AS row RETURN row");
        assert_eq!(
            lists.rows,
            vec![
                vec![Value::List(vec![s("a"), s("b"), s("c")].into())],
                vec![Value::List(vec![s("1"), s("2")].into())],
                vec![Value::List(vec![s("3"), s("4"), s("5"), s("6")].into())],
                vec![Value::List(vec![s("9"), s("")].into())],
            ]
        );
    }

    #[test]
    fn header_record_is_extracted_before_the_blank_line_policy() {
        let dir = test_root();
        let root = dir.path();
        // A blank line after the header yields no row; the header is the
        // file's first record.
        fs::write(root.join("rows.csv"), "id,name\n\n1,abba\n").unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let result = exec_cypher_on(
            &view,
            "LOAD CSV WITH HEADERS FROM 'rows.csv' AS r RETURN r.id, r.name",
        );
        assert_eq!(
            result.rows,
            vec![vec![
                Value::String("1".into()),
                Value::String("abba".into()),
            ]]
        );
        // A leading blank line is the header record (one empty name): the
        // blank-line policy never runs ahead of header extraction, so map
        // rows cannot silently degrade to list rows.
        fs::write(root.join("leading.csv"), "\n1,2\n").unwrap();
        let result = exec_cypher_on(
            &view,
            "LOAD CSV WITH HEADERS FROM 'leading.csv' AS r RETURN keys(r), r['']",
        );
        assert_eq!(
            result.rows,
            vec![vec![
                Value::List(vec![Value::String("".into())].into()),
                Value::String("1".into()),
            ]]
        );
    }

    #[test]
    fn reused_view_reads_edited_source_on_each_execute() {
        let dir = test_root();
        let root = dir.path();
        let path = root.join("rows.csv");
        fs::write(&path, "first\n").unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let query = "UNWIND [1,2,3] AS n LOAD CSV FROM 'rows.csv' AS row RETURN row";

        assert_eq!(exec_cypher_on(&view, query).rows, vec![csv_row("first"); 3]);
        assert_eq!(view.csv.read_count(), 1);
        fs::write(&path, "second\n").unwrap();
        assert_eq!(
            exec_cypher_on(&view, query).rows,
            vec![csv_row("second"); 3]
        );
        assert_eq!(view.csv.read_count(), 2);
    }

    #[test]
    fn reused_view_clears_canonical_memo_for_distinct_source_spellings() {
        let dir = test_root();
        let root = dir.path();
        let path = root.join("rows.csv");
        fs::write(&path, "first\n").unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);

        let relative = "LOAD CSV FROM 'rows.csv' AS row RETURN row";
        assert_eq!(exec_cypher_on(&view, relative).rows, vec![csv_row("first")]);
        fs::write(&path, "second\n").unwrap();
        let absolute = format!(
            "LOAD CSV FROM '{}' AS row RETURN row",
            local_file_url(&path)
        );
        assert_eq!(
            exec_cypher_on(&view, &absolute).rows,
            vec![csv_row("second")]
        );
        assert_eq!(view.csv.read_count(), 2);
    }

    #[test]
    fn reused_view_reports_removed_source_on_next_execute() {
        let dir = test_root();
        let root = dir.path();
        let path = root.join("rows.csv");
        fs::write(&path, "first\n").unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let query = "LOAD CSV FROM 'rows.csv' AS row RETURN row";

        assert_eq!(exec_cypher_on(&view, query).rows, vec![csv_row("first")]);
        fs::remove_file(path).unwrap();
        assert_eq!(
            try_exec_cypher_on(&view, query).err().unwrap(),
            Error::execution("LOAD CSV file unavailable")
        );
        assert_eq!(view.csv.read_count(), 1);
    }

    #[cfg(unix)]
    #[test]
    fn reused_view_observes_in_root_symlink_retarget_next_execute() {
        use std::os::unix::fs::symlink;

        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("first.csv"), "first\n").unwrap();
        fs::write(root.join("second.csv"), "second\n").unwrap();
        let alias = root.join("alias.csv");
        symlink("first.csv", &alias).unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let first =
            "UNWIND ['alias.csv','first.csv','alias.csv'] AS src LOAD CSV FROM src AS row RETURN row";
        assert_eq!(exec_cypher_on(&view, first).rows, vec![csv_row("first"); 3]);
        assert_eq!(view.csv.read_count(), 1);

        fs::remove_file(&alias).unwrap();
        symlink("second.csv", &alias).unwrap();
        let second = "UNWIND ['alias.csv','second.csv'] AS src LOAD CSV FROM src AS row RETURN row";
        assert_eq!(
            exec_cypher_on(&view, second).rows,
            vec![csv_row("second"); 2]
        );
        assert_eq!(view.csv.read_count(), 2);
    }

    /// LABS-1845: the materializing form charges each source's output
    /// (records x carried width) to the running total before its first row
    /// is built — the wide-row `UNWIND` charge rule, same numbers,
    /// different producer. A file a plain (non-folding) projection
    /// consumes must refuse at a lowered cap instead of building the rows.
    #[test]
    fn loading_a_plain_projection_charges_the_row_clones() {
        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("many.csv"), "1\n".repeat(50_000)).unwrap();
        let mut graph = csv_rooted_graph(root);
        graph.configure_expression_memory_limit(Some(1 << 20));
        let view = ExecGraph::read(&graph);
        let error = try_exec_cypher_on(&view, "LOAD CSV FROM 'many.csv' AS row RETURN row")
            .err()
            .expect("a materialized LOAD CSV must be refused at the lowered cap");
        assert!(
            matches!(
                &error,
                Error::Capacity {
                    kind: CapacityKind::Expression,
                    ..
                }
            ),
            "expected the typed expression-capacity refusal, got: {error}"
        );
    }

    /// LABS-1845 (ADR-0555): the same file, the same lowered cap, the same
    /// clause — only the consumer differs. An aggregation folds the rows as
    /// the records are read, so the charge the plain projection refuses is
    /// never booked and the answer is one integer however long the file is:
    /// the parked feed never materializes (the distinguishing input is the
    /// consumer, the file is identical to the refusing test above).
    #[test]
    fn a_folding_consumer_streams_the_file_whatever_the_record_count() {
        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("many.csv"), "1\n".repeat(50_000)).unwrap();
        let mut graph = csv_rooted_graph(root);
        graph.configure_expression_memory_limit(Some(1 << 20));
        let view = ExecGraph::read(&graph);
        let result = exec_cypher_on(
            &view,
            "LOAD CSV FROM 'many.csv' AS row RETURN count(row) AS c",
        );
        assert_eq!(result.rows, vec![vec![Value::Int(50_000)]]);
    }

    /// The ordered window is a fold too (the classification's `may_cut`
    /// half): `ORDER BY ... LIMIT` over the file parks the generator and the
    /// bounded top-k heap folds the rows as the records are read, so the
    /// plain projection's refusal at the same lowered cap never fires —
    /// pinned for this producer independently of the aggregation test, so
    /// a park narrowed to aggregations alone would collect the file and
    /// fail here.
    #[test]
    fn an_ordered_window_streams_the_file_whatever_the_record_count() {
        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("many.csv"), "1\n".repeat(50_000)).unwrap();
        let mut graph = csv_rooted_graph(root);
        graph.configure_expression_memory_limit(Some(1 << 20));
        let view = ExecGraph::read(&graph);
        let result = exec_cypher_on(
            &view,
            "LOAD CSV FROM 'many.csv' AS row RETURN row[0] AS v ORDER BY v LIMIT 3",
        );
        assert_eq!(
            result.rows,
            vec![vec![Value::String("1".into())]; 3],
            "the window answers from the streamed records"
        );
    }

    /// The collecting drain books the same PROFILE facts the streaming walk
    /// does: a feed whose rows a consumer collects (a parked generator
    /// whose window resolves to none) reports the rows it generated, not
    /// zero. Pinned at the spine — the feed's own stats after
    /// `materialize` — because no 64-bit bound resolves a parked window to
    /// none through Cypher (`skip + limit` cannot overflow `usize`).
    #[test]
    fn a_collected_feed_reports_its_rows_like_a_streamed_one() {
        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("rows.csv"), "a\nb\nc\n").unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let params = BTreeMap::new();
        let url = Expr::String("rows.csv".into());
        let variable = SmallName::from_name("row");
        let feed = load_csv_feed(
            ExecCtx::new(&view, &params),
            vec![Row::new()],
            LoadCsvSpec {
                with_headers: false,
                url: &url,
                variable: &variable,
                field_terminator: None,
            },
            true,
        )
        .unwrap();
        let stats = feed.stats();
        assert_eq!(feed.materialize().unwrap().len(), 3);
        assert_eq!(stats.rows(), 3, "the collecting drain notes every row");
    }

    /// LABS-1845: a streamed `LOAD CSV` reports its own clause facts after
    /// its consumer drained it — the rows it generated (3, the file's
    /// records), one row's size as its in-flight memory, no expression
    /// bytes (no list was built) — at its own PROFILE position, while the
    /// consuming aggregation reports its own.
    #[test]
    fn a_streamed_load_csv_records_its_own_clause_facts() {
        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("rows.csv"), "a\nb\nc\n").unwrap();
        let graph = csv_rooted_graph(root);
        let records = profiled_write_routed(
            &graph,
            "LOAD CSV FROM 'rows.csv' AS row RETURN count(row) AS c",
        );
        assert_eq!(
            records.len(),
            2,
            "the LOAD CSV and its consumer, one record each"
        );
        let load = &records[0];
        assert_eq!(load.clause, 0);
        assert_eq!(
            load.rows, 3,
            "the parked feed's facts report after the drain"
        );
        assert_eq!(load.expression_built_bytes, 0, "no list was built");
        assert!(
            load.memory_bytes > 0,
            "one row's size is the in-flight memory"
        );
        assert_eq!(records[1].clause, 1);
    }

    /// LABS-1845: the charge facts are the materializing form's alone — a
    /// source opened for streaming never counts its records. The count
    /// (which re-walks the remaining text) is read off the open read only
    /// by `charge_facts`, and it bills the rows the walk will build: the
    /// seam's own contract is pinned here — open, then count, then pull —
    /// with a blank record in the file, which yields no row and so is not
    /// billed.
    #[test]
    fn the_charge_count_is_the_rows_the_open_source_will_add() {
        let dir = test_root();
        let root = dir.path();
        fs::write(root.join("rows.csv"), "a\n\nb\nc\n").unwrap();
        let graph = csv_rooted_graph(root);
        let view = ExecGraph::read(&graph);
        let params = BTreeMap::new();
        let url = Expr::String("rows.csv".into());
        let variable = SmallName::from_name("row");
        let mut source = LoadCsvSource::new(LoadCsvSpec {
            with_headers: false,
            url: &url,
            variable: &variable,
            field_terminator: None,
        })
        .unwrap();
        let ctx = ExecCtx::new(&view, &params);
        // Nothing open yet: the spine never reads the facts before an
        // open succeeds, and pulling with nothing open is spent.
        assert!(source.pull(ctx).unwrap().is_none());
        assert!(source.open(ctx, Row::new()).unwrap());
        assert_eq!(
            source.charge_facts().unwrap().0,
            3,
            "three records yield a row; the blank one is not billed"
        );
        let mut pulled = 0;
        while source.pull(ctx).unwrap().is_some() {
            pulled += 1;
        }
        assert_eq!(pulled, 3, "the count matches what the walk built");
    }
}