GitHub - zoolpher/sql-engine: A lightweight SQL database engine built from scratch in C++ to explore database internals, query parsing, storage engines, indexing, and query execution step by step. · GitHub
Skip to content

About

A lightweight SQL database engine built from scratch in C++ to explore database internals, query parsing, storage engines, indexing, and query execution step by step.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

29 Commits

Folders and files

Repository files navigation

SQL-Engine Logo

C++ SQL Engine

A lightweight, from-scratch relational database management system (RDBMS) built entirely in C++. This project demonstrates how a real SQL database works under the hood, taking a raw string query and putting it through a full compilation pipeline to physically modify data on a hard drive.

🌟 Specialties

  • Zero Dependencies: Built entirely using the C++17 Standard Library without relying on external parsing tools (like Bison/Yacc) or storage libraries.
  • Custom Pipeline: Features a hand-written Lexical Analyzer, Recursive Descent Parser, and Abstract Syntax Tree (AST).
  • Persistent Storage: Translates in-memory data structures into physical .csv files so data survives across reboots.

⚙️ Features

  • Interactive REPL: A fully functioning command-line interface (sqlight>) to enter queries and receive formatted table outputs.
  • DDL (Data Definition Language): Create and destroy tables dynamically (CREATE TABLE, DROP TABLE, SHOW TABLES).
  • CRUD Operations: Full data manipulation support (INSERT, SELECT, UPDATE, DELETE).
  • Where Clauses: Support for targeted data manipulation using WHERE filters.
  • Disk Persistence: Automatically loads tables from the data/ directory on startup and saves them upon modification.

⚠️ Drawbacks (Current Limitations)

  • No Concurrency: This engine is designed for a single user and does not support transactions, locking, or multi-threading.
  • No Advanced Queries: Currently does not support JOINs, GROUP BY, ORDER BY, or complex nested WHERE clauses (like AND/OR).
  • Performance: The storage engine uses sequential scans (O(N)) to find data. It does not implement B-Trees or database indexing yet.

🚀 How to Run

1. Clone the Repository

Fork this repository and clone it to your local machine:

git clone https://github.com/your-username/sql-engine.git
cd sql-engine

2. Build the Project (CMake)

You will need CMake and a modern C++ compiler installed on your system.

For Windows (PowerShell):

# Generate the build files
cmake -B build

# Compile the executable
cmake --build build

For Linux / WSL / macOS (Bash):

# Generate the build files
cmake -B build

# Compile the executable
cmake --build build

3. Launch the Engine

Once compiled, you can start the interactive REPL:

Windows:

./build/sql-engine.exe

Linux / macOS:

./build/sql-engine

(Type .exit to quit the engine at any time).


🧠 How It Works (The Pipeline)

When you type a query like SELECT * FROM users; and press Enter, it goes through a 5-step pipeline. We have thoroughly documented each step in the docs/ folder:

  1. Lexer: The string is chopped into tiny tokens (e.g., SELECT, *, FROM, users).
  2. Parser: The tokens are evaluated against strict SQL grammar rules to ensure the syntax is valid.
  3. AST (Abstract Syntax Tree): The parser packages the validated query into a structured object (e.g., a SelectStatement).
  4. Executor: Evaluates the AST object and figures out which backend system commands need to be run.
  5. Storage Engine: The backend system that physically creates files, searches for rows, and edits the data on your hard drive.

Feel free to explore the docs/ folder to dive deeper into the C++ implementation of each step!


Author

— @zoolpher

  • B.Tech Computer Science (final year)
  • Focus: Systems programming · Database internals · Low-latency software
  • Built sql-engine from scratch as a hands-on exploration of compilation pipelines, AST parsing, and persistent storage in C++17.

License

This project is licensed under the MIT License - see the LICENSE file for details. You are free to fork, modify, and use this project for your own learning and development!

About

A lightweight SQL database engine built from scratch in C++ to explore database internals, query parsing, storage engines, indexing, and query execution step by step.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages