sqlite-vector-rs 0.2.2

SQLite extension providing PGVector-like native vector types with HNSW indexing
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
use std::fmt;

use bytemuck::{Pod, cast_slice};
use half::f16;

/// Errors from vector type operations.
#[derive(Debug, Clone, PartialEq)]
pub enum VectorTypeError {
    UnknownType(String),
    DimensionMismatch { expected: usize, got: usize },
    NonFiniteValue,
}

impl fmt::Display for VectorTypeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownType(name) => write!(f, "unknown vector type: {name}"),
            Self::DimensionMismatch { expected, got } => {
                write!(f, "expected {expected} dimensions, got {got}")
            }
            Self::NonFiniteValue => write!(f, "vector contains NaN or Inf"),
        }
    }
}

impl std::error::Error for VectorTypeError {}

/// Supported vector element types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VectorType {
    Float2,
    Float4,
    Float8,
    Int1,
    Int2,
    Int4,
}

impl VectorType {
    /// Parse a SQL type name into a VectorType.
    pub fn from_name(name: &str) -> Result<Self, VectorTypeError> {
        match name {
            "float2" => Ok(Self::Float2),
            "float4" => Ok(Self::Float4),
            "float8" => Ok(Self::Float8),
            "int1" => Ok(Self::Int1),
            "int2" => Ok(Self::Int2),
            "int4" => Ok(Self::Int4),
            other => Err(VectorTypeError::UnknownType(other.to_string())),
        }
    }

    /// Size in bytes of one element.
    pub fn element_size(&self) -> usize {
        match self {
            Self::Float2 => 2,
            Self::Float4 => 4,
            Self::Float8 => 8,
            Self::Int1 => 1,
            Self::Int2 => 2,
            Self::Int4 => 4,
        }
    }

    /// Expected blob size for a given dimension.
    pub fn blob_size(&self, dim: usize) -> usize {
        dim * self.element_size()
    }

    /// Validate that a blob has the correct size for the given dimension.
    pub fn validate_blob(&self, blob: &[u8], dim: usize) -> Result<(), VectorTypeError> {
        let expected = self.blob_size(dim);
        if blob.len() != expected {
            return Err(VectorTypeError::DimensionMismatch {
                expected: dim,
                got: blob.len() / self.element_size(),
            });
        }
        Ok(())
    }

    /// Check that all float values are finite (not NaN or Inf).
    /// No-op for integer types. The `dim` parameter is used to verify
    /// the blob length before casting.
    pub fn validate_finite(&self, blob: &[u8], dim: usize) -> Result<(), VectorTypeError> {
        self.validate_blob(blob, dim)?;
        match self {
            Self::Float2 => {
                let values: &[f16] = cast_slice(blob);
                if values.iter().any(|v| !v.is_finite()) {
                    return Err(VectorTypeError::NonFiniteValue);
                }
            }
            Self::Float4 => {
                let values: &[f32] = cast_slice(blob);
                if values.iter().any(|v| !v.is_finite()) {
                    return Err(VectorTypeError::NonFiniteValue);
                }
            }
            Self::Float8 => {
                let values: &[f64] = cast_slice(blob);
                if values.iter().any(|v| !v.is_finite()) {
                    return Err(VectorTypeError::NonFiniteValue);
                }
            }
            Self::Int1 | Self::Int2 | Self::Int4 => {} // integers are always finite
        }
        Ok(())
    }

    /// Cast a typed slice to a byte blob. Generic helper.
    pub fn slice_to_blob<T: Pod>(&self, values: &[T]) -> Vec<u8> {
        cast_slice(values).to_vec()
    }

    /// Cast a byte blob back to a typed slice. Generic helper.
    /// Caller must ensure the blob was created with the matching type.
    pub fn blob_to_slice<'a, T: Pod>(&self, blob: &'a [u8]) -> &'a [T] {
        cast_slice(blob)
    }

    /// Returns true if this is a float type (has NaN/Inf concerns).
    pub fn is_float(&self) -> bool {
        matches!(self, Self::Float2 | Self::Float4 | Self::Float8)
    }

    /// SQL type name string.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Float2 => "float2",
            Self::Float4 => "float4",
            Self::Float8 => "float8",
            Self::Int1 => "int1",
            Self::Int2 => "int2",
            Self::Int4 => "int4",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytemuck::cast_slice;
    use half::f16;

    // ----------------------------------------------------------------
    // from_name
    // ----------------------------------------------------------------

    #[test]
    fn from_name_all_valid() {
        assert_eq!(VectorType::from_name("float2"), Ok(VectorType::Float2));
        assert_eq!(VectorType::from_name("float4"), Ok(VectorType::Float4));
        assert_eq!(VectorType::from_name("float8"), Ok(VectorType::Float8));
        assert_eq!(VectorType::from_name("int1"), Ok(VectorType::Int1));
        assert_eq!(VectorType::from_name("int2"), Ok(VectorType::Int2));
        assert_eq!(VectorType::from_name("int4"), Ok(VectorType::Int4));
    }

    #[test]
    fn from_name_unknown_returns_error() {
        let err = VectorType::from_name("float3").unwrap_err();
        assert_eq!(err, VectorTypeError::UnknownType("float3".to_string()));
    }

    #[test]
    fn from_name_case_sensitive() {
        // "Float4" is not a valid name — the matcher is lowercase-only.
        assert!(VectorType::from_name("Float4").is_err());
        assert!(VectorType::from_name("INT1").is_err());
        assert!(VectorType::from_name("").is_err());
    }

    // ----------------------------------------------------------------
    // name — round-trip with from_name
    // ----------------------------------------------------------------

    #[test]
    fn name_round_trips_with_from_name() {
        let variants = [
            VectorType::Float2,
            VectorType::Float4,
            VectorType::Float8,
            VectorType::Int1,
            VectorType::Int2,
            VectorType::Int4,
        ];
        for vt in variants {
            assert_eq!(VectorType::from_name(vt.name()), Ok(vt));
        }
    }

    // ----------------------------------------------------------------
    // element_size
    // ----------------------------------------------------------------

    #[test]
    fn element_size_correct() {
        assert_eq!(VectorType::Float2.element_size(), 2);
        assert_eq!(VectorType::Float4.element_size(), 4);
        assert_eq!(VectorType::Float8.element_size(), 8);
        assert_eq!(VectorType::Int1.element_size(), 1);
        assert_eq!(VectorType::Int2.element_size(), 2);
        assert_eq!(VectorType::Int4.element_size(), 4);
    }

    // ----------------------------------------------------------------
    // blob_size
    // ----------------------------------------------------------------

    #[test]
    fn blob_size_is_element_size_times_dim() {
        for vt in [
            VectorType::Float2,
            VectorType::Float4,
            VectorType::Float8,
            VectorType::Int1,
            VectorType::Int2,
            VectorType::Int4,
        ] {
            for dim in [0, 1, 3, 128, 1536] {
                assert_eq!(vt.blob_size(dim), vt.element_size() * dim);
            }
        }
    }

    // ----------------------------------------------------------------
    // validate_blob
    // ----------------------------------------------------------------

    #[test]
    fn validate_blob_correct_size_ok() {
        let blob = vec![0u8; VectorType::Float4.blob_size(4)]; // 16 bytes
        assert!(VectorType::Float4.validate_blob(&blob, 4).is_ok());
    }

    #[test]
    fn validate_blob_too_short_returns_error() {
        let blob = vec![0u8; 12]; // 12 bytes for a 4-dim float4 should be 16
        let err = VectorType::Float4.validate_blob(&blob, 4).unwrap_err();
        assert_eq!(
            err,
            VectorTypeError::DimensionMismatch {
                expected: 4,
                got: 3
            }
        );
    }

    #[test]
    fn validate_blob_too_long_returns_error() {
        let blob = vec![0u8; 20]; // 20 bytes for a 4-dim float4 should be 16
        let err = VectorType::Float4.validate_blob(&blob, 4).unwrap_err();
        assert_eq!(
            err,
            VectorTypeError::DimensionMismatch {
                expected: 4,
                got: 5
            }
        );
    }

    #[test]
    fn validate_blob_int_types() {
        let blob = vec![0u8; 6]; // 6 × i16 == 3 dims
        assert!(VectorType::Int2.validate_blob(&blob, 3).is_ok());

        let err = VectorType::Int2.validate_blob(&blob, 4).unwrap_err();
        assert_eq!(
            err,
            VectorTypeError::DimensionMismatch {
                expected: 4,
                got: 3
            }
        );
    }

    // ----------------------------------------------------------------
    // validate_finite
    // ----------------------------------------------------------------

    #[test]
    fn validate_finite_all_finite_f32_ok() {
        let values: Vec<f32> = vec![1.0, -2.5, 0.0, f32::MAX];
        let blob = VectorType::Float4.slice_to_blob(&values);
        assert!(VectorType::Float4.validate_finite(&blob, 4).is_ok());
    }

    #[test]
    fn validate_finite_nan_f32_errors() {
        let values: Vec<f32> = vec![1.0, f32::NAN, 3.0];
        let blob = VectorType::Float4.slice_to_blob(&values);
        assert_eq!(
            VectorType::Float4.validate_finite(&blob, 3).unwrap_err(),
            VectorTypeError::NonFiniteValue
        );
    }

    #[test]
    fn validate_finite_inf_f32_errors() {
        let values: Vec<f32> = vec![1.0, f32::INFINITY];
        let blob = VectorType::Float4.slice_to_blob(&values);
        assert_eq!(
            VectorType::Float4.validate_finite(&blob, 2).unwrap_err(),
            VectorTypeError::NonFiniteValue
        );
    }

    #[test]
    fn validate_finite_neg_inf_f64_errors() {
        let values: Vec<f64> = vec![0.0, f64::NEG_INFINITY];
        let blob = VectorType::Float8.slice_to_blob(&values);
        assert_eq!(
            VectorType::Float8.validate_finite(&blob, 2).unwrap_err(),
            VectorTypeError::NonFiniteValue
        );
    }

    #[test]
    fn validate_finite_all_finite_f64_ok() {
        let values: Vec<f64> = vec![1.0, -2.5, 0.0, f64::MAX];
        let blob = VectorType::Float8.slice_to_blob(&values);
        assert!(VectorType::Float8.validate_finite(&blob, 4).is_ok());
    }

    #[test]
    fn validate_finite_nan_f16_errors() {
        let values: Vec<f16> = vec![f16::from_f32(1.0), f16::NAN];
        let blob = VectorType::Float2.slice_to_blob(&values);
        assert_eq!(
            VectorType::Float2.validate_finite(&blob, 2).unwrap_err(),
            VectorTypeError::NonFiniteValue
        );
    }

    #[test]
    fn validate_finite_inf_f16_errors() {
        let values: Vec<f16> = vec![f16::INFINITY];
        let blob = VectorType::Float2.slice_to_blob(&values);
        assert_eq!(
            VectorType::Float2.validate_finite(&blob, 1).unwrap_err(),
            VectorTypeError::NonFiniteValue
        );
    }

    #[test]
    fn validate_finite_all_finite_f16_ok() {
        let values: Vec<f16> = vec![f16::from_f32(1.0), f16::from_f32(-0.5), f16::from_f32(0.0)];
        let blob = VectorType::Float2.slice_to_blob(&values);
        assert!(VectorType::Float2.validate_finite(&blob, 3).is_ok());
    }

    #[test]
    fn validate_finite_integer_types_always_ok() {
        // Integer types never contain NaN/Inf; validate_finite should be a no-op.
        let i8_blob = VectorType::Int1.slice_to_blob::<i8>(&[i8::MIN, 0, i8::MAX]);
        let i16_blob = VectorType::Int2.slice_to_blob::<i16>(&[i16::MIN, 0, i16::MAX]);
        let i32_blob = VectorType::Int4.slice_to_blob::<i32>(&[i32::MIN, 0, i32::MAX]);

        assert!(VectorType::Int1.validate_finite(&i8_blob, 3).is_ok());
        assert!(VectorType::Int2.validate_finite(&i16_blob, 3).is_ok());
        assert!(VectorType::Int4.validate_finite(&i32_blob, 3).is_ok());
    }

    // ----------------------------------------------------------------
    // is_float
    // ----------------------------------------------------------------

    #[test]
    fn is_float_true_for_float_variants() {
        assert!(VectorType::Float2.is_float());
        assert!(VectorType::Float4.is_float());
        assert!(VectorType::Float8.is_float());
    }

    #[test]
    fn is_float_false_for_int_variants() {
        assert!(!VectorType::Int1.is_float());
        assert!(!VectorType::Int2.is_float());
        assert!(!VectorType::Int4.is_float());
    }

    // ----------------------------------------------------------------
    // slice_to_blob / blob_to_slice — round-trip
    // ----------------------------------------------------------------

    #[test]
    fn round_trip_f32() {
        let original: Vec<f32> = vec![1.0, -2.5, 3.125, 0.0];
        let blob = VectorType::Float4.slice_to_blob(&original);
        assert_eq!(blob.len(), original.len() * 4);
        let recovered: &[f32] = VectorType::Float4.blob_to_slice(&blob);
        assert_eq!(recovered, original.as_slice());
    }

    #[test]
    fn round_trip_f64() {
        let original: Vec<f64> = vec![1.0, -2.5, f64::MAX, f64::MIN_POSITIVE];
        let blob = VectorType::Float8.slice_to_blob(&original);
        assert_eq!(blob.len(), original.len() * 8);
        let recovered: &[f64] = VectorType::Float8.blob_to_slice(&blob);
        assert_eq!(recovered, original.as_slice());
    }

    #[test]
    fn round_trip_f16() {
        let original: Vec<f16> = vec![f16::from_f32(1.0), f16::from_f32(-0.5), f16::from_f32(0.0)];
        let blob = VectorType::Float2.slice_to_blob(&original);
        assert_eq!(blob.len(), original.len() * 2);
        let recovered: &[f16] = VectorType::Float2.blob_to_slice(&blob);
        assert_eq!(recovered, original.as_slice());
    }

    #[test]
    fn round_trip_i8() {
        let original: Vec<i8> = vec![i8::MIN, -1, 0, 1, i8::MAX];
        let blob = VectorType::Int1.slice_to_blob(&original);
        assert_eq!(blob.len(), original.len());
        let recovered: &[i8] = VectorType::Int1.blob_to_slice(&blob);
        assert_eq!(recovered, original.as_slice());
    }

    #[test]
    fn round_trip_i16() {
        let original: Vec<i16> = vec![i16::MIN, -1, 0, 1, i16::MAX];
        let blob = VectorType::Int2.slice_to_blob(&original);
        assert_eq!(blob.len(), original.len() * 2);
        let recovered: &[i16] = VectorType::Int2.blob_to_slice(&blob);
        assert_eq!(recovered, original.as_slice());
    }

    #[test]
    fn round_trip_i32() {
        let original: Vec<i32> = vec![i32::MIN, -1, 0, 1, i32::MAX];
        let blob = VectorType::Int4.slice_to_blob(&original);
        assert_eq!(blob.len(), original.len() * 4);
        let recovered: &[i32] = VectorType::Int4.blob_to_slice(&blob);
        assert_eq!(recovered, original.as_slice());
    }

    // Verify that slice_to_blob produces the same bytes as bytemuck::cast_slice
    // directly, ensuring no extra copies or byte-swaps are introduced.
    #[test]
    fn slice_to_blob_matches_bytemuck_cast_slice() {
        let values: Vec<f32> = vec![1.0_f32, 2.0, 3.0];
        let expected: &[u8] = cast_slice(&values);
        let got = VectorType::Float4.slice_to_blob(&values);
        assert_eq!(got.as_slice(), expected);
    }
}