aedb 0.3.1

Embedded Rust storage engine with transactional commits, WAL durability, and snapshot-consistent reads
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
use super::execute_query_with_options;
use super::tests::setup;
use crate::catalog::schema::ColumnDef;
use crate::catalog::types::{ColumnType, Row, Value};
use crate::query::error::QueryError;
use crate::query::plan::{Aggregate, Order, Query, QueryOptions, col, lit};

#[test]
fn bounded_scan_is_enforced_when_full_scan_not_allowed() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let err = execute_query_with_options(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["*"]).from("users"),
        &QueryOptions::default(),
        1,
        10_000,
        None,
    )
    .expect_err("should reject full scan");
    assert!(matches!(err, QueryError::InvalidQuery { .. }));
}

#[test]
fn default_execute_query_rejects_unbounded_full_scan() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let err = super::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["*"]).from("users"),
    )
    .expect_err("default execute_query should reject unbounded full scan");
    assert!(matches!(err, QueryError::InvalidQuery { .. }));
}

#[test]
fn non_join_page_size_is_capped_by_max_scan_rows() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = execute_query_with_options(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["*"])
            .from("users")
            .order_by("id", Order::Asc)
            .limit(50),
        &QueryOptions::default(),
        9,
        10,
        None,
    )
    .expect("bounded page");
    assert_eq!(result.rows.len(), 10);
    assert!(result.cursor.is_some());
    assert!(result.rows_examined <= 100);
}

#[test]
fn aggregate_limit_does_not_bypass_scan_bound() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let err = execute_query_with_options(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["*"])
            .from("users")
            .aggregate(Aggregate::Count)
            .limit(1),
        &QueryOptions::default(),
        7,
        10,
        None,
    )
    .expect_err("aggregate should still honor scan bound");
    assert!(matches!(err, QueryError::ScanBoundExceeded { .. }));
}

#[test]
fn cursor_does_not_bypass_scan_bound() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let first = execute_query_with_options(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["*"])
            .from("users")
            .order_by("id", Order::Asc)
            .limit(5),
        &QueryOptions::default(),
        9,
        100,
        None,
    )
    .expect("first page");
    let err = execute_query_with_options(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["*"])
            .from("users")
            .order_by("id", Order::Asc)
            .limit(5),
        &QueryOptions {
            cursor: first.cursor,
            ..QueryOptions::default()
        },
        9,
        10,
        None,
    )
    .expect_err("cursor path should still honor scan bound");
    assert!(matches!(err, QueryError::ScanBoundExceeded { .. }));
}

#[test]
fn join_scan_bound_uses_cardinality_aware_estimate_when_right_side_is_primary_key() {
    let (mut keyspace, mut catalog) = setup();
    catalog
        .create_table(
            "A",
            "app",
            "profiles",
            vec![
                ColumnDef {
                    name: "user_id".into(),
                    col_type: ColumnType::Integer,
                    nullable: false,
                },
                ColumnDef {
                    name: "country".into(),
                    col_type: ColumnType::Text,
                    nullable: false,
                },
            ],
            vec!["user_id".into()],
        )
        .expect("profiles table");
    for i in 0..50 {
        keyspace.upsert_row(
            "A",
            "app",
            "profiles",
            vec![Value::Integer(i)],
            Row::from_values(vec![Value::Integer(i), Value::Text("US".into())]),
            1,
        );
    }
    let snapshot = keyspace.snapshot();
    let result = execute_query_with_options(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["u.id", "p.country"])
            .from("users")
            .alias("u")
            .inner_join("profiles", "u.id", "user_id")
            .with_last_join_alias("p")
            .limit(10),
        &QueryOptions::default(),
        1,
        1_000,
        None,
    )
    .expect("primary-key join should stay within scan bound");
    assert_eq!(result.rows.len(), 10);
    assert!(result.rows_examined <= 50);
}

/// A pure-window scan (LIMIT, no cursor/order/filter) may stop reading at the
/// page window, but the page itself must be identical to full evaluation:
/// primary-key order with OFFSET applied before LIMIT.
#[test]
fn pure_window_scan_returns_exact_page() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"]).from("users").offset(5).limit(10),
    )
    .expect("windowed scan");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    assert_eq!(ids, (5..15).collect::<Vec<_>>());
    assert!(result.truncated);
}

