rushdb-query 3.0.7

Internal component of the RushDB server: query planner and executor. No stable API; install rushdb-server instead.
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
//! Per-query working-set cap (config #3): a query that would materialize more
//! than the configured budget returns `resource exhausted` instead of
//! ballooning RSS. The default (0) is off, so existing behavior is unchanged.

use std::sync::Arc;

use rushdb_lmpg::plan::FieldChange;
use rushdb_lmpg::Lmpg;
use rushdb_query::document::parse_document;
use rushdb_query::{find_records_in_view, QueryError};
use rushdb_storage::resources::StorageResources;
use rushdb_storage::Store;
use rushdb_types::ids::TenantId;
use serde_json::json;

fn store_with_cap(cap: u64) -> Arc<Store> {
    Arc::new(
        Store::new_with_resources(StorageResources {
            query_memory_max_bytes: cap,
            ..StorageResources::default()
        })
        .unwrap(),
    )
}

fn seed(lmpg: &Lmpg) {
    for i in 0..300 {
        lmpg.create_record("Big", None, vec![("n".into(), FieldChange::Set(json!(i)))])
            .unwrap();
    }
}

#[test]
fn query_memory_cap_rejects_an_oversized_working_set() {
    let store = store_with_cap(1024);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    seed(&lmpg);
    let parsed = parse_document(&json!({ "labels": ["Big"] })).unwrap();
    let view = Arc::new(store.read_view().unwrap());
    let err = find_records_in_view(&lmpg, &parsed.search, &|| false, view).unwrap_err();
    assert!(
        matches!(err, QueryError::ResourceLimit(_)),
        "expected a resource cap, got {err:?}"
    );
}

#[test]
fn cancellation_interrupts_candidate_discovery() {
    // The unscoped candidate scan must poll cancellation between batches, not
    // only per-candidate during predicate evaluation.
    let store = store_with_cap(0);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    for i in 0..5000u64 {
        lmpg.create_record("Big", None, vec![("n".into(), FieldChange::Set(json!(i)))])
            .unwrap();
    }
    let view = Arc::new(store.read_view().unwrap());
    let parsed = parse_document(&json!({})).unwrap();
    let polls = std::sync::atomic::AtomicUsize::new(0);
    let cancelled = || {
        // First poll (the pre-dispatch check) passes; the next (inside the
        // candidate scan) cancels.
        polls.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1 >= 2
    };
    let err = find_records_in_view(&lmpg, &parsed.search, &cancelled, view).unwrap_err();
    assert!(matches!(err, QueryError::Cancelled), "{err:?}");
}

#[test]
fn query_memory_cap_is_off_by_default() {
    let store = store_with_cap(0);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    seed(&lmpg);
    let parsed = parse_document(&json!({ "labels": ["Big"] })).unwrap();
    let view = Arc::new(store.read_view().unwrap());
    let page = find_records_in_view(&lmpg, &parsed.search, &|| false, view).unwrap();
    assert_eq!(page.total_matched, 300);
}

#[test]
fn record_collect_respects_the_configured_working_set_cap() {
    use rushdb_query::{CollectSpec, Direction};
    let store = store_with_cap(64 * 1024);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    let root = lmpg.create_record("Root", None, vec![]).unwrap();
    for _ in 0..4 {
        let child = lmpg
            .create_record(
                "Child",
                None,
                vec![(
                    "payload".into(),
                    FieldChange::Set(json!("x".repeat(32 * 1024))),
                )],
            )
            .unwrap();
        lmpg.create_relationship("HAS", root, child, vec![])
            .unwrap();
    }
    let mut parsed = parse_document(&json!({ "labels": ["Root"] })).unwrap();
    parsed.search.collects = vec![CollectSpec {
        steps: vec![],
        alias: "children".into(),
        rel_type: Some("HAS".into()),
        direction: Direction::Outgoing,
        target_labels: vec![],
        fields: vec![],
        nested: vec![],
        filter: None,
        order_by: vec![],
        skip: 0,
        unique: true,
        limit: None,
    }];
    let view = Arc::new(store.read_view().unwrap());
    let err = find_records_in_view(&lmpg, &parsed.search, &|| false, view).unwrap_err();
    assert!(
        matches!(err, QueryError::ResourceLimit(_) | QueryError::Storage(_)),
        "collect expansion must be charged against the working-set cap: {err:?}"
    );
}

