infino 0.1.0

A fast retrieval engine that stores data on object storage and runs SQL, full-text search, and vector search over it from a single system — search-on-Parquet.
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Infino Authors

//! List-level skip pruning — reader-side.
//!
//! Walks a `Manifest`'s `parts` and applies the
//! aggregate skip tests in [`ManifestPartEntry`] to identify
//! candidate parts for a given query shape. Survivors are
//! the parts the query layer should load (via
//! [`Manifest::part`]) for per-superfile pruning.
//!
//! These functions are standalone — they don't depend on
//! the in-memory `Manifest` or its `ManifestPartLoader`.
//! That keeps them testable in isolation and lets the
//! query-layer integration choose its own loading shape.
//!
//! ## Correctness invariants
//!
//! - **Monotonic**: every part the flat (superfile-level) prune
//!   would visit is also a survivor here. Aggregate
//!   summaries are constructed to over-approximate the union
//!   of superfile-level skip data, so a query that matches any
//!   superfile in a part necessarily matches the part's
//!   aggregate.
//! - **"Always-keep" defaults**: parts with empty `*_agg`
//!   entries for the queried column trivially survive (e.g.
//!   pre-aggregate manifests, or entries where a particular
//!   column has no info).
//!
//! [`Manifest`]: super::Manifest
//! [`Manifest::part`]: super::Manifest::part
//! [`Manifest`]: super::list::Manifest
//! [`ManifestPartEntry`]: super::list::ManifestPartEntry

use crate::{
    superfile::fts::reader::BoolMode,
    supertable::manifest::{
        list::{Manifest, ManifestPartEntry},
        part::PartId,
    },
};

/// Filter the list's parts to those whose
/// `fts_summary_agg[column].term_range` overlaps the prefix
/// `[prefix, prefix_upper_bound)`.
///
/// Parts without an `fts_summary_agg` entry for this column
/// (no info) survive — same "always-keep" treatment the
/// list-level pruner gives to missing aggregates.
pub fn prune_parts_for_fts_prefix(list: &Manifest, column: &str, prefix: &[u8]) -> Vec<PartId> {
    let upper = prefix_upper_bound(prefix);
    list.parts
        .iter()
        .filter_map(|entry| {
            if part_overlaps_prefix(entry, column, prefix, upper.as_deref()) {
                Some(entry.part_id)
            } else {
                None
            }
        })
        .collect()
}

fn part_overlaps_prefix(
    entry: &ManifestPartEntry,
    column: &str,
    prefix: &[u8],
    upper: Option<&[u8]>,
) -> bool {
    let Some(agg) = entry.fts_summary_agg.get(column) else {
        // No info → always-keep.
        return true;
    };
    let Some((min_term, max_term)) = agg.term_range.as_ref() else {
        // Every superfile had an empty FST for this column;
        // nothing to match. Skip.
        return false;
    };
    // Overlap check: [prefix, upper) intersects [min_term, max_term]
    // iff prefix <= max_term && (upper is None || min_term < upper).
    if prefix > max_term.as_slice() {
        return false;
    }
    !matches!(upper, Some(u) if min_term.as_slice() >= u)
}

/// Compute the lex-upper-bound for a prefix: the smallest
/// byte string that doesn't start with `prefix`. `None`
/// signals "no upper bound" (e.g., a prefix of all 0xFF
/// bytes — every byte string starts with that or has no
/// successor in lex order).
///
/// `[prefix, prefix_upper_bound())` is the set of all byte
/// strings starting with `prefix`.
fn prefix_upper_bound(prefix: &[u8]) -> Option<Vec<u8>> {
    let mut out = prefix.to_vec();
    while let Some(&b) = out.last() {
        if b == 0xff {
            out.pop();
        } else {
            *out.last_mut().expect("non-empty") = b + 1;
            return Some(out);
        }
    }
    None
}

