i heard you wanted sql in your nosql
i heard you wanted sql in your nosql
What if you could query Redis with SQL?
SELECT * FROM users__hash WHERE key = 'user:1001'Becomes:
HGETALL user:1001
The idea
Every developer knows SQL. Not every developer wants to memorize HGETALL, SMEMBERS, ZRANGEBYSCORE, and the other Redis, and Mongo, and you get the idea. What if you just wrote SELECT and let something else deal with it, doing the mapping from sql to whatever kv engine?
I had some credits to burn one weekend. This happened.
How it works
Parse SQL with sqlparser, match against patterns, render Redis
commands with Tera templates. The table name encodes the data structure
- users__hash is a hash, posts__list is a
list, scores__zset is a sorted set. Ugly? Sure. Works? Also
yes.
INSERT INTO leaderboard__zset (key, member, score) VALUES ('game:week1', 'user:1001', '2500')Becomes:
ZADD game:week1 2500 user:1001
The BNF grammar defines the mappings. Lua templates handle the output. Adding new patterns is just adding rules. It’s pattern matching all the way down.
Does it work?
For simple stuff, yes. SELECT, INSERT, UPDATE, DELETE across strings, hashes, lists, sets, sorted sets. Probably 80% of what you’d actually do with Redis.
The other 20% is where it gets weird. Nested operations, complex filters, anything that requires thinking. Redis isn’t relational. SQL pretends everything is. Eventually that lie caught up with me.
That said, I genuinely think someone with more patience than me could finish this.
The architecture holds up. It’s just tedious work: more patterns, more templates, more edge cases.
I lost interest after the proof of concept worked. Classic.
Links
- github.com/allen-munsch/rust-sql-to-nosql
- redis.sql.bnf - the grammar