/// A residually-filtered scan must stay unbounded: the filter drops source
/// rows, so stopping at the page window would under-fill the page. `email`
/// has no index, forcing the residual full-scan path.
#[test]
fn residual_filtered_scan_is_not_window_bounded() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .where_(col("email").like(lit("%@example.com")))
            .limit(5),
    )
    .expect("residual scan");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    // Odd ids carry @example.com addresses; a wrongly bounded source would
    // only surface the first few.
    assert_eq!(ids, vec![1, 3, 5, 7, 9]);
    assert!(result.truncated);
}

/// A residually-filtered index lookup hydrates rows lazily but must still
/// produce the exact page full evaluation would: candidates stream in index
/// (name-lexicographic) order and the residual LIKE filter plus LIMIT pick
/// the first matches.
#[test]
fn residual_index_scan_returns_exact_page_via_lazy_hydration() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .where_(col("name").like(lit("u%7%")))
            .limit(5),
    )
    .expect("residual index scan");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    // by_name index order is lexicographic: u17 < u27 < u37 < u47 < u57 are
    // the first names containing a '7'.
    assert_eq!(ids, vec![17, 27, 37, 47, 57]);
    assert!(result.truncated);
}

/// A row-fetch failure on a candidate the page actually needs must surface as
/// a query error, not truncate the result. A spilled row with no value store
/// attached makes the fetch fail deterministically.
#[test]
fn lazy_hydration_surfaces_fetch_errors_for_needed_rows() {
    let (mut keyspace, catalog) = setup();
    let table = keyspace
        .table_by_namespace_key_mut(&crate::catalog::namespace_key("A", "app"), "users")
        .expect("users table");
    table.rows.insert(
        crate::storage::encoded_key::EncodedKey::from_values(&[Value::Integer(37)]),
        crate::storage::keyspace::StoredRow::spilled_versioned(
            1,
            crate::storage::value_store::PersistentValueRef {
                offset: 0,
                len: 8,
                blake3_hash: [0; 32],
            },
        ),
    );
    let snapshot = keyspace.snapshot();
    // u37 is the third residual match; a LIMIT 5 page needs it.
    let err = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .where_(col("name").like(lit("u%7%")))
            .limit(5),
    )
    .expect_err("fetch of poisoned row must fail the query");
    assert!(
        format!("{err:?}").contains("value store"),
        "unexpected error: {err:?}"
    );
}

/// A page fully satisfied before the failing candidate succeeds: lazy
/// hydration never touches rows past the pipeline's stop point (the same
/// contract as the window-bounded scan paths).
#[test]
fn lazy_hydration_skips_unneeded_rows_entirely() {
    let (mut keyspace, catalog) = setup();
    let table = keyspace
        .table_by_namespace_key_mut(&crate::catalog::namespace_key("A", "app"), "users")
        .expect("users table");
    table.rows.insert(
        crate::storage::encoded_key::EncodedKey::from_values(&[Value::Integer(37)]),
        crate::storage::keyspace::StoredRow::spilled_versioned(
            1,
            crate::storage::value_store::PersistentValueRef {
                offset: 0,
                len: 8,
                blake3_hash: [0; 32],
            },
        ),
    );
    let snapshot = keyspace.snapshot();
    // LIMIT 1 needs u17 plus the u27 lookahead — u37 is never fetched.
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .where_(col("name").like(lit("u%7%")))
            .limit(1),
    )
    .expect("page satisfied before poisoned row");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    assert_eq!(ids, vec![17]);
    assert!(result.truncated);
}

/// A residual match past the first candidate chunk (64 postings) must still be
/// found: the chunked stream advances its window until the index range is
/// exhausted. `u99` is the only name matching `u%99%` and sits near the end of
/// the 100-entry name-ordered candidate range.
#[test]
fn chunked_residual_stream_finds_matches_past_first_chunk() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .where_(col("name").like(lit("u%99%")))
            .limit(5),
    )
    .expect("chunked residual scan");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    assert_eq!(ids, vec![99]);
    assert!(!result.truncated);
    assert!(result.cursor.is_none());
}

/// A residual pattern with no matches drains the whole candidate range and
/// returns an empty, non-truncated result.
#[test]
fn chunked_residual_stream_handles_zero_matches() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .where_(col("name").like(lit("u%zzz%")))
            .limit(5),
    )
    .expect("no-match residual scan");
    assert!(result.rows.is_empty());
    assert!(!result.truncated);
}

/// Read-set-capturing queries take the eager candidate path (touched pks must
/// be recorded up front) but must return the same page as the streamed path.
#[test]
fn capturing_residual_query_matches_streamed_page() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let query = || {
        Query::select(&["id"])
            .from("users")
            .where_(col("name").like(lit("u%7%")))
            .limit(5)
    };
    let streamed = super::tests::execute_query(&snapshot, &catalog, "A", "app", query())
        .expect("streamed page");
    let mut collector = crate::query::executor::ReadSetCollector::new();
    let captured = crate::query::executor::execute_query_with_options_capturing(
        crate::query::executor::CapturingQueryExecutionRequest {
            snapshot: &snapshot,
            catalog: &catalog,
            project_id: "A",
            scope_id: "app",
            query: query(),
            options: &QueryOptions {
                allow_full_scan: true,
                ..QueryOptions::default()
            },
            snapshot_seq: 0,
            max_scan_rows: usize::MAX,
            read_set: Some(&mut collector),
        },
    )
    .expect("capturing page");
    assert_eq!(streamed.rows, captured.rows);
    assert_eq!(streamed.truncated, captured.truncated);
    // The streamed capture is coarse (whole-table range): it never enumerates
    // the full candidate set, so invalidation must come from a range record.
    let read_set = collector.into_inner();
    assert!(
        !read_set.ranges.is_empty(),
        "capturing residual query must record an invalidation range"
    );
}

/// COUNT(*) with no predicate must not depend on row contents: the synthetic
/// count source must produce the identical result (value, rows_examined) to
/// the content-scanning path, forced here via an always-true predicate.
#[test]
fn count_star_matches_scanning_aggregate() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let fast = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["count_star"])
            .from("users")
            .aggregate(Aggregate::Count),
    )
    .expect("count fast path");
    let scanned = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["count_star"])
            .from("users")
            .where_(col("age").gte(lit(i64::MIN)))
            .aggregate(Aggregate::Count),
    )
    .expect("count via scan");
    assert_eq!(fast.rows, scanned.rows);
    assert_eq!(fast.rows_examined, scanned.rows_examined);
    assert_eq!(fast.rows.len(), 1);
}

/// The synthetic count source must preserve the scan-bound guard: a COUNT(*)
/// (full evaluation, no LIMIT) over a table larger than max_scan_rows is
/// rejected exactly as the scanning path rejects it.
#[test]
fn count_star_preserves_scan_bound_guard() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let err = execute_query_with_options(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["count_star"])
            .from("users")
            .aggregate(Aggregate::Count),
        &QueryOptions::default(),
        0,
        50,
        None,
    )
    .expect_err("count over max_scan_rows must be rejected");
    match err {
        QueryError::ScanBoundExceeded {
            estimated_rows,
            max_scan_rows,
        } => {
            assert_eq!(estimated_rows, 100);
            assert_eq!(max_scan_rows, 50);
        }
        other => panic!("unexpected error: {other:?}"),
    }
}

/// IN over the primary key resolves to point probes: ascending-PK output
/// order, collapsed duplicates, and skipped missing keys — identical to the
/// scan-and-filter result — while examining only the probed rows.
#[test]
fn pk_in_probes_match_scan_semantics() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .where_(col("id").in_(vec![
                lit(7_i64),
                lit(3_i64),
                lit(3_i64),
                lit(99_i64),
                lit(1000_i64),
            ]))
            .limit(10),
    )
    .expect("pk in probe");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    assert_eq!(ids, vec![3, 7, 99]);
    assert!(
        result.rows_examined <= 4,
        "IN over the pk must probe, not scan: rows_examined = {}",
        result.rows_examined
    );
}

/// A mixed-type IN list must keep the coercing scan semantics (U64 5 matches
/// the Integer pk 5 via compare_values), which type-tagged probes cannot
/// honor — so the probe gate has to fall back to the scan.
#[test]
fn pk_in_mixed_types_fall_back_to_coercing_scan() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .where_(col("id").in_(vec![Value::U64(5), Value::Integer(7)]))
            .limit(10),
    )
    .expect("mixed-type in");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    // U64(5) must still match the Integer pk 5 through comparison coercion.
    assert_eq!(ids, vec![5, 7]);
}

/// ORDER BY pk DESC LIMIT walks the PK map in reverse and stops at the page
/// window instead of scanning and sorting the whole table.
#[test]
fn pk_ordered_desc_scan_returns_bounded_latest_page() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .order_by("id", Order::Desc)
            .limit(5),
    )
    .expect("desc pk scan");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    assert_eq!(ids, vec![99, 98, 97, 96, 95]);
    assert!(result.truncated);
    assert!(result.cursor.is_some());
    assert!(
        result.rows_examined <= 6,
        "desc pk scan must stop at the window: rows_examined = {}",
        result.rows_examined
    );
}

/// The PK-ordered walk applies OFFSET in-walk without materializing skipped
/// rows, matching the general path's page exactly.
#[test]
fn pk_ordered_scan_applies_offset_in_walk() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id"])
            .from("users")
            .order_by("id", Order::Asc)
            .offset(10)
            .limit(5),
    )
    .expect("offset pk scan");
    let ids: Vec<i64> = result
        .rows
        .iter()
        .map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        })
        .collect();
    assert_eq!(ids, (10..15).collect::<Vec<_>>());
}

/// Descending cursor pages seek and cover the whole table exactly once, in
/// reverse PK order, without re-examining prior pages.
#[test]
fn pk_ordered_desc_cursor_walk_covers_all_rows() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let mut collected: Vec<i64> = Vec::new();
    let mut cursor: Option<String> = None;
    for page in 0..30 {
        let result = execute_query_with_options(
            &snapshot,
            &catalog,
            "A",
            "app",
            Query::select(&["id"])
                .from("users")
                .order_by("id", Order::Desc)
                .limit(7),
            &QueryOptions {
                cursor: cursor.take(),
                allow_full_scan: true,
                ..QueryOptions::default()
            },
            3,
            usize::MAX,
            None,
        )
        .expect("desc cursor page");
        if page > 0 {
            assert!(
                result.rows_examined <= 8,
                "cursor page must seek, not rescan: rows_examined = {}",
                result.rows_examined
            );
        }
        collected.extend(result.rows.iter().map(|r| match r.values[0] {
            Value::Integer(id) => id,
            ref other => panic!("unexpected id value: {other:?}"),
        }));
        cursor = result.cursor;
        if cursor.is_none() {
            break;
        }
    }
    assert_eq!(collected, (0..100).rev().collect::<Vec<_>>());
}

/// The bounded PK-ordered walk must return the identical page to full
/// evaluation, forced here through an always-true indexed predicate (which
/// routes the query through the general candidate + sort machinery).
#[test]
fn pk_ordered_desc_matches_general_path() {
    let (keyspace, catalog) = setup();
    let snapshot = keyspace.snapshot();
    let fast = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id", "name"])
            .from("users")
            .order_by("id", Order::Desc)
            .limit(12),
    )
    .expect("bounded walk");
    let general = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["id", "name"])
            .from("users")
            .where_(col("age").gte(lit(i64::MIN)))
            .order_by("id", Order::Desc)
            .limit(12),
    )
    .expect("general path");
    assert_eq!(fast.rows, general.rows);
    assert_eq!(fast.truncated, general.truncated);
}

/// COUNT(*) over a table carrying tombstones takes the key-only tier merge
/// and must not count tombstone-only keys.
#[test]
fn count_star_ignores_tombstone_only_keys() {
    let (mut keyspace, catalog) = setup();
    let table = keyspace
        .table_by_namespace_key_mut(&crate::catalog::namespace_key("A", "app"), "users")
        .expect("users table");
    table.row_tombstones.insert(
        crate::storage::encoded_key::EncodedKey::from_values(&[Value::Integer(1000)]),
        1,
    );
    let snapshot = keyspace.snapshot();
    let result = super::tests::execute_query(
        &snapshot,
        &catalog,
        "A",
        "app",
        Query::select(&["count_star"])
            .from("users")
            .aggregate(Aggregate::Count),
    )
    .expect("count with tombstones");
    assert_eq!(result.rows.len(), 1);
    assert_eq!(result.rows_examined, 100);
}