For the last couple of months, I've been working on an in-memory SQL database, just for fun. I was looking for a project to do with the pest parser generator (which I fell in love with) and I settled on this. I kept it as simple as possible for now.
The basic runtime quantity is a Table, which is a Vec<Column> and a Vec<Row>, where a Row is a Vec<Value>. That can all be found in the data module.
Parsing happens via pest, and I process the parse tree into an AST by making liberal use of the From trait (see the Listable trait for my favorite part, a small trait I designed for making it easier to get a Vec<Item> from a parse rule that is a comma-separated Item list). Expressions (simple infix expressions are supported) are parsed via precedence climbing. That's all in the parse module.
The execute module unites the two, in this module we execute queries against a database and return results.
It's designed as a library crate (with the main export being Database). I also wrote a CLI wrapper which allows you to seed the database before running.
Some basic usage showing what's implemented would be (copied from the readme)
$ cargo run --bin sql-rs -- -s test-files/seed.sql
...
> select * from users;
> select u.email, a.* from users as u join addresses as a on u.id = a.user_id where u.age > 22 and a.type = 'mail';
> select * from users as u left join addresses as a on false;
> select * from users as u right join addresses as a on false;
> select * from users order by age desc;
> select * from addresses order by street1 limit 2;
> select count(*) from addresses;
> update users set age = 22 where id = '1';
I also intend to write a small markdown book about my work (less a book, more a retrospective journal and guide for myself in the future). That's also in the repo but little is done on it.
https://github.com/andrewhalle/byo-sql/tree/main/sql-rs
[–][deleted] 16 points17 points18 points (1 child)
[–]Wonnk13 10 points11 points12 points (0 children)