SQL-RS
A lightweight, embedded database written in Rust that combines traditional relational database features with vector database capabilities for AI/ML applications.
Features
Core Database Features
- Traditional Database: B-tree based storage with ACID properties
- Vector Database: High-dimensional embeddings with similarity search
- CLI Interface: Simple command-line interface for all operations
- Single File: Database stored in a single file for easy portability
- Rust Edition 2024: Modern Rust with latest features and improvements
Advanced Features
- Write-Ahead Logging (WAL): Durability and crash recovery support
- Transaction Support: BEGIN, COMMIT, ROLLBACK operations
- Full SQL Support: CREATE, INSERT, SELECT, UPDATE, DELETE, DROP TABLE
- WHERE Clauses: Filter data with comparison operators (=, !=, >, <, >=, <=)
- Logical Operators: AND/OR for complex filtering
- ORDER BY: Sort results in ascending or descending order
- LIMIT/OFFSET: Pagination support for query results
- Aggregates: COUNT, SUM, AVG, MIN, MAX
- GROUP BY: Group and aggregate data
- JOINs: INNER, LEFT, RIGHT joins
- Indexes: CREATE INDEX and DROP INDEX support
- Multiple Data Types: INTEGER, FLOAT, TEXT, BLOB, BOOLEAN, NULL
Vector Database Features
- HNSW Index: Fast approximate nearest neighbor search
- Multiple Distance Metrics: Cosine, Euclidean, Dot Product
- Vector Quantization: 8-bit and 16-bit quantization for memory efficiency
- Metadata Filtering: Search vectors with metadata filters
- Batch Operations: Efficient batch insert and search
- Vector Persistence: Automatic persistence to storage engine
- Vector Statistics: Collection-level statistics and information
Installation
As a Library
Add this to your Cargo.toml:
[]
= "0.1"
Build from Source
The compiled binary will be at target/release/sql-rs.
Usage
Create a Database
Database Information
View comprehensive database statistics including size, collections, and tables:
Output:
╔════════════════════════════════════════════════════════╗
║ SQL-RS Database Statistics ║
╠════════════════════════════════════════════════════════╣
║ Database Path: mydb.db ║
╠────────────────────────────────────────────────────────╣
║ Total Pages: 5 ║
║ Page Size: 4096 bytes ║
║ Total Size: 0.02 MB ║
╠────────────────────────────────────────────────────────╣
║ Vector Collections: 1 ║
║ Tables: 0 ║
╠────────────────────────────────────────────────────────╣
║ Vector Collections: ║
║ • embeddings (Vectors: 100, Dimension: 384, Metric: Cosine) ║
╚════════════════════════════════════════════════════════╝
Traditional Database Operations
Create a Table
Insert Data
Query Data
# Select all
# Filter with WHERE
# Sort results
# Pagination
# Aggregates
# GROUP BY
# JOINs
Update Data
Delete Data
Create Index
Drop Table/Index
Vector Database Operations
Create a Vector Collection
Add Vectors
Search Similar Vectors
Filtered Search
Search vectors with metadata filters:
List Collections
Delete Collection
Import Vectors from JSON
Import multiple vectors from a JSON file:
JSON format:
Architecture
SQL-RS follows a modular architecture:
- Storage Layer: Page-based B-tree storage with WAL and transaction support
- Vector Layer: HNSW index for approximate nearest neighbor search
- Query Engine: SQL parser and executor with full CRUD operations
- Transaction Manager: ACID-compliant transaction handling
- CLI Layer: Command-line interface using clap
- Statistics Module: Database and vector collection statistics
Distance Metrics
SQL-RS supports three distance metrics for vector similarity:
- Cosine: Measures angle between vectors (default, best for semantic search)
- Euclidean: Measures straight-line distance (best for geometric similarity)
- Dot Product: Measures vector alignment (best for normalized vectors)
Vector Quantization
Reduce memory footprint with vector quantization:
- 8-bit quantization: 4x compression with minimal accuracy loss
- 16-bit quantization: 2x compression with near-perfect accuracy
Performance
- SQL Insert: >10k rows/sec
- SQL Query: <10ms for indexed lookups
- Vector Search: <100ms for 1M vectors (approximate with HNSW)
- Memory Footprint: <50MB for typical workloads
- Page Size: 4KB pages for efficient I/O
Examples
Vector Database Examples
Run the vector database examples:
# Simple vector test
# Semantic search demo
# Performance benchmark
Simple Vector Test:
- Creates a vector collection
- Adds vectors with metadata
- Performs similarity search
- Demonstrates vector retrieval
Semantic Search Demo:
- Document embedding simulation
- Category-based filtering
- Multi-query demonstration
- Filtered search by metadata
Vector Benchmark:
- Batch insertion performance
- Search performance metrics
- Memory usage statistics
Basic Usage Examples
See the examples/ directory for complete usage examples.
Testing
SQL-RS has comprehensive test coverage with 147+ passing tests:
# Run all tests
# Run specific test suites
Development
Built with:
- Rust Edition 2024: Latest Rust features and improvements
- clap 4.5: Command-line interface parsing
- serde/serde_json: Serialization and deserialization
- bincode: Binary encoding for efficient storage
- parking_lot: High-performance synchronization primitives
- memmap2: Memory-mapped file I/O
- thiserror/anyhow: Robust error handling
- tempfile: Test file management
Project Structure
src/
├── lib.rs # Main library entry point
├── main.rs # CLI entry point
├── cli/ # Command-line interface
│ └── commands.rs # Command handlers
├── storage/ # B-tree, WAL, transactions, statistics
│ ├── btree.rs # B-tree implementation
│ ├── wal.rs # Write-ahead logging
│ ├── transaction.rs # Transaction manager
│ ├── file.rs # File management
│ ├── page.rs # Page management
│ └── stats.rs # Database statistics
├── vector/ # HNSW index, similarity metrics
│ ├── collection.rs # Vector collection
│ ├── hnsw.rs # HNSW index
│ ├── similarity.rs # Distance metrics
│ ├── quantization.rs # Vector quantization
│ └── store.rs # Vector store
├── query/ # SQL parser and executor
│ ├── parser.rs # SQL parsing
│ ├── planner.rs # Query planning
│ └── executor.rs # Query execution
└── types/ # Core data types and schemas
Code Quality
- rustfmt.toml: Consistent code formatting configuration
- clippy.toml: Lint rules and warnings configuration
- 147+ tests: Comprehensive unit and integration tests
- Apache 2.0 License: Permissive open-source license
Building
Roadmap
See TODO.md for planned features and improvements including:
- Interactive REPL mode
- Query optimizer
- Import/export (CSV, JSON)
- Database backup/restore
- CI/CD pipeline
- And more...
Documentation
- ARCHITECTURE.md: Detailed system design and architecture
- TODO.md: Feature roadmap and planned improvements
- SPEC.md: Technical specifications
- VECTOR_ENHANCEMENTS.md: Vector database features
- AGENTS.md: Development guide for contributors
License
Copyright 2025 SQL-RS Contributors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.