Introduction
Databases can seem like magic until you build one. This project implements a meaningful subset of SQL in three modules — a lexer, a parser and executor, and an interactive shell — and shows how real systems handle types, NULLs and aggregates.
Problem statement
Objectives
- Write a tokenizer with regular expressions
- Write a recursive-descent parser
- Execute queries over in-memory rows
- Organise a program into modules
Features
- Multi-line statements with an interactive prompt
- Typed columns with validation and NULLs
- WHERE with AND/OR and LIKE wildcards
- Aggregates with GROUP BY, ORDER BY and LIMIT
- UPDATE, DELETE, DROP, SHOW TABLES and DESCRIBE
Concepts demonstrated
- Lexing and parsing
- Query execution
- NULL semantics
- Modular design
System architecture
sql_lexer.tokenize turns text into typed tokens (numbers, quoted strings with '' escapes, operators, keywords and names). sql_engine.Parser offers accept/expect helpers, and Database.execute dispatches on the first keyword to a method per statement that parses and executes it. WHERE clauses become a list of AND-groups combined with OR. Aggregates skip NULLs, except COUNT(*). main.py reads statements, echoes them when input is redirected and renders results as bordered tables.
Folder structure
Complete source code
main.py
What each file does
- main.py
- The interactive shell: reads statements ending in ';', renders result tables and prints errors.
- sql_lexer.py
- The tokenizer and the SQLError exception shared by the other modules.
- sql_engine.py
- The Table and Parser classes and the Database executor for every statement type.
How to run it
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
CREATE TABLE students (roll INT, name TEXT, city TEXT, marks REAL); INSERT INTO students VALUES (1, 'Asha', 'Pune', 91.5), (2, 'Ravi', 'Delhi', 78), (3, 'Meera', 'Pune', 85), (4, 'Kiran', 'Chennai', 66.25), (5, 'Arjun', 'Delhi', 72); INSERT INTO students (roll, name) VALUES (6, 'O''Brien'); SELECT * FROM students; SELECT name, marks FROM students WHERE marks >= 75 ORDER BY marks DESC; SELECT name FROM students WHERE city = 'Delhi' OR city = 'Chennai' AND marks > 70; SELECT name, city FROM students WHERE name LIKE 'a%'; SELECT city, COUNT(*), AVG(marks), MAX(marks) FROM students GROUP BY city ORDER BY city; SELECT COUNT(*), COUNT(marks), SUM(marks) FROM students; UPDATE students SET city = 'Mumbai', marks = 70 WHERE roll = 6; DELETE FROM students WHERE marks < 70; SELECT roll, name, city FROM students ORDER BY name LIMIT 3; INSERT INTO students VALUES (7, 'Sana', 'Goa', 'high'); SELECT grade FROM students; SELECT * FROM teachers; SELEC * FROM students; SHOW TABLES; DESCRIBE students; quit
Testing
- Run
python main.py < tests/sample-input.txtand compare withtests/expected-output.txt. - The O''Brien row is inserted with NULL city and marks, which aggregates ignore: COUNT(*) is 6 but COUNT(marks) is 5.
city = 'Delhi' OR city = 'Chennai' AND marks > 70returns Ravi and Arjun, because AND binds tighter than OR.- Inserting 'high' into a REAL column, an unknown column, an unknown table and the typo SELEC all produce errors.
Common errors
Strings containing quotes break the tokenizer
Match '...' with '' as an escaped quote, then replace '' with ' in the value.
A bad row in a multi-row INSERT leaves the others inserted
Validate every row first and extend the table only at the end.
Possible improvements
- Add parentheses in WHERE expressions
- Support JOIN between two tables
- Persist tables to JSON or CSV files
Additional challenges
- 1Add an index (a dictionary from value to rows) and use it for = conditions.
- 2Implement HAVING after GROUP BY.
- 3Add transactions with BEGIN, COMMIT and ROLLBACK.
Built and tested it yourself? Mark it to update your dashboard.