Skip to content

Latest commit

ย 

History

History
175 lines (120 loc) ยท 5.36 KB

README.md

File metadata and controls

175 lines (120 loc) ยท 5.36 KB

๐Ÿ“ฆ MiniSQL โ€” Database System Implementation in C++

๐Ÿง  A full-fledged relational database engine from scratch
๐Ÿ“˜ Inspired by: Database Systems: The Complete Book by Garcia-Molina, Ullman, Widom


๐Ÿ“ Overview

MiniSQL is a complete educational database system implemented entirely in C++, developed over the period of 5 months, graduate semester long project. It simulates the core internals of a relational database engine, including query parsing, compilation, relational algebra execution, indexing via B+ Trees, and binary-level storage.

This project was developed in multiple phases, each representing a major subsystem of a real-world DBMS. From catalog management to join algorithms, from scan and filter operations to query plan visualization โ€” everything was built by hand, with a focus on learning and adhering to theory from foundational texts.


โœจ Features

โœ… Custom SQL-like parser using Flex/Bison
โœ… Execution of relational algebra operations: Scan, Select, Join, Project, Aggregate, GroupBy, Duplicate Removal
โœ… Join algorithms implemented:

  • Nested-Loop Join
  • Hash Join
  • Symmetric Hash Join

โœ… Custom-built B+ Tree Indexing (with disk persistence and binary serialization)
โœ… File-based table storage using custom heap files (.dat format)
โœ… Metadata stored via lightweight SQLite-backed catalog
โœ… Graphviz-based query plan visualizer
โœ… Interactive shell with support for:

  • .help
  • .tables
  • .schema
  • .quit โœ… One-command build system via Makefile

๐Ÿ–ฅ๏ธ CLI in Action

Example of entering a query and getting tabular output + plan summary

CLI Screenshot


๐Ÿง  Query Plan Visualization (Graphviz)

Exported DOT graph rendered for a join query

Graphviz Plan


๐Ÿ“ฆ B+ Tree Structure (Create Index)

Shows the tree building from insertions during index creation

BPlusTree


๐Ÿงฎ Disk-backed Heap File (Visual Debug)

Illustrating table storage in tpc-h data

tpc-h data


๐Ÿ—‚๏ธ Project Structure

MiniSQL/
โ”œโ”€โ”€ code/
โ”‚   โ”œโ”€โ”€ headers/              # All header files (Schema.h, Catalog.h, DBFile.h, etc.)
โ”‚   โ”œโ”€โ”€ source/               # All source files (RelOp.cc, File.cc, QueryCompiler.cc, etc.)
โ”‚   โ”œโ”€โ”€ project/              # Phase entry points (e.g., test-phase-1.cc, main.cc)
โ”‚   โ”œโ”€โ”€ execs/                # Output binaries (e.g., main.out, test-phase-*.out)
โ”‚   โ””โ”€โ”€ makefile              # Master Makefile for compilation
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ *.tbl                 # TPC-H formatted raw data files
โ”‚   โ””โ”€โ”€ *.schema              # Optional schema descriptors
โ”œโ”€โ”€ catalog.sqlite            # SQLite-backed metadata catalog
โ”œโ”€โ”€ Dockerfile                # ๐Ÿณ (optional) Docker container setup
โ”œโ”€โ”€ screenshots/              # Folder for screenshots (for README)
โ””โ”€โ”€ README.md                 # ๐Ÿ“˜ This file

โš™๏ธ Build & Run

๐Ÿ“ฅ Requirements

  • Linux (or WSL/macOS)
  • g++, make, flex, bison, sqlite3, libsqlite3-dev, graphviz

๐Ÿ› ๏ธ Compile

cd code
make init
make all

๐Ÿ’ป Start the CLI

./execs/main.out

๐Ÿงช Sample SQL Queries

-- 1. Simple selection

SELECT * FROM nation WHERE n_regionkey = 1;

-- 2. Join with filter

SELECT c.c_name, o.o_orderkey 
FROM customer c, orders o 
WHERE c.c_custkey = o.o_custkey AND c.c_name = 'Customer#000000011';

-- 3. Aggregate with GROUP BY

SELECT c_nationkey, SUM(c_acctbal) 
FROM customer 
GROUP BY c_nationkey;

-- 4. Create Index

CREATE INDEX idx_cname ON customer(c_name);

๐Ÿ’ก After creating an index, MiniSQL will automatically use it for eligible queries via ScanIndex.

๐Ÿ•น๏ธ CLI Commands

Command Description
.help List available commands
.tables Show all tables
.schema Show schema of a table
.quit Exit the CLI

Each SQL query must end with a semicolon ;. Multi-line queries are supported.

๐Ÿ“š References ๐Ÿ“˜ Database Systems: The Complete Book, 2nd ed. โ€” Garcia-Molina, Ullman, Widom

๐Ÿงช TPC-H Benchmark data

๐Ÿงฐ SQLite3 (for catalog metadata)

๐Ÿง  Graphviz (for query plan visualizations)

๐Ÿ™ Acknowledgments

This project is the result of deep effort and iterative implementation over an entire semester. Grateful thanks to:

๐Ÿง‘โ€๐Ÿซ Course instructors & TAs for the structure and feedback

๐Ÿง  The authors of the database textbook that made concepts click

๐Ÿงฐ Tools like SQLite, Graphviz, and Flex/Bison

๐Ÿ™Œ Everyone who reviewed my code, debugged with me, or supported this journey

๐ŸŽฏ Conclusion Building a database engine from scratch is one of the most enlightening journeys in systems programming and data engineering. This project taught me how the database engines we use daily are engineered layer by layer โ€” from bit-level file management to high-level SQL interpretation.

This project is now a powerful sandbox for exploring ideas like query optimization, indexing strategies, and operator design.

Made with โค๏ธ, C++, and coffee. Happy querying! โ˜•