lua-ql: Query Rust Databases From Lua

A new Lua query language interface called lua-ql lets developers run ad hoc Prela queries without paying Rust’s compile cost each time. Its author, Remy Wang, built it as a quick side project and calls the code sloppy for now.

The problem with compiled queries

Prela is a query language Wang invented to avoid writing a SQL parser. It isn’t a language in the strict sense. It’s a set of Rust types and methods that feel like one. That brings a real benefit, since type checking, syntax highlighting and go-to-definition from the Rust toolchain work on Prela queries right away.

The cost is that rustc compiles every query. That often takes longer than running the query. It’s fine for a query that runs repeatedly. For a one-off question, the overhead is too much.

Why Lua fits

The obvious fix is to design a syntax and write a parser, but Wang wanted to skip that. He chose Lua, a language built to be embedded. A database query is a small program living inside a host language, and query languages need things like arithmetic expressions and variables for CTEs, which Lua already has.

The method is simple. Relational operators are implemented in Lua, but they return AST nodes instead of computing results. The AST then goes to the Rust backend. Dataframe libraries use a similar idea. It suits Prela especially well because Prela rests on Tarski’s algebra of relations, which is a pure algebra. Classic relational algebra operators need attribute names to make sense.

What the code looks like

In the example, :with acts as a semijoin and :select as relation composition, while gt, lt and eq are filters. The & operator is overloaded to mean Prela’s relation product, and field access such as customer.mktsegment is overloaded to mean customer.select(mktsegment). Lua’s local variables stand in for CTEs: the relations l and o aren’t materialized, they define views.

Aggregations and generalized projections are harder. The Rust version passes closures to map or fold over relations, and those are compiled and inlined. Sending a Lua closure across to Rust would mean a round trip for every row, which would be very slow. Instead, operators inside closures are overloaded to work on columns. In a reduce call, arguments like price and disc are AST nodes representing whole columns, and * and – are overloaded to match.

A TPC-H comparison

Wang says many of the resulting queries read better than the SQL originals. His example is TPC-H Q18. The SQL version repeats the GROUP BY attributes on the first line, which he finds noisy. The Prela version groups only on l.order, because that field functionally determines o.totalprice and the other order fields. It needs no HAVING clause, since .gt filters the aggregated groups directly. And joins are implicit, so lines like c_custkey = o_custkey and o_orderkey = l_orderkey disappear.

Who this helps

The design targets a specific gap. Rust-backed databases get strong tooling when queries are Rust code, but that tooling comes with a compile step that doesn’t suit interactive work. Someone exploring a dataset wants to type a query and see rows, not wait on rustc between every tweak.

Lua sits on the other side of that trade. A Lua script runs immediately, so a developer can adjust a filter, rerun, and get an answer. The heavy lifting still happens in the Rust engine, because Lua only describes the query and never touches the rows. That split keeps the per-row work in compiled code, which is the whole reason the column-overloading trick matters.

The approach also keeps the query language small. Variables, arithmetic and local views come from Lua itself, so the project doesn’t need its own versions of them. Wang’s earlier decision to avoid a parser carries through: the only new code is the layer that turns Lua operator calls into AST nodes.

Security and limits

Using Lua this way has an obvious risk: users shouldn’t be able to execute arbitrary Lua. Wang says sandboxing and withholding the standard library should probably cover it, and he notes that SQL queries need sanitizing anyway.

There’s one language annoyance. Lua can’t overload comparisons like < and ==, which is why the examples use named methods such as gt and eq.

What to expect next

The code is public, but Wang says he spent only a few hours on it and it needs work. He might clean it up after the holidays. Anyone who wants to try the Lua query language should treat it as a prototype for now. Wang hasn’t announced a release schedule or benchmarks.

Source: remy.wang

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.