<!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>Hash-ssessing the freshness of SPARQL pipelines</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Damien Graux</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Fabrizio Orlandi</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Declan O'Sullivan</string-name>
          <email>declan.osullivang@tcd.ie</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>ADAPT SFI Centre, Trinity College Dublin</institution>
          ,
          <country country="IE">Ireland</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Inria, Universite Co</institution>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>te d'Azur</institution>
          ,
          <addr-line>CNRS, I3S</addr-line>
          ,
          <country country="FR">France</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>The recent increase of RDF usage has witnessed a rising need of \veri cation" around data obtained from SPARQL endpoints. It is now possible to deploy Semantic Web pipelines and to adapt them to a wide range of needs and use-cases. Practically, these complex ETL pipelines relying on SPARQL endpoints to extract relevant information often have to be relaunched from scratch every once in a while in order to refresh their data. Such a habit adds load on the network and is heavy resource-wise, while sometimes unnecessary if data remains untouched. In this article, we present a useful method to help data consumers (and pipeline designers) identify when data has been updated in a way that impacts the pipeline's result set. This method is based on standard SPARQL 1.1 features and relies on digitally signing parts of query result sets to inform data consumers about their eventual change.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>
        During the past decades, the number of linked open datasets has rapidly
increased1. These datasets are structured following the W3C standard Resource
Description Framework (RDF) [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ] and share knowledge on various domains, from
the generalist ones such as DBpedia [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] or WikiData [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] to the most specialised
ones, e.g. SemanGit [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ]. This abundance of open datasets and SPARQL [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]
endpoints led not only researchers but also businesses to integrate RDF graphs into
their complex data pipelines. In particular, businesses are increasingly
leveraging Semantic Web technologies to structure their own data and create value,
sometimes integrating external Linked Data to enrich their analyses [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ].
      </p>
      <p>Bene ting from the two decades of developments made by the community,
it is now possible to deploy Semantic Web pipelines and to adapt them to a
wide range of needs and use-cases. Recent developments have been, for example,
focused on distributed systems or on connecting Semantic Web data management
systems together with non-RDF centric systems, paving the road to querying
heterogeneous data. As a consequence of this increasing complexity of the
usecases, the pipelines themselves are getting more complicated, and often rely on
several distinct data sources in order to compute their nal results.
1 From 2010 to 2020, the LOD-cloud has grown from 203 to 1 255 datasets,
approximately: https://lod-cloud.net/
Copyright © 2021 for this paper by its authors. Use permitted under Creative
Commons License Attribution 4.0 International (CC BY 4.0).</p>
      <p>Hence, as data available may change, these pipelines (or parts of them) are
frequently re-run in order to get fresher results. However, lots of times they are
re-run unnecessarily as datasets have not been updated in the meantime in ways
that impact the result sets of the pipeline. All these operations are leading to a
waste of computation power and loads on the network.</p>
      <p>
        In this article, mainly dedicated to SPARQL practitioners and data pipeline
designers, we review the possibilities provided by the SPARQL 1.1 standard [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]
to sign query result sets. In particular, we will discuss how these methods can
be used to optimise data pipelines avoiding expensive re-computation of results
when data triples have not been updated.
2
      </p>
    </sec>
    <sec id="sec-2">
      <title>SPARQL 1.1 hashing capabilities</title>
      <p>The SPARQL standard provides a large set of built-in functions, from ones
dedicated to strings to speci c ones about dates. These can be used by query
designers to re ne their result set. In particular, the standard o ers a set of ve
hash functions2: MD5, SHA1, SHA256, SHA384 &amp; SHA512.</p>
      <sec id="sec-2-1">
        <title>General signature of the hash functions:</title>
      </sec>
      <sec id="sec-2-2">
        <title>Example using MD5:</title>
        <p>simple literal hash_function (simple literal arg) H = md5("ab") = md5("ab"^^xsd:string)
simple literal hash_function (xsd:string arg) H = "187ef4436122d1cc2f40dc2b92f0eba0"</p>
        <p>These functions accept either RDF literals or strings as argument and return
the hash as a literal. In addition, a xsd:string or its corresponding literal should
return the same result. In the `MD5' example above, the hash value represents
the result of a simple SPARQL query3.</p>
        <p>Practically, these functions can be used to hash a complete RDF graph
accessible through a SPARQL endpoint. Indeed, one can extract all the triples
available with select * where f?s ?p ?og, and then hash all of them,
aggregated with a group concat function. This could look like so:
SELECT (SHA1(GROUP_CONCAT(?tripleStr ; separator= \n ))) AS ?nTriples
WHERE { ?s ?p ?o</p>
        <p>BIND(CONCAT(STR(?s), " ", STR(?p), " ", STR(?o)) AS ?tripleStr) }
In the previous query, the triples ?s ?p ?o are cast by element to a string (STR),
and then concatenated to form a \triple". The recomposed list of triples is then
grouped into one single string (GROUP CCONCAT) and nally hashed.</p>
        <p>
          Although easy to understand, this \nave" approach has some drawbacks.
First, the result depends on the order of the triples returned by the triplestore:
a workaround can be achieved adding e.g. ORDER BY ?s ?p ?o datatype(?o)
lcase(lang(?o)). Second, this method has a scalability issue, as all the graph is
loaded in-memory before the hash call. We therefore recommend this approach to
sign small RDF graphs, e.g. ontologies or small result sets. Finally, this method
2 https://www.w3.org/TR/sparql11-query/#func-hash
3 select * wheref values ?x f"ab" "ab"^^xsd:stringg bind (md5(?x) as ?H)g
does not address the complex case of blank node identi cation as e.g. f :a p og
and f :b p og do not have the same hashes (see [
          <xref ref-type="bibr" rid="ref3">3</xref>
          ] for algorithmic solutions).
3
        </p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>Tracking result updates of SPARQL queries</title>
      <p>Signing an RDF graph through a SPARQL query is not as reliable as the
traditional and complete method that transforms the entire graph beforehand.
However, it allows users to compare di erent query results for the same query on
the same engine. As we know, on the same endpoint, the same query (without
calls to functions like RAND or NOW) is supposed to return the same result set for
the same dataset. Therefore, we think this SPARQL-based \lightweight" signing
approach could be useful for ETL pipeline designers.</p>
      <p>Indeed, a common challenge for pipeline designers is to know when a refresh
(i.e. a re-run, often from scratch) is needed, following a data update. Often,
there is no way to know a priori that datasets have been updated and, thereby,
pipelines are often run even when nothing has been modi ed. This,
unfortunately, leads to time-consuming and (sometimes) costly processes in terms of
both resources and network bandwidth, as multiple intermediate results involved
by the pipelines are shu ed.</p>
      <p>We suggest to use the aforedescribed approach to check on the endpoint side
if the results of a SPARQL query have changed. A hash of the results could be
computed by the endpoint and be compared with a previously obtained one. In
case of a mismatch, the query (and the rest of the pipeline) could be run again.
Assuming Q is the considered SPARQL select query, we propose the following
steps to generate the query which computes the hash of the results of Q:
1. Extract and sort the list of distinguished variables V (if a * is given, the
considered variables are the ones involved in the where);
2. Wrap Q in a select * query ordered by V;
3. Embed the obtained query in a select query computing the hash of the
grouped concatenation of the cast (to string) distinguished variables.</p>
      <p>To give an example, if we consider the query which extracts from DBpedia
the current members of English-named Punk rock groups, Q=
SELECT ?members ?bandName WHERE {
?band dbo:genre dbr:Punk_rock . ?band dbp:currentMembers ?members .
?band foaf:name ?bandName FILTER(langMatches(lang(?bandName), "en")) }
Its sorted list of distinguished variables would be ?bandName ?members. And to
obtain a (MD5-)hash of the results of Q, we should run:
SELECT MD5(GROUP_CONCAT(CONCAT(STR(?bandName),STR(?members)); separator= \n ))
as ?H WHERE {
SELECT * WHERE { # Collecting all the ordered results
SELECT ?members ?bandName WHERE { # The original query
?band dbo:genre dbr:Punk_rock . ?band dbp:currentMembers ?members.
?band foaf:name ?bandName FILTER(langMatches(lang(?bandName), "en")) }
} ORDER BY ?bandName ?members } # Ordering by distinguished variables</p>
      <p>The three steps to generate the query4 that obtains the hash are easy to
automate and allow users to know when to relaunch their pipelines. All this
while making as much computations as possible on the endpoint side in charge
of computing the hash.</p>
      <p>Performance: For comparison, we reviewed three queries (Q1, Q2, Q3) on a
YAGO45 KG loaded on Stardog, running on a 4 cores and 32GB memory VM.
Their result sets respectively contain 2 916, 50 000 and 100 000 results
corresponding to 186KB, 5.3MB and 10.7MB and were on average computed in 331ms,
675ms and 1315ms. Their MD5 hashed versions all returned one hash string of
46 bytes and were computed in 364ms, 1353ms and 2492ms respectively. Thus,
as expected, performing the hashes does not imply a large temporal overhead
and greatly reduces the network tra c.</p>
      <p>Web-Interface: To help SPARQL practitioners and pipeline designers with our
method, we also developed a Web interface (see Figure 1 for a screenshot) and
serve it online . The page allows users to paste their SPARQL query (Q) in
order to obtain a new query (Q hash) which should be run to obtain the hash
of the results of Q. The interface also provides a way to select the hash function
among the ones from the standard. Technically the query generation is done
through a JavaScript routine whose parser relies on SPARQL.js6.
4 The queries can be tested directly on DBpedia: the query Q and its Q hash .</p>
      <p>As of July 5th 2021, Q hash returned ?H = "967d2c8c0a82038d8478d476fa41e14f"
and on September 6th it returned "a41d8b97289ea1ef1af2a2ec54cff96c".
5 YAGO4 has 209 million triples: https://yago-knowledge.org/downloads/yago-4
6 https://github.com/RubenVerborgh/SPARQL.js</p>
    </sec>
    <sec id="sec-4">
      <title>Conclusions</title>
      <p>This paper describes how to improve existing Semantic Web data pipelines with
a SPARQL-based method that helps in identifying when query results have
changed. It allows to re-run pipelines only when interesting parts of the original
datasets have been updated. By using SPARQL to compute the signature of the
query results, it avoids large result sets to be sent over the network while letting
the triplestore optimise as much as possible all the computations. We hope this
will inspire developers to use the hash functions provided by the standard, and
serve our method at: https://dgraux.github.io/SPARQL-hash/ where our query
converter can be used directly by developers to generate queries computing the
hash of their result sets.</p>
      <p>Acknowledgments. This research was conducted with the nancial support of
the European Unions H2020 programme under the EDGE Marie
SklodowskaCurie grant agreement No. 713567 at the ADAPT SFI Research Centre at Trinity
College Dublin, which is funded by Science Foundation Ireland and the European
Regional Development Fund (ERDF) Grant #13/RC/2106 P2.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Auer</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bizer</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kobilarov</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Lehmann</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Cyganiak</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ives</surname>
            ,
            <given-names>Z.</given-names>
          </string-name>
          :
          <article-title>DBpedia: A nucleus for a web of open data</article-title>
          .
          <source>In: The semantic web</source>
          . Springer (
          <year>2007</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Harris</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Seaborne</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Prud</surname>
          </string-name>
          'hommeaux, E.:
          <article-title>Sparql 1.1 query language</article-title>
          .
          <source>W3C recommendation</source>
          <volume>21</volume>
          (
          <issue>10</issue>
          ),
          <volume>778</volume>
          (
          <year>2013</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Hogan</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          :
          <article-title>Canonical forms for isomorphic and equivalent RDF graphs: algorithms for leaning and labelling blank nodes</article-title>
          .
          <source>ACM Transactions on the Web</source>
          (
          <year>2017</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Junior</surname>
            ,
            <given-names>A.C.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Orlandi</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Graux</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Hossari</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <given-names>O</given-names>
            <surname>'Sullivan</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            ,
            <surname>Hartz</surname>
          </string-name>
          ,
          <string-name>
            <given-names>C.</given-names>
            ,
            <surname>Dirschl</surname>
          </string-name>
          ,
          <string-name>
            <surname>C.</surname>
          </string-name>
          :
          <article-title>Knowledge graph-based legal search over german court cases</article-title>
          . In: The Semantic Web:
          <article-title>ESWC 2020 Satellite Events</article-title>
          .
          <source>LNCS</source>
          , vol.
          <volume>12124</volume>
          , pp.
          <volume>293</volume>
          {
          <fpage>297</fpage>
          . Springer (
          <year>2020</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Kubitza</surname>
            ,
            <given-names>D.O.</given-names>
          </string-name>
          , Bockmann,
          <string-name>
            <given-names>M.</given-names>
            ,
            <surname>Graux</surname>
          </string-name>
          ,
          <string-name>
            <surname>D.:</surname>
          </string-name>
          <article-title>SemanGit: A linked dataset from git</article-title>
          .
          <source>In: International Semantic Web Conference</source>
          . pp.
          <volume>215</volume>
          {
          <fpage>228</fpage>
          . Springer (
          <year>2019</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Manola</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          , et al.:
          <article-title>RDF primer</article-title>
          .
          <source>W3C recommendation</source>
          (
          <year>2004</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Vrandecic</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          , Krotzsch, M.:
          <article-title>Wikidata: a free collaborative knowledgebase</article-title>
          .
          <source>Communications of the ACM</source>
          <volume>57</volume>
          (
          <issue>10</issue>
          ),
          <volume>78</volume>
          {
          <fpage>85</fpage>
          (
          <year>2014</year>
          )
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>