#[test]
fn records_explicit_zero_budget_does_not_mean_unlimited() {
    // An exhausted request budget arrives as `Some(0)`. It must be a hard zero
    // cap, not silently disable the limit and admit the whole result set.
    let store = store_with_cap(0);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    seed(&lmpg);
    let parsed = parse_document(&json!({ "labels": ["Big"] })).unwrap();
    let view = Arc::new(store.read_view().unwrap());
    let err = rushdb_query::find_records_in_view_with_budget(
        &lmpg,
        &parsed.search,
        &|| false,
        view,
        Some(0),
    )
    .unwrap_err();
    assert!(
        matches!(err, QueryError::ResourceLimit(_)),
        "explicit zero budget must reject, got {err:?}"
    );
}

#[test]
fn collect_expansion_is_cumulative_across_page_records() {
    // `$collect` must be charged cumulatively: two roots whose collected
    // children together exceed the budget must fail even though each root
    // alone would fit the full remaining budget.
    use rushdb_query::{CollectSpec, Direction};
    let store = store_with_cap(96 * 1024);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    for _ in 0..2 {
        let root = lmpg.create_record("Root", None, vec![]).unwrap();
        let child = lmpg
            .create_record(
                "Child",
                None,
                vec![(
                    "payload".into(),
                    FieldChange::Set(json!("x".repeat(64 * 1024))),
                )],
            )
            .unwrap();
        lmpg.create_relationship("HAS", root, child, vec![])
            .unwrap();
    }
    let mut parsed = parse_document(&json!({ "labels": ["Root"] })).unwrap();
    parsed.search.collects = vec![CollectSpec {
        steps: vec![],
        alias: "children".into(),
        rel_type: Some("HAS".into()),
        direction: Direction::Outgoing,
        target_labels: vec![],
        fields: vec![],
        nested: vec![],
        filter: None,
        order_by: vec![],
        skip: 0,
        unique: true,
        limit: None,
    }];
    let view = Arc::new(store.read_view().unwrap());
    let err = find_records_in_view(&lmpg, &parsed.search, &|| false, view).unwrap_err();
    assert!(
        matches!(err, QueryError::ResourceLimit(_) | QueryError::Storage(_)),
        "collect expansion must be cumulative across records: {err:?}"
    );
}

#[test]
fn aggregate_applies_the_configured_limit() {
    let store = store_with_cap(128);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    let result = rushdb_query::aggregate_with_cancel_in_view(
        &lmpg,
        &rushdb_query::AggregateRequest::default(),
        &|| false,
        Arc::new(store.read_view().unwrap()),
    );
    assert!(matches!(result, Err(QueryError::ResourceLimit(_))));
}

/// Seeds `count` trips with a few scalar columns in one commit.
fn seed_trips(lmpg: &Lmpg, count: u32) {
    let rows: Vec<serde_json::Value> = (0..count)
        .map(|i| {
            json!({
                "vendor": if i % 3 == 0 { "A" } else { "B" },
                "hour": i % 24,
                "fare": f64::from(i % 50) + 0.5,
                "distance": f64::from(i % 7),
                "note": format!("trip {i}"),
            })
        })
        .collect();
    let options = rushdb_lmpg::import::ImportOptions::default();
    let plan = rushdb_lmpg::import::serialize_bfs(&json!(rows), "Trip", &options);
    rushdb_lmpg::import::import_bfs(lmpg, &plan, &options).unwrap();
}