/// Filter the list's parts to those whose
/// `fts_summary_agg[column].term_bloom` allows at least one query
/// term (mode = Or) or all of them (mode = And) — i.e. the
/// list-level analogue of superfile-level `fts_bloom_skip`.
///
/// Parts without a bloom union entry for this column (e.g.,
/// pre-aggregate manifests or aggregates that fell back
/// to "no info" due to a shape mismatch) survive — same
/// always-keep treatment as the rest of `list_prune`. An
/// empty `query_terms` slice yields an empty mask; callers
/// should special-case that upstream.
///
/// Used by `bm25_search` (exact-term) to prune entire parts
/// before lazy-loading. Complements
/// `prune_parts_for_fts_prefix` (which uses term-range
/// overlap on prefix queries) and superfile-level
/// `fts_bloom_skip` (applied after a part is loaded).
pub fn prune_parts_for_fts_terms(
    list: &Manifest,
    column: &str,
    query_terms: &[&str],
    mode: BoolMode,
) -> Vec<PartId> {
    if query_terms.is_empty() {
        return Vec::new();
    }
    list.parts
        .iter()
        .filter_map(|entry| {
            if part_matches_terms(entry, column, query_terms, mode) {
                Some(entry.part_id)
            } else {
                None
            }
        })
        .collect()
}

fn part_matches_terms(
    entry: &ManifestPartEntry,
    column: &str,
    query_terms: &[&str],
    mode: BoolMode,
) -> bool {
    let Some(agg) = entry.fts_summary_agg.get(column) else {
        return true; // no info → always-keep
    };
    let Some(bloom) = agg.term_bloom.as_ref() else {
        // No bloom info → always-keep (correctness over selectivity).
        return true;
    };
    match mode {
        BoolMode::Or => query_terms.iter().any(|t| bloom.contains(t.as_bytes())),
        BoolMode::And => query_terms.iter().all(|t| bloom.contains(t.as_bytes())),
    }
}

/// Filter the list's parts to those whose `id_range`
/// overlaps the inclusive range `[query_min, query_max]`.
///
/// The id column is `Decimal128(38, 0)` (the supertable-
/// injected `_id` column), so this is the type-specialized
/// hot path for `WHERE _id BETWEEN ? AND ?`. For other
/// scalar columns, use [`prune_parts_for_scalar_min_max_bytes`].
pub fn prune_parts_for_id_range(list: &Manifest, query_min: i128, query_max: i128) -> Vec<PartId> {
    list.parts
        .iter()
        .filter_map(|entry| {
            let (lo, hi) = entry.id_range;
            // `(query_min, query_max)` overlaps `(lo, hi)` iff
            // query_min <= hi && query_max >= lo.
            if query_min <= hi && query_max >= lo {
                Some(entry.part_id)
            } else {
                None
            }
        })
        .collect()
}

/// Filter parts whose `vector_summary_agg[column]` envelope
/// can possibly contain a vector within `query_cutoff` of
/// `query`. Conservative: a part survives iff
/// `distance(query, envelope_center) ≤ envelope_radius +
/// query_cutoff`. Parts with no vector summary for this
/// column survive (no info).
///
/// Distance is L2; for cosine workloads, the query vector + centroids
/// should be normalized at the caller layer (matching the convention the
/// superfile-level vector skip already uses).
pub fn prune_parts_for_vector(
    list: &Manifest,
    column: &str,
    query: &[f32],
    query_cutoff: f32,
) -> Vec<PartId> {
    list.parts
        .iter()
        .filter_map(|entry| {
            let Some(agg) = entry.vector_summary_agg.get(column) else {
                return Some(entry.part_id);
            };
            if agg.centroid_envelope.is_empty() {
                // Empty envelope — no info; keep.
                return Some(entry.part_id);
            }
            let envelope = decode_centroid_envelope(&agg.centroid_envelope);
            if envelope.len() != query.len() {
                // Dim mismatch — keep (the per-superfile prune
                // will reject correctly).
                return Some(entry.part_id);
            }
            let dist = l2_distance(query, &envelope);
            if dist <= agg.envelope_radius + query_cutoff {
                Some(entry.part_id)
            } else {
                None
            }
        })
        .collect()
}

fn decode_centroid_envelope(bytes: &[u8]) -> Vec<f32> {
    let dim = bytes.len() / 4;
    let mut out = Vec::with_capacity(dim);
    for i in 0..dim {
        let s = i * 4;
        out.push(f32::from_le_bytes([
            bytes[s],
            bytes[s + 1],
            bytes[s + 2],
            bytes[s + 3],
        ]));
    }
    out
}

fn l2_distance(a: &[f32], b: &[f32]) -> f32 {
    let mut sum = 0.0_f32;
    for i in 0..a.len().min(b.len()) {
        let d = a[i] - b[i];
        sum += d * d;
    }
    sum.sqrt()
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, sync::Arc};

    use arrow_array::{ArrayRef, Decimal128Array, Int64Array};
    use uuid::Uuid;

    use super::*;
    use crate::supertable::{
        FtsSummaryAgg, ScalarStatsAgg, SuperfileEntry, SuperfileUri, VectorSummary,
        manifest::{
            ClusterCentroids, aggregates,
            bloom::BloomBuilder,
            list::{FORMAT_VERSION, PartitionStrategy},
            part::ContentHash,
        },
    };

    #[test]
    fn prefix_upper_bound_basic() {
        assert_eq!(prefix_upper_bound(b"abc"), Some(b"abd".to_vec()));
        assert_eq!(prefix_upper_bound(b"ab\xff"), Some(b"ac".to_vec()));
        assert_eq!(prefix_upper_bound(b"\xff\xff"), None);
        assert_eq!(prefix_upper_bound(b""), None);
    }

    // ---- Helpers for the aggregates::compute and
    //      prune_parts_for_* tests below.

    fn seg(
        id_min: i128,
        id_max: i128,
        title_terms: &[&str],
        vec_centroid: Option<Vec<f32>>,
        vec_radius: f32,
    ) -> Arc<SuperfileEntry> {
        let id = Uuid::new_v4();
        let mut fts = HashMap::new();
        if !title_terms.is_empty() {
            let mut bloom = BloomBuilder::with_n_blocks(16);
            for t in title_terms {
                bloom.insert(t.as_bytes());
            }
            let term_range = {
                let mut sorted = title_terms
                    .iter()
                    .map(|t| t.as_bytes().to_vec())
                    .collect::<Vec<_>>();
                sorted.sort();
                (
                    sorted.first().cloned().unwrap_or_default(),
                    sorted.last().cloned().unwrap_or_default(),
                )
            };
            fts.insert(
                "title".into(),
                FtsSummaryAgg::new_with_params(
                    bloom.finish(),
                    title_terms.len() as u32,
                    term_range,
                ),
            );
        }
        let mut vec_summary = HashMap::new();
        if let Some(c) = vec_centroid {
            vec_summary.insert(
                "emb".into(),
                VectorSummary {
                    centroid: c,
                    radius: vec_radius,
                    clusters: ClusterCentroids::empty(),
                },
            );
        }
        Arc::new(SuperfileEntry {
            superfile_id: id,
            uri: SuperfileUri(id),
            n_docs: ((id_max - id_min) + 1) as u64,
            id_min,
            id_max,
            scalar_stats: HashMap::new(),
            fts_summary: fts,
            vector_summary: vec_summary,
            partition_key: Vec::new(),
            partition_hint: None,
            subsection_offsets: None,
        })
    }

    fn entry_from_superfiles(superfiles: &[Arc<SuperfileEntry>], seed: u8) -> ManifestPartEntry {
        let aggs = aggregates::compute(superfiles, None);
        ManifestPartEntry {
            part_id: PartId(Uuid::from_bytes([seed; 16])),
            uri: format!("manifests/part-{seed:02x}.avro.zst"),
            n_superfiles: superfiles.len() as u64,
            size_bytes_compressed: 1024,
            size_bytes_uncompressed: 4096,
            content_hash: ContentHash([seed; 32]),
            partition_key: Vec::new(),
            id_range: aggs.id_range,
            scalar_stats_agg: aggs.scalar_stats_agg,
            fts_summary_agg: aggs.fts_summary_agg,
            vector_summary_agg: aggs.vector_summary_agg,
        }
    }

    fn list_with(entries: Vec<ManifestPartEntry>) -> Manifest {
        Manifest {
            format_version: FORMAT_VERSION.into(),
            manifest_id: 1,
            options_hash: ContentHash([0u8; 32]),
            schema: Vec::new(),
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 64,
            },
            parts: entries,
        }
    }

    // ---- aggregates::compute — value correctness.

    #[test]
    fn aggregates_compute_empty_returns_default() {
        let aggs = aggregates::compute(&[], None);
        assert_eq!(aggs.id_range, (0, 0));
        assert!(aggs.scalar_stats_agg.is_empty());
        assert!(aggs.fts_summary_agg.is_empty());
        assert!(aggs.vector_summary_agg.is_empty());
    }

    #[test]
    fn aggregates_compute_id_range_is_min_max_across_superfiles() {
        let s_a = seg(100, 199, &["alpha"], None, 0.0);
        let s_b = seg(0, 99, &["beta"], None, 0.0);
        let s_c = seg(500, 599, &["gamma"], None, 0.0);
        let aggs = aggregates::compute(&[s_a, s_b, s_c], None);
        assert_eq!(aggs.id_range, (0, 599));
    }

    #[test]
    fn aggregates_compute_fts_term_range_union() {
        // Three superfiles with different term ranges; the
        // empty-FST one contributes nothing to the union.
        let s_a = seg(0, 10, &["alpha", "bravo", "charlie"], None, 0.0);
        let s_b = seg(11, 20, &["bravo", "charlie", "delta"], None, 0.0);
        let id = Uuid::new_v4();
        let mut empty_fts = HashMap::new();
        empty_fts.insert(
            "title".into(),
            FtsSummaryAgg::new_with_params(
                BloomBuilder::with_n_blocks(16).finish(),
                0,
                (Vec::new(), Vec::new()),
            ),
        );
        let s_c = Arc::new(SuperfileEntry {
            superfile_id: id,
            uri: SuperfileUri(id),
            n_docs: 5,
            id_min: 21,
            id_max: 25,
            scalar_stats: HashMap::new(),
            fts_summary: empty_fts,
            vector_summary: HashMap::new(),
            partition_key: Vec::new(),
            partition_hint: None,
            subsection_offsets: None,
        });

        let aggs = aggregates::compute(&[s_a, s_b, s_c], None);
        let fts_agg = aggs.fts_summary_agg.get("title").expect("title agg");
        let (mn, mx) = fts_agg.term_range.as_ref().expect("range");
        assert_eq!(mn, b"alpha", "min of mins across non-empty FSTs");
        assert_eq!(mx, b"delta", "max of maxes across non-empty FSTs");
    }

    #[test]
    fn aggregates_compute_fts_all_empty_yields_none_range() {
        let id = Uuid::new_v4();
        let mut empty_fts = HashMap::new();
        empty_fts.insert(
            "title".into(),
            FtsSummaryAgg::new_with_params(
                BloomBuilder::with_n_blocks(16).finish(),
                0,
                (Vec::new(), Vec::new()),
            ),
        );
        let s = Arc::new(SuperfileEntry {
            superfile_id: id,
            uri: SuperfileUri(id),
            n_docs: 0,
            id_min: 0,
            id_max: 0,
            scalar_stats: HashMap::new(),
            fts_summary: empty_fts,
            vector_summary: HashMap::new(),
            partition_key: Vec::new(),
            partition_hint: None,
            subsection_offsets: None,
        });

        let aggs = aggregates::compute(&[s], None);
        // Column not in the map (skipped entirely) — list-
        // level pruner treats this as "no info, always-keep".
        assert!(
            !aggs.fts_summary_agg.contains_key("title")
                || aggs
                    .fts_summary_agg
                    .get("title")
                    .expect("agg")
                    .term_range
                    .is_none()
        );
    }

    #[test]
    fn aggregates_compute_vector_envelope_bounds_all_superfile_balls() {
        let s_a = seg(0, 10, &[], Some(vec![1.0, 0.0, 0.0]), 0.5);
        let s_b = seg(11, 20, &[], Some(vec![0.0, 1.0, 0.0]), 0.5);
        let aggs = aggregates::compute(&[s_a.clone(), s_b.clone()], None);
        let v = aggs.vector_summary_agg.get("emb").expect("vec agg");
        let mean = [0.5, 0.5, 0.0];
        // Each superfile's centroid is ~0.707 from the mean; +
        // radius 0.5 → envelope_radius >= 1.207.
        assert!(
            v.envelope_radius >= 1.207 - 0.01,
            "envelope radius must dominate each seg ball; got {}",
            v.envelope_radius
        );
        let decoded: Vec<f32> = v
            .centroid_envelope
            .chunks_exact(4)
            .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
            .collect();
        assert_eq!(decoded.len(), 3);
        for (i, x) in decoded.iter().enumerate() {
            assert!(
                (x - mean[i]).abs() < 1e-5,
                "envelope[{}]={} expected {}",
                i,
                x,
                mean[i]
            );
        }
    }

    #[test]
    fn aggregates_compute_scalar_min_max_per_column() {
        use std::collections::HashMap as Map;
        fn make(id_min: i128, ts_lo: i64, ts_hi: i64) -> Arc<SuperfileEntry> {
            let id = Uuid::new_v4();
            let mut cols: Map<String, ScalarStatsAgg> = Map::new();
            let mn: ArrayRef = Arc::new(Int64Array::from(vec![ts_lo]));
            let mx: ArrayRef = Arc::new(Int64Array::from(vec![ts_hi]));
            cols.insert("ts".into(), ScalarStatsAgg::from_min_max(mn, mx));
            Arc::new(SuperfileEntry {
                superfile_id: id,
                uri: SuperfileUri(id),
                n_docs: 1,
                id_min,
                id_max: id_min,
                scalar_stats: cols,
                fts_summary: HashMap::new(),
                vector_summary: HashMap::new(),
                partition_key: Vec::new(),
                partition_hint: None,
                subsection_offsets: None,
            })
        }
        let segs = vec![make(0, 100, 200), make(1, 50, 150), make(2, 300, 400)];
        let aggs = aggregates::compute(&segs, None);
        let s = aggs
            .scalar_stats_agg
            .get("ts")
            .expect("ts scalar agg present");
        // The aggregate min/max are length-1 arrays of the column type.
        assert_eq!(s.min.len(), 1, "ts min must be a length-1 array");
        assert_eq!(s.max.len(), 1, "ts max must be a length-1 array");
    }

    #[test]
    fn aggregates_compute_id_range_for_uint64_column_via_stats_table() {
        // The id column's min/max as Arrow stats survive the
        // aggregate path even though id_min/id_max are
        // tracked separately.
        use std::collections::HashMap as Map;
        fn make(id_lo: i128, id_hi: i128) -> Arc<SuperfileEntry> {
            let id = Uuid::new_v4();
            let mut cols: Map<String, ScalarStatsAgg> = Map::new();
            let mn: ArrayRef = Arc::new(
                Decimal128Array::from(vec![id_lo])
                    .with_precision_and_scale(38, 0)
                    .expect("decimal128"),
            );
            let mx: ArrayRef = Arc::new(
                Decimal128Array::from(vec![id_hi])
                    .with_precision_and_scale(38, 0)
                    .expect("decimal128"),
            );
            cols.insert("_id".into(), ScalarStatsAgg::from_min_max(mn, mx));
            Arc::new(SuperfileEntry {
                superfile_id: id,
                uri: SuperfileUri(id),
                n_docs: 1,
                id_min: id_lo,
                id_max: id_hi,
                scalar_stats: cols,
                fts_summary: HashMap::new(),
                vector_summary: HashMap::new(),
                partition_key: Vec::new(),
                partition_hint: None,
                subsection_offsets: None,
            })
        }
        let segs = vec![make(0, 99), make(100, 199), make(200, 299)];
        let aggs = aggregates::compute(&segs, None);
        assert_eq!(aggs.id_range, (0, 299));
        assert!(aggs.scalar_stats_agg.contains_key("_id"));
    }

    // ---- list_prune — query-shape correctness.

    #[test]
    fn prune_parts_for_id_range_filters_non_overlapping_parts() {
        let part0 = entry_from_superfiles(&[seg(0, 99, &[], None, 0.0)], 0);
        let part1 = entry_from_superfiles(&[seg(100, 199, &[], None, 0.0)], 1);
        let part2 = entry_from_superfiles(&[seg(200, 299, &[], None, 0.0)], 2);
        let part3 = entry_from_superfiles(&[seg(300, 399, &[], None, 0.0)], 3);
        let list = list_with(vec![part0, part1.clone(), part2.clone(), part3]);

        let survivors = prune_parts_for_id_range(&list, 150, 250);
        let ids: Vec<_> = survivors.into_iter().collect();
        assert_eq!(ids.len(), 2);
        assert!(ids.contains(&part1.part_id));
        assert!(ids.contains(&part2.part_id));
    }

    #[test]
    fn prune_parts_for_fts_prefix_filters_disjoint_term_ranges() {
        let part0 =
            entry_from_superfiles(&[seg(0, 10, &["alpha", "bravo", "charlie"], None, 0.0)], 0);
        let part1 =
            entry_from_superfiles(&[seg(11, 20, &["delta", "echo", "foxtrot"], None, 0.0)], 1);
        let part2 = entry_from_superfiles(&[seg(21, 30, &["hotel", "kilo", "lima"], None, 0.0)], 2);
        let list = list_with(vec![part0, part1.clone(), part2]);

        let survivors = prune_parts_for_fts_prefix(&list, "title", b"echo");
        assert_eq!(survivors.len(), 1);
        assert_eq!(survivors[0], part1.part_id);
    }

    #[test]
    fn prune_parts_for_fts_prefix_keeps_part_with_no_aggregate() {
        // Part has no FTS aggregate for the queried column —
        // always-keep.
        let part = entry_from_superfiles(&[seg(0, 10, &[], None, 0.0)], 0);
        let list = list_with(vec![part.clone()]);
        let survivors = prune_parts_for_fts_prefix(&list, "missing", b"any");
        assert_eq!(survivors, vec![part.part_id]);
    }

    #[test]
    fn prune_parts_for_vector_filters_far_parts() {
        let part_a = entry_from_superfiles(&[seg(0, 10, &[], Some(vec![10.0, 0.0, 0.0]), 0.5)], 0);
        let part_b =
            entry_from_superfiles(&[seg(11, 20, &[], Some(vec![-10.0, 0.0, 0.0]), 0.5)], 1);
        let list = list_with(vec![part_a.clone(), part_b]);
        let survivors = prune_parts_for_vector(&list, "emb", &[10.0, 0.0, 0.0], 1.0);
        assert_eq!(survivors.len(), 1);
        assert_eq!(survivors[0], part_a.part_id);
    }

    #[test]
    fn prune_parts_for_vector_keeps_overlapping_envelope() {
        let part_a = entry_from_superfiles(&[seg(0, 10, &[], Some(vec![1.0, 0.0, 0.0]), 1.0)], 0);
        let part_b = entry_from_superfiles(&[seg(11, 20, &[], Some(vec![-1.0, 0.0, 0.0]), 1.0)], 1);
        let list = list_with(vec![part_a, part_b]);
        let survivors = prune_parts_for_vector(&list, "emb", &[0.0, 0.0, 0.0], 1.0);
        assert_eq!(
            survivors.len(),
            2,
            "both envelopes contain origin within cutoff"
        );
    }

    #[test]
    fn pruning_is_monotonic_no_false_negatives() {
        // Property: any superfile the flat (superfile-level)
        // pruner would visit is necessarily in a part the
        // list-level pruner keeps. Aggregates over-
        // approximate the superfile-level skip data.
        let segs_part0 = vec![
            seg(0, 10, &["apple"], None, 0.0),
            seg(11, 20, &["banana", "cherry"], None, 0.0),
        ];
        let segs_part1 = vec![
            seg(21, 30, &["alpha"], None, 0.0),
            seg(31, 40, &["echo", "foxtrot"], None, 0.0),
        ];
        let part0 = entry_from_superfiles(&segs_part0, 0);
        let part1 = entry_from_superfiles(&segs_part1, 1);
        let list = list_with(vec![part0.clone(), part1.clone()]);

        let survivors = prune_parts_for_fts_prefix(&list, "title", b"ban");
        assert!(
            survivors.contains(&part0.part_id),
            "must keep matching part"
        );

        let survivors2 = prune_parts_for_fts_prefix(&list, "title", b"ec");
        assert!(survivors2.contains(&part1.part_id));
    }
}