Lua as a Query Language
remywang
54 points
16 comments
November 29, 2026
Related Discussions
Found 5 related stories in 76.8ms across 4,908 title embeddings via pgvector HNSW
- Lua for Data Representation vinhnx · 30 pts · November 25, 2026 · 56% similar
- Programming at Light Speed: Building the Gravity Query Engine poly2it · 18 pts · October 30, 2026 · 46% similar
- Show HN: I created a library for correctly using SQLite in Go programs sethops1 · 21 pts · November 11, 2026 · 41% similar
- Serving Qwen via AWS Lambdas for fun and 80x negative profit octember · 30 pts · November 25, 2026 · 40% similar
- Arrows to arrows, categories to queries (2025) firstbase · 19 pts · November 04, 2026 · 40% similar
Discussion Highlights (12 comments)
phillc73
A little while ago I had a similar itch, but my query language at home happened to be Janet[1]. It's built over DuckDB and Rayforce[2]. Single query, either engine. [1] https://github.com/jennystats/janet-query [2] https://github.com/RayforceDB/rayforce
shakna
> Sadly comparisons like < and == cannot be overloaded in Lua :( You can overload == in 5.3. Before that, there was a slightly-less-overloadable __eq method. But as of 5.3, as long as the bound __eq method is the same underlying function, it will be called. I assume you meant instead, that the return value of the comparison is always coerced to a boolean, so you can't re-use it to get arbitrary infix operators? For that sort of thing, I've used Mike Pall's hack in the past: local sm = setmetatable local function infix(f) local mt = { __sub = function(self, b) return f(self[1], b) end } return sm({}, { __sub = function(a, _) return sm({ a }, mt) end }) end local shl = infix(function(a, b) return a*(2^b) end) print(5 -shl- 4)
behnamoh
You can do this with Ruby and Elixir too, and the syntax would end up much nicer!
PinkSheep
Tarantool[1] has been using Lua for the queries since inception. Eventually 3rd party users complained enough that they added a SQL as a query frontend. [1]: https://en.wikipedia.org/wiki/Tarantool
Rohansi
This is almost what C# does with LINQ to SQL, etc. Very cool hack.
harrykris
once i used lua to rice linux, the language was fun to use "it's not really a query langauge. It's just a bunch of Rust types and methods that give the feel of a query language" I FEEL THIS LMAO nice project anyways love it
inkyoto
There has been no shortage of query and SQL-like DSL's in different programming languages; however, when it comes to querying, Prolog is heavily underrated. The interesting property of Prolog is that it needs neither a DSL nor a differentiation between input and output parameters at the level of its relational predicates. Joins, filtering, projection and grouping can all also be expressed using native Prolog constructs. The same Prolog query can also be run in both directions, including the reverse. That is, if I known the «answer» (i.e. the result), I can ask Prolog to give me an exhaustive set of «questions» that may lead to the «answer». The first sample query (named «exhibit1») from the blog post can be rewritten in Prolog thusly (SWI-Prolog is used in the example): % The imaginary data model % lineitem(Order, ShipDate, ExtendedPrice, Discount). % order(Order, Customer, OrderDate, ShipPriority). % customer(Customer, MarketSegment). exhibit1(Price, Discount) :- lineitem(Order, ShipDate, Price, Discount), order(Order, Customer, OrderDate, _), customer(Customer, 'BUILDING'), ShipDate #> 19950315, OrderDate #< 19950315. Note that it uses the bog standard Prolog syntax. We can now ask to: 1. Retrieve all matching prices and discounts: ?- exhibit1(Price, Discount). 2. Retrieve discounts associated with a price of 200: ?- exhibit1(200, Discount). 3. Retrieve prices associated with a discount of 10%: ?- exhibit1(Price, 1r10). 4. Determine whether a particular price-discount combination exists: ?- exhibit1(200, 1r10). 5. Retrieve prices and discounts subject to additional constraints: ?- { Price > 100, Discount < 1/5 }, exhibit1(Price, Discount). 6. Collect all results into a list: ?- findall(Price-Discount, exhibit1(Price, Discount), Results). Note how the query itself («exhibit1») does not change and remains the same for all queries .
lenkite
> Overall the queries came out very nicely, many of them even easier to read than the original SQL! Really ? I found the SQL far easier to read at a single glance compared to the special Lua syntax that I had to pause, focus and think on to grok.
ianm218
Since the author mentioned Rust and Lua I've bee chipping away at OmniLua for awhile https://github.com/ianm199/omnilua a project that bundles Lua 5.1-5.5 in one Rust project. Might be useful for this kind of thing!
steve_taylor
Not much is really gained here in terms of shifting query validation from runtime to build time as Lua is dynamically typed.
hoppp
The SQL is more readable for me, but I am very used to looking at that.
lasajar
Very nice, Lua is great for creating small DSLs. Honestly I wish Lua was more mainstream as an embedded language.