#[test]
fn grouped_averages_over_many_records_fit_query_memory_max() {
    // The aggregate working set is the match ordinals, the row being
    // aggregated and the buckets, under `query-memory-max`. Before, the
    // candidate vector's *capacity* was charged twice over (16 B then 8 B
    // per slot) and tripled again for an `$avg`, so 30,000 matches needed
    // over 768 KiB and every row was rebuilt in full (the `note` column
    // included).
    let store = store_with_cap(600_000);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    seed_trips(&lmpg, 30_000);
    let request = rushdb_query::AggregateRequest {
        labels: vec!["Trip".into()],
        group_by: vec!["vendor".into()],
        avg_terms: vec![("dist".into(), "distance".into())],
        maxs: vec![("max_fare".into(), "fare".into())],
        counts: vec![("n".into(), "*".into())],
        ..Default::default()
    };
    let view = Arc::new(store.read_view().unwrap());
    let (buckets, total) =
        rushdb_query::aggregate_with_cancel_in_view(&lmpg, &request, &|| false, view).unwrap();
    assert_eq!(total, 30_000);
    assert_eq!(buckets.len(), 2);
    let a = buckets
        .iter()
        .find(|b| b.keys["vendor"] == json!("A"))
        .unwrap();
    assert_eq!(a.count_roots, 10_000);
    assert_eq!(a.maxs["max_fare"], json!(49.5));
    let expected_distance = (0..30_000u32)
        .filter(|i| i % 3 == 0)
        .map(|i| f64::from(i % 7))
        .sum::<f64>()
        / 10_000.0;
    assert_eq!(a.sums["dist"], json!(expected_distance));

    // A walk of the label holds no list of every member: each part reads
    // its own range of ordinals, so 30,000 records aggregate within 200 KB.
    // The cap still binds what a part holds: one part's members (8,192
    // ordinals, 64 KiB) do not fit 40 KB.
    for (cap, fits) in [(200_000, true), (40_000, false)] {
        let store = store_with_cap(cap);
        let lmpg = Lmpg::new(store.clone(), TenantId::single());
        seed_trips(&lmpg, 30_000);
        let view = Arc::new(store.read_view().unwrap());
        let outcome = rushdb_query::aggregate_with_cancel_in_view(&lmpg, &request, &|| false, view);
        match outcome {
            Ok((buckets, total)) => assert!(fits && total == 30_000 && buckets.len() == 2),
            Err(error) => assert!(
                !fits
                    && matches!(&error, QueryError::ResourceLimit(message)
                        if message.contains("query-memory-max")),
                "{error:?}"
            ),
        }
    }
}

#[test]
fn aggregate_limits_retained_groups_under_query_memory_max() {
    // Buckets are what an aggregate retains: 100 groups with 1 KiB keys do
    // not fit a 64 KiB working set, whether or not `skip` discards them.
    let store = store_with_cap(64 * 1024);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    for i in 0..100 {
        lmpg.create_record(
            "Groups",
            None,
            vec![(
                "group".into(),
                FieldChange::Set(json!(format!("{i}{}", "x".repeat(1024)))),
            )],
        )
        .unwrap();
    }
    let request = rushdb_query::AggregateRequest {
        labels: vec!["Groups".into()],
        group_by: vec!["group".into()],
        skip: 100,
        ..Default::default()
    };
    let view = Arc::new(store.read_view().unwrap());
    let result = rushdb_query::aggregate_with_cancel_in_view(&lmpg, &request, &|| false, view);
    assert!(
        matches!(result, Err(QueryError::ResourceLimit(_))),
        "{result:?}"
    );
}

