Show HN: I wrote a RDBMS (SQLite clone) from scratch in pure Python
github.com
github.com
My perspective is that writing this kind of system in a language such as Python is actually a great thing because for myself Python is more widely readable and approachable compared to C++ or C which is what databases are often programmed in. If someone wants to be serious they can port it to a low level language . As it stands it's educational and useful for studying.
I wrote a distributed pseudo multi model SQL/graph Cypher/Document and dyanmodb style database in Python https://GitHub.com/samsquire/hash-db for the same goal of learning how database engines could work in a distributed way.
I had tried out one or two Java-based RDBMSes back in the day, via programs written in Java, for fun.
I think one was HSQLDB.
https://en.m.wikipedia.org/wiki/HSQLDB
There was also another interesting one called PointBase, which was developed by Bruce Scott, an Oracle founder, and others.
Both C and Python are very "approachable" if you ignore the bad language design, inconsistencies and other "gotchas" and only take the "easy" parts, disregarding edge cases. However, with C you could at least have a fighting chance to learn how to do things right, but with Python you'll never even know what the real thing is like.
What do you mean? Surely the concepts are similar regardless of using Python or C?
The real problems you will have to solve when working with a database anyone would want to use are, for example:
* Memory layout of the buffers used to write / read / cache this data. In Python, you don't even have a concept of memory alignment / layout -- it's all happening somewhere in the interpreter.
* How to best service multiple requests concurrently. Again, Python offers nothing here, and nothing to experiment with. But this is a huge part of working with databases. The whole two-stage commit, transaction, consistency guarantees -- it's the central point of databases, but Python gives you no tools to even try anything like that.
* Working with various underlying storage... most of it has no Python interface (but does have C interface). Eg. if you want to understand how to optimize storing data by using some filesystem -- those filesystems do often expose similar interfaces that go beyond VFS, but they won't be immediately available to Python.
* Working with vectorization of queries -- again, Python doesn't have a concept of ISA, doesn't have any way to instruct the code to utilize any particular CPU instructions... but this is where a lot of work is done by people who work on real databases. Not being able to get any meaningful access to query optimization, planning becomes pointless / useless -- what are your concerns going to be when you write a query planner if you still have no idea how it's going to be executed?
* Similar stuff goes for networking -- whenever you need to solve something that goes beyond the absolute trivial you will at best rely on Python bindings to some library that actually does that rather than on Python code proper. In other words, if your goal was to learn how to do it, you will not achieve that goal because the actual work will happen elsewhere.
So... I don't know... there is no way you can really learn how to make databases in Python. You can probably learn something, depending on what's your baseline, but you will not be ready to make a real thing, if all you have is Python. It's a difference between toy doctor set and learning to be a surgeon...
Often when learning, you do not implement every difficult edge case, the most complex. You are just trying to get the jist of it. You want a smaller problem to solve.
Maybe as a learning project, it isn't important to have concurrency, networking, memory management. Unless, any of those things happens to be of interest to learn about also, then add them back in.
I don't think this is trying to be an argument for Python as good to build an RDB in. (of course it isn't for all the reasons you list)
Python just happens to be an easy language for beginners, so why not build a basic RDB to learn about that too.
Giving someone Python to make a database, is like giving a student in a culinary school a dull knife: it's hard to do it with the right tools, but it becomes mission impossible when you are also crippled by your tools.
It's the same analogy I used before: using toy "doctor set" vs. learning to be a surgeon. There's no path that will bring you from using a toy set to be a surgeon. It serves a different purpose: entertainment / roleplay. You don't mean to roleplay as a programmer by using Python, right?
The experts are doing databases in C and C++ and Rust. But I'm not a C, Rust or C++ expert, so I need to start somewhere, where I am today.
I start small accomplishable goals to get the idea of the problem solution so I'm not distracted by boilerplate C, C++ or Rust. My multiversion concurrency control is in Java.
You might think all the things are easy but they're not easy to everyone. We have to start somewhere and one way to start is to write the parser in a simple language so you're not wrestling with memory management.
If I tried to do all the things you mentioned in C++, Rust or C it would be too much work in one step. I need to start small to have an achievable result.
Not everyone is Stonebraker or Linus Torvalds.
Probably here on github -> https://github.com/CsharpDatabase/CsharpSQLite - and possibly some more clones after that.
I know this was never meant to be fast, but could you produce some benchmarks for shits and giggles?
* https://www.brendangregg.com/activebenchmarking.html
* https://www.brendangregg.com/blog/2018-06-30/benchmarking-ch...
The JSON tutorial on their site is excellent - shows how to build a basic parser for JSON, then goes into some great detail about how to improve its performance: https://lark-parser.readthedocs.io/en/latest/json_tutorial.h...
Here's the grammar used for the RDBMS project: https://github.com/spandanb/learndb-py/blob/master/learndb/l...
Even a dict with expected keys and construction via the bitwise or operator (which would roughly match the form of a lot of the grammar) would be better wouldn't it? Imports could be imports, just mixed in somehow.
This is just first thoughts at a glance, maybe I'm missing something.
A DSL that is a string, while having some downsides, does not have the arbitrary limitations of the "host" language. Either approach has its pros an cons.
It's the tooling aspect that's my gripe with it in a string really, it makes it less likely (and more editor-specific) that I can have syntax highlighting, LSP, etc. That's incidentally the only way in which I don't prefer SQL to Django ORM - i.e. it really isn't that it's a DSL (SQL) that bothers me, it's the string.
But hopefully obviously it's not a big deal, I was just surprised at the look of it when I saw it described as 'really nice' and that the project describes itself as 'focussed on ergonomics'. It just doesn't seem brilliant to me, fine perhaps, par for the course apparently, but not remarkable.
The only one I've seen is this one: https://parsy.readthedocs.io/en/latest/tutorial.html
1: https://github.com/massung/parse
2: https://wiki.call-cc.org/eggref/5/comparse
5: https://www.gnu.org/software/guile/manual/html_node/PEG-Pars...
T̶h̶e̶r̶e̶ ̶m̶a̶y̶ ̶b̶e̶ ̶s̶o̶m̶e̶ ̶p̶o̶s̶t̶ ̶p̶a̶r̶s̶i̶n̶g̶ ̶v̶a̶l̶i̶d̶a̶t̶i̶o̶n̶ ̶t̶h̶a̶t̶ ̶c̶a̶n̶ ̶b̶e̶ ̶d̶o̶n̶e̶ ̶h̶e̶r̶e̶-̶ ̶b̶u̶t̶ ̶t̶h̶a̶t̶ ̶w̶o̶u̶l̶d̶ ̶b̶e̶ ̶s̶o̶m̶e̶t̶h̶i̶n̶g̶ ̶t̶h̶a̶t̶'̶s̶ ̶b̶e̶y̶o̶n̶d̶ ̶t̶h̶e̶ ̶d̶o̶m̶a̶i̶n̶ ̶o̶f̶ ̶t̶h̶e̶ ̶p̶a̶r̶s̶e̶r̶.̶
Edit: I see what you mean. I surveyed a bunch a parser generator libraries, and they also seemed to use a text based DSL- rather than DSL based on python structures. What you're describing would have made the grammar development more ergonomic and simple.
The rationale is that it's more terse and has less visual clutter than a DSL over Python, which makes it easier to read and write.
Their IDE was super useful for debugging the grammar: https://www.lark-parser.org/ide/
We use Lark for a SQL-like language tailored for using AI models in EvaDB: https://github.com/georgia-tech-db/evadb/blob/master/evadb/p... https://github.com/georgia-tech-db/evadb/
If you like Lark, please consider sponsoring them: https://github.com/sponsors/lark-parser
Would a logical next step be Generate an optimal query plan from the AST(somehow..)?
SQLite is very hard to read, but this one is actually quite comprehensible. Especially the VM part: https://github.com/spandanb/learndb-py/blob/master/learndb/v...
Compare it with this: https://github.com/sqlite/sqlite/blob/master/src/vdbe.c
That's said, I'm curious how complete this LearnDB is. SQLite is hard to read not only it's old but also it covers a lot of SQL and following SQL spec makes hings complicated. SQLite has great test suite so it's nice if you run the suit against this implementation.
I love databases and Python, so this was really interesting to walk through. Thanks for the post.
I’m not asking this to imply that it can/should. I just want to know what all was attempted besides b-trees and SQL.
I’d like to do something like this someday. Nice work!
It doesn't have a notion of atomically batching multiple statements, i.e. transaction. But beyond that, it's a single file database, which can only have a single process (learndb instance) that is operating on the database (file). So you get consistency and isolation via being a single connection database. Durability, you get to the extent that the file system is durable. So it's somewhere on the ACIDity spectrum.
Re: Query planning/optimization
I haven't implemented this; but I've considered where the optimization could module sit: The parser spits out an AST. This or a derived intermediate representation could be optimized,i.e. the AST could be rewritten or nodes deleted, before the VM executes the AST.
I am curious about the benefits and limitations of using Python in this project as opposed to C++. How well is LearnDB able to support low-level concurrency control and storage management?
OP rewrote the actual database in Python, so (if it's a 1:1 equivalence), you would still use the python `sqlite3` module to connect to OP's project.
As mentioned, it's an educational project, not really meant to be used as a replacement for sqlite in projects etc though.
i'm not sure current python is capable of running it
I dont see "JOIN" there.
So either the DB doesnt support it or the documentation (on the splash page!) is wrong.
Thanks for the downvote.
Please don't do this here.
Additionally, it could have been anyone.
Mad props to the author. Many Python programmers never had proper training in computer science, so it is encouraging to see people filling in the gaps of their knowledge.
Someone in our community built something and had the courage to release it. Your criticism is unfair.
I think it doesn't even get close to being a criticism, and it's certainly unclear if the goal is to literally clone SQLite or to implement SQLite-ish. This is a fair question.
I don’t recommend writing a DB in Python. So much of DB development is dealing with race conditions and consistency.
Python files require an interpreter. Bad for a DB.
And dependency management is a pain. Are you thinking a virtualenv for this?
> > What I Cannot Create, I Do Not Understand -Richard Feynman
> In the spirit of Feynman's immortal words, the goal of this project is to better understand the internals of databases by implementing a relational database management system (RDBMS) (sqlite clone) from scratch.
> This project was motivated by a desire to: 1) understand databases more deeply and 2) work on a fun project.
It sounds like they couldn't care less if it's production-ready.
https://github.com/paul-gauthier/aider https://www.mentat.codes/ https://www.gitwit.dev/ https://www.second.dev/
Your comment seems to have nothing to do with the post and mostly has a bunch of links. If there is some relevance please make it clearer by editing your comment.
Your projects might be very cool in which case just submit them to HN on their own instead of commenting.
Commenting on other people's downvotes is usually not helpful. Accept the votes, learn from the experience and take that forward when participating in this community. The guidelines are pretty good, please read them as well: https://news.ycombinator.com/newsguidelines.html
We do not need a comment about "someone should apply AI/ML to this" on every post any more than we need a "this but NFT" or "this but blockchain" or "this but React" or "this but OOP" comment. Your comment is just noise.