<!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>Linear Recursion in G-CORE</article-title>
      </title-group>
      <contrib-group>
        <aff id="aff0">
          <label>0</label>
          <institution>Department of Computer Science, Universidad de Chile and IMFD</institution>
        </aff>
      </contrib-group>
      <abstract>
        <p>G-CORE is a query language with two key characteristics: It is closed under graphs and incoporates paths as rst-class citizens. Currently G-CORE does not have recursion. In this paper we propose this extension and show how to code classical polynomial graph algorithms with it. G-CORE is a graph database query language designed by a working group belonging to the Linked Data Benchmark Council [1]. One of its most novel features is the ability to express paths as rst-class citizens. However, path queries are still not enough to express a wide variety of "natural" queries, particularly classical graph algorithms like topological sort, BFS, Eulerian circuit, etc. We propose to extend G-CORE with (linear) recursive functionalities by introducing a syntax and a semantics that essentially follow the ideas of similar constructs in SQL and SPARQL1. What is more novel is that we take the core basic polynomial algorithms in graph theory and show, rst, that they cannot be coded in standard G-CORE; and second, how to write queries that code them in G-CORE extended with linear recursion. Finally, we tested the code in an evaluator for G-CORE that is being developed by Roberto Garc a at the University of Talca. Basic Notions. A Path Property Graph (PPG) is an edge and node labeled graph where edges and nodes additionally have property-value pairs. In addition, a PPG may also have a collection of paths, where a path is a concatenation of existing, adjacent, edges in the graph. A basic G-CORE query is an expression of the form: CONSTRUCT f MATCH</p>
      </abstract>
      <kwd-group>
        <kwd>G-CORE</kwd>
        <kwd>Recursion</kwd>
        <kwd>Graph Query Language</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>
        ON G WHERE