#[test]
fn a_filtered_grouped_aggregate_reads_only_the_fields_it_names() {
    // The residual filter and the metrics read their properties by point
    // reads; a 64 KiB payload column is never decoded, so the working set
    // stays near the ordinals.
    let store = store_with_cap(96 * 1024);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    for i in 0..40 {
        lmpg.create_record(
            "Wide",
            None,
            vec![
                ("kind".into(), FieldChange::Set(json!(i % 2))),
                ("n".into(), FieldChange::Set(json!(i))),
                (
                    "payload".into(),
                    FieldChange::Set(json!("x".repeat(64 * 1024))),
                ),
            ],
        )
        .unwrap();
    }
    let parsed =
        parse_document(&json!({"labels": ["Wide"], "where": {"n": {"$gte": 20}}})).unwrap();
    let request = rushdb_query::AggregateRequest {
        labels: parsed.search.labels,
        where_: parsed.search.where_,
        group_by: vec!["kind".into()],
        sums: vec![("sum".into(), "n".into())],
        ..Default::default()
    };
    let view = Arc::new(store.read_view().unwrap());
    let (buckets, total) =
        rushdb_query::aggregate_with_cancel_in_view(&lmpg, &request, &|| false, view).unwrap();
    assert_eq!(total, 20);
    let even = buckets.iter().find(|b| b.keys["kind"] == json!(0)).unwrap();
    assert_eq!(
        even.sums["sum"],
        json!(f64::from((20..40).step_by(2).sum::<i32>()))
    );
}

#[test]
fn aggregate_collect_budget_covers_work_discarded_by_child_pagination() {
    use rushdb_query::{AggregateRequest, CollectSpec, Direction};
    let store = store_with_cap(0);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    let root = rushdb_lmpg::import::import_document(&lmpg, "Root", &json!({"n": 0})).unwrap();
    let child = rushdb_lmpg::import::import_document(
        &lmpg,
        "Child",
        &json!({"payload": "x".repeat(128 * 1024)}),
    )
    .unwrap();
    lmpg.create_relationship("HAS", root, child, vec![])
        .unwrap();
    let request = AggregateRequest {
        labels: vec!["Root".into()],
        collects: vec![CollectSpec {
            steps: vec![],
            alias: "children".into(),
            rel_type: Some("HAS".into()),
            direction: Direction::Outgoing,
            target_labels: vec![],
            fields: vec![],
            nested: vec![],
            filter: None,
            order_by: vec![],
            skip: 1,
            unique: true,
            limit: None,
        }],
        ..Default::default()
    };
    let view = Arc::new(store.read_view().unwrap());
    let (buckets, _) = rushdb_query::aggregate_in_view(&lmpg, &request, view.clone()).unwrap();
    let result = rushdb_query::aggregate_to_rows_in_view_with_budget(
        &lmpg,
        &view,
        &buckets,
        &request,
        Some(64 * 1024),
    );
    assert!(
        matches!(result, Err(QueryError::ResourceLimit(_))),
        "{result:?}"
    );
    let rows = rushdb_query::aggregate_to_rows_in_view_with_budget(
        &lmpg,
        &view,
        &buckets,
        &request,
        Some(8 * 1024 * 1024),
    )
    .unwrap();
    assert_eq!(rows[0]["children"], json!([]));
}

#[test]
fn the_response_budget_caps_the_page_not_the_working_set() {
    // READ-1: the request's response byte budget (the HTTP body limit) used
    // to cap the candidate and match ordinals too, so a label over ~2M
    // records refused even a 1-row page. The working set is capped by
    // `query-memory-max` alone, and it holds only the matches a page keeps:
    // candidates stream past and the total comes from the label counter.
    let read = |cap: u64, response_budget: usize, limit: u64| {
        let store = store_with_cap(cap);
        let lmpg = Lmpg::new(store.clone(), TenantId::single());
        seed(&lmpg);
        let parsed = parse_document(&json!({ "labels": ["Big"], "limit": limit })).unwrap();
        let view = Arc::new(store.read_view().unwrap());
        rushdb_query::find_records_in_view_with_budget(
            &lmpg,
            &parsed.search,
            &|| false,
            view,
            Some(response_budget),
        )
    };
    // 300 candidates, a 1-row page: fits a 1 KiB working set and a 1 KiB
    // response budget, with the exact total.
    let page = read(1024, 1024, 1).unwrap();
    assert_eq!(page.total_matched, 300);
    assert_eq!(page.records.len(), 1);
    // `query-memory-max` still binds what a page keeps: 300 ordinals are
    // 2,400 bytes.
    assert!(matches!(
        read(1024, 1024 * 1024, 1_000),
        Err(QueryError::ResourceLimit(_))
    ));
}

/// Seeds `count` `Doc` records (`doc_id`, `tenant`, a short `body`) in one
/// commit.
fn seed_docs(lmpg: &Lmpg, count: u32) {
    let rows: Vec<serde_json::Value> = (0..count)
        .map(|i| json!({ "doc_id": i, "tenant": i % 500, "body": format!("doc {i}") }))
        .collect();
    let options = rushdb_lmpg::import::ImportOptions::default();
    let plan = rushdb_lmpg::import::serialize_bfs(&json!(rows), "Doc", &options);
    rushdb_lmpg::import::import_bfs(lmpg, &plan, &options).unwrap();
}

/// What a transport does with a query document: an aggregate renders its
/// buckets, anything else is a records page; both under a `response`-byte
/// budget (the body limit), on a store whose `query-memory-max` is `cap`.
fn run_document(
    cap: u64,
    docs: u32,
    document: &serde_json::Value,
    response: usize,
) -> Result<(u64, Vec<serde_json::Value>), QueryError> {
    let store = store_with_cap(cap);
    let lmpg = Lmpg::new(store.clone(), TenantId::single());
    seed_docs(&lmpg, docs);
    run_in(&lmpg, &store, document, Some(response))
}

fn run_in(
    lmpg: &Lmpg,
    store: &Store,
    document: &serde_json::Value,
    response: Option<usize>,
) -> Result<(u64, Vec<serde_json::Value>), QueryError> {
    let parsed = parse_document(document).unwrap();
    let view = Arc::new(store.read_view().unwrap());
    if let Some(aggregate) = parsed.aggregate {
        let (buckets, total) = rushdb_query::aggregate_with_cancel_in_view(
            lmpg,
            &aggregate,
            &|| false,
            Arc::clone(&view),
        )?;
        let rows = rushdb_query::aggregate_to_rows_in_view_with_budget(
            lmpg, &view, &buckets, &aggregate, response,
        )?;
        return Ok((total, rows));
    }
    let page = rushdb_query::find_records_in_view_with_budget(
        lmpg,
        &parsed.search,
        &|| false,
        view,
        response,
    )?;
    Ok((page.total_matched, page.records))
}

const DEFAULT_QUERY_MEMORY_MAX: u64 = 256 * 1024 * 1024;

fn resource_message(result: Result<(u64, Vec<serde_json::Value>), QueryError>) -> String {
    match result {
        Err(QueryError::ResourceLimit(message)) => message,
        other => panic!("expected a resource limit, got {other:?}"),
    }
}

#[test]
fn a_projection_returns_every_row_its_real_bytes_allow() {
    // BENCH-9: a select of plain fields went to the aggregate executor,
    // which materialized every match and refused its rows on an estimate
    // of ~770 bytes per row, ~40 times their JSON. 20,000 `{"doc_id": n}`
    // rows are ~330 KB; they fit a 2 MiB response budget that the estimate
    // (15 MB) did not.
    let tenants: Vec<u32> = (0..500).collect();
    let document = json!({
        "labels": ["Doc"],
        "where": { "tenant": { "$in": tenants } },
        "select": { "doc_id": "$record.doc_id" },
        "limit": 100_000
    });
    let parsed = parse_document(&document).unwrap();
    assert!(parsed.aggregate.is_none(), "a projection is a listing");
    assert_eq!(
        parsed.search.projection,
        vec![("doc_id".to_owned(), "doc_id".to_owned())]
    );
    let (total, rows) =
        run_document(DEFAULT_QUERY_MEMORY_MAX, 20_000, &document, 2 * 1024 * 1024).unwrap();
    assert_eq!(total, 20_000);
    assert_eq!(rows.len(), 20_000);
    assert_eq!(rows[7], json!({ "doc_id": 7 }));
    let real: usize = rows.iter().map(|row| row.to_string().len()).sum();
    assert!(real < 512 * 1024, "{real} bytes of rows");
}

