Prela creator uses Lua as a query language to skip a parser
In a post on remy.wang, the creator of the Prela query language describes an allergy to writing parsers, so severe that it led to inventing Prela just to avoid parsing SQL. Prela is not really a query language, the author says. It is a set of Rust types and methods that feel like one. The upside is that Rust tooling such as type checking, syntax highlighting and go-to-definition works right away. The downside is that rustc compiles every query, which usually takes even longer than running it. That is fine for a query that runs again and again, but too much overhead for ad hoc ones.
The probable right answer, the author concedes, is to design a syntax and write a parser. Instead, the post turns to Lua, a language built to be embedded. Database queries are basically little programs inside a host language, and they often need arithmetic expressions and variables (CTEs, or common table expressions), which Lua already has. The idea is to implement relational operators in Lua, have them return abstract syntax tree (AST) nodes, and pass that tree to the Rust backend. This is basically how dataframe libraries implement relational algebra. The author says it suits Prela particularly well because its foundation, Tarski's Algebra of Relations, is a pure algebra, while relational algebra operators need attribute names to make sense.
The first example filters line items by ship date, order date and the customer market segment "BUILDING", then selects extended price and discount. Here :with performs a semijoin, :select performs relation composition, and gt, lt and eq are filters. The & operator is overloaded as Prela's relation product, field access such as customer.mktsegment is overloaded to mean customer.select(mktsegment), and Lua local variables act as CTEs that define views without materializing them.
Aggregations are trickier. The Rust version passes in closures that are compiled and inlined, but calling a Lua closure from Rust with a round trip for every row would be very slow. So operators inside closures are overloaded to work on whole columns, and reduce applies the closure to column types. In a revenue calculation, price * (1 - disc) operates on AST nodes that represent columns.
The author says the queries came out very nicely, many even easier to read than the original SQL. The showcase is Q18 of the TPC-H benchmark. The Prela version unnests the subquery, groups only on l.order because it functionally determines the total price and other attributes, applies a .gt(300) filter straight to the aggregated groups with no special HAVING clause, and leaves joins implicit.
One caveat is security, since users should not be able to run arbitrary Lua. Sandboxing and leaving out the standard library can probably address that, the author suggests, adding that SQL queries need sanitizing anyway. The code is shared but, after only a few hours of "vibing", pretty sloppy. The author might clean it up after the holidays.
Key facts
- Prela is a set of Rust types and methods, so each query goes through rustc, which usually takes even longer than executing it.
- The new approach implements relational operators in Lua that return AST nodes, which are passed to the Rust backend, with no new parser to write.
- To avoid a slow per-row round trip into Lua, operators inside aggregation closures are overloaded to work on columns.
- In the TPC-H Q18 example, the Prela version groups only on l.order, filters groups with .gt(300) instead of HAVING, and leaves joins implicit.
- The code reflects a few hours of work and the author calls it sloppy; sandboxing is suggested as a probable fix for the risk of arbitrary Lua execution.
Why it matters
Prela's Rust design brings ready-made tooling but makes every query wait for rustc, which the author considers too costly for ad hoc work. The Lua layer is a way to get dynamic queries while keeping a Rust backend.
The broader design point is that queries are small programs. Embedding them in an existing language supplies arithmetic and variables for free. The author likens the AST-building method to dataframe libraries and argues it fits Prela especially well because Tarski's Algebra of Relations is a pure algebra.
Who it affects
The most direct case is anyone who wants to run one-off Prela queries without paying Rust compile time each time. The technique may also interest people designing query languages or embedded domain-specific languages who would rather not write a parser: operator overloading in a host language does much of the surface work here.
How to use it
In the examples, you bind relations such as db.lineitem to Lua locals, chain methods like :with, :select, :group_by and :reduce, and combine relations with &. Filters are written as methods, such as gt, lt and eq; a footnote in the post laments that comparisons like < and == cannot be overloaded in Lua. For background on the underlying operators, the author points readers to the Prela tutorial.
The post says the code is available, with the warning that it is pretty sloppy. No release, version, license or production readiness is announced.
How solid is it
This is a first-person account on remy.wang of an early prototype, written by Prela's own creator after only a few hours on the code. The readability verdict, that many queries are even easier to read than the SQL originals, is the author's judgment, and the post shows Q18 as an example. No benchmarks or timing figures are given for the Lua front end, rustc compilation or query execution.
Risks and caveats
Security is the caveat the author raises: users should not be able to execute arbitrary Lua code. Sandboxing and not exporting the standard library are offered as a probable remedy, and the post does not say this has been implemented. The author adds that SQL queries would need sanitizing too.
The code is sloppy by the author's own account, and cleanup is only a possibility for after the holidays. Per-row Lua closures would be very slow, so aggregation logic relies on operators overloaded to work over columns.
“Somehow I developed an allergy to writing parsers, so severe that I made up an entire query language (called Prela) just to avoid having to parse SQL.”
— Prela's creator, writing on remy.wang