Skip to content

HyperQL - Advanced query language and execution engine

License

Notifications You must be signed in to change notification settings

tomWhiting/hyperQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

HyperQL - Unified Query Language for Hyperspatial Database

Rust License Build Status

HyperQL is a revolutionary query language that unifies SQL-like syntax with graph traversal, geometric queries, and cascade operations. It enables seamless querying across multiple paradigms within the Hyperspatial database's unified hyperbolic space.

🌟 Why HyperQL?

Traditional databases force developers to use different query languages for different data models:

  • SQL for relational data
  • Cypher or SPARQL for graph traversals
  • Custom APIs for vector similarity
  • Domain-specific languages for time-series analysis

HyperQL eliminates this fragmentation by providing a unified syntax that can:

  • Query entities by properties (SQL-like WHERE clauses)
  • Traverse relationships (graph-style navigation)
  • Find similar entities (vector similarity searches)
  • Apply geometric filters (hyperbolic distance constraints)
  • Propagate cascading measures (hierarchical computations)
  • Combine all paradigms in a single query

πŸš€ Key Features

Multi-Paradigm Integration

HyperQL seamlessly combines multiple query paradigms:

SELECT entity, measure_value, influence_score
FROM users u
TRAVERSE connected_to -> friends f
       -> colleagues c
WHERE u.age > 25
  AND similarity(u.interests, f.interests) > 0.8
  AND hyperbolic_distance(u, f) < 2.0
  AND c.department = 'engineering'
CASCADE user_influence FROM u TO f WITH decay_factor(0.9)
ORDER BY measure_value DESC, influence_score DESC
LIMIT 100

Spatial Intelligence

Leverage the hyperbolic positioning system for:

  • Natural clustering queries without explicit grouping
  • Hierarchical traversals that respect learned structure
  • Multi-signal similarity searches combining various signals
  • Temporal queries over position trajectories

Advanced Cascade System

Powerful cascade operations for hierarchical computations:

-- Aggregate sales up organizational hierarchy
CASCADE total_sales = SUM(sales_amount)
FROM transactions t
PROPAGATE UP THROUGH organizational_hierarchy

-- Distribute budget down to teams
CASCADE budget_allocation = DISTRIBUTE(total_budget, allocation_weights)
FROM departments d
PROPAGATE DOWN TO teams

-- Compute influence propagation through social networks
CASCADE influence_score = INFLUENCE(base_score, decay_factor: 0.9)
FROM influencers i
PROPAGATE THROUGH social_network
WITH MAX_DEPTH 3

πŸ“– Query Examples

Basic Relational Queries

-- Traditional SQL-style query
SELECT name, age, email
FROM users
WHERE age > 30 AND department = 'engineering'
ORDER BY name

Graph Traversal Queries

-- Find friends of friends with similar interests
SELECT u.name, f.name, ff.name
FROM users u
TRAVERSE friends -> f
       -> friends -> ff
WHERE similarity(u.interests, ff.interests) > 0.7
  AND u.id != ff.id

Geometric Queries

-- Find entities within hyperbolic distance
SELECT entity, hyperbolic_distance(entity, @anchor) as distance
FROM products
WHERE hyperbolic_distance(entity, @anchor) < 1.5
ORDER BY distance ASC

Vector Similarity Queries

-- K-nearest neighbors with filtering
SELECT item, similarity(item.embedding, @query_vector) as score
FROM items
WHERE category = 'electronics' 
  AND price < 500
ORDER BY similarity(item.embedding, @query_vector) DESC
LIMIT 10

Complex Multi-Paradigm Queries

-- Recommendation system combining multiple signals
SELECT 
    p.title,
    similarity(p.content_embedding, u.preference_vector) as content_score,
    hyperbolic_distance(p, u) as position_score,
    cascade_measure as social_score
FROM products p, users u
TRAVERSE u.friends -> f
WHERE u.id = @user_id
  AND p.category IN @preferred_categories
  AND similarity(p.content_embedding, u.preference_vector) > 0.6
CASCADE social_influence 
  FROM f.purchased_items 
  TO p 
  WITH influence_weight(f.social_score)
ORDER BY 
  (0.4 * content_score + 0.3 * position_score + 0.3 * social_score) DESC
LIMIT 20

Temporal Trajectory Queries

-- Analyze movement patterns over time
SELECT 
    entity,
    trajectory_similarity(entity.position_history, @pattern) as match_score
