Lua as a Query Language






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…



Source link