As part of work on the Attean Semantic Web toolkit, I found some time to work through limit-by-resource, an oft-requested SPARQL feature and one that my friend Kjetil lobbied for during the SPARQL 1.1 design phase. As I recall, the biggest obstacle to pursuing limit-by-resource in SPARQL 1.1 was that nobody had a clear idea of how to fit it nicely into the existing SPARQL syntax and semantics. With hindsight, and some time spent working on a prototype, I now suspect that this was because we first needed to nail down the design of aggregates and let aggregation become a first-class feature of the language.
Now, with a standardized syntax and semantics for aggregation in SPARQL, limit-by-resource seems like just a small enhancement to the existing language and implementations by the addition of window functions. I implemented a RANK operator in Attean, used in conjunction with the already-existing GROUP BY. RANK works on groups just like aggregates, but instead of producing a single row for each group, the rows of the group are sorted, and given an integer rank which is bound to a new variable. The groups are then “un-grouped,” yielding a single result set. Limit-by-resource, then, is a specific use-case for ranking, where groups are established by the resource in question, ranking is either arbitrary or user-defined, and a filter is added to only keep rows with a rank less than a given threshold.
I think the algebraic form of these operations should be relatively intuitive and straightforward. New Window
and Ungroup
algebra expressions are introduced akin to Aggregation
and AggregateJoin
, respectively. Window(G, var, WindowFunc, args, order comparators)
operates over a set of grouped results (either the output of Group
or another Window
), and Ungroup(G)
flattens out a set of grouped results into a multiset.
If we wanted to use limit-by-resource to select the two eldest students per school, we might end up with something like this:
Project(
Filter(
?rank <= 2,
Ungroup(
Window(
Group((?school), BGP(?p :name ?name . ?p :school ?school . ?p :age ?age .)),
?rank,
Rank,
(),
(DESC(?age)),
)
)
),
{?age, ?name, ?school}
)
Students with their ages and schools are matched with a BGP. Grouping is applied based on the school. Rank
with ordering by age is applied so that, for example, the result for the eldest student in each school is given ?rank=1, the second eldest ?rank=2, and so on. Finally, we apply a filter so that we keep only results where ?rank is 1 or 2.
The syntax I prototyped in Attean allows a single window function application applied after a GROUP BY
clause:
PREFIX : <http://example.org/>
SELECT ?age ?name ?school WHERE {
?p :name ?name ;
:school ?school ;
:age ?age .
}
GROUP BY ?school
RANK(DESC(?age)) AS ?rank
HAVING (?rank <= 2)
However, a more complete solution might align more closely with existing SQL window function syntaxes, allowing multiple functions to be used at the same time (appearing syntactically in the same place as aggregation functions).
PREFIX : <http://example.org/>
SELECT ?age ?name ?school WHERE {
?p :name ?name ;
:school ?school ;
:age ?age .
}
GROUP BY ?school
HAVING (RANK(ORDER BY DESC(?age)) <= 2)
or:
PREFIX : <http://example.org/>
SELECT ?age ?name ?school WHERE {
{
SELECT ?age ?name ?school (RANK(GROUP BY ?school ORDER BY DESC(?age)) AS ?rank) WHERE {
?p :name ?name ;
:school ?school ;
:age ?age .
}
}
FILTER(?rank <= 2)
}