Lua as a Query Language

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:
local l = db.lineitem
local o = l.order
local result = l
:with(l.shipdate:gt(19950315)
& o.date:lt(19950315)
& o.customer.mktsegment:eq("BUILDING"))
:select(l.extendedprice & l.discount)
In the above, :with and :select implement
semijoin and relation composition, respectively, and gt,
lt, eq are various filters. The
& operator is overloaded to implement the relation
product “.and” in Prela, and another nice trick is
overloading the field access customer.mktsegment to
implement customer.select(mktsegment). If the last 2
sentences flew over your head, take a peek at the Prela 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:
local l = db.lineitem
local o = l.order
local revenue = l
:with(l.shipdate:gt(19950315)
& o.date:lt(19950315)
& o.customer.mktsegment:eq("BUILDING"))
:group_by(o & o.date & o.shippriority)
:select(l.extendedprice & l.discount)
:reduce(function(price, disc) return (price * (1 - disc)):sum() end)
Check out the last line: in the closure, price and
disc are really columns of values (or rather, AST nodes
representing columns), and *, - are overloaded
to work over columns.
Overall the queries came out very nicely, many of them even easier to
read than the original SQL! For example, for Q18 of TPC-H:
SELECT c_name, c_custkey, o_orderkey, o_orderdate, o_totalprice, sum(l_quantity)
FROM customer, orders, lineitem
WHERE o_orderkey in (
SELECT l_orderkey
FROM lineitem
GROUP BY l_orderkey
HAVING sum(l_quantity) > :1
)
AND c_custkey = o_custkey
AND o_orderkey = l_orderkey
GROUP BY c_name, c_custkey, o_orderkey, o_orderdate, o_totalprice
Note how the GROUP BY attributes are repeated also on
the first line which is rather noisy. In Prela:
local l, o = db.lineitem, db.order
local result = l
:group_by(l.order)
:select(l.quantity)
:sum()
:gt(300)
& o.totalprice & o.date & o.customer.name & o.customer
After 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…
