<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>A Proposal for Nested Results in SPARQL?</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Sebastien Ferre??</string-name>
          <email>ferre@irisa.fr</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Univ Rennes, CNRS, IRISA Campus de Beaulieu</institution>
          ,
          <addr-line>35042 Rennes</addr-line>
          ,
          <country country="FR">France</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>Tables are a common form of query results, notably in SPARQL. However, due to the at structure of tables, all structure from the RDF graph is lost, and this can lead to duplications in the table contents, and di culties to interpret the results. We propose an extension of SPARQL 1.1 aggregations to get nested results, i.e. tables where cells may contain embedded tables instead of RDF terms, and so recursively.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Motivation</title>
      <p>
        The SPARQL query language [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] o ers a powerful way to extract and compute
information from RDF datasets. For SELECT queries, the results are presented in a
table. Each column corresponds to a projected variable in the SELECT clause, and each
row corresponds to a solution, mapping those variables to RDF terms. Such tables are
universally understood, and can be read in two directions, by row or by column. They
make a good use of the screen space, compared for instance to graph visualizations, and
can therefore display a lot of information at once. They can be dynamically ltered
and ordered according to each column. Despite those advantages, tables in general
and SPARQL results in particular have drawbacks due to the fact that they are in
rst normal form (1NF), a notion from relational databases that states that table cells
only contain atomic values, here RDF terms. Although it sounds like a reasonable
constraint, it has negative consequences on the readability of query results as shown
in the following example.
      </p>
      <p>Table 1 shows an excerpt of the results of the following SPARQL query on DBpedia,
which retrieves lms directed by Danny Boyle, along with their music composers and
actors, and also the birth year of actors.
It can be observed that the table contains a lot of redundancies. For instance, the birth
year of an actor is repeated for each music composer. The number of rows for a lm is
? This research is supported by ANR project PEGASE (ANR-16-CE23-0011-08).
?? Copyright c 2020 for this paper by its authors. User permitted under Creative</p>
      <p>Commons License Attribution 4.0 International (CC BY 4.0).
equal to its number of music composers times its number of actors. When ordering by
birth year, one needs to order rst by lm so as to keep rows grouped by lm.</p>
      <p>The key contribution of this paper is to allow table cells to contain smaller tables
instead of RDF terms. Those nested tables are obtained by allowing variables to map
to sets of solution mappings, in addition to RDF terms. Tables can be deeply nested,
with tables containing tables that in turn contain tables, and so on. In practice, RDF
terms and nested tables are not mixed randomly inside a table. For a given column,
either all cells contain an RDF term or all cells contain a nested table. Moreover, all
nested tables in a column are de ned on the same variables. This means that our nested
tables follow a regular schema, although more complex than that of at tables.</p>
      <p>Table 2 is the nested version of Table 1. The main table has a single row per lm, and
two of its columns, \music composers" and \actors", contain nested tables. The former
column contains one-column nested tables that contain the list of music composers of
each lm. The latter column contains two-columns nested tables that contain the list of
actors of each lm, along with their birth year. It can be observed that the nested table
does not contain redundancies anymore, and that the dependencies between columns
is made explicit. Music composers and actors are dependent on the lm, but not on
each other. The birth year is dependent on the actor.
2</p>
    </sec>
    <sec id="sec-2">
      <title>Related Work</title>
      <p>
        Nested tables were proposed and formalized in relational databases, where nesting is
not only used for query results but also for data tables [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]. The motivation was to relax
the rst normal form (1NF). The authors extend relational algebra with two operators,
nest and unnest, that operate on subsets of columns. In this work, as we start from
RDF graphs instead of tables, we only need a mechanism to nest the table of results,
there is no need for unnesting. We also need to adapt the nest operation to SPARQL
algebra and grammar.
      </p>
      <p>
        A lot of work have proposed various forms of visualization of SPARQL results (e.g.,
maps, charts) in order to help their understanding [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. They are a valuable complement
to the tabular view but that they cannot fully replace it in the general case. A
JSONbased query language has been proposed to facilitate the exposition of SPARQL results
in APIs [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]. In particular it can generate nested tables similar to ours. However, it only
covers a fragment of SPARQL graph patterns, and the grouping criteria is limited to
a single variable per table. Some extensions of SPARQL have also been proposed. For
instance, a new CLUSTER BY clause was proposed to group the rows of the table of
results into a hierarchical clustering [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ]. In contrast, nested tables can be seen as a
form of 2D hierarchical clustering because they involve grouping subsets of columns
and subsets of rows at the same time.
3
      </p>
    </sec>
    <sec id="sec-3">
      <title>A New Aggregation Construct</title>
      <p>
        Our proposal is to extend the algebra and grammar of SPARQL 1.1 with a new
aggregation construct [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] that computes nested tables (i.e. sequences of solution mappings)
instead of RDF terms.
      </p>
      <p>In the above example, Table 1 can be seen as the global multiset of solutions, and
Table 2 as the expected result after applying aggregations that compute nested tables
instead of RDF terms. After grouping by lm, the nested tables in column \music
composers" are obtained by projecting each solution group on variable \music composer",
and by removing duplicates. Similarly for column \actors" by projecting on \actor"
and \birth year", by removing duplicates, and by ordering rows by birth year. To sum
up, the new aggregations need at least projecting on a subset of variables, removing
duplicates, and ordering results. Those computations correspond to the category of
solution modi ers, which are formalized as transformations between sequences of solution
modi ers, and hence as table-to-table transformations. In addition to solution
modiers, we allow groupings (clause GROUP BY) in the de nition of table aggregations, and
hence aggregations in the de nition of table aggregations. This makes the de nition of
aggregations recursive, which enables deeply nested tables.</p>
      <p>The main impact of the extension is that a variable may now be bound to a nested
table in addition to RDF terms. For the sake of simplicity, we consider that those
variables can only be used as projection variables, and produce an error when used in
other contexts (e.g., in an expression).</p>
      <p>
        We now extend the grammar of SPARQL 1.1 [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] so as to give a concrete syntax to
the extended algebra presented above. It simply consists in extending the Aggregate
rule with one new production.
      </p>
      <p>Aggregate ::= : : : j 'f' SelectClause SolutionModi er 'g'
The ellipsis stands for existing constructs like COUNT(DISTINCT ?x). Symbol
SelectClause covers projection (SELECT) and removal of duplicates (DISTINCT,
REDUCED). Symbol SolutionModi er covers ordering (ORDER BY), top-k results (LIMIT)
and o set (OFFSET), grouping (GROUP BY), and ltering as a solution modi er (HAVING).
This production is therefore enough to cover all needed features. We add braces to
delimit the aggregation construct, and also by analogy with subqueries. Indeed, our
table aggregations are syntactically and semantically equivalent to subqueries where
the graph pattern (clause WHERE) is implicit.</p>
      <p>In the example on lms, the query in the extended SPARQL that returns the nested
table (Table 2) is the following.</p>
      <sec id="sec-3-1">
        <title>SELECT ?f ({SELECT DISTINCT ?mc} AS ?mcs)</title>
        <p>({SELECT DISTINCT ?a ?y ORDER BY ?y} AS ?as)
WHERE { ?f a dbo:Film ; dbo:director dbr:Danny_Boyle ;
dbo:musicComposer ?mc ;
dbo:starring ?a .</p>
        <p>?a dbo:birthYear ?y . }
GROUP BY ?f</p>
        <p>Clause GROUP BY ?f de nes the groups of solutions over which the two table
aggregations are evaluated. Variable ?mcs holds the list of distinct music composers for
each lm, a one-column nested table. Variable ?as holds the list of distinct actors and
their birth year, in increasing order of birth year, a two-column nested table.</p>
        <p>Note that nothing forbids to combine term aggregations with table aggregations.
For example, in the above query, one could add the term aggregation (COUNT(DISTINCT
?a) AS ?na) in order to add a column giving the number of actors, for each lm, in
addition to their list. If those lists are too long, they can be bounded in size by adding
a LIMIT clause in the table aggregations.</p>
        <p>To illustrate deeply nested tables and top-k results, we extend the above query to
get the lists of spouses of each actor as tables nested into the actor tables. We also add
a LIMIT to get only the three top actors per lm.</p>
      </sec>
      <sec id="sec-3-2">
        <title>SELECT ?f ({SELECT DISTINCT ?mc} AS ?mcs) ({SELECT DISTINCT ?a ?y ({SELECT DISTINCT ?sp} AS ?sps) ORDER BY ?y LIMIT 3} AS ?as)</title>
        <p>WHERE { ?f a dbo:Film ; dbo:director dbr:Danny_Boyle ;
dbo:musicComposer ?mc ;
dbo:starring ?a .</p>
        <p>?a dbo:birthYear ?y ; dbo:spouse ?sp }
GROUP BY ?f</p>
        <p>It can be observed in the above queries that the nesting schema is entirely expressed
in the main SELECT clause (table aggregations can also be used in subqueries). This
suggests that nested results can be obtained by post-processing the at results, without
actually extending SPARQL. However, it would require to retrieve more answers than
the number of desired answers, because of the redundancies induced by at results.
In the example, 11 answers in the at results are used to generate 2 answers in the
nested results. The relation between the two numbers is hard to predict, and can be
of combinatorial nature. Integrating nested tables into SPARQL enables to control
the number of returned results, and creates opportunities for optimization in query
evaluation by trying to avoid redundancies altogether.</p>
        <p>Another advantage of the SPARQL extension is to extend SPARQL's expressivity
with a new class of aggregations that would be otherwise very di cult to express. The
most interesting one is the combination of grouping and top-k results, like \the three
youngest actors of each lm" or \the last two mayors of the three most populated cities
of each country".
4</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>Conclusion</title>
      <p>
        We have proposed an extension of SPARQL 1.1 aggregations that enables nested tables
as query results, i.e. tables that can contain tables in their cells, and so recursively.
Nested tables improve the readability of results by avoiding redundancies in their
contents, and by exhibiting the dependencies and independencies between their columns.
The proposed extension is fully backward compatible, and should be relatively easy to
implement in existing query engines. Nested results have been integrated into
Sparklis [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ], a SPARQL query-builder, as a new view on results. This has to be done by
post-processing of at results until implementations of the proposed extension are
available.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Bikakis</surname>
            ,
            <given-names>N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Sellis</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          :
          <article-title>Exploration and visualization in the web of big linked data: A survey of the state of the art</article-title>
          .
          <source>In: Int. Work. Linked Web Data Management (LWDM)</source>
          (
          <year>2016</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Ferre</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          : Sparklis:
          <article-title>An expressive query builder for SPARQL endpoints with guidance in natural language</article-title>
          .
          <source>Semantic Web: Interoperability, Usability, Applicability</source>
          <volume>8</volume>
          (
          <issue>3</issue>
          ),
          <volume>405</volume>
          {
          <fpage>418</fpage>
          (
          <year>2017</year>
          ), http://www.irisa.fr/LIS/ferre/sparklis/
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Hoe</surname>
            <given-names>er</given-names>
          </string-name>
          , P.,
          <string-name>
            <surname>Granitzer</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Sabol</surname>
            ,
            <given-names>V.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Lindstaedt</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          :
          <article-title>Linked data query wizard: A tabular interface for the semantic web</article-title>
          .
          <source>In: The Semantic Web: ESWC 2013 Satellite Events</source>
          , pp.
          <volume>173</volume>
          {
          <fpage>177</fpage>
          . Springer (
          <year>2013</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Kaminski</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kostylev</surname>
            ,
            <given-names>E.V.</given-names>
          </string-name>
          ,
          <string-name>
            <given-names>Cuenca</given-names>
            <surname>Grau</surname>
          </string-name>
          ,
          <string-name>
            <surname>B.</surname>
          </string-name>
          :
          <article-title>Semantics and expressive power of subqueries and aggregates in SPARQL 1.1</article-title>
          . In: Int. Conf. World Wide Web. pp.
          <volume>227</volume>
          {
          <fpage>238</fpage>
          .
          <string-name>
            <surname>ACM</surname>
          </string-name>
          (
          <year>2016</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Lawrynowicz</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          :
          <article-title>Query results clustering by extending SPARQL with CLUSTER BY</article-title>
          .
          <source>In: OTM Confederated Int. Conf. on the Move to Meaningful Internet Systems</source>
          . pp.
          <volume>826</volume>
          {
          <fpage>835</fpage>
          . Springer (
          <year>2009</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Lisena</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Meron~</surname>
            o-Pen~uela,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kuhn</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Troncy</surname>
          </string-name>
          , R.:
          <article-title>Easy web API development with SPARQL transformer</article-title>
          .
          <source>In: Int. Semantic Web Conf</source>
          . pp.
          <volume>454</volume>
          {
          <fpage>470</fpage>
          . Springer (
          <year>2019</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Roth</surname>
            ,
            <given-names>M.A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Korth</surname>
            ,
            <given-names>H.F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Silberschatz</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          :
          <article-title>Extended algebra and calculus for nested relational databases</article-title>
          .
          <source>ACM Transactions on Database Systems (TODS) 13(4)</source>
          ,
          <volume>389</volume>
          {
          <fpage>417</fpage>
          (
          <year>1988</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          <source>8. SPARQL 1</source>
          .
          <article-title>1 query language (</article-title>
          <year>2012</year>
          ), http://www.w3.org/TR/sparql11-query/, w3C Recommendation
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>