hawdb-core 0.14.0

An embedded Rust graph database for local data-plane workloads
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
// Copyright 2026 Nowledge
// Licensed under the Apache License, Version 2.0.

//! Shared finite Float32 values and exact distance operators for SQL and Cypher.
//! The canonical payload is a little-endian u32 dimension followed by f32 bits.
//! Byte-backed storage permits unaligned, allocation-free views of row pages.

use crate::{HawDBError, Result, ValueRef, VectorDimension};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Vector {
    encoded: Vec<u8>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct VectorRef<'a> {
    encoded: &'a [u8],
    dimension: VectorDimension,
}

impl Vector {
    pub fn new(coordinates: &[f32]) -> Result<Self> {
        let dimension = u32::try_from(coordinates.len())
            .ok()
            .and_then(VectorDimension::new)
            .ok_or_else(|| invalid("dimension must be a positive u32"))?;
        if coordinates.iter().any(|value| !value.is_finite()) {
            return Err(invalid("coordinates must be finite"));
        }
        let bytes = coordinates
            .len()
            .checked_mul(4)
            .and_then(|n| n.checked_add(4))
            .ok_or_else(|| invalid("payload length overflow"))?;
        let mut encoded = Vec::with_capacity(bytes);
        encoded.extend_from_slice(&dimension.get().to_le_bytes());
        for value in coordinates {
            encoded.extend_from_slice(&value.to_le_bytes());
        }
        Ok(Self { encoded })
    }

    /// Takes ownership of an already admitted payload without an additional copy.
    pub fn from_encoded(encoded: Vec<u8>) -> Result<Self> {
        Self::from_encoded_with_work(encoded, || Ok(()), |_| Ok(()), std::convert::identity)
    }

    /// Validates in bounded units before taking ownership, without rescanning.
    pub fn from_encoded_with_work<T, E>(
        encoded: Vec<u8>,
        start: impl FnMut() -> std::result::Result<T, E>,
        finish: impl FnMut(T) -> std::result::Result<(), E>,
        invalid: impl Fn(HawDBError) -> E,
    ) -> std::result::Result<Self, E> {
        VectorRef::from_encoded_with_work(&encoded, start, finish, invalid)?;
        Ok(Self { encoded })
    }

    pub fn dimension(&self) -> VectorDimension {
        VectorDimension::new(u32::from_le_bytes(
            self.encoded[..4].try_into().expect("validated dimension"),
        ))
        .expect("nonzero dimension")
    }
    pub fn retained_capacity(&self) -> usize {
        self.encoded.capacity()
    }

    pub fn as_ref(&self) -> VectorRef<'_> {
        VectorRef {
            dimension: self.dimension(),
            encoded: &self.encoded,
        }
    }

    pub fn encoded(&self) -> &[u8] {
        &self.encoded
    }
}

impl<'a> VectorRef<'a> {
    /// Validates length and finite components before any caller-owned allocation.
    pub fn from_encoded(encoded: &'a [u8]) -> Result<Self> {
        Self::from_encoded_with_work(encoded, || Ok(()), |_| Ok(()), std::convert::identity)
    }

    /// Each work unit scans at most 64 KiB; callbacks own admission and cancellation.
    pub fn from_encoded_with_work<T, E>(
        encoded: &'a [u8],
        mut start: impl FnMut() -> std::result::Result<T, E>,
        mut finish: impl FnMut(T) -> std::result::Result<(), E>,
        map_invalid: impl Fn(HawDBError) -> E,
    ) -> std::result::Result<Self, E> {
        let shape = || -> Result<VectorDimension> {
            let prefix: [u8; 4] = encoded
                .get(..4)
                .and_then(|bytes| bytes.try_into().ok())
                .ok_or_else(|| invalid("truncated dimension"))?;
            let dimension = VectorDimension::new(u32::from_le_bytes(prefix))
                .ok_or_else(|| invalid("dimension must be positive"))?;
            let expected = usize::try_from(dimension.get())
                .ok()
                .and_then(|n| n.checked_mul(4))
                .and_then(|n| n.checked_add(4))
                .ok_or_else(|| invalid("payload length overflow"))?;
            if encoded.len() != expected {
                return Err(invalid("payload length does not match dimension"));
            }
            Ok(dimension)
        };
        let dimension = shape().map_err(&map_invalid)?;
        for block in encoded[4..].chunks(64 * 1024) {
            let unit = start()?;
            let finite = block.chunks_exact(4).all(|bytes| {
                f32::from_le_bytes(bytes.try_into().expect("fixed-width coordinate")).is_finite()
            });
            finish(unit)?;
            if !finite {
                return Err(map_invalid(invalid("coordinates must be finite")));
            }
        }
        Ok(Self { dimension, encoded })
    }

    pub const fn dimension(self) -> VectorDimension {
        self.dimension
    }
    pub const fn encoded(self) -> &'a [u8] {
        self.encoded
    }

    pub fn coordinates(self) -> impl ExactSizeIterator<Item = f32> + 'a {
        self.encoded[4..]
            .chunks_exact(4)
            .map(|bytes| f32::from_le_bytes(bytes.try_into().expect("fixed-width coordinate")))
    }

    /// The caller must admit the payload before materializing a borrowed value.
    pub fn to_owned(self) -> Vector {
        Vector {
            encoded: self.encoded.to_vec(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VectorDistance {
    Cosine,
    Euclidean,
}

impl VectorDistance {
    pub fn from_function_name(name: &str) -> Option<Self> {
        if name.eq_ignore_ascii_case("cosine_distance") {
            Some(Self::Cosine)
        } else if name.eq_ignore_ascii_case("l2_distance") {
            Some(Self::Euclidean)
        } else {
            None
        }
    }

    pub const fn function_name(self) -> &'static str {
        match self {
            Self::Cosine => "cosine_distance",
            Self::Euclidean => "l2_distance",
        }
    }

    pub fn distance(self, left: VectorRef<'_>, right: VectorRef<'_>) -> Result<f64> {
        self.distance_with_work(left, right, || Ok(()))
    }

    /// Cooperatively scores canonical values without copying their payloads.
    pub fn distance_with_work(
        self,
        left: VectorRef<'_>,
        right: VectorRef<'_>,
        checkpoint: impl FnMut() -> Result<()>,
    ) -> Result<f64> {
        if left.dimension != right.dimension {
            return Err(invalid("distance dimensions must match"));
        }
        self.distance_coordinates(left.coordinates(), right.coordinates(), checkpoint)
    }

    /// Scores admitted raw projection vectors with the same mathematical
    /// contract as SQL/Cypher, without creating canonical payload copies.
    pub fn distance_slices_with_work(
        self,
        left: &[f32],
        right: &[f32],
        checkpoint: impl FnMut() -> Result<()>,
    ) -> Result<f64> {
        if left.len() != right.len() {
            return Err(invalid("distance dimensions must match"));
        }
        if left.is_empty() || u32::try_from(left.len()).is_err() {
            return Err(invalid("dimension must be a positive u32"));
        }
        self.distance_coordinates(left.iter().copied(), right.iter().copied(), checkpoint)
    }

    fn distance_coordinates(
        self,
        left: impl Iterator<Item = f32>,
        right: impl Iterator<Item = f32>,
        mut checkpoint: impl FnMut() -> Result<()>,
    ) -> Result<f64> {
        let (mut dot, mut left_norm, mut right_norm, mut squared) =
            (0.0f64, 0.0f64, 0.0f64, 0.0f64);
        for (index, (left, right)) in left.zip(right).enumerate() {
            // Read at most 64 KiB across both operands between checkpoints.
            if index % (64 * 1024 / (2 * std::mem::size_of::<f32>())) == 0 {
                checkpoint()?;
            }
            if !left.is_finite() || !right.is_finite() {
                return Err(invalid("coordinates must be finite"));
            }
            let (left, right) = (f64::from(left), f64::from(right));
            match self {
                Self::Euclidean => squared += (left - right).powi(2),
                Self::Cosine => {
                    dot += left * right;
                    left_norm += left * left;
                    right_norm += right * right;
                }
            }
        }
        checkpoint()?;
        match self {
            Self::Euclidean => Ok(squared.sqrt()),
            Self::Cosine if left_norm == 0.0 || right_norm == 0.0 => {
                Err(invalid("cosine distance requires nonzero vectors"))
            }
            Self::Cosine => {
                Ok(1.0 - (dot / (left_norm.sqrt() * right_norm.sqrt())).clamp(-1.0, 1.0))
            }
        }
    }

    pub fn evaluate(self, left: ValueRef<'_>, right: ValueRef<'_>) -> Result<Option<f64>> {
        match (left, right) {
            (ValueRef::Null, _) | (_, ValueRef::Null) => Ok(None),
            (ValueRef::Vector(left), ValueRef::Vector(right)) => {
                self.distance(left, right).map(Some)
            }
            _ => Err(invalid("distance arguments must be vectors")),
        }
    }
}

fn invalid(reason: &str) -> HawDBError {
    HawDBError::Semantic(format!("invalid vector: {reason}"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn projection_distance_preserves_signed_cosine_and_canonical_parity() {
        let query = [1.0, 0.0];
        let values = [[1.0, 0.0], [0.0, 1.0], [-0.6, 0.8], [-1.0, 0.0]];
        for metric in [VectorDistance::Cosine, VectorDistance::Euclidean] {
            for coordinates in values {
                let canonical = metric
                    .distance(
                        Vector::new(&query).unwrap().as_ref(),
                        Vector::new(&coordinates).unwrap().as_ref(),
                    )
                    .unwrap();
                let projection = metric
                    .distance_slices_with_work(&query, &coordinates, || Ok(()))
                    .unwrap();
                assert_eq!(projection.to_bits(), canonical.to_bits());
            }
        }
        let distance = |coordinates: &[f32]| {
            VectorDistance::Cosine
                .distance_slices_with_work(&query, coordinates, || Ok(()))
                .unwrap()
        };
        assert!(distance(&values[2]) > 1.0);
        assert!(distance(&values[2]) < distance(&values[3]));
        assert_eq!(distance(&values[3]), 2.0);
        for coordinates in [&[][..], &[0.0][..], &[f32::NAN, 0.0][..], &[0.0, 0.0][..]] {
            assert!(VectorDistance::Cosine
                .distance_slices_with_work(&query, coordinates, || Ok(()))
                .is_err());
        }
    }

    #[test]
    fn projection_distance_preserves_cooperative_failure() {
        let coordinates = vec![1.0; 20_000];
        let mut checkpoints = 0;
        let error = VectorDistance::Cosine
            .distance_slices_with_work(&coordinates, &coordinates, || {
                checkpoints += 1;
                if checkpoints == 2 {
                    Err(HawDBError::ProjectionBusy)
                } else {
                    Ok(())
                }
            })
            .unwrap_err();
        assert!(matches!(error, HawDBError::ProjectionBusy));
        assert_eq!(checkpoints, 2);
    }

    #[test]
    fn vector_encoding_rejects_malformed_and_nonfinite_payloads() {
        let value = Vector::new(&[1.0, -2.5]).unwrap();
        assert_eq!(value.encoded(), &[2, 0, 0, 0, 0, 0, 128, 63, 0, 0, 32, 192]);
        assert_eq!(
            Vector::from_encoded(value.encoded().to_vec()).unwrap(),
            value
        );
        let mut unaligned = vec![0];
        unaligned.extend_from_slice(value.encoded());
        assert_eq!(
            VectorRef::from_encoded(&unaligned[1..]).unwrap(),
            value.as_ref()
        );
        for size in 0..value.encoded().len() {
            assert!(VectorRef::from_encoded(&value.encoded()[..size]).is_err());
        }
        assert!(VectorRef::from_encoded(&[0, 0, 0, 0]).is_err());
        assert!(VectorRef::from_encoded(&u32::MAX.to_le_bytes()).is_err());
        for component in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
            assert!(Vector::new(&[component]).is_err());
            let mut encoded = 1u32.to_le_bytes().to_vec();
            encoded.extend_from_slice(&component.to_le_bytes());
            assert!(VectorRef::from_encoded(&encoded).is_err());
        }
        assert!(Vector::new(&[]).is_err());
    }

    #[test]
    fn vector_distance_is_mathematical_and_shared() {
        let left = Vector::new(&[1.0, 0.0]).unwrap();
        let opposite = Vector::new(&[-1.0, 0.0]).unwrap();
        let zero = Vector::new(&[0.0, 0.0]).unwrap();
        assert_eq!(
            VectorDistance::Cosine
                .distance(left.as_ref(), opposite.as_ref())
                .unwrap(),
            2.0
        );
        assert_eq!(
            VectorDistance::Euclidean
                .distance(left.as_ref(), zero.as_ref())
                .unwrap(),
            1.0
        );
        assert!(VectorDistance::Cosine
            .distance(left.as_ref(), zero.as_ref())
            .is_err());
        assert!(VectorDistance::Euclidean
            .distance(left.as_ref(), Vector::new(&[1.0]).unwrap().as_ref())
            .is_err());
        assert_eq!(
            VectorDistance::Cosine
                .evaluate(ValueRef::Null, ValueRef::Vector(zero.as_ref()))
                .unwrap(),
            None
        );
        let large = Vector::new(&[f32::MAX, f32::MAX]).unwrap();
        assert!(VectorDistance::Euclidean
            .distance(large.as_ref(), zero.as_ref())
            .unwrap()
            .is_finite());
        assert!(
            VectorDistance::Cosine
                .distance(large.as_ref(), large.as_ref())
                .unwrap()
                .abs()
                < 1e-12
        );
    }
}

impl std::hash::Hash for VectorRef<'_> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.encoded.hash(state);
    }
}

#[cfg(test)]
mod work_tests {
    use super::*;

    #[test]
    fn shared_vector_validation_stops_between_bounded_coordinate_blocks() {
        let vector = Vector::new(&vec![1.0; 32769]).unwrap();
        let calls = std::cell::Cell::new(0);
        let error = Vector::from_encoded_with_work(
            vector.encoded().to_vec(),
            || {
                calls.set(calls.get() + 1);
                if calls.get() == 2 {
                    Err("cancelled".to_owned())
                } else {
                    Ok(())
                }
            },
            |_| Ok(()),
            |error| error.to_string(),
        )
        .unwrap_err();
        assert_eq!(error, "cancelled");
        assert_eq!(calls.get(), 2);
        let mut encoded = vector.encoded().to_vec();
        let last = encoded.len() - 4;
        encoded[last..].copy_from_slice(&f32::NAN.to_le_bytes());
        assert!(Vector::from_encoded(encoded).is_err());
    }
}