#[test]
fn an_aggregate_renders_rows_charged_by_their_real_bytes() {
    // A per-root aggregate (a metric beside a field, no `groupBy`) is one
    // row per record: its render is charged by each row's real JSON, not by
    // an estimate of every bucket up front.
    let document = json!({
        "labels": ["Doc"],
        "select": { "doc_id": "$record.doc_id", "n": { "$count": "*" } },
        "limit": 100_000
    });
    assert!(parse_document(&document).unwrap().aggregate.is_some());
    let (total, rows) =
        run_document(DEFAULT_QUERY_MEMORY_MAX, 20_000, &document, 2 * 1024 * 1024).unwrap();
    assert_eq!(total, 20_000);
    assert_eq!(rows.len(), 20_000);
    assert_eq!(rows[3], json!({ "doc_id": 3, "n": 1 }));
}

#[test]
fn a_budget_error_names_the_limit_that_tripped() {
    let projection = json!({
        "labels": ["Doc"],
        "select": { "doc_id": "$record.doc_id" },
        "limit": 100_000
    });
    let aggregate = json!({
        "labels": ["Doc"],
        "select": { "doc_id": "$record.doc_id", "n": { "$count": "*" } },
        "limit": 100_000
    });
    for document in [&projection, &aggregate] {
        // The rows (~40 KB) do not fit a 16 KiB response.
        let response = resource_message(run_document(
            DEFAULT_QUERY_MEMORY_MAX,
            2_000,
            document,
            16 * 1024,
        ));
        assert!(
            response.contains("response size limit (16384 bytes)")
                && response.contains("--body-limit")
                && !response.contains("query-memory-max"),
            "{response}"
        );
        // 2,000 match ordinals do not fit a 4 KiB working set.
        let working = resource_message(run_document(4096, 2_000, document, 1024 * 1024));
        assert!(
            working.contains("query-memory-max (4096 bytes)")
                && working.contains("--query-memory-max")
                && !working.contains("body-limit"),
            "{working}"
        );
    }
}

#[test]
fn a_projection_page_stops_reading_after_its_rows() {
    // Without an order, a projection reads its page and stops: the total
    // comes from the label counter, so 20 times the matches costs about the
    // same reads. The aggregate executor read every match.
    let reads = |docs: u32| -> u64 {
        let document = json!({
            "labels": ["Doc"],
            "select": { "doc_id": "$record.doc_id" },
            "limit": 10
        });
        // Reads are counted by the durable backend.
        let path = rushdb_storage::TempPath::store("projection-reads");
        let store = Arc::new(Store::open(&path).unwrap());
        let lmpg = Lmpg::new(store.clone(), TenantId::single());
        seed_docs(&lmpg, docs);
        // Accounting is switched on and left on: other tests in this binary
        // may flip the global flag, and this thread's own tally is checked.
        rushdb_storage::read_accounting::enable(true);
        let _ = rushdb_storage::read_accounting::take_local();
        let (total, rows) = run_in(&lmpg, &store, &document, None).unwrap();
        let reads = rushdb_storage::read_accounting::take_local().values().sum();
        assert_eq!(total, u64::from(docs));
        assert_eq!(rows.len(), 10);
        assert_eq!(rows[0], json!({ "doc_id": 0 }));
        drop((lmpg, store));
        rushdb_storage::remove_store(&path).unwrap();
        reads
    };
    let small = reads(1_000);
    let large = reads(20_000);
    assert!(small > 0);
    assert!(
        large < small.saturating_mul(2),
        "1K matches: {small} reads; 20K matches: {large} reads"
    );
}