ruvector-postgres 2.0.5

High-performance PostgreSQL vector database extension v2 - pgvector drop-in replacement with 230+ SQL functions, SIMD acceleration, Flash Attention, GNN layers, hybrid search, multi-tenancy, self-healing, and self-learning capabilities
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
# RuVector PostgreSQL Extension - Testing Guide

## Overview

This document describes the comprehensive test framework for ruvector-postgres, a high-performance PostgreSQL vector similarity search extension.

## Test Organization

### Test Structure

```
tests/
├── unit_vector_tests.rs              # Unit tests for RuVector type
├── unit_halfvec_tests.rs             # Unit tests for HalfVec type
├── integration_distance_tests.rs     # pgrx integration tests
├── property_based_tests.rs           # Property-based tests with proptest
├── pgvector_compatibility_tests.rs   # pgvector regression tests
├── stress_tests.rs                   # Concurrency and memory stress tests
├── simd_consistency_tests.rs         # SIMD vs scalar consistency
├── quantized_types_test.rs           # Quantized vector types
├── parallel_execution_test.rs        # Parallel query execution
└── hnsw_index_tests.sql              # SQL-level index tests
```

## Test Categories

### 1. Unit Tests

**Purpose**: Test individual components in isolation.

**Files**:
- `unit_vector_tests.rs` - RuVector type
- `unit_halfvec_tests.rs` - HalfVec type

**Coverage**:
- Vector creation and initialization
- Varlena serialization/deserialization
- Vector arithmetic operations
- String parsing and formatting
- Memory layout and alignment
- Edge cases and boundary conditions

**Example**:
```rust
#[test]
fn test_varlena_roundtrip_basic() {
    unsafe {
        let v1 = RuVector::from_slice(&[1.0, 2.0, 3.0]);
        let varlena = v1.to_varlena();
        let v2 = RuVector::from_varlena(varlena);
        assert_eq!(v1, v2);
        pgrx::pg_sys::pfree(varlena as *mut std::ffi::c_void);
    }
}
```

### 2. pgrx Integration Tests

**Purpose**: Test the extension running inside PostgreSQL.

**File**: `integration_distance_tests.rs`

**Coverage**:
- SQL operators (`<->`, `<=>`, `<#>`, `<+>`)
- Distance functions (L2, cosine, inner product, L1)
- SIMD consistency across vector sizes
- Error handling and validation
- Symmetry properties

**Example**:
```rust
#[pg_test]
fn test_l2_distance_basic() {
    let a = RuVector::from_slice(&[0.0, 0.0, 0.0]);
    let b = RuVector::from_slice(&[3.0, 4.0, 0.0]);
    let dist = ruvector_l2_distance(a, b);
    assert!((dist - 5.0).abs() < 1e-5);
}
```

### 3. Property-Based Tests

**Purpose**: Verify mathematical properties hold for random inputs.

**File**: `property_based_tests.rs`

**Framework**: `proptest`

**Properties Tested**:

#### Distance Functions
- Non-negativity: `d(a,b) ≥ 0`
- Symmetry: `d(a,b) = d(b,a)`
- Identity: `d(a,a) = 0`
- Triangle inequality: `d(a,c) ≤ d(a,b) + d(b,c)`
- Bounded ranges (cosine: [0,2])

#### Vector Operations
- Normalization produces unit vectors
- Addition identity: `v + 0 = v`
- Subtraction inverse: `(a + b) - b = a`
- Scalar multiplication: associativity, identity
- Dot product: commutativity
- Norm squared equals self-dot product

**Example**:
```rust
proptest! {
    #[test]
    fn prop_l2_distance_non_negative(
        v1 in prop::collection::vec(-1000.0f32..1000.0f32, 1..100),
        v2 in prop::collection::vec(-1000.0f32..1000.0f32, 1..100)
    ) {
        if v1.len() == v2.len() {
            let dist = euclidean_distance(&v1, &v2);
            prop_assert!(dist >= 0.0);
            prop_assert!(dist.is_finite());
        }
    }
}
```

### 4. pgvector Compatibility Tests

**Purpose**: Ensure drop-in compatibility with pgvector.

**File**: `pgvector_compatibility_tests.rs`

**Coverage**:
- Distance calculation parity
- Operator symbol compatibility
- Array conversion functions
- Text format parsing
- Known regression values
- High-dimensional vectors
- Nearest neighbor ordering

**Example**:
```rust
#[pg_test]
fn test_pgvector_example_l2() {
    // Example from pgvector docs
    let a = RuVector::from_slice(&[1.0, 2.0, 3.0]);
    let b = RuVector::from_slice(&[3.0, 2.0, 1.0]);
    let dist = ruvector_l2_distance(a, b);
    // sqrt(8) ≈ 2.828
    assert!((dist - 2.828427).abs() < 0.001);
}
```

### 5. Stress Tests

**Purpose**: Verify stability under load and concurrency.

**File**: `stress_tests.rs`

**Coverage**:
- Concurrent vector creation (8 threads × 100 vectors)
- Concurrent distance calculations (16 threads × 1000 ops)
- Large batch allocations (10,000 vectors)
- Memory reuse patterns
- Thread safety (shared read-only access)
- Varlena round-trip stress (10,000 iterations)

**Example**:
```rust
#[test]
fn test_concurrent_distance_calculations() {
    let num_threads = 16;
    let calculations_per_thread = 1000;
    let v1 = Arc::new(RuVector::from_slice(&[1.0, 2.0, 3.0, 4.0, 5.0]));
    let v2 = Arc::new(RuVector::from_slice(&[5.0, 4.0, 3.0, 2.0, 1.0]));

    let handles: Vec<_> = (0..num_threads)
        .map(|_| {
            let v1 = Arc::clone(&v1);
            let v2 = Arc::clone(&v2);
            thread::spawn(move || {
                for _ in 0..calculations_per_thread {
                    let _ = v1.dot(&*v2);
                }
            })
        })
        .collect();

    for handle in handles {
        handle.join().unwrap();
    }
}
```

### 6. SIMD Consistency Tests

**Purpose**: Verify SIMD implementations match scalar fallback.

**File**: `simd_consistency_tests.rs`

**Coverage**:
- AVX-512, AVX2, NEON vs scalar
- Various vector sizes (1, 7, 8, 15, 16, 31, 32, 64, 128, 256)
- Negative values
- Zero vectors
- Small and large values
- Random data (100 iterations)

**Example**:
```rust
#[test]
fn test_euclidean_scalar_vs_simd_various_sizes() {
    for size in [8, 16, 32, 64, 128, 256] {
        let a: Vec<f32> = (0..size).map(|i| i as f32 * 0.1).collect();
        let b: Vec<f32> = (0..size).map(|i| (size - i) as f32 * 0.1).collect();

        let scalar = scalar::euclidean_distance(&a, &b);

        #[cfg(target_arch = "x86_64")]
        if is_x86_feature_detected!("avx2") {
            let simd = simd::euclidean_distance_avx2_wrapper(&a, &b);
            assert!((scalar - simd).abs() < 1e-5);
        }
    }
}
```

## Running Tests

### All Tests
```bash
cd /home/user/ruvector/crates/ruvector-postgres
cargo test
```

### Specific Test Suite
```bash
# Unit tests only
cargo test --lib

# Integration tests only
cargo test --test '*'

# Specific test file
cargo test --test unit_vector_tests

# Property-based tests
cargo test --test property_based_tests
```

### pgrx Tests
```bash
# Requires PostgreSQL 14, 15, or 16
cargo pgrx test pg16

# Run specific pgrx test
cargo pgrx test pg16 test_l2_distance_basic
```

### With Coverage
```bash
# Install tarpaulin
cargo install cargo-tarpaulin

# Generate coverage report
cargo tarpaulin --out Html --output-dir coverage
```

## Test Metrics

### Current Coverage

**Overall**: ~85% line coverage

**By Component**:
- Core types: 92%
- Distance functions: 95%
- Operators: 88%
- Index implementations: 75%
- Quantization: 82%

### Performance Benchmarks

**Distance Calculations** (1M pairs, 128 dimensions):
- Scalar: 120ms
- AVX2: 45ms (2.7x faster)
- AVX-512: 32ms (3.8x faster)

**Vector Operations**:
- Normalization: 15μs/vector (1024 dims)
- Varlena roundtrip: 2.5μs/vector
- String parsing: 8μs/vector

## Debugging Failed Tests

### Common Issues

1. **Floating Point Precision**
   ```rust
   // ❌ Too strict
   assert_eq!(result, expected);

   // ✅ Use epsilon
   assert!((result - expected).abs() < 1e-5);
   ```

2. **SIMD Availability**
   ```rust
   #[cfg(target_arch = "x86_64")]
   if is_x86_feature_detected!("avx2") {
       // Run AVX2 test
   }
   ```

3. **PostgreSQL Memory Management**
   ```rust
   unsafe {
       let ptr = v.to_varlena();
       // Use ptr...
       pgrx::pg_sys::pfree(ptr as *mut std::ffi::c_void);
   }
   ```

### Verbose Output
```bash
cargo test -- --nocapture --test-threads=1
```

### Running Single Test
```bash
cargo test test_l2_distance_basic -- --exact
```

## CI/CD Integration

### GitHub Actions
```yaml
name: Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Run tests
        run: cargo test --all-features
      - name: Run pgrx tests
        run: cargo pgrx test pg16
```

## Test Development Guidelines

### 1. Test Naming
- Use descriptive names: `test_l2_distance_basic`
- Group related tests: `test_l2_*`, `test_cosine_*`
- Indicate expected behavior: `test_parse_invalid`

### 2. Test Structure
```rust
#[test]
fn test_feature_scenario() {
    // Arrange
    let input = setup_test_data();

    // Act
    let result = perform_operation(input);

    // Assert
    assert_eq!(result, expected);
}
```

### 3. Edge Cases
Always test:
- Empty input
- Single element
- Very large input
- Negative values
- Zero values
- Boundary values

### 4. Error Cases
```rust
#[test]
#[should_panic(expected = "dimension mismatch")]
fn test_invalid_dimensions() {
    let a = RuVector::from_slice(&[1.0, 2.0]);
    let b = RuVector::from_slice(&[1.0, 2.0, 3.0]);
    let _ = a.add(&b); // Should panic
}
```

## Future Test Additions

### Planned
- [ ] Fuzzing tests with cargo-fuzz
- [ ] Performance regression tests
- [ ] Index corruption recovery tests
- [ ] Multi-node distributed tests
- [ ] Backup/restore validation

### Nice to Have
- [ ] SQL injection tests
- [ ] Authentication/authorization tests
- [ ] Compatibility matrix (PostgreSQL versions)
- [ ] Platform-specific tests (Windows, macOS, ARM)

## Resources

- [pgrx Testing Documentation]https://github.com/tcdi/pgrx
- [proptest Book]https://altsysrq.github.io/proptest-book/
- [Rust Testing Guide]https://doc.rust-lang.org/book/ch11-00-testing.html
- [pgvector Test Suite]https://github.com/pgvector/pgvector/tree/master/test

## Support

For test failures or questions:
1. Check existing issues: https://github.com/ruvnet/ruvector/issues
2. Run with verbose output
3. Check PostgreSQL logs
4. Create minimal reproduction case