hn.today

Lua as a Query Language

remy.wang76 points17 comments
Screenshot of Lua as a Query Language

Prela is a Rust library that expresses database queries through types and methods rather than a separate query syntax. This brings Rust’s tooling and type checking, but every query must be compiled by rustc, making one-off, dynamic queries impractical. The proposed alternative is to use Lua as an embedded query language, avoiding the need to design and parse a custom syntax. Lua functions implement relational operations but return abstract syntax tree nodes, which a Rust backend can execute. Relations can be stored in Lua local variables as views, while overloaded operators and field access express filters, combinations, and projections. This approach also uses Lua variables for constructs similar to common table expressions.

Aggregations require care: calling a Lua closure once per database row would incur costly transitions between Lua and Rust. Instead, the closure’s arguments represent columns, and overloaded arithmetic builds expressions over those columns; the backend then applies the reduction. The resulting queries can be more concise than SQL. In the TPC-H Q18 example, Prela avoids repeating grouping columns, applies a filter directly to aggregated results, and uses implicit joins, relying on functional dependencies and the underlying relational algebra. The main drawback is security: executing user-provided Lua requires sandboxing and withholding access to the standard library. Lua also cannot overload comparison operators such as less-than or equality, limiting how naturally some conditions can be written.

Read on remy.wang17 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.