Lua as a Query Language remy.wang · 20 points · 4 comments · HN thread TL;DR The...
HN Best 200 @best
Lua as a Query Language remy.wang · 20 points · 4 comments · HN thread
TL;DR The author built Prela, a query language that is really Rust types and methods, and now proposes embedding it in Lua so ad hoc queries skip the rustc compile step. Relational operators in Lua return AST nodes that are passed to the Rust backend, with local variables serving as CTEs and overloaded operators handling expressions. The post includes TPC-H Q18 rewritten in the style and notes security as the main caveat.
- Prela was first written as Rust types and methods, so it gets type checking, highlighting and go-to-definition for free, but every query must be compiled by rustc, which often takes longer than running it.
- The proposal overloads Lua operators (&, *, -) and methods like :with, :select and :group_by so they build an AST instead of computing results, which is then handed to the Rust backend.
- Lua local variables act as CTEs; relations are views and are not materialized.
- Aggregations avoid per-row round trips into Lua by overloading operators to work on columns, so a reduce closure applies to column values.
- The TPC-H Q18 example uses implicit joins and functional dependence to group only by l.order, and the author claims it reads more easily than the SQL.
- Security is the main caveat: user Lua would need sandboxing and a stripped standard library. The code is a few hours of sloppy vibe-coded work.
- Lua cannot overload < or ==, so the author uses methods like :eq instead.
Discussion The thread is small, with four comments, and mostly focuses on Lua's operator overloading limits. It is friendly and technical, with commenters offering tricks and alternatives rather than debating the design.
- Lua can overload == since 5.3 and infix tricks exist (some): shakna corrected the claim that == cannot be overloaded, noting that 5.3 calls __eq when the metamethod is the same function. They also shared Mike Pall's infix-operator hack using __sub, which allows syntax like 5 -shl- 4.
- Method syntax like a:eq(b) is good enough (a few): The author replied that the comparison result is coerced to bool, so arbitrary infix operators are not reusable for comparisons, and that a:eq(b) is cleaner than the hack.
- Similar query language built on DuckDB exists (a few): phillc73 described Janet, a personal query language on top of DuckDB and Rayforce that supports either engine for a single query.
- Ruby or Elixir would give nicer syntax (a few): behnamoh argued that the same DSL approach works in Ruby and Elixir with much nicer syntax than Lua.
Links from the comments