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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Infino Authors

//! Manifest-level skip pruning helpers.
//!
//! Each helper takes a pinned [`ManifestSnapshot`] snapshot plus a query
//! shape and returns a `Vec<bool>` mask — one slot per superfile, in
//! manifest order — where `true` means "keep" and `false` means
//! "prune".  The masks are pure functions of manifest metadata
//! ([`SuperfileEntry::scalar_stats`], [`SuperfileEntry::fts_summary`],
//! [`SuperfileEntry::vector_summary`]) — **no store calls**.
//! Pruned superfiles are dropped before the query layer issues any
//! per-superfile work, so an irrelevant superfile never causes a
//! `SuperfileReaderCache::reader` call (the load-bearing perf claim of
//! the skip layer).
//!
//! Helpers are independent and idempotent. In v1, the BM25
//! query paths consume `fts_bloom_skip` (exact-term) and
//! `fts_prefix_skip` (prefix); vector and SQL paths do not yet
//! consume their helpers (see those modules' headers).
//!
//! ## Conservatism
//!
//! All helpers err on the side of keeping a superfile when in
//! doubt:
//!
//! - Unknown column → keep all (per-superfile search will surface
//!   the column-missing error to the caller).
//! - All-zero or absent summary → keep (treat as "may match").
//! - Empty query (no terms / `prefix == ""`) → keep all.
//!
//! False-positive keeps cost a per-superfile search call but never
//! a wrong answer. False-negative prunes would silently drop
//! relevant docs and are forbidden.
//!
//! ## Vector centroid skip
//!
//! Conservative pre-cutoff pruning is hard for IVF vectors
//! because we don't know the global top-k cutoff distance until
//! at least one superfile has been searched. v1
//! [`vector_centroid_skip`] returns all-keep and exposes
//! [`superfiles_sorted_by_centroid_distance`] so a future
//! incremental top-k pruning layer has the ordering it needs
//! without yet committing to a specific early-termination
//! algorithm.

use std::{cmp::Ordering, sync::Arc};

use datafusion::scalar::ScalarValue;

use crate::{
    superfile::{
        fts::reader::BoolMode,
        vector::distance::{Metric, distance},
    },
    supertable::manifest::{ManifestSnapshot, SuperfileEntry},
};

/// Bloom-skip mask for an exact-term BM25 search.
///
/// For each superfile, look up every tokenized query term in the
/// superfile's per-column term-presence bloom:
///
/// - `BoolMode::Or`  — keep if **any** term is possibly-present
///   (a doc containing any term contributes a positive score).
/// - `BoolMode::And` — keep if **all** terms are possibly-present
///   (a relevant doc must contain every term, so a single
///   definitely-absent term prunes the whole superfile).
///
/// `query_terms` are the terms after the same tokenizer used at
/// index time. Per the v1 tokenizer (`AsciiLowerTokenizer`) that
/// means already-lowercased ASCII tokens — no whitespace splits
/// inside individual entries.
///
/// An empty `query_terms` slice short-circuits to all-keep (the
/// BM25 search itself returns an empty result, but pruning
/// superfiles preemptively would mask that signal).
pub fn fts_bloom_skip(
    superfiles: &[Arc<SuperfileEntry>],
    column: &str,
    query_terms: &[&str],
    mode: BoolMode,
) -> Vec<bool> {
    if query_terms.is_empty() {
        return vec![true; superfiles.len()];
    }
    superfiles
        .iter()
        .map(|entry| match entry.fts_summary.get(column) {
            None => true,
            Some(summary) => match mode {
                BoolMode::Or => query_terms
                    .iter()
                    .any(|t| summary.may_contain(t.as_bytes())),
                BoolMode::And => query_terms
                    .iter()
                    .all(|t| summary.may_contain(t.as_bytes())),
            },
        })
        .collect()
}

/// Term-range skip mask for a prefix BM25 search.
///
/// For each superfile, check whether `[prefix, prefix_upper_bound)`
/// overlaps the superfile's lex term range (via
/// [`FtsSummaryAgg::may_match_prefix`]). A non-overlapping superfile
/// cannot contain any term beginning with `prefix` and is pruned.
///
/// `prefix` is the same lowercased byte sequence the prefix search uses
/// against the FST.
///
/// An empty `prefix` (every term matches) short-circuits to
/// all-keep.
///
/// [`FtsSummaryAgg::may_match_prefix`]: crate::supertable::manifest::FtsSummaryAgg::may_match_prefix
pub fn fts_prefix_skip(
    superfiles: &[Arc<SuperfileEntry>],
    column: &str,
    prefix: &[u8],
) -> Vec<bool> {
    if prefix.is_empty() {
        return vec![true; superfiles.len()];
    }
    superfiles
        .iter()
        .map(|entry| match entry.fts_summary.get(column) {
            None => true,
            // `may_match_prefix` returns false for a `None` range (0-term
            // superfile — nothing matches, prune).
            Some(summary) => summary.may_match_prefix(prefix),
        })
        .collect()
}

/// Vector centroid skip mask for a kNN search.
///
/// **v1 returns all-keep.** Cluster-aware skip in IVF with
/// 1-bit RaBitQ shortlist + full-precision rerank requires a
/// running top-k cutoff distance to drive triangle-inequality
/// pruning, which only becomes available *during* fan-out. The
/// machinery for incremental cutoff-driven termination lands
/// once the bench harness has the per-stage latency numbers to
/// motivate the right shape.
///
/// Until then, callers can use
/// [`superfiles_sorted_by_centroid_distance`] to bias fan-out
/// order toward likely-close superfiles — that alone gives a
/// near-cutoff result fast for cache-aware top-k merging.
pub fn vector_centroid_skip(
    manifest: &ManifestSnapshot,
    _column: &str,
    _query: &[f32],
) -> Vec<bool> {
    vec![true; manifest.superfiles.len()]
}

/// Indices into `manifest.superfiles` sorted ascending by the
/// per-superfile centroid's distance to `query` under `metric`.
///
/// Superfiles without a vector summary for `column` are sorted to
/// the end (treated as worst-case). Used as a fan-out hint for
/// vector search: searching closer-centroid superfiles first means
/// later superfiles are likelier to be skippable once the running
/// top-k has converged.
///
/// Returns indices, not entries, to keep the caller in control
/// of how to materialize the ordered fan-out (rayon `par_iter`
/// over indices is the typical shape).
pub fn superfiles_sorted_by_centroid_distance(
    manifest: &ManifestSnapshot,
    column: &str,
    query: &[f32],
    metric: Metric,
) -> Vec<usize> {
    let mut scored: Vec<(usize, f32)> = manifest
        .superfiles
        .iter()
        .enumerate()
        .map(|(i, entry)| match entry.vector_summary.get(column) {
            Some(vs) if vs.centroid.len() == query.len() => {
                (i, distance(metric, query, &vs.centroid))
            }
            _ => (i, f32::INFINITY),
        })
        .collect();
    // pdqsort: per-query superfile skip ordering. (superfile_idx, dist)
    // tuples are unique by superfile_idx, so any tie-break is fine.
    scored.sort_unstable_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal));
    scored.into_iter().map(|(i, _)| i).collect()
}

/// Comparison operator in a normalized scalar-skip predicate.
///
/// These mirror the SQL comparison operators the
/// `SupertableProvider` lowers from a DataFusion `Expr` into
/// infino's own predicate form. Any operator we can't normalize is
/// simply never handed to [`scalar_skip`] — the superfile is kept and
/// DataFusion's `FilterExec` still applies the predicate to rows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScalarOp {
    Eq,
    NotEq,
    Lt,
    LtEq,
    Gt,
    GtEq,
}

/// One conjunct of a SQL `WHERE` clause, normalized to
/// `column <op> literal`. The literal is a DataFusion
/// [`ScalarValue`]; [`scalar_skip`] coerces it to the column's
/// stored stat type at compare time.
#[derive(Debug, Clone)]
pub struct ScalarPredicate {
    /// Scalar column name; must match a key in
    /// `SuperfileEntry::scalar_stats` to contribute any pruning.
    pub column: String,
    /// Comparison operator.
    pub op: ScalarOp,
    /// Right-hand-side literal from the query.
    pub value: ScalarValue,
}

/// Scalar-skip mask for a conjunction of `column <op> literal`
/// predicates (a SQL `WHERE` of `AND`-ed simple comparisons).
///
/// For each superfile, consult the per-column min/max persisted in
/// [`SuperfileEntry::scalar_stats`] and keep the superfile unless
/// some predicate *proves* no row in the superfile can satisfy it.
/// Because the predicates are conjunctive, a single
/// definitely-false predicate prunes the whole superfile.
///
/// Conservatism (never a false prune): a superfile is kept when
///
/// - the column has no persisted stats (the writer skips types
///   whose ordering isn't well-defined, and all-null columns),
/// - either bound is NULL,
/// - the literal can't be coerced to the column's stat type, or
/// - the values are otherwise incomparable.
///
/// An empty predicate slice keeps every superfile.
///
/// This is the SQL-side sibling of [`fts_bloom_skip`] /
/// [`fts_prefix_skip`]: **infino owns superfile selection.**
/// DataFusion only executes over the surviving superfiles (and does
/// its own row-group/page pruning inside each Parquet superfile).
pub fn scalar_skip(
    superfiles: &[Arc<SuperfileEntry>],
    predicates: &[ScalarPredicate],
) -> Vec<bool> {
    if predicates.is_empty() {
        return vec![true; superfiles.len()];
    }
    superfiles
        .iter()
        .map(|entry| predicates.iter().all(|p| superfile_may_match(entry, p)))
        .collect()
}

/// Whether `entry` *could* contain a row satisfying `pred`, judged
/// only from the superfile's persisted min/max. Conservative: any
/// uncertainty returns `true` (keep).
fn superfile_may_match(entry: &SuperfileEntry, pred: &ScalarPredicate) -> bool {
    let Some(agg) = entry.scalar_stats.get(&pred.column) else {
        // No stats for this column — can't prove irrelevance.
        return true;
    };
    let (Ok(min), Ok(max)) = (
        ScalarValue::try_from_array(agg.min.as_ref(), 0),
        ScalarValue::try_from_array(agg.max.as_ref(), 0),
    ) else {
        return true;
    };
    scalar_value_may_match(&min, &max, pred.op, &pred.value)
}

/// Conservative `min`/`max`-vs-`value` comparison core, shared by the
/// superfile tier ([`superfile_may_match`]) and the part tier (the scalar
/// part prune in [`crate::supertable::query::prune`]). Returns `true`
/// (keep) on any uncertainty: null bounds, an un-coercible literal, or
/// otherwise-incomparable values. Never a false prune.
pub(crate) fn scalar_value_may_match(
    min: &ScalarValue,
    max: &ScalarValue,
    op: ScalarOp,
    value: &ScalarValue,
) -> bool {
    if min.is_null() || max.is_null() {
        return true;
    }
    // Coerce the query literal to the stored stat type so a
    // Utf8-literal-vs-LargeUtf8-stat (or differing int width)
    // mismatch doesn't degrade to "incomparable → keep" and lose
    // pruning power.
    let v = match value.cast_to(&min.data_type()) {
        Ok(v) if !v.is_null() => v,
        _ => return true,
    };
    let cmp_v_min = v.partial_cmp(min);
    let cmp_v_max = v.partial_cmp(max);
    match op {
        // keep iff min <= v <= max
        ScalarOp::Eq => match (cmp_v_min, cmp_v_max) {
            (Some(lo), Some(hi)) => lo != Ordering::Less && hi != Ordering::Greater,
            _ => true,
        },
        // prune only when the superfile is a single constant == v
        ScalarOp::NotEq => {
            let constant = min.partial_cmp(max) == Some(Ordering::Equal);
            let equals_v = cmp_v_min == Some(Ordering::Equal);
            !(constant && equals_v)
        }
        // keep iff some row could be < v, i.e. min < v
        ScalarOp::Lt => matches!(cmp_v_min, Some(Ordering::Greater) | None),
        // keep iff min <= v
        ScalarOp::LtEq => !matches!(cmp_v_min, Some(Ordering::Less)),
        // keep iff max > v, i.e. v < max
        ScalarOp::Gt => matches!(cmp_v_max, Some(Ordering::Less) | None),
        // keep iff max >= v
        ScalarOp::GtEq => !matches!(cmp_v_max, Some(Ordering::Greater)),
    }
}

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

    use arrow_array::{ArrayRef, Int64Array, LargeStringArray};
    use arrow_schema::{DataType, Field, Schema};
    use datafusion::scalar::ScalarValue;
    use uuid::Uuid;

    use super::*;
    use crate::{
        superfile::{
            builder::{FtsConfig, VectorConfig},
            vector::{distance::Metric, rerank_codec::RerankCodec},
        },
        supertable::{
            SupertableOptions,
            manifest::{
                ClusterCentroids, FtsSummaryAgg, ManifestSnapshot, ScalarStatsAgg, SuperfileEntry,
                SuperfileUri, VectorSummary, bloom::BloomBuilder,
            },
        },
        test_helpers::default_tokenizer,
    };

    fn opts_simple() -> Arc<SupertableOptions> {
        let schema = Arc::new(Schema::new(vec![Field::new(
            "title",
            DataType::LargeUtf8,
            false,
        )]));
        let tk = default_tokenizer();
        Arc::new(
            SupertableOptions::new(
                schema,
                vec![FtsConfig {
                    column: "title".into(),
                }],
                vec![],
                Some(tk),
            )
            .expect("opts"),
        )
    }

    fn opts_with_vector() -> Arc<SupertableOptions> {
        // dim ≥ 16 per SupertableOptions invariant.
        let dim = 16;
        let schema = Arc::new(Schema::new(vec![Field::new(
            "emb",
            DataType::FixedSizeList(
                Arc::new(Field::new("item", DataType::Float32, true)),
                dim as i32,
            ),
            false,
        )]));
        Arc::new(
            SupertableOptions::new(
                schema,
                vec![],
                vec![VectorConfig {
                    column: "emb".into(),
                    dim,
                    n_cent: 4,
                    rot_seed: 0,
                    metric: Metric::Cosine,
                    rerank_codec: RerankCodec::Fp32,
                }],
                None,
            )
            .expect("opts"),
        )
    }

    fn empty_superfile() -> SuperfileEntry {
        let uri = SuperfileUri::new_v4();
        SuperfileEntry {
            superfile_id: Uuid::new_v4(),
            uri,
            n_docs: 0,
            id_min: 0,
            id_max: 0,
            scalar_stats: HashMap::new(),
            fts_summary: HashMap::new(),
            vector_summary: HashMap::new(),
            partition_key: Vec::new(),
            partition_hint: None,
            subsection_offsets: None,
        }
    }

    /// Build a one-column FTS summary with the given indexed terms.
    fn fts_summary_with(column: &str, terms: &[&str]) -> (String, FtsSummaryAgg) {
        let mut bb = BloomBuilder::new();
        for t in terms {
            bb.insert(t.as_bytes());
        }
        let term_range = match (terms.first(), terms.last()) {
            (Some(min), Some(max)) => (min.as_bytes().to_vec(), max.as_bytes().to_vec()),
            _ => (Vec::new(), Vec::new()),
        };
        let summary = FtsSummaryAgg::new_with_params(bb.finish(), terms.len() as u32, term_range);
        (column.to_string(), summary)
    }

    fn superfile_with_terms(column: &str, terms: &[&str]) -> Arc<SuperfileEntry> {
        let mut e = empty_superfile();
        let (k, v) = fts_summary_with(column, terms);
        e.fts_summary.insert(k, v);
        Arc::new(e)
    }

    fn superfile_with_centroid(
        column: &str,
        centroid: Vec<f32>,
        radius: f32,
    ) -> Arc<SuperfileEntry> {
        let mut e = empty_superfile();
        e.vector_summary.insert(
            column.to_string(),
            VectorSummary {
                centroid,
                radius,
                clusters: ClusterCentroids::empty(),
            },
        );
        Arc::new(e)
    }

    // ---- fts_bloom_skip ----------------------------------------------

    #[test]
    fn bloom_skip_keeps_superfiles_with_any_query_term_in_or_mode() {
        let s_a = superfile_with_terms("title", &["alpha", "beta"]);
        let s_b = superfile_with_terms("title", &["gamma", "delta"]);
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s_a, s_b]);
        let mask = fts_bloom_skip(&m.superfiles, "title", &["alpha", "missing"], BoolMode::Or);
        // Superfile A has alpha → keep. Superfile B has neither → prune.
        assert_eq!(mask, vec![true, false]);
    }

    #[test]
    fn bloom_skip_requires_all_terms_present_in_and_mode() {
        let s_a = superfile_with_terms("title", &["alpha", "beta"]);
        let s_b = superfile_with_terms("title", &["alpha", "gamma"]);
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s_a, s_b]);
        let mask = fts_bloom_skip(&m.superfiles, "title", &["alpha", "beta"], BoolMode::And);
        // Superfile A has both. Superfile B is missing 'beta' → prune.
        assert_eq!(mask, vec![true, false]);
    }

    #[test]
    fn bloom_skip_unknown_column_keeps_all() {
        let s = superfile_with_terms("title", &["alpha"]);
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s]);
        let mask = fts_bloom_skip(&m.superfiles, "no_such_column", &["alpha"], BoolMode::Or);
        assert_eq!(mask, vec![true]);
    }

    #[test]
    fn bloom_skip_empty_terms_keeps_all() {
        let s = superfile_with_terms("title", &["alpha"]);
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s]);
        let mask = fts_bloom_skip(&m.superfiles, "title", &[], BoolMode::Or);
        assert_eq!(mask, vec![true]);
    }

    #[test]
    fn bloom_skip_with_no_superfiles_returns_empty_vec() {
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![]);
        let mask = fts_bloom_skip(&m.superfiles, "title", &["alpha"], BoolMode::Or);
        assert!(mask.is_empty());
    }

    // ---- fts_prefix_skip ---------------------------------------------

    #[test]
    fn prefix_skip_prunes_superfiles_outside_prefix_range() {
        // Superfile A: terms in ['apple', 'banana'] → prefix "rust"
        //            doesn't overlap.
        // Superfile B: terms in ['python', 'rust']  → prefix "rust"
        //            overlaps the upper end.
        let s_a = superfile_with_terms("title", &["apple", "banana"]);
        let s_b = superfile_with_terms("title", &["python", "rust"]);
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s_a, s_b]);
        let mask = fts_prefix_skip(&m.superfiles, "title", b"rust");
        assert_eq!(mask, vec![false, true]);
    }

    #[test]
    fn prefix_skip_keeps_superfiles_with_matching_prefix_inside_range() {
        // Terms ['rusting', 'rusty'] → prefix "rust" overlaps.
        let s = superfile_with_terms("title", &["rusting", "rusty"]);
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s]);
        let mask = fts_prefix_skip(&m.superfiles, "title", b"rust");
        assert_eq!(mask, vec![true]);
    }

    #[test]
    fn prefix_skip_empty_prefix_keeps_all() {
        let s = superfile_with_terms("title", &["alpha"]);
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s]);
        let mask = fts_prefix_skip(&m.superfiles, "title", b"");
        assert_eq!(mask, vec![true]);
    }

    #[test]
    fn prefix_skip_unknown_column_keeps_all() {
        let s = superfile_with_terms("title", &["alpha"]);
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s]);
        let mask = fts_prefix_skip(&m.superfiles, "no_such_column", b"alp");
        assert_eq!(mask, vec![true]);
    }

    #[test]
    fn prefix_skip_zero_term_superfile_pruned() {
        // Empty term_range = no terms indexed. Prefix can't match.
        let s = Arc::new(empty_superfile());
        let m = ManifestSnapshot::new_from_superfiles(opts_simple(), vec![s]);
        let mask = fts_prefix_skip(&m.superfiles, "title", b"rust");
        // No FTS summary on the superfile → keep (column-missing
        // path). Sanity: this is the "unknown column" path, not
        // the "0-term FTS column" path.
        assert_eq!(mask, vec![true]);
    }

    // ---- vector_centroid_skip + ordering ------------------------------

    #[test]
    fn vector_centroid_skip_v1_keeps_all_superfiles() {
        let s_a = superfile_with_centroid("emb", vec![0.0; 16], 0.5);
        let s_b = superfile_with_centroid("emb", vec![10.0; 16], 0.5);
        let m = ManifestSnapshot::new_from_superfiles(opts_with_vector(), vec![s_a, s_b]);
        let q = vec![0.0f32; 16];
        let mask = vector_centroid_skip(&m, "emb", &q);
        assert_eq!(mask, vec![true, true]);
    }

    #[test]
    fn superfiles_sorted_by_centroid_distance_orders_by_metric() {
        // L2-sq metric on simple 1-hot centroids.
        let opts = opts_with_vector();
        let near = superfile_with_centroid(
            "emb",
            {
                let mut v = vec![0.0f32; 16];
                v[0] = 1.0;
                v
            },
            0.0,
        );
        let far = superfile_with_centroid(
            "emb",
            {
                let mut v = vec![0.0f32; 16];
                v[7] = 1.0;
                v
            },
            0.0,
        );
        let m = ManifestSnapshot::new_from_superfiles(opts, vec![far.clone(), near.clone()]);
        let q = {
            let mut v = vec![0.0f32; 16];
            v[0] = 1.0;
            v
        };
        let order = superfiles_sorted_by_centroid_distance(&m, "emb", &q, Metric::L2Sq);
        // `near` (idx 1) should come before `far` (idx 0).
        assert_eq!(order, vec![1, 0]);
    }

    #[test]
    fn superfiles_sorted_by_centroid_distance_pushes_missing_summary_to_end() {
        let with_v = superfile_with_centroid("emb", vec![1.0f32; 16], 0.0);
        let without_v = Arc::new(empty_superfile());
        let m = ManifestSnapshot::new_from_superfiles(opts_with_vector(), vec![without_v, with_v]);
        let q = vec![1.0f32; 16];
        let order = superfiles_sorted_by_centroid_distance(&m, "emb", &q, Metric::L2Sq);
        // Index 1 (has summary) sorted before index 0 (missing).
        assert_eq!(order, vec![1, 0]);
    }

    // ---- scalar_skip -------------------------------------------------

    fn seg_with_int_stats(col: &str, min: i64, max: i64) -> Arc<SuperfileEntry> {
        let mut e = empty_superfile();
        let mn: ArrayRef = Arc::new(Int64Array::from(vec![min]));
        let mx: ArrayRef = Arc::new(Int64Array::from(vec![max]));
        e.scalar_stats
            .insert(col.to_string(), ScalarStatsAgg::from_min_max(mn, mx));
        Arc::new(e)
    }

    fn seg_with_str_stats(col: &str, min: &str, max: &str) -> Arc<SuperfileEntry> {
        let mut e = empty_superfile();
        let mn: ArrayRef = Arc::new(LargeStringArray::from(vec![min]));
        let mx: ArrayRef = Arc::new(LargeStringArray::from(vec![max]));
        e.scalar_stats
            .insert(col.to_string(), ScalarStatsAgg::from_min_max(mn, mx));
        Arc::new(e)
    }

    fn pred(column: &str, op: ScalarOp, value: ScalarValue) -> ScalarPredicate {
        ScalarPredicate {
            column: column.to_string(),
            op,
            value,
        }
    }

    #[test]
    fn scalar_skip_empty_predicates_keeps_all() {
        let segs = vec![
            seg_with_int_stats("x", 0, 10),
            seg_with_int_stats("x", 100, 110),
        ];
        assert_eq!(scalar_skip(&segs, &[]), vec![true, true]);
    }

    #[test]
    fn scalar_skip_eq_prunes_superfiles_whose_range_excludes_value() {
        let segs = vec![
            seg_with_int_stats("x", 0, 10),
            seg_with_int_stats("x", 100, 110),
        ];
        // x = 5 → only A's [0,10] can contain it.
        let mask = scalar_skip(
            &segs,
            &[pred("x", ScalarOp::Eq, ScalarValue::Int64(Some(5)))],
        );
        assert_eq!(mask, vec![true, false]);
        // x = 105 → only B's [100,110].
        let mask = scalar_skip(
            &segs,
            &[pred("x", ScalarOp::Eq, ScalarValue::Int64(Some(105)))],
        );
        assert_eq!(mask, vec![false, true]);
        // Range boundary is inclusive.
        let mask = scalar_skip(
            &segs,
            &[pred("x", ScalarOp::Eq, ScalarValue::Int64(Some(10)))],
        );
        assert_eq!(mask, vec![true, false]);
    }

    #[test]
    fn scalar_skip_range_ops_prune_by_min_or_max() {
        let segs = vec![
            seg_with_int_stats("x", 0, 10),
            seg_with_int_stats("x", 100, 110),
        ];
        // x > 50 → A.max=10 can't; B kept.
        assert_eq!(
            scalar_skip(
                &segs,
                &[pred("x", ScalarOp::Gt, ScalarValue::Int64(Some(50)))]
            ),
            vec![false, true]
        );
        // x < 50 → A.min=0 ok; B.min=100 can't.
        assert_eq!(
            scalar_skip(
                &segs,
                &[pred("x", ScalarOp::Lt, ScalarValue::Int64(Some(50)))]
            ),
            vec![true, false]
        );
        // x >= 110 → A can't (max 10); B can (max 110).
        assert_eq!(
            scalar_skip(
                &segs,
                &[pred("x", ScalarOp::GtEq, ScalarValue::Int64(Some(110)))]
            ),
            vec![false, true]
        );
        // x <= 0 → A can (min 0); B can't (min 100).
        assert_eq!(
            scalar_skip(
                &segs,
                &[pred("x", ScalarOp::LtEq, ScalarValue::Int64(Some(0)))]
            ),
            vec![true, false]
        );
    }

    #[test]
    fn scalar_skip_conjunction_prunes_when_any_predicate_excludes() {
        // A=[0,3], B=[6,7]; WHERE x >= 5 AND x <= 8.
        let segs = vec![seg_with_int_stats("x", 0, 3), seg_with_int_stats("x", 6, 7)];
        let preds = [
            pred("x", ScalarOp::GtEq, ScalarValue::Int64(Some(5))),
            pred("x", ScalarOp::LtEq, ScalarValue::Int64(Some(8))),
        ];
        // A: max=3 < 5 → the >=5 conjunct prunes it. B kept.
        assert_eq!(scalar_skip(&segs, &preds), vec![false, true]);
    }

    #[test]
    fn scalar_skip_unknown_column_keeps_all() {
        let segs = vec![seg_with_int_stats("x", 0, 10)];
        let mask = scalar_skip(
            &segs,
            &[pred("not_a_col", ScalarOp::Eq, ScalarValue::Int64(Some(5)))],
        );
        assert_eq!(mask, vec![true]);
    }

    #[test]
    fn scalar_skip_coerces_utf8_literal_against_largeutf8_stats() {
        // Stats stored as LargeUtf8; predicate literal is Utf8.
        let segs = vec![
            seg_with_str_stats("name", "apple", "mango"),
            seg_with_str_stats("name", "tango", "zulu"),
        ];
        // name = 'banana' → within A's [apple, mango], outside B's.
        let mask = scalar_skip(
            &segs,
            &[pred(
                "name",
                ScalarOp::Eq,
                ScalarValue::Utf8(Some("banana".into())),
            )],
        );
        assert_eq!(mask, vec![true, false]);
    }

    #[test]
    fn scalar_skip_null_stats_keeps_superfile() {
        let mut e = empty_superfile();
        let mn: ArrayRef = Arc::new(Int64Array::from(vec![None::<i64>]));
        let mx: ArrayRef = Arc::new(Int64Array::from(vec![None::<i64>]));
        e.scalar_stats
            .insert("x".to_string(), ScalarStatsAgg::from_min_max(mn, mx));
        let segs = vec![Arc::new(e)];
        let mask = scalar_skip(
            &segs,
            &[pred("x", ScalarOp::Eq, ScalarValue::Int64(Some(5)))],
        );
        assert_eq!(mask, vec![true]);
    }

    #[test]
    fn scalar_skip_not_eq_prunes_only_constant_superfile() {
        let segs = vec![seg_with_int_stats("x", 5, 5), seg_with_int_stats("x", 5, 9)];
        // x != 5 → constant all-5 superfile matches nothing → prune;
        // the ranged superfile is kept.
        let mask = scalar_skip(
            &segs,
            &[pred("x", ScalarOp::NotEq, ScalarValue::Int64(Some(5)))],
        );
        assert_eq!(mask, vec![false, true]);
    }
}