HeliosDB Procedures Architecture
HeliosDB Procedures Architecture
Overview
The heliosdb-procedures package provides a multi-language stored procedure engine for HeliosDB v3.0 with support for four procedural languages and advanced debugging capabilities.
Supported Languages
1. PL/pgSQL (PostgreSQL)
- Full PostgreSQL procedural language compatibility
- DECLARE blocks with typed variables
- Control flow: IF/ELSIF/ELSE, LOOP, WHILE, FOR
- Exception handling: BEGIN/EXCEPTION/END blocks
- Cursor support: DECLARE CURSOR, FETCH, CLOSE
- RAISE statements for logging and errors
- Dynamic SQL with EXECUTE
2. PL/SQL (Oracle)
- Oracle PL/SQL compatibility
- %TYPE and %ROWTYPE attributes
- BULK COLLECT for bulk operations
- FORALL for bulk DML
- Exception handling with named exceptions
- Nested blocks and procedures
- REF CURSOR support
3. DB2 SQL PL (IBM DB2)
- IBM DB2 procedural language support
- SQLSTATE and SQLCODE error handling
- GET DIAGNOSTICS for detailed error info
- SIGNAL and RESIGNAL for custom errors
- Compound SQL (BEGIN ATOMIC … END)
- FOR loops with implicit cursors
- Transaction control
4. HeliosScript (Custom)
- Modern Rust-like syntax
- Strong typing with type inference
- Pattern matching (match expressions)
- Result<T, E> error handling
- Iterator methods (map, filter, collect)
- Async/await support
- sql! macro for embedded SQL
Architecture
┌─────────────────────────────────────────────────────────────┐│ ProcedureEngine ││ - Procedure registry ││ - Execution coordination ││ - Statistics tracking │└─────────────────┬───────────────────────────────────────────┘ │ ├── Interpreters │ ├── PlPgSQLInterpreter │ ├── PlSQLInterpreter │ ├── DB2SqlPLInterpreter │ └── HeliosScriptInterpreter │ ├── Parser │ ├── Procedure definition parser │ ├── Language-specific AST parsers │ └── Expression parser │ ├── Compiler │ ├── AST → Bytecode compilation │ ├── Instruction set │ └── Optimization passes │ ├── Runtime │ ├── Variable scoping │ ├── Cursor management │ ├── Transaction control │ └── Value system │ └── Debugger ├── Breakpoint management ├── Step execution ├── Call stack tracking └── Performance profilingCore Components
1. ProcedureEngine (lib.rs)
Main entry point for the stored procedure system.
Responsibilities:
- Procedure lifecycle management (CREATE, DROP, EXECUTE)
- Language dispatcher
- Execution statistics tracking
- Caching compiled procedures
- Debugging session management
Key Methods:
pub fn new() -> Selfpub async fn create_procedure(&self, source: &str) -> Result<String>pub async fn execute_procedure(&self, name: &str, params: Vec<(String, Value)>) -> Result<Value>pub fn drop_procedure(&self, name: &str) -> Result<()>pub fn get_stats(&self, name: &str) -> Option<ExecutionStats>2. Interpreter Framework (interpreter.rs)
Core interpreter trait and bytecode virtual machine.
Components:
Interpretertrait: Common interface for all languagesInterpreterContext: Execution context with variables and stateBytecodeCompiler: Compiles AST to bytecodeBytecodeVM: Executes bytecode instructionsInstruction: Bytecode instruction set
Instruction Set:
- Variable operations: LoadConst, LoadVar, StoreVar
- Arithmetic: Add, Sub, Mul, Div, Mod, Neg
- Comparison: Equal, NotEqual, LessThan, GreaterThan, etc.
- Logical: And, Or, Not
- Control flow: Jump, JumpIfFalse, Call, Return
- SQL operations: ExecuteQuery, ExecuteDML, FetchCursor
- Exception handling: BeginTry, Catch, Throw
- Transaction control: BeginTransaction, Commit, Rollback
3. Parser (parser.rs)
SQL procedural language parser with nom combinators.
AST Node Types:
- DeclareVariable: Variable declarations
- Assignment: Variable assignments
- If/Loop/While/For: Control flow
- Select/Insert/Update/Delete: SQL statements
- DeclareCursor/OpenCursor/FetchCursor: Cursor operations
- Begin/Exception/End: Exception handling
- Return/Exit/Continue: Control statements
- Literal/Variable/BinaryOp/UnaryOp: Expressions
4. Runtime (runtime.rs)
Runtime system with variable scoping and type system.
Value Types:
- Null, Boolean, Integer, Float, Decimal
- String, Date, Timestamp
- Array, Record (structured types)
Features:
- Variable scoping with parent scopes
- Type-safe operations (add, sub, mul, div)
- Cursor management with result sets
- Transaction depth tracking
- Constant enforcement
5. Language Interpreters
PlPgSQLInterpreter (plpgsql.rs)
PostgreSQL procedural language implementation.
Features:
- Full PL/pgSQL syntax support
- Exception handling with custom error types
- Cursor support
- Dynamic SQL
- RAISE statements
PlSQLInterpreter (plsql.rs)
Oracle PL/SQL implementation.
Oracle-Specific Features:
- %TYPE and %ROWTYPE attributes
- BULK COLLECT operations
- FORALL bulk DML
- Named exceptions
- REF CURSOR support
DB2SqlPLInterpreter (db2_sql_pl.rs)
IBM DB2 SQL PL implementation.
DB2-Specific Features:
- SQLSTATE/SQLCODE error handling
- GET DIAGNOSTICS statements
- SIGNAL/RESIGNAL error handling
- Compound SQL blocks
- FOR loops with implicit cursors
HeliosScriptInterpreter (heliosscript.rs)
Custom Rust-like procedural language.
Modern Features:
- Rust-like syntax (let bindings, match expressions)
- Type inference
- Pattern matching
- Iterator methods
- Result<T, E> error handling
- sql! macro for embedded SQL
6. Debugger (debugger.rs)
Comprehensive debugging support.
Features:
- Breakpoint management (line, conditional)
- Step execution (next, into, out, continue)
- Call stack inspection
- Variable inspection
- Expression evaluation
- Performance profiling
- Hotspot analysis
Debug Session:
pub fn start_session(&mut self, procedure_name: &str) -> Result<Uuid>pub fn set_breakpoint(&mut self, session_id: Uuid, line: usize) -> Result<()>pub fn step_next(&mut self, session_id: Uuid) -> Result<()>pub fn get_call_stack(&self, session_id: Uuid) -> Result<Vec<StackFrame>>pub fn get_local_variables(&self, session_id: Uuid) -> Result<HashMap<String, Value>>Execution Flow
1. CREATE PROCEDURE ├── Parse procedure definition ├── Validate language syntax ├── Compile to bytecode ├── Store metadata and bytecode └── Return procedure name
2. EXECUTE PROCEDURE ├── Load procedure metadata ├── Validate parameters ├── Create execution context ├── Load interpreter for language ├── Execute bytecode in VM ├── Handle exceptions ├── Update statistics └── Return result
3. DEBUG PROCEDURE ├── Start debug session ├── Set breakpoints ├── Execute with debugging enabled ├── Pause at breakpoints ├── Inspect variables and stack ├── Step execution └── Continue or stopPerformance Optimizations
- Bytecode Compilation: One-time compilation to bytecode
- Procedure Caching: Compiled procedures cached in memory
- Fast Variable Lookup: DashMap for concurrent access
- Lazy Evaluation: Only compile when needed
- Stack-based VM: Efficient bytecode execution
- Profiling Data: Identify hotspots for optimization
Error Handling
Comprehensive error types:
NotFound: Procedure not foundParseError: Syntax error in sourceRuntimeError: Execution errorTypeError: Type mismatchDivisionByZero: Math errorSqlError: SQL execution errorCursorError: Cursor operation errorTransactionError: Transaction errorException: User-raised exceptionTimeout: Execution timeoutStackOverflow: Recursion limit exceeded
Configuration
pub struct EngineConfig { pub max_execution_time_ms: u64, // Default: 30000 (30s) pub max_recursion_depth: usize, // Default: 100 pub enable_profiling: bool, // Default: true pub enable_debugging: bool, // Default: false pub cache_compiled_procedures: bool, // Default: true}Thread Safety
- All components are thread-safe using Arc and DashMap
- Concurrent procedure execution supported
- Lock-free reads for variables
- Atomic statistics updates
Future Enhancements
- JIT Compilation: Compile hot procedures to native code
- Query Optimizer: Optimize embedded SQL
- Distributed Execution: Execute procedures across nodes
- Package Support: Oracle-style package support
- Triggers: Event-driven procedure execution
- Scheduled Jobs: Time-based procedure execution
- More Languages: T-SQL, JavaScript, Python integration
- Performance Hints: Inline hints for optimization
- Code Coverage: Track procedure code coverage
- Auto-tuning: Automatic performance optimization
Dependencies
- nom: Parser combinators
- logos: Lexer generation
- pest: Parser generation
- tokio: Async runtime
- dashmap: Concurrent hash map
- parking_lot: Fast locks
- serde/bincode: Serialization
- rust_decimal: Decimal math
- chrono: Date/time support
- uuid: Session IDs
- tracing: Logging
- metrics: Performance metrics
Testing
Comprehensive test suite covering:
- Parser correctness
- Bytecode compilation
- VM execution
- Variable scoping
- Cursor operations
- Exception handling
- Debugger functionality
- Performance benchmarks
Run tests:
cargo test --package heliosdb-proceduresExamples
See examples/basic_procedures.rs for comprehensive usage examples of all four languages.
Run examples:
cargo run --package heliosdb-procedures --example basic_procedures