lance-index 13.0.0

Lance indices implementation
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! Bounded staging and native code-to-code tile scoring of index codes.
//!
//! Distances are symmetric values between the index representations `x̂` (the
//! vectors that the stored codes reconstruct). They are computed directly from
//! the codes; no floating-point vector is reconstructed:
//!
//! | metric  | distance                                          |
//! |---------|---------------------------------------------------|
//! | l2      | squared L2 `‖x̂a − x̂b‖²`                           |
//! | cosine  | `1 − cos(x̂a, x̂b)`, clamped to `[0, 2]`            |
//! | dot     | `1 − x̂a·x̂b`                                       |
//! | hamming | number of differing bits (binary IVF_FLAT)        |
//!
//! Cosine renormalizes the reconstructions, as the metric (and exact or refined
//! search) defines it. It is evaluated as
//! `(‖x̂a − x̂b‖² − (‖x̂a‖ − ‖x̂b‖)²) / (2‖x̂a‖‖x̂b‖)` from norms staged once per
//! row, so identical codes score exactly zero. A zero-norm reconstruction has
//! no cosine distance (NaN). Unrefined ANN search over quantized cosine indices
//! reports `‖q̂ − x̂‖²` of normalized vectors instead, about twice this value.
//!
//! Each distance depends only on its two rows, with the lower storage position
//! as the anchor, so results are bit-identical for every batch, block, spill and
//! concurrency layout.

use super::{
    bq::pairwise::RQPairScorer, flat::pairwise::FlatPairScorer, pq::pairwise::PQPairScorer,
    quantizer::Quantizer, sq::pairwise::SQPairScorer,
};
use crate::scalar::RowIdRemapper;
use arrow_array::cast::AsArray;
use arrow_array::types::UInt64Type;
use arrow_array::{Array, ArrayRef, ArrowPrimitiveType, RecordBatch, UInt64Array};
use arrow_ipc::{reader::StreamReader, writer::StreamWriter};
use arrow_schema::{DataType, Field, Schema};
use lance_core::utils::tokio::spawn_cpu;
use lance_core::{Error, ROW_ID, Result};
use lance_io::{
    spill::{Spill, SpillStore},
    traits::{Reader, Writer},
};
use lance_linalg::distance::DistanceType;
use std::{io::Cursor, ops::Range, sync::Arc};
use tokio::io::AsyncWriteExt;

/// Default budget for staged codes. Larger partitions use session spill storage.
pub const PAIRWISE_MEMORY_LIMIT: usize = 256 * 1024 * 1024;

/// Candidate payload scored per pass over an anchor block. Together with the
/// anchor rows it stays in L2 cache, so each candidate row is loaded from
/// memory once per block rather than once per anchor.
const CANDIDATE_CHUNK_BYTES: usize = 256 * 1024;
const MIN_CANDIDATE_CHUNK_ROWS: usize = 16;

/// Row IDs plus Arrow overhead in the staged size estimate.
const STAGED_ROW_OVERHEAD_BYTES: usize = 16;

/// Staged `‖x̂‖` (f64) per row, for cosine.
pub(crate) const NORM_COLUMN: &str = "__pairwise_norm";

/// One staged batch of index rows in storage order.
#[derive(Clone, Debug)]
pub struct PairwiseVectorBatch {
    /// Current row IDs; null where compaction remapping deleted the row.
    pub row_ids: UInt64Array,
    /// Quantizer-specific staged payload, including the row IDs.
    pub codes: RecordBatch,
    batch_id: usize,
    selected: Vec<bool>,
}

impl PairwiseVectorBatch {
    /// Position of this batch in the partition's storage order.
    pub fn batch_id(&self) -> usize {
        self.batch_id
    }

    pub fn num_rows(&self) -> usize {
        self.row_ids.len()
    }

    /// Per-row eligibility for pairing. Rows with null IDs are never eligible.
    pub fn selected(&self) -> &[bool] {
        &self.selected
    }

    /// Restrict pairing to rows whose current row ID satisfies `keep`,
    /// evaluated once per row rather than once per pair.
    pub fn filter_rows(&mut self, keep: impl Fn(u64) -> bool) {
        for (selected, id) in self.selected.iter_mut().zip(self.row_ids.iter()) {
            *selected = *selected && id.is_some_and(&keep);
        }
    }
}

/// Qualifying pairs of one scoring block, ordered by anchor row, then by
/// candidate row, in storage order.
#[derive(Debug, Default, PartialEq)]
pub struct PairwiseHits {
    pub row_id_a: Vec<u64>,
    pub row_id_b: Vec<u64>,
    pub distances: Vec<f32>,
}

/// Native distances from one anchor row to a range of candidate rows.
pub(crate) trait PairKernel {
    fn distances(&self, anchor_row: usize, candidates: Range<usize>, out: &mut [f32]);
}

/// A quantizer's staged layout and code-to-code kernel.
pub(crate) trait PairScorer {
    type Kernel<'a>: PairKernel
    where
        Self: 'a;

    /// Staged payload bytes per row, excluding row IDs.
    fn row_bytes(&self) -> usize;

    /// Stage `rows` of a source index batch as payload columns.
    fn stage(&self, source: &RecordBatch, rows: Range<usize>) -> Result<Vec<(Field, ArrayRef)>>;

    /// Bind the kernel to two staged batches.
    fn kernel<'a>(
        &'a self,
        anchor: &'a RecordBatch,
        candidates: &'a RecordBatch,
    ) -> Result<Self::Kernel<'a>>;
}

pub(crate) enum PairwiseScorer {
    Flat(FlatPairScorer),
    Product(PQPairScorer),
    Scalar(SQPairScorer),
    Rabit(RQPairScorer),
}

impl PairwiseScorer {
    /// `centroid` is the partition centroid; `source` is the index file schema.
    pub(crate) fn new(
        quantizer: &Quantizer,
        centroid: ArrayRef,
        metric: DistanceType,
        source: &Schema,
    ) -> Result<Self> {
        Ok(match quantizer {
            Quantizer::Flat(_) | Quantizer::FlatBin(_) => {
                Self::Flat(FlatPairScorer::new(quantizer.column(), source, metric)?)
            }
            Quantizer::Product(pq) => Self::Product(PQPairScorer::new(pq, &centroid, metric)?),
            Quantizer::Scalar(sq) => Self::Scalar(SQPairScorer::new(sq, metric)?),
            Quantizer::Rabit(rq) => Self::Rabit(RQPairScorer::new(rq, centroid, metric)?),
        })
    }

    /// Estimated staged bytes per row, including row IDs.
    pub(crate) fn row_bytes(&self) -> usize {
        STAGED_ROW_OVERHEAD_BYTES
            + match self {
                Self::Flat(scorer) => scorer.row_bytes(),
                Self::Product(scorer) => scorer.row_bytes(),
                Self::Scalar(scorer) => scorer.row_bytes(),
                Self::Rabit(scorer) => scorer.row_bytes(),
            }
    }

    /// Stage `rows` of a source index batch once: remapped row IDs followed by
    /// the quantizer payload. Replays never touch the source index again.
    pub(crate) fn stage(
        &self,
        source: &RecordBatch,
        rows: Range<usize>,
        remapper: Option<&dyn RowIdRemapper>,
    ) -> Result<RecordBatch> {
        let ids = source
            .column_by_name(ROW_ID)
            .ok_or_else(|| Error::internal("index batch missing row IDs"))?
            .as_primitive_opt::<UInt64Type>()
            .ok_or_else(|| Error::internal("index row IDs must be UInt64"))?
            .slice(rows.start, rows.len());
        let ids = match remapper {
            Some(remapper) => ids
                .iter()
                .map(|id| id.and_then(|id| remapper.remap_row_id(id)))
                .collect::<UInt64Array>(),
            None => ids,
        };
        let payload = match self {
            Self::Flat(scorer) => scorer.stage(source, rows)?,
            Self::Product(scorer) => scorer.stage(source, rows)?,
            Self::Scalar(scorer) => scorer.stage(source, rows)?,
            Self::Rabit(scorer) => scorer.stage(source, rows)?,
        };
        let mut fields = Vec::with_capacity(payload.len() + 1);
        let mut columns = Vec::with_capacity(payload.len() + 1);
        fields.push(Field::new(ROW_ID, DataType::UInt64, true));
        columns.push(Arc::new(ids) as ArrayRef);
        for (field, column) in payload {
            fields.push(field);
            columns.push(column);
        }
        Ok(RecordBatch::try_new(
            Arc::new(Schema::new(fields)),
            columns,
        )?)
    }

    fn score_block(
        &self,
        anchor: &PairwiseVectorBatch,
        anchor_rows: Range<usize>,
        candidates: &PairwiseVectorBatch,
        threshold: f32,
    ) -> Result<PairwiseHits> {
        fn score<S: PairScorer>(
            scorer: &S,
            anchor: &PairwiseVectorBatch,
            anchor_rows: Range<usize>,
            candidates: &PairwiseVectorBatch,
            threshold: f32,
        ) -> Result<PairwiseHits> {
            let kernel = scorer.kernel(&anchor.codes, &candidates.codes)?;
            let chunk_rows =
                (CANDIDATE_CHUNK_BYTES / scorer.row_bytes().max(1)).max(MIN_CANDIDATE_CHUNK_ROWS);
            Ok(collect_pairs(
                &kernel,
                chunk_rows,
                anchor,
                anchor_rows,
                candidates,
                threshold,
            ))
        }
        match self {
            Self::Flat(s) => score(s, anchor, anchor_rows, candidates, threshold),
            Self::Product(s) => score(s, anchor, anchor_rows, candidates, threshold),
            Self::Scalar(s) => score(s, anchor, anchor_rows, candidates, threshold),
            Self::Rabit(s) => score(s, anchor, anchor_rows, candidates, threshold),
        }
    }
}

/// Score anchor rows against every later candidate row. Candidates are processed
/// in cache-sized chunks with anchors in the inner loop; hits are buffered per
/// anchor row so the output stays row-major.
fn collect_pairs<K: PairKernel>(
    kernel: &K,
    chunk_rows: usize,
    anchor: &PairwiseVectorBatch,
    anchor_rows: Range<usize>,
    candidates: &PairwiseVectorBatch,
    threshold: f32,
) -> PairwiseHits {
    let is_diagonal = anchor.batch_id == candidates.batch_id;
    let num_candidates = candidates.num_rows();
    let first = if is_diagonal {
        anchor_rows.start + 1
    } else {
        0
    };
    let a_ids = anchor.row_ids.values();
    let b_ids = candidates.row_ids.values();
    let mut rows: Vec<Vec<(u32, f32)>> = vec![Vec::new(); anchor_rows.len()];
    let mut scratch = vec![0.0f32; chunk_rows.min(num_candidates)];
    let mut chunk_start = first;
    while chunk_start < num_candidates {
        let chunk_end = chunk_start.saturating_add(chunk_rows).min(num_candidates);
        for (a, hits) in anchor_rows.clone().zip(rows.iter_mut()) {
            if !anchor.selected[a] {
                continue;
            }
            let start = if is_diagonal {
                chunk_start.max(a + 1)
            } else {
                chunk_start
            };
            if start >= chunk_end {
                continue;
            }
            let out = &mut scratch[..chunk_end - start];
            kernel.distances(a, start..chunk_end, out);
            let a_id = a_ids[a];
            for (b, &distance) in (start..chunk_end).zip(out.iter()) {
                // NaN fails the threshold test; infinite distances are dropped too.
                if distance <= threshold
                    && distance.is_finite()
                    && candidates.selected[b]
                    && b_ids[b] != a_id
                {
                    hits.push((b as u32, distance));
                }
            }
        }
        chunk_start = chunk_end;
    }
    let total = rows.iter().map(Vec::len).sum();
    let mut output = PairwiseHits {
        row_id_a: Vec::with_capacity(total),
        row_id_b: Vec::with_capacity(total),
        distances: Vec::with_capacity(total),
    };
    for (a, hits) in anchor_rows.zip(rows) {
        output
            .row_id_a
            .extend(std::iter::repeat_n(a_ids[a], hits.len()));
        for (b, distance) in hits {
            output.row_id_b.push(b_ids[b as usize]);
            output.distances.push(distance);
        }
    }
    output
}

/// `1 − cos` of two vectors from their squared L2 distance and norms.
/// Identical vectors give exactly zero; a zero norm gives NaN.
#[inline]
pub(crate) fn cosine_from_l2(l2: f32, anchor_norm: f64, candidate_norm: f64) -> f32 {
    let denominator = 2.0 * anchor_norm * candidate_norm;
    if denominator > 0.0 {
        let norm_gap = anchor_norm - candidate_norm;
        ((f64::from(l2) - norm_gap * norm_gap) / denominator).clamp(0.0, 2.0) as f32
    } else {
        f32::NAN
    }
}

/// Convert squared L2 distances to cosine distances in place.
pub(crate) fn l2_to_cosine(distances: &mut [f32], anchor_norm: f64, candidate_norms: &[f64]) {
    for (distance, &norm) in distances.iter_mut().zip(candidate_norms) {
        *distance = cosine_from_l2(*distance, anchor_norm, norm);
    }
}

pub(crate) fn norm_field() -> Field {
    Field::new(NORM_COLUMN, DataType::Float64, false)
}

/// Values of a primitive staged column.
pub(crate) fn column_values<'a, T: ArrowPrimitiveType>(
    batch: &'a RecordBatch,
    name: &str,
) -> Result<&'a [T::Native]> {
    Ok(batch
        .column_by_name(name)
        .and_then(|column| column.as_primitive_opt::<T>())
        .ok_or_else(|| {
            Error::internal(format!(
                "pairwise batch missing {} column {name}",
                T::DATA_TYPE
            ))
        })?
        .values())
}

/// Flattened values of a fixed-size-list staged column.
pub(crate) fn list_values<'a, T: ArrowPrimitiveType>(
    batch: &'a RecordBatch,
    name: &str,
) -> Result<&'a [T::Native]> {
    Ok(batch
        .column_by_name(name)
        .and_then(|column| column.as_fixed_size_list_opt())
        .and_then(|list| list.values().as_primitive_opt::<T>())
        .ok_or_else(|| {
            Error::internal(format!(
                "pairwise batch missing {} list column {name}",
                T::DATA_TYPE
            ))
        })?
        .values())
}

pub(crate) enum EncodedPartition {
    Memory(Vec<RecordBatch>),
    Spilled(SpilledPartition),
}

pub(crate) struct SpilledPartition {
    ranges: Vec<Range<usize>>,
    reader: Box<dyn Reader>,
    // Keep the spill alive until its reader has been dropped.
    _spill: Box<dyn Spill>,
}

impl SpilledPartition {
    async fn read_batch(&self, batch_id: usize) -> Result<RecordBatch> {
        let range = self.ranges.get(batch_id).ok_or_else(|| {
            Error::invalid_input(format!("pairwise spill batch_id={batch_id} out of range"))
        })?;
        let bytes = self.reader.get_range(range.clone()).await?;
        spawn_cpu(move || {
            StreamReader::try_new(Cursor::new(bytes), None)?
                .next()
                .ok_or_else(|| Error::internal("empty pairwise spill batch"))?
                .map_err(Error::from)
        })
        .await
    }
}

/// Invocation-owned staged codes and quantizer state. Replays do not access
/// the original index or source-table vectors.
pub struct PairwisePartition {
    pub(crate) encoded: EncodedPartition,
    pub(crate) scorer: Arc<PairwiseScorer>,
    pub(crate) batch_size: usize,
    pub(crate) num_rows: usize,
}

impl PairwisePartition {
    /// Stage each source batch as one in-memory staged batch, without row ID
    /// remapping. PQ sources must hold column-major codes. Benchmark support
    /// only; production staging goes through the IVF storage reader.
    #[doc(hidden)]
    pub fn stage_in_memory(
        quantizer: &Quantizer,
        centroid: ArrayRef,
        metric: DistanceType,
        sources: &[RecordBatch],
    ) -> Result<Self> {
        let schema = sources
            .first()
            .ok_or_else(|| Error::invalid_input("pairwise staging needs a source batch"))?
            .schema();
        let scorer = PairwiseScorer::new(quantizer, centroid, metric, &schema)?;
        let batches = sources
            .iter()
            .map(|source| scorer.stage(source, 0..source.num_rows(), None))
            .collect::<Result<Vec<_>>>()?;
        Ok(Self {
            batch_size: sources.iter().map(RecordBatch::num_rows).max().unwrap_or(0),
            num_rows: sources.iter().map(RecordBatch::num_rows).sum(),
            encoded: EncodedPartition::Memory(batches),
            scorer: Arc::new(scorer),
        })
    }

    /// Number of rows per staged batch, possibly smaller than the requested
    /// maximum to bound wide code arrays. The final batch can contain fewer rows.
    pub fn vector_batch_size(&self) -> usize {
        self.batch_size
    }

    pub fn num_batches(&self) -> usize {
        self.num_rows.div_ceil(self.batch_size)
    }

    /// Estimated staged bytes per row, a proxy for per-pair scoring work.
    pub fn row_bytes(&self) -> usize {
        self.scorer.row_bytes()
    }

    /// Whether staged batches live in spill storage. Each spilled
    /// [`Self::read_vectors`] decodes a fresh copy of the batch, while
    /// in-memory reads share the staged buffers.
    pub fn is_spilled(&self) -> bool {
        matches!(self.encoded, EncodedPartition::Spilled(_))
    }

    /// Read one staged batch. All rows with non-null IDs start out selected.
    pub async fn read_vectors(&self, batch_id: usize) -> Result<PairwiseVectorBatch> {
        let codes = match &self.encoded {
            EncodedPartition::Memory(batches) => {
                batches.get(batch_id).cloned().ok_or_else(|| {
                    Error::invalid_input(format!("pairwise batch_id={batch_id} out of range"))
                })?
            }
            EncodedPartition::Spilled(spill) => spill.read_batch(batch_id).await?,
        };
        let row_ids = codes
            .column_by_name(ROW_ID)
            .and_then(|ids| ids.as_primitive_opt::<UInt64Type>())
            .ok_or_else(|| Error::internal("pairwise batch missing row IDs"))?
            .clone();
        let selected = (0..row_ids.len()).map(|i| row_ids.is_valid(i)).collect();
        Ok(PairwiseVectorBatch {
            row_ids,
            codes,
            batch_id,
            selected,
        })
    }

    /// Score `anchor_rows` of `anchor` against every later row of `candidates`
    /// (all of its rows for a later batch, rows after the anchor within the
    /// same batch) and keep selected pairs with distinct IDs and finite
    /// `distance <= threshold`. Hits are ordered by anchor row, then candidate row.
    ///
    /// ```
    /// # use lance_index::vector::pairwise::PairwisePartition;
    /// # async fn example(partition: &PairwisePartition) -> lance_core::Result<()> {
    /// let batch = partition.read_vectors(0).await?;
    /// let hits = partition.score_block(&batch, 0..batch.num_rows(), &batch, 0.05)?;
    /// assert!(hits.row_id_a.len() < batch.num_rows() * batch.num_rows());
    /// # Ok(()) }
    /// ```
    pub fn score_block(
        &self,
        anchor: &PairwiseVectorBatch,
        anchor_rows: Range<usize>,
        candidates: &PairwiseVectorBatch,
        threshold: f32,
    ) -> Result<PairwiseHits> {
        if candidates.batch_id < anchor.batch_id {
            return Err(Error::invalid_input(format!(
                "pairwise candidate batch_id={} precedes anchor batch_id={}",
                candidates.batch_id, anchor.batch_id
            )));
        }
        if anchor_rows.start > anchor_rows.end || anchor_rows.end > anchor.num_rows() {
            return Err(Error::invalid_input(format!(
                "pairwise anchor rows {anchor_rows:?} out of range for {} rows",
                anchor.num_rows()
            )));
        }
        self.scorer
            .score_block(anchor, anchor_rows, candidates, threshold)
    }
}

/// Each Arrow stream is independently readable, so replay needs one local
/// range read, without opening a file or scanning earlier batches.
pub(crate) struct PairwiseSpillWriter {
    writer: Box<dyn Writer>,
    spill: Box<dyn Spill>,
    ranges: Vec<Range<usize>>,
    offset: usize,
}

impl PairwiseSpillWriter {
    pub(crate) async fn new(store: &dyn SpillStore) -> Result<Self> {
        let (writer, spill) = store.new_spill().await?;
        Ok(Self {
            writer,
            spill,
            ranges: Vec::new(),
            offset: 0,
        })
    }

    pub(crate) async fn write(&mut self, batch: RecordBatch) -> Result<()> {
        let bytes = spawn_cpu(move || -> Result<Vec<u8>> {
            let mut writer = StreamWriter::try_new(Vec::new(), batch.schema_ref())?;
            writer.write(&batch)?;
            writer.finish()?;
            Ok(writer.into_inner()?)
        })
        .await?;
        let end = self
            .offset
            .checked_add(bytes.len())
            .ok_or_else(|| Error::invalid_input("pairwise spill offset overflow"))?;
        self.writer.write_all(&bytes).await?;
        self.ranges.push(self.offset..end);
        self.offset = end;
        Ok(())
    }

    pub(crate) async fn finish(mut self) -> Result<SpilledPartition> {
        Writer::shutdown(self.writer.as_mut()).await?;
        drop(self.writer);
        let reader = self.spill.reader().await?;
        Ok(SpilledPartition {
            ranges: self.ranges,
            reader,
            _spill: self.spill,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vector::flat::index::FlatQuantizer;
    use crate::vector::quantizer::Quantization;
    use arrow_array::types::{Float32Type, UInt64Type};
    use lance_datagen::{array, gen_batch};
    use lance_io::spill::LocalSpillStore;
    use rstest::rstest;

    fn flat_batch() -> RecordBatch {
        let quantizer = FlatQuantizer::new(2, DistanceType::L2);
        gen_batch()
            .col(ROW_ID, array::step::<UInt64Type>())
            .col(quantizer.column(), array::rand_vec::<Float32Type>(2.into()))
            .into_batch_rows(32.into())
            .unwrap()
    }

    fn flat_scorer() -> PairwiseScorer {
        let batch = flat_batch();
        PairwiseScorer::new(
            &Quantizer::Flat(FlatQuantizer::new(2, DistanceType::L2)),
            Arc::new(arrow_array::Float32Array::from(vec![0.0f32; 2])),
            DistanceType::L2,
            batch.schema_ref(),
        )
        .unwrap()
    }

    #[tokio::test]
    async fn test_pairwise_spill_releases_disk_budget() {
        let batch = flat_batch();
        let store = LocalSpillStore::default();
        let mut writer = PairwiseSpillWriter::new(&store).await.unwrap();
        writer.write(batch.clone()).await.unwrap();
        let encoded = writer.finish().await.unwrap();
        let bytes = encoded.reader.size().await.unwrap();
        let spill_path =
            std::path::PathBuf::from(lance_io::local::to_local_path(encoded.reader.path()));
        let spill_dir = spill_path.parent().unwrap().to_owned();
        drop(encoded);
        let mut aborted = PairwiseSpillWriter::new(&store).await.unwrap();
        aborted.write(batch.clone()).await.unwrap();
        drop(aborted);
        assert_eq!(
            std::fs::read_dir(spill_dir).unwrap().count(),
            0,
            "cancelled preparation must remove temporary files"
        );

        let capped = LocalSpillStore::with_cap(bytes as u64).unwrap();
        let mut writer = PairwiseSpillWriter::new(&capped).await.unwrap();
        writer.write(batch.clone()).await.unwrap();
        let held = writer.finish().await.unwrap();
        let mut blocked = PairwiseSpillWriter::new(&capped).await.unwrap();
        let err = blocked.write(batch.clone()).await.unwrap_err();
        assert!(matches!(err, Error::DiskCapExceeded { .. }), "{err}");
        assert!(err.to_string().contains("cap"));
        drop(blocked);
        drop(held);

        // Dropping the prepared data must release its disk reservation, so the
        // same session can prepare another partition without leaking quota.
        let mut replacement = PairwiseSpillWriter::new(&capped).await.unwrap();
        replacement.write(batch).await.unwrap();
        drop(replacement.finish().await.unwrap());
    }

    #[tokio::test]
    async fn test_pairwise_spill_roundtrip_and_bounds() {
        let scorer = flat_scorer();
        let batch = flat_batch();
        let staged = [
            scorer.stage(&batch, 0..32, None).unwrap(),
            scorer.stage(&batch, 0..1, None).unwrap(),
        ];
        let store = LocalSpillStore::default();
        let mut writer = PairwiseSpillWriter::new(&store).await.unwrap();
        for batch in &staged {
            writer.write(batch.clone()).await.unwrap();
        }
        let prepared = PairwisePartition {
            encoded: EncodedPartition::Spilled(writer.finish().await.unwrap()),
            scorer: Arc::new(scorer),
            batch_size: 32,
            num_rows: 33,
        };
        assert_eq!(prepared.num_batches(), 2);
        for _ in 0..3 {
            for (id, expected) in staged.iter().enumerate() {
                let codes = prepared.read_vectors(id).await.unwrap();
                assert_eq!(codes.batch_id(), id);
                assert_eq!(&codes.codes, expected);
                assert_eq!(
                    &codes.row_ids,
                    expected[ROW_ID].as_primitive::<UInt64Type>()
                );
            }
        }
        for id in [2, usize::MAX] {
            let err = prepared.read_vectors(id).await.unwrap_err();
            assert!(matches!(err, Error::InvalidInput { .. }));
            assert!(err.to_string().contains("batch_id"));
        }
    }

    /// Distance `|value_a - value_b|` over one scalar per row.
    struct AbsDiff<'a> {
        anchor: &'a [f32],
        candidates: &'a [f32],
    }

    impl PairKernel for AbsDiff<'_> {
        fn distances(&self, anchor_row: usize, candidates: Range<usize>, out: &mut [f32]) {
            let a = self.anchor[anchor_row];
            for (out, &b) in out.iter_mut().zip(&self.candidates[candidates]) {
                *out = (a - b).abs();
            }
        }
    }

    fn scalar_batch(batch_id: usize, ids: Vec<Option<u64>>) -> PairwiseVectorBatch {
        let row_ids = UInt64Array::from(ids);
        let codes = RecordBatch::try_new(
            Arc::new(Schema::new(vec![Field::new(
                ROW_ID,
                DataType::UInt64,
                true,
            )])),
            vec![Arc::new(row_ids.clone())],
        )
        .unwrap();
        let selected = (0..row_ids.len()).map(|i| row_ids.is_valid(i)).collect();
        PairwiseVectorBatch {
            row_ids,
            codes,
            batch_id,
            selected,
        }
    }

    fn pairs(hits: &PairwiseHits) -> Vec<(u64, u64, f32)> {
        hits.row_id_a
            .iter()
            .zip(&hits.row_id_b)
            .zip(&hits.distances)
            .map(|((&a, &b), &d)| (a, b, d))
            .collect()
    }

    #[rstest]
    fn test_collect_pairs_tile_order(#[values(1, 2, 3, 64)] chunk_rows: usize) {
        let values_a = [0.0f32, 1.0, 1.0, 5.0, 2.0];
        let values_b = [1.0f32, 0.5, 9.0, 0.0];
        let mut anchor = scalar_batch(0, vec![Some(10), Some(11), None, Some(13), Some(14)]);
        // Row 14 is filtered out once per batch, as a deleted row would be.
        anchor.filter_rows(|id| id != 14);
        let candidates = scalar_batch(1, vec![Some(20), Some(21), Some(22), Some(10)]);
        let diagonal = AbsDiff {
            anchor: &values_a,
            candidates: &values_a,
        };
        // Diagonal tile: upper triangle only; null and filtered rows are skipped
        // both as anchors and candidates; the threshold is inclusive.
        let hits = collect_pairs(&diagonal, chunk_rows, &anchor, 0..5, &anchor, 1.0);
        assert_eq!(pairs(&hits), vec![(10, 11, 1.0)]);
        let hits = collect_pairs(&diagonal, chunk_rows, &anchor, 1..3, &anchor, 4.0);
        assert_eq!(pairs(&hits), vec![(11, 13, 4.0)]);

        // Off-diagonal tile: every candidate row, row-major, excluding the
        // anchor's own ID (a remapper can alias two rows to one ID).
        let tile = AbsDiff {
            anchor: &values_a,
            candidates: &values_b,
        };
        let hits = collect_pairs(&tile, chunk_rows, &anchor, 0..5, &candidates, 1.0);
        assert_eq!(
            pairs(&hits),
            vec![
                (10, 20, 1.0),
                (10, 21, 0.5),
                (11, 20, 0.0),
                (11, 21, 0.5),
                (11, 10, 1.0),
            ]
        );
    }

    #[test]
    fn test_collect_pairs_drops_non_finite() {
        let values = [0.0f32, f32::NAN, f32::NEG_INFINITY, 0.0];
        let batch = scalar_batch(0, (0..4).map(Some).collect());
        let kernel = AbsDiff {
            anchor: &values,
            candidates: &values,
        };
        let hits = collect_pairs(&kernel, 2, &batch, 0..4, &batch, f32::MAX);
        assert_eq!(pairs(&hits), vec![(0, 3, 0.0)]);
    }

    #[test]
    fn test_cosine_from_l2() {
        assert_eq!(cosine_from_l2(0.0, 3.0, 3.0), 0.0);
        // [1, 0] vs [0, 2]: orthogonal.
        assert_eq!(cosine_from_l2(5.0, 1.0, 2.0), 1.0);
        // [1, 0] vs [-2, 0]: opposite.
        assert_eq!(cosine_from_l2(9.0, 1.0, 2.0), 2.0);
        // Roundoff outside [0, 2] is clamped.
        assert_eq!(cosine_from_l2(9.5, 1.0, 2.0), 2.0);
        assert!(cosine_from_l2(1.0, 0.0, 1.0).is_nan());
    }

    #[tokio::test]
    async fn test_score_block_validates_tiles() {
        let scorer = flat_scorer();
        let batch = flat_batch();
        let prepared = PairwisePartition {
            encoded: EncodedPartition::Memory(vec![
                scorer.stage(&batch, 0..16, None).unwrap(),
                scorer.stage(&batch, 16..32, None).unwrap(),
            ]),
            scorer: Arc::new(scorer),
            batch_size: 16,
            num_rows: 32,
        };
        let first = prepared.read_vectors(0).await.unwrap();
        let second = prepared.read_vectors(1).await.unwrap();
        let err = prepared
            .score_block(&second, 0..1, &first, 1.0)
            .unwrap_err();
        assert!(matches!(err, Error::InvalidInput { .. }));
        assert!(err.to_string().contains("precedes"), "{err}");
        let err = prepared
            .score_block(&first, 0..17, &second, 1.0)
            .unwrap_err();
        assert!(err.to_string().contains("out of range"), "{err}");
        let all = prepared
            .score_block(&first, 0..16, &second, f32::MAX)
            .unwrap();
        assert_eq!(all.row_id_a.len(), 16 * 16);
        let diagonal = prepared
            .score_block(&first, 0..16, &first, f32::MAX)
            .unwrap();
        assert_eq!(diagonal.row_id_a.len(), 16 * 15 / 2);
    }
}