Quelg: Language-Integrated Query with Nested Data Structures and Grouping


Overview

The base language Quel is essentially the same as Cooper’s source language without effects (which is 'nearly the same' as Nested Relational Calculus), and Cheney et al.’s T-LINQ without quotation and code generation. Quelg is an extension of Quel, a language that extends aggregate functions and has grouping. The type-safe and composable embedded query language is transformed into the form from which the efficient SQL can be produced. The language and the optimizations can be safely extended to account for many variations in the SQL back-ends, as well for domain-specific knowledge.

See the paper "Language-Integrated Query with Nested Data Structures and Grouping"(FLOPS 2020) for further information.


Example

For example, we consider the products table, which has columns of product ID (pid), name, and price (price), and the orders table, which has columns of order ID (oid), product ID (pid), and quantity (qty). From these tables, we consider a query for sales by grouping by oid. We call this query \(Q_1\), and in Quelg we can write:

\( Q_1 = \mathcal{G}_{({\rm oid}, \alpha)}({\rm for}~(p \leftarrow {\rm table}(``{\rm products}")) \\ ~~~~~~~~~~~~~~~~~~~~~~~{\rm for}~(o \leftarrow {\rm table}(``{\rm orders}")) \\ ~~~~~~~~~~~~~~~~~~~~~~~{\rm where}~(p.{\rm pid} = o.{\rm pid}) \\ ~~~~~~~~~~~~~~~~~~~~~~~{\rm yield}~\{{\rm oid} = o.{\rm oid}, {\rm sales} = p.{\rm price} * o.{\rm qty}\}) \\ ~~~~~~~~{\rm Where}~~\alpha = \{({\rm sales}, {\rm SUM}, {\rm sales\_sum})\} \)
A query written in Quelg like the above query is represented in our implementation code in OCaml as:

  module Q1(S:SYM_SCHEMA) = struct
    open S
    let table_orders   = table ("orders", orders ())
    let table_products = table ("products", products ())

    let key = ooid
    let alpha = [(osale, (string "sum"), (string "sale_sum"))]
    let query = foreach (fun () -> table_products) @@ fun p ->
                foreach (fun () -> table_orders) @@ fun o ->
                where ((pid p) =% (opid o)) @@ fun () ->
                yield @@ osales (oid o) ((price p) *% (qty o))

    let q1 = group key alpha query @@ fun v key -> q1_res key (sale_sum v)

    let observe = observe
  end 

We evaluated nine queries including \(Q_1\) in the paper. You can see our implementation and examples of nine query here.


Credits

The key developers of Quelg are: