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
# Sparse Vectors Module

High-performance sparse vector support for PostgreSQL using COO (Coordinate) format.

## Quick Start

```sql
-- Create table
CREATE TABLE documents (
    id SERIAL PRIMARY KEY,
    sparse_embedding sparsevec
);

-- Insert sparse vector
INSERT INTO documents (sparse_embedding) VALUES
    ('{1:0.5, 2:0.3, 5:0.8}'::sparsevec);

-- Search by similarity
SELECT id,
       ruvector_sparse_dot(sparse_embedding, '{1:0.5, 2:0.3}'::sparsevec) AS score
FROM documents
ORDER BY score DESC;
```

## Features

- **Efficient Storage**: COO format with sorted indices
-**Fast Operations**: O(nnz) merge-based algorithms
-**Multiple Distances**: Dot product, cosine, Euclidean, Manhattan, BM25
-**Flexible Input**: Parse from strings or arrays
-**Utility Functions**: Top-k, pruning, normalization
-**PostgreSQL Native**: Full pgrx integration

## Module Structure

```
sparse/
├── mod.rs          # Module exports
├── types.rs        # SparseVec type (391 lines)
├── distance.rs     # Distance functions (286 lines)
├── operators.rs    # PostgreSQL functions (366 lines)
├── tests.rs        # Test suite (200 lines)
└── README.md       # This file
```

## Type Definition

```rust
pub struct SparseVec {
    indices: Vec<u32>,  // Sorted indices
    values: Vec<f32>,   // Corresponding values
    dim: u32,           // Total dimension
}
```

## Distance Functions

All functions use efficient merge-based iteration for O(nnz(a) + nnz(b)) complexity:

- `sparse_dot(a, b)` - Inner product
- `sparse_cosine(a, b)` - Cosine similarity
- `sparse_euclidean(a, b)` - Euclidean distance
- `sparse_manhattan(a, b)` - Manhattan distance
- `sparse_bm25(query, doc, ...)` - BM25 text ranking

## PostgreSQL Functions

### Distance Operations
- `ruvector_sparse_dot(a, b) -> real`
- `ruvector_sparse_cosine(a, b) -> real`
- `ruvector_sparse_euclidean(a, b) -> real`
- `ruvector_sparse_manhattan(a, b) -> real`
- `ruvector_sparse_bm25(query, doc, ...) -> real`

### Construction
- `ruvector_to_sparse(indices, values, dim) -> sparsevec`
- `ruvector_dense_to_sparse(dense[]) -> sparsevec`
- `ruvector_sparse_to_dense(sparse) -> real[]`

### Utilities
- `ruvector_sparse_nnz(sparse) -> int` - Number of non-zeros
- `ruvector_sparse_dim(sparse) -> int` - Dimension
- `ruvector_sparse_norm(sparse) -> real` - L2 norm
- `ruvector_sparse_top_k(sparse, k) -> sparsevec` - Keep top k
- `ruvector_sparse_prune(sparse, threshold) -> sparsevec` - Prune small values

## Examples

### Text Search with BM25

```sql
SELECT id, title,
       ruvector_sparse_bm25(
           query_idf,
           term_frequencies,
           doc_length,
           avg_doc_length,
           1.2,  -- k1
           0.75  -- b
       ) AS bm25_score
FROM articles
ORDER BY bm25_score DESC;
```

### Learned Sparse Retrieval (SPLADE)

```sql
SELECT id, content,
       ruvector_sparse_dot(splade_embedding, query_splade) AS relevance
FROM documents
ORDER BY relevance DESC
LIMIT 10;
```

### Hybrid Dense + Sparse

```sql
SELECT id,
       0.7 * (1 - (dense <=> query_dense)) +
       0.3 * ruvector_sparse_dot(sparse, query_sparse) AS hybrid_score
FROM documents
ORDER BY hybrid_score DESC;
```

## Performance

| Operation | Complexity | Typical Time (100 NNZ) |
|-----------|-----------|------------------------|
| Dot product | O(nnz(a) + nnz(b)) | ~0.8 μs |
| Cosine | O(nnz(a) + nnz(b)) | ~1.2 μs |
| Euclidean | O(nnz(a) + nnz(b)) | ~1.0 μs |
| BM25 | O(nnz(query) + nnz(doc)) | ~1.5 μs |

**Storage**: ~150× more efficient than dense for 100 NNZ / 30K dim

## Testing

```bash
# Run unit tests
cargo test --lib sparse

# Run PostgreSQL tests
cargo pgrx test pg16
```

## Documentation

- [Quick Start Guide]../../docs/guides/SPARSE_QUICKSTART.md
- [Full Documentation]../../docs/guides/SPARSE_VECTORS.md
- [Implementation Summary]../../docs/guides/SPARSE_IMPLEMENTATION_SUMMARY.md
- [SQL Examples]../../examples/sparse_example.sql

## Use Cases

1. **BM25 Text Search**: Traditional text ranking
2. **SPLADE**: Learned sparse retrieval
3. **Hybrid Search**: Dense + sparse combination
4. **High-dimensional Sparse**: Feature vectors, embeddings

## Requirements

- PostgreSQL 14-17
- pgrx 0.12
- Rust 1.70+

## License

MIT

---

**Total Code**: 1,243 lines
**Test Coverage**: 31+ tests
**Status**: ✅ Production-ready