(1)
where f is a full construct pattern, is a full graph pattern and a Boolean
condition [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. The core of a G-CORE query consists in the complete graph pattern
that de nes the content of the MATCH clause. The MATCH clause is evaluated
against the PPG G and returns a set of bindings that are ltered with the WHERE
clause, to nally build a new PPG H with the CONSTRUCT clause.
1 Ongoing research
Related Work. Adding recursion to database query languages has been
extensively studied both from a theoretical [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] as well as from a practical point
of view (e.g. SQL, SPARQL [
        <xref ref-type="bibr" rid="ref4 ref5">4, 5</xref>
        ]). In SQL it was included in the SQL-99
standard and was developed via common table expressions (CTE'S) that have a base
SELECT statement and a recursive SELECT statement, that allows to express
graph queries like DFS, BFS, topological sort, connected components, etc. Since
queries must be linear and many interesting queries are not [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ], optimizations
have been proposed in [
        <xref ref-type="bibr" rid="ref7 ref8">7, 8</xref>
        ] (r-sql proposal) that allow only linear recursion and
not the explicit negation that is a limitation when implementing recursion [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ].
      </p>
      <p>
        The graph algorithms BFS and DFS were implemented in [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ] with the
recursive SQL operator, where only trees were allowed as input since otherwise the
query would loop in nitely. The topological order was studied in [
        <xref ref-type="bibr" rid="ref10">10</xref>
        ], making a
BFS starting from the node whose outdegree is zero. In the case of the connected
components it is shown in [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ] how to obtain them adding recursion.
      </p>
      <p>
        For SPARQL, Reutter et al [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] proposed a recursion operator based on SQL
and make a comparison with property paths. In their study they formalize the
syntax of the recursive operator and develop algorithms for evaluating it in
practical scenarios. Also, a comparative study of the expressiveness of property
paths and the recursive SQL operator was made in [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ].
2
      </p>
      <p>
        Adding a Recursive Operator to G-CORE
Among the main issues when adding recursion to query languages are the
complexity of the evaluation, the expressiveness and the e orts to keep the
declarative character of queries. From a theoretical point of view, recursive operators
are based on the theory of least xed points [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ], that is, the increasing
accumulation of results until eventually the evaluation does not add anything else
and stops. Our proposal follows these ideas and is based both on the recursive
SPARQL operator de ned by Reutter et al [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] and the recursive SQL operator
[
        <xref ref-type="bibr" rid="ref4">4</xref>
        ].
      </p>
      <p>Proposed Syntax of linear recursive queries.</p>
      <p>WITH RECURSIVE t AS f qbase UNION qrec g qout;
(2)
where t is a temporary PPG, qbase is a usual G-CORE query, qrec is a positive
GCORE query that can use the temporary graph t and qout a recursive G-CORE
query itself.</p>
      <p>Semantics of recursive queries. First, recall that the queries qbase and qrec are
usual G-CORE queries, therefore, are of the form (1). Second, recall that the
evaluation of a usual query of the form (1) against a PPG G outputs a binding
table M (G), and from it the CONSTRUCT clause returns the PPG consisting of
the union of f (`) for each ` 2 M (G). In what follows we will denote by Mb
the binding table and by Cb(`) the PPG returned by the CONSTRUCT clause of
over the row ` of Mb of the base query qbase. Similarly we denote Cr and Mr
respectively for the query qrec.
Proposed Semantics of linear recursive queries. Let q be a recursive G-CORE
query (like (2)) and G be a PPG. The answer ans(q; G) of the query corresponds
to the least xed point of the sequence given by:</p>
      <p>G0 = G;</p>
      <p>G 1 = ;;
Gi+1 = Gi [ fans(qrec; G + (Gi</p>
      <p>
        Gi 1))g;
where: (1) the di erence (Gi Gi 1) is similar to the G-CORE di erence operator
de ned in [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ], except that the di erence of the sets of nodes is rede ned as
(NinNi 1) [ fa 2 Ni 1 : a is adjacent in Gi to a node in NinNi 1g; and (2)
the semantics of ans(q; G + H) means the match of q can use G or H or both
(note that ans(q; G [ H) would not the same).
      </p>
      <p>The answer ans(q; G) of the recursive G-CORE query q over G is given by
the following procedure:
Algorithm 1 Computing the answer of a recursive query in G-CORE
Data: PPG G, Queue of PPG'S Q = ;, PPG t = ;
for each row l of the binding table Mb(G) do</p>
      <p>Insert(Cb(l); Q)
while Q 6= ; do</p>
      <p>Set r Extract(Q)
t t [ r
for each row l of the binding table Mr(G + r) do</p>
      <p>Insert(Cr(l); Q)
return ans(qout; t + G)</p>
      <p>The idea is simple: rst with the base query we construct the base case. Then
with the recursive query we recursively construct a temporary graph which codes
the solution that the output query will use.</p>
      <p>Thus, rst we begin with the queue Q empty and the temporary graph t
empty. Then we evaluate the matching clause of qbase and for each row of the
produced binding table Mb(G), insert Cb(l) into Q. Then we enter the while loop.
As long as Q 6= ;, we extract the top element of Q and update the temporary
graph with it. Then for each row ` of the binding table Mr(G + r) obtained from
the query qrec against G + r (recall that this means that it could use the original
graph G as well as the recently obtained graph r), we insert into the queue Q
the new results obtained by the recursive construct Cr(l). When we exhaust the
queue Q the temporary graph t is nally ready to be used, and the query qout is
evaluated against t + G. If qout is a standard G-CORE query the process outputs
the result. If qout is a recursive G-CORE query, we proceed as before again.</p>
      <p>Notice that the xed point exists whenever the query is monotone. On the
form of query de nition in (2) and tis semantics restricts queries to be linear
[3{5, 11], that is, you can consult only the new elements added to t.</p>
    </sec>
    <sec id="sec-2">
      <title>An Example: Topological Sort</title>
      <p>In graph theory there are problems that have been widely studied and can be
solved recursively in polynomial time. Due to space restriction, we will show as
an example how the case of topological sort can be coded in recursive G-CORE.</p>
      <p>The classic algorithm to obtain the topological sort given an acyclic directed
graph is Kahn algorithm (1962). It is not di cult to see that topological sort
cannot be expressed in G-CORE. The next query illustrates with the graph of
Fig. 1 how to code topological sort in G-CORE:</p>
      <sec id="sec-2-1">
        <title>WITH RECURSIVE t AS (</title>
        <p>CONSTRUCT (n) SET n.depth :=0,</p>
      </sec>
      <sec id="sec-2-2">
        <title>MATCH (n) ON G,</title>
      </sec>
      <sec id="sec-2-3">
        <title>WHERE NOT EXISTS (CONSTRUCT (y),</title>
      </sec>
      <sec id="sec-2-4">
        <title>MATCH (n)-[:BossOf]-&gt;(y) ON G)</title>
      </sec>
      <sec id="sec-2-5">
        <title>UNION{</title>
        <p>CONSTRUCT (x) SET x.depth:=n.depth+1,</p>
      </sec>
      <sec id="sec-2-6">
        <title>MATCH (x) ON G, (n) ON t</title>
      </sec>
      <sec id="sec-2-7">
        <title>WHERE EXISTS (CONSTRUCT (x),</title>
      </sec>
      <sec id="sec-2-8">
        <title>MATCH (x)-[:BossOf]-&gt;(n) ON G)}</title>
      </sec>
      <sec id="sec-2-9">
        <title>SELECT z.id,</title>
      </sec>
      <sec id="sec-2-10">
        <title>MATCH (z) ON t,</title>
      </sec>
      <sec id="sec-2-11">
        <title>ORDER BY MAX(z.depth) DESC</title>
        <p>The above query rst looks for those nodes which have no outgoing edges (base
case) and are assigned with depth equal to 0 as a property. Then the recursive
case comes and the nodes which have out edges to the nodes of the base case are
added with property depth equal to 1 and so on until all the nodes in G have
been added. Finally, we order by depth and we take the maximum value of the
property depth.</p>
      </sec>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>1. LBDC. http://ldbcouncil.org/.</mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <given-names>Renzo</given-names>
            <surname>Angles</surname>
          </string-name>
          , Marcelo Arenas, Pablo Barcelo,
          <string-name>
            <given-names>Peter</given-names>
            <surname>Boncz</surname>
          </string-name>
          , George Fletcher, Claudio Gutierrez, Tobias Lindaaker, Marcus Paradies, Stefan Plantikow, Juan Sequeda, Oskar Van, Alex
          <string-name>
            <surname>Averbuch</surname>
            , Hassan Chaa, Irini Fundulaki, Alastair Green, Josep Lluis, Larriba Pey, Jan Michels, Raquel Pau, Arnau Prat, Tomer Sagi, and
            <given-names>Yinglong</given-names>
          </string-name>
          <string-name>
            <surname>Xia</surname>
          </string-name>
          .
          <article-title>G-CORE A Core for Future Graph query Languages Designed by the LDBC Graph query Language Task Force *</article-title>
          .
          <source>In: SIGMOD</source>
          ,
          <year>2017</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3. Richard Hull Serge Abiteboul and
          <string-name>
            <given-names>Victor</given-names>
            <surname>Vianu</surname>
          </string-name>
          .
          <source>Foundations of Databases. Addison-Wesley</source>
          ,
          <year>1995</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <given-names>Jim</given-names>
            <surname>Melton</surname>
          </string-name>
          and
          <string-name>
            <surname>Alan R Simon. SQL: 1999-Understanding Relational Language Components</surname>
          </string-name>
          . Morgan Kaufmann,
          <year>2001</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Juan L. Reutter</surname>
            , Adrian Soto, and
            <given-names>Domagoj</given-names>
          </string-name>
          <string-name>
            <surname>Vrgoc</surname>
          </string-name>
          . Recursion in SPARQL. pages
          <volume>19</volume>
          {
          <fpage>35</fpage>
          , Berlin, Heidelberg,
          <year>2015</year>
          . Springer-Verlag.
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <given-names>N</given-names>
            <surname>Yakovets</surname>
          </string-name>
          ,
          <string-name>
            <given-names>P</given-names>
            <surname>Godfrey</surname>
          </string-name>
          , and
          <string-name>
            <given-names>J</given-names>
            <surname>Gryz</surname>
          </string-name>
          .
          <article-title>Evaluation of SPARQL property paths via recursive SQL</article-title>
          .
          <source>CEUR Workshop Proceedings</source>
          ,
          <volume>1087</volume>
          , 01
          <year>2013</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <given-names>Gabriel</given-names>
            <surname>Aranda</surname>
          </string-name>
          , Susana Nieva, Fernando Saenz-Perez, and
          <string-name>
            <surname>Jaime</surname>
          </string-name>
          Sanchez-Hernandez.
          <article-title>Formalizing a broader recursion coverage in SQL</article-title>
          . pages
          <volume>93</volume>
          {
          <fpage>108</fpage>
          , Berlin, Heidelberg,
          <year>2013</year>
          . Springer Berlin Heidelberg.
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <given-names>Carlos</given-names>
            <surname>Ordonez</surname>
          </string-name>
          .
          <article-title>Optimization of linear recursive queries in SQL</article-title>
          .
          <source>IEEE Transactions on Knowledge and Data Engineering</source>
          ,
          <volume>22</volume>
          ,
          <year>2010</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Devin</surname>
            <given-names>W.</given-names>
          </string-name>
          <string-name>
            <surname>Homan</surname>
          </string-name>
          . Transitive Closure in SQL. http://dwhoman.com/blog/sql-transitive-closure.html.
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10. FusionBox. Graph Algorithms in a Database . https://www.fusionbox.com/blog/detail/graph
          <article-title>-algorithms-in-a-databaserecursive-ctes-and-topological-sort-with-postgres/620/.</article-title>
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <given-names>Torsten</given-names>
            <surname>Grust</surname>
          </string-name>
          .
          <source>Advanced SQL</source>
          . https://db.inf.unituebingen.de/static les/teaching/ss17/advanced-sql/slides/advanced-sql-
          <volume>05</volume>
          .pdf.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>