Lua 作为查询语言
Lua as a Query Language

原始链接: https://remy.wang/blog/lua-ql.html

作者创建了 Prela,一种嵌入 Rust 的查询语言,但用 rustc 编译动态查询太慢。为避免编写解析器,他们使用内嵌 Lua:关系运算构建 AST 节点而非物化数据,AST 被传递给 Rust 后端。Lua 变量充当 CTE,字段访问和 `&` 编码关系操作。聚合闭包中的运算符被重载以作用于列,避免逐行 Lua/Rust 往返。结果是可读的查询,通常比 SQL 更简单,而执行仍留在 Rust 中。一个关键注意事项是安全性:Lua 必须沙箱化。

一篇题为“Lua as a Query Language”的 Hacker News 帖子有 6 分和 2 条评论。一位评论者指出 Lua 比较运算符重载的限制——`==` 在 5.3 中可重载,但结果会被强制转换为布尔值——并分享了 Mike Pall 使用 `__sub` 创建中缀运算符的技巧。另一位评论者描述了一个类似项目 janet-query,它基于 DuckDB 和 Rayforce 构建。
相关文章

原文
lua-ql

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 itself turned out to be very nice, but there is one problem: it's not really a query langauge. It's just a bunch of Rust types and methods that give the feel of a query language. On one hand that's very convinient, because it means all the tooling built for Rust, including type checking, syntax highlighting, go-to-definition, etc., all work for Prela immediately. On the other hand, every query is compiled by rustc which usually takes even more time than executing the query itself... That's fine if the same query is meant to run again and again, but would be too much overhead for ad hoc queries.

So how do we support dynamic Prela queries? The right thing to do is probably just design a syntax and write a parser, but again I'm allergic. Also, DB queries are basically little programs embedded within the host language, and we already have an excellent language born to be embedded -- Lua! More practically, query languages often require constructs like arithmetic expressions and variables (CTEs) which are of course already available in Lua.

The main idea is pretty simple: implement relational operators in Lua, but make them return AST nodes instead, and pass the AST to the Rust backend. This is basically how dataframe libraries implement relational algebra, but it's particularly nice for Prela because its algebraic foundation, Tarski's Algebra of Relations, is a pure algebra, unlike relational algebra operators which need to be annotated with attribtue names to make sense. In any case, here's an example of how that looks like:

tutorial; but they are really just operators defined over (binary) relations. We're also leveraging Lua's local variables to implement CTEs: indeed, the relations l and o are not materialized but define views.

Things get a little tricky when we get to aggregations and maps/"generalized projections". In the Rust implementation, we just pass in closures to be mapped/folded over the relations, which works fine because the closures are statically compiled and inlined. However, it'd be hella slow if we pass a Lua closure to the Rust side and do a round trip running the closure for every row. Instead, we'll once again overload operators that appear in closures to work over columns, and have the reduce function apply the closure to column types:

unnesting the subquery, we leverage the fact that l.order functionally determines o.totalprice etc. to only group on l.order. We also don't need a special HAVING clause and can use the .gt filter directly on the aggregated groups. Finally, joins in Prela are implicit, again thanks to Tarski, which lets us drop the noisy c_custkey = o_custkey and o_orderkey = l_orderkey.

One caveat in (ab)using Lua as the query language is security, as you don't want the user executing arbitrary Lua code. That can probably be addressed by sandboxing and not exporting the standard library. OTOH one'd need to sanitize SQL queries anyways.

The code is here but I've only spent a few hours vibing so it's pretty sloppy. I might clean it up after the holidays...

联系我们 contact @ memedata.com