← Sun, Nov 29 digest

Lua as a Query Language

read article ↗ hn thread ↗

Remy Wang uses Lua as an embedded query language frontend for a Rust relational query engine, avoiding custom parsers and heavy compilation overhead.

summary is enough A concise and entertaining blog post detailing a clever DSL experiment that can be fully understood via its code examples.

summary

Remy Wang embeds Lua as a query language frontend for Prela, a Rust-based relational query engine, to avoid the compilation overhead of dynamic Rust queries. By overloading Lua operators and methods, queries build AST nodes that are passed down to the Rust backend for execution. Leveraging Tarski's Algebra of Relations allows for implicit joins and concise syntax that can outshine traditional SQL. However, caveats like security sandboxing remain if handling untrusted user input.

key points

  1. Prela's original Rust-based query formulation required compiling via rustc, which is too slow for ad hoc queries.
  2. Lua operators are overloaded to generate AST nodes representing columns and relations instead of executing immediately.
  3. Tarski's Algebra of Relations enables implicit joins, eliminating the noisy table-matching conditions common in SQL.

who should read it

Database engineers, systems developers, and language designers interested in embedded DSLs.

discussion positive

consensus

Commenters appreciate the clever use of Lua as a lightweight DSL, drawing parallels to LINQ and other language-embedded query approaches, though opinions vary on readability compared to SQL.

pushback

Some critics argue that SQL remains significantly easier to read at a glance, and note that Lua's dynamic typing loses the build-time safety guarantees of Rust.

best comment by 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 …

It offers a clever technical workaround using Mike Pall's hack to achieve custom infix operators in Lua since standard comparison operators cannot be overloaded.

Generated by an LLM from the article and thread. Check the sources before relying on it.