FROM moving_objects
WHERE timerange(entity.timestamps, '2024-01-01', '2024-12-31')
  AND trajectory_length(entity.position_history) > 100
ORDER BY match_score DESC

πŸ”§ Usage Modes

1. Ad-Hoc Queries

Direct query execution for interactive analysis:

use hyperql::*;

#[tokio::main]
async fn main() -> Result<()> {
    let engine = HyperQLEngine::new().await?;
    
    let query = r#"
        SELECT name, age
        FROM users 
        WHERE age > 25
        TRAVERSE friends -> f
        WHERE similarity(interests, f.interests) > 0.8
    "#;
    
    let results = engine.execute(query).await?;
    for row in results {
        println!("{:?}", row);
    }
    
    Ok(())
}

2. Compiled Queries

Pre-compiled queries for production performance:

use hyperql::*;

#[tokio::main]
async fn main() -> Result<()> {
    let engine = HyperQLEngine::new().await?;
    
    // Compile query once
    let compiled = engine.compile(r#"
        SELECT entity, measure_value
        FROM users u
        TRAVERSE connected_to -> friends f
        WHERE u.age > $age_threshold
          AND similarity(u.interests, f.interests) > $similarity_threshold
        CASCADE user_influence FROM u TO f
        ORDER BY measure_value DESC
        LIMIT $limit
    "#).await?;
    
    // Execute multiple times with different parameters
    let results1 = compiled.execute(
        &[("age_threshold", 25), ("similarity_threshold", 0.8), ("limit", 50)]
    ).await?;
    
    let results2 = compiled.execute(
        &[("age_threshold", 30), ("similarity_threshold", 0.9), ("limit", 20)]
    ).await?;
    
    Ok(())
}

3. Hybrid Mode

Combine compiled templates with dynamic query construction:

use hyperql::*;

#[tokio::main]
async fn main() -> Result<()> {
    let engine = HyperQLEngine::new().await?;
    
    // Build query dynamically
    let mut query_builder = QueryBuilder::new()
        .select(["entity", "score"])
        .from("products")
        .where_clause("category = $category")
        .order_by("score DESC")
        .limit(10);
    
    // Add conditional traversal
    if include_recommendations {
        query_builder = query_builder
            .traverse("recommended_by -> users u")
            .where_and("u.trust_score > $trust_threshold");
    }
    
    let compiled = query_builder.compile(&engine).await?;
    let results = compiled.execute(
        &[("category", "electronics"), ("trust_threshold", 0.7)]
    ).await?;
    
    Ok(())
}

πŸ—οΈ Architecture

HyperQL follows a traditional compiler pipeline optimized for hyperbolic queries:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Query     │───▢│    AST      │───▢│ Execution   │───▢│   Results   β”‚
β”‚   Text      β”‚    β”‚             β”‚    β”‚   Plan      β”‚    β”‚             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                   β”‚                   β”‚                   β”‚
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚ Parser  β”‚         β”‚Compiler β”‚         β”‚Executor β”‚         β”‚Hyperbolicβ”‚
   β”‚ (Winnow)β”‚         β”‚         β”‚         β”‚ Engine  β”‚         β”‚  Space   β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Components

  • Parser: Winnow-based parser converts query text to AST
  • Compiler: AST transforms into optimized execution plans
  • Optimizer: Query plans are optimized for hyperbolic operations
  • Executor: Plans execute against the hyperbolic space engine
  • Runtime: Lua and WASM integration for custom functions

πŸ“ Module Structure

src/
β”œβ”€β”€ ast/                 # Abstract syntax tree definitions
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ query.rs        # Core query structures
β”‚   β”œβ”€β”€ expression.rs   # Expression trees
β”‚   β”œβ”€β”€ literal.rs      # Literal values
β”‚   β”œβ”€β”€ identifier.rs   # Entity identifiers
β”‚   β”œβ”€β”€ function.rs     # Function calls
β”‚   β”œβ”€β”€ predicate.rs    # Boolean expressions
β”‚   β”œβ”€β”€ traverse.rs     # Graph traversal
β”‚   β”œβ”€β”€ cascade.rs      # Cascade operations
β”‚   └── aggregate.rs    # Aggregation functions
β”œβ”€β”€ parser/             # Winnow-based query parser
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ lexer.rs        # Tokenization
β”‚   β”œβ”€β”€ grammar.rs      # Grammar rules
β”‚   └── combinators.rs  # Parser combinators
β”œβ”€β”€ compiler/           # AST to execution plan compilation
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ planner.rs      # Query planning
β”‚   β”œβ”€β”€ type_checker.rs # Type analysis
β”‚   └── code_gen.rs     # Code generation
β”œβ”€β”€ executor/           # Query execution engine
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ engine.rs       # Execution engine
β”‚   β”œβ”€β”€ operators.rs    # Query operators
β”‚   └── results.rs      # Result handling
β”œβ”€β”€ functions/          # Built-in function library
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ similarity.rs   # Vector similarity functions
β”‚   β”œβ”€β”€ geometric.rs    # Hyperbolic geometry functions
β”‚   β”œβ”€β”€ aggregates.rs   # Aggregation functions
β”‚   └── temporal.rs     # Time-based functions
β”œβ”€β”€ cascade/            # Cascade system for measure propagation
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ propagation.rs  # Propagation algorithms
β”‚   β”œβ”€β”€ measures.rs     # Measure definitions
β”‚   β”œβ”€β”€ hierarchy.rs    # Hierarchy detection
β”‚   └── scheduler.rs    # Cascade scheduling
β”œβ”€β”€ optimizer/          # Query optimization
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ rules.rs        # Optimization rules
β”‚   β”œβ”€β”€ cost_model.rs   # Cost modeling
β”‚   └── statistics.rs   # Query statistics
β”œβ”€β”€ context/            # Query execution context
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ variables.rs    # Variable binding
β”‚   └── scope.rs        # Scope management
β”œβ”€β”€ runtime/            # Custom function runtime
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ lua.rs          # Lua integration
β”‚   β”œβ”€β”€ wasm.rs         # WASM integration
β”‚   β”œβ”€β”€ sandbox.rs      # Security sandbox
β”‚   └── function_registry.rs # Function registry
β”œβ”€β”€ ir/                 # Intermediate representation
β”‚   β”œβ”€β”€ mod.rs
β”‚   β”œβ”€β”€ operators.rs    # IR operators
β”‚   β”œβ”€β”€ plans.rs        # Execution plans
β”‚   └── serialization.rs # Plan serialization
β”œβ”€β”€ error.rs            # Error types and handling
β”œβ”€β”€ types.rs            # Core type definitions
└── lib.rs             # Library root with documentation

πŸ”Œ Integration with Hyperspatial

HyperQL is tightly integrated with the Hyperspatial database engine:

Hyperbolic Engine Integration

  • Learned Positions: Leverages hyperbolic positions for geometric queries
  • Distance Calculations: Efficient hyperbolic distance computations
  • Spatial Indexing: Utilizes hyperbolic spatial indices for fast retrieval
  • Clustering: Natural clustering based on hyperbolic proximity

Persistence Layer Integration

  • Entity Access: Direct access to stored entities and properties
  • Relationship Traversal: Efficient traversal of stored relationships
  • Index Utilization: Leverages existing indices for query acceleration
  • Transaction Support: Full ACID transaction support

Vector Operations Integration

  • Embedding Storage: Access to stored vector embeddings
  • Similarity Indices: Utilizes vector similarity indices (HNSW, IVF)
  • Approximate Search: Efficient approximate nearest neighbor search
  • Multi-Modal Vectors: Support for multiple embedding types per entity

Measure System Integration

  • Cascade Computation: Integration with measure propagation system
  • Incremental Updates: Efficient updates when underlying data changes
  • Dependency Management: Automatic cascade dependency resolution
  • Parallel Execution: Multi-threaded cascade computation

🎯 Custom Functions with Lua and WASM

Lua Integration

Extend HyperQL with custom Lua functions:

use hyperql::runtime::lua::LuaRuntime;

#[tokio::main]
async fn main() -> Result<()> {
    let mut runtime = LuaRuntime::new().await?;
    
    // Register custom Lua function
    runtime.register_function("custom_similarity", r#"
        function custom_similarity(vec1, vec2, weights)
            local sum = 0
            for i = 1, #vec1 do
                sum = sum + (vec1[i] * vec2[i] * weights[i])
            end
            return sum / (#vec1 * #vec2)
        end
    "#).await?;
    
    let engine = HyperQLEngine::with_runtime(runtime).await?;
    
    // Use custom function in queries
    let results = engine.execute(r#"
        SELECT entity, custom_similarity(entity.embedding, @query_vector, @weights) as score
        FROM products
        WHERE custom_similarity(entity.embedding, @query_vector, @weights) > 0.8
        ORDER BY score DESC
    "#).await?;
    
    Ok(())
}

WASM Integration

Deploy compiled functions for maximum performance:

use hyperql::runtime::wasm::WasmRuntime;

#[tokio::main]
async fn main() -> Result<()> {
    let mut runtime = WasmRuntime::new().await?;
    
    // Load compiled WASM module
    let wasm_bytes = std::fs::read("custom_functions.wasm")?;
    runtime.load_module("custom", &wasm_bytes).await?;
    
    let engine = HyperQLEngine::with_runtime(runtime).await?;
    
    // Use WASM functions in queries
    let results = engine.execute(r#"
        SELECT entity, custom.fast_similarity(entity.embedding, @query) as score
        FROM large_dataset
        WHERE custom.fast_similarity(entity.embedding, @query) > 0.9
    "#).await?;
    
    Ok(())
}

πŸš€ Installation

Add HyperQL to your Cargo.toml:

[dependencies]
hyperQL = "0.1.0"
tokio = { version = "1.0", features = ["full"] }

πŸ“š Getting Started

Basic Setup

use hyperql::*;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize the HyperQL engine
    let engine = HyperQLEngine::new().await?;
    
    // Execute a simple query
    let results = engine.execute(r#"
        SELECT name, age
        FROM users
        WHERE age > 25
        ORDER BY name
    "#).await?;
    
    // Process results
    for row in results {
        let name: String = row.get("name")?;
        let age: i32 = row.get("age")?;
        println!("{}: {} years old", name, age);
    }
    
    Ok(())
}

Advanced Configuration

use hyperql::*;
use hyperql::config::*;

#[tokio::main]
async fn main() -> Result<()> {
    let config = EngineConfig::builder()
        .max_parallel_queries(100)
        .enable_query_cache(true)
        .cascade_thread_pool_size(16)
        .lua_sandbox_enabled(true)
        .wasm_memory_limit(1_000_000)
        .build();
    
    let engine = HyperQLEngine::with_config(config).await?;
    
    // Engine is now ready with custom configuration
    Ok(())
}

πŸ”¬ Performance Characteristics

Query Complexity

  • Simple Filters: O(n) with index acceleration
  • Graph Traversals: O(d^k) where d=degree, k=depth
  • Similarity Searches: O(log n) with proper indexing
  • Cascade Operations: O(h * n) where h=hierarchy height
  • Complex Multi-Paradigm: Optimized based on selectivity

Scalability

  • Horizontal Scaling: Query parallelization across cores
  • Memory Efficiency: Streaming results for large datasets
  • Index Utilization: Automatic index selection and usage
  • Caching: Query result and plan caching

Benchmarks

Operation Dataset Size Performance
Simple Filter 1M entities ~50ms
Graph Traversal (depth 3) 100K entities ~200ms
K-NN Search (k=100) 10M vectors ~10ms
CASCADE Propagation 1M hierarchy ~500ms
Multi-Paradigm Query 1M entities ~150ms

πŸ›£οΈ Roadmap

Version 0.2.0 (Q2 2025)

  • Advanced optimization rules
  • Distributed query execution
  • Streaming query results
  • Enhanced WASM runtime

Version 0.3.0 (Q3 2025)

  • Machine learning function library
  • Temporal query extensions
  • Advanced cascade algorithms
  • Query debugging tools

Version 1.0.0 (Q4 2025)

  • Production stability
  • Complete SQL compatibility layer
  • Visual query builder
  • Enterprise features

🀝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Development Setup

# Clone the repository
git clone https://github.com/hyperspatial/hyperQL.git
cd hyperQL

# Install dependencies
cargo build

# Run tests
cargo test

# Run examples
cargo run --example basic_query

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • The Rust community for excellent async ecosystem
  • The Winnow parser combinator library
  • The hyperbolic geometry research community
  • Contributors to vector similarity algorithms

HyperQL - Revolutionizing database queries through unified multi-paradigm syntax and hyperbolic intelligence.

For more information, visit our documentation or join our community discussions.

About

HyperQL - Advanced query language and execution engine

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages