Lua as a Query Language

Hacker News by 5 min read 17x views
Lua as a Query Language

Share Post

Somehow I developed an allergy to penning parsers, so serious that I made up an complete query tongue (called Prela) fair to evade having to parse SQL. Prela itself turned out to be extremely nice, but there is one problem: it's not really a query langauge. It's fair a bunch of Rust types and methods that provision the awareness of a query language. On one hand that's very convinient, since it method all the tooling built for Rust, including type checking, syntax highlighting, go-to-definition, etc., all activity for Prela immediately. On the another hand, all query is compiled by rustc which normally takes equal additional period than executing the query itself... That's fine if the identical query is meant to run again and again, but would be too much overhead for ad hoc queries.

So how do we assistance energetic Prela queries? The right item to do is likely fair scheme a syntax and compose a parser, but again I'm allergic. Also, DB queries are basically small programs embedded within the presenter language, and we already have an outstanding tongue born to be embedded -- Lua! More practically, query languages frequently require constructs akin arithmetic expressions and variables (CTEs) which are of course already accessible in Lua.

The chief idea is beautiful simple: execute relational operators in Lua, but create them come back AST nodes instead, and continue the AST to the Rust backend. This is basically how dataframe libraries implement relational algebra, but it's particularly nice for Prela since its algebraic foundation, Tarski's Algebra of Relations, is a pure algebra, dissimilar relational algebra operators which need to be annotated alongside attribtue names to create 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 connection composition, respectively, and gt, lt, eq are assorted filters.1 The & controller is overloaded to execute the relation product ".and" in Prela, and another nice trick is overloading the site admission customer.mktsegment to implement customer.select(mktsegment). If the final 2 sentences flew complete your head, obtain a peek at the Prela tutorial; but they are really fair operators defined complete (binary) relations. We're also leveraging Lua's local variables to execute CTEs: indeed, the relations l and o are not materialized but define views.

Things get a small tricky whenever we get to aggregations and maps/"generalized projections". In the Rust implementation, we fair pass in closures to be mapped/folded complete the relations, which plant fine because the closures are statically compiled and inlined. However, it'd be hella dilatory if we continue a Lua closure to the Rust flank and do a round trip operating the closure for all row. Instead, we'll formerly again overload operators that appear in closures to activity complete columns, and have the decrease function use 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 final line: in the closure, cost and disc are really columns of values (or rather, AST nodes representing columns), and *, - are overloaded to activity complete columns.

Overall the queries came out extremely nicely, many of them equal easier to read than the first 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 additionally on the archetypal row which is fairly 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 lone collection on l.order. We additionally don't need a particular HAVING clause and can use the .gt display immediately on the aggregated groups. Finally, joins in Prela are implicit, again gratitude to Tarski, which lets us autumn the noisy c_custkey = o_custkey and o_orderkey = l_orderkey.

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

The code is here but I've lone spent a few hours vibing so it's beautiful sloppy. I might clean it up following the holidays...

Other Article Hacker News
↑
Close Right Ads
Close Left Ads