vibesql
A SQL parser and semantic analyzer written in Rust, conforming to modern ISO SQL standards.
Overview
vibesql provides a complete SQL parsing and analysis pipeline that can be used as a middleware layer between transport and storage layers in database systems. It parses SQL into a rich AST, performs semantic analysis with type checking, and provides a catalog system for schema management.
Features
- Full SQL Lexer - Comprehensive tokenizer with support for SQL keywords, operators, and literals
- Complete AST - Rich abstract syntax tree for expressions, queries, and statements
- Semantic Analyzer - Type checking, name resolution, and query validation
- Catalog System - Schema management with tables, columns, and function signatures
- Zero Dependencies - Pure Rust implementation using only the standard library
Supported SQL
Queries:
- SELECT with projections, aliases, and DISTINCT
- FROM with table references and aliases
- JOINs (INNER, LEFT, RIGHT, FULL, CROSS, NATURAL)
- WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET
- Window functions (OVER, PARTITION BY, ORDER BY, frame clauses)
- Subqueries and CTEs (WITH clause)
- Set operations (UNION, INTERSECT, EXCEPT)
DML:
- INSERT (VALUES and SELECT)
- UPDATE with SET assignments
- DELETE with WHERE
- MERGE (WHEN MATCHED/NOT MATCHED)
DDL:
- CREATE/ALTER/DROP TABLE
- CREATE/ALTER/DROP VIEW
- CREATE/DROP INDEX
- CREATE/DROP FUNCTION
Expressions:
- Arithmetic, comparison, and logical operators
- CASE expressions
- CAST and type conversions
- Array and struct constructors
- Function calls (scalar and aggregate)
- BETWEEN, IN, LIKE, IS NULL
Types:
- Numeric: INTEGER, BIGINT, NUMERIC, REAL, DOUBLE PRECISION
- String: VARCHAR, TEXT, BINARY, BLOB
- Temporal: DATE, TIME, TIMESTAMP, INTERVAL
- Complex: ARRAY, ROW/STRUCT, JSON, UUID
Installation
Add to your Cargo.toml:
[]
= "0.1"
Usage
Basic Parsing
use Parser;
let sql = "SELECT id, name FROM users WHERE age > 21";
let mut parser = new;
let statements = parser.parse.expect;
for stmt in statements
With Semantic Analysis
use ;
// Build a catalog with tables and built-in functions
let catalog = new
.with_builtins
.add_table
.build;
// Parse and analyze
let sql = "SELECT id, name FROM users WHERE age > 21";
let mut parser = new;
let statements = parser.parse.expect;
let mut analyzer = with_catalog;
// Analyze queries for type information, column resolution, etc.
Custom Functions and Types
use ;
use SqlType;
// Add custom functions
let catalog = new
.with_builtins
.add_scalar_function
.add_aggregate_function
.add_window_function
.build;
// Add custom type aliases
let mut registry = new;
registry.add_alias;
registry.add_alias;
Error Handling
use ;
let sql = "SELECT * FORM users"; // Typo: FORM instead of FROM
let mut parser = new;
match parser.parse
Examples
CSV Database
The csv_database example demonstrates using vibesql as a SQL frontend for a simple CSV-backed database:
# Seed with sample relational data
# Run queries
# Join tables
# Aggregations
# Export to CSV
Architecture
vibesql
├── lexer/ # Tokenizer (keywords, operators, literals)
├── parser/ # SQL parser (expressions, queries, statements)
├── ast/ # Abstract syntax tree definitions
├── analyzer/ # Semantic analysis and type checking
├── catalog/ # Schema management (tables, functions, type registry)
├── types/ # SQL type system
└── error/ # Error types and reporting
License
MIT