GQL

Linear Composition in GQL

The workhorse of Graph Query Language (GQL) is pattern matching. GQL is oblivious to how a graph is stored. The table resulting from pattern matching is manipulated by a sequence of operators that modify it in an imperative style that that is referred to as “Linear Composition” or “Sequential Composition”. In “Linear Composition” we can simply add clauses to the already existing query which apply new operations to the result of already processed clauses.

In the following example we will demonstrate how a sequence of operators can continue after the RETURN clause.

Let’s start with a simple Graph.

Let’s say we interested in the nodes that have the longest path and then we want to find out all the paths between those two nodes. We can start with finding the longest path i.e. Saqib -> Angela -> Scott -> Uroosa and then use Linear Composition to add another Pattern Matching query to identify the all the paths between Saqib and Uroosa

Let’s start by create some sample dataset:

create graph social_graph{

 node person ({name string})
, edge relationship (person)-[{type string}]->(person)
};

use social_graph;


INSERT (n1:person {_id:'saqib', name:"Saqib"})
  , (n2:person {_id:'angela', name:"Angela"})
  , (n3:person {_id:'scott', name:"Scott"})
  , (n4:person {_id:'uroosa', name:"Uroosa"})
  , (n5:person {_id:'fatima', name:"Fatima"})
  , (n6:person {_id:'eleni', name:"Eleni"})
  , (n1)-[link1:relationship {type:"friend"}]->(n2)
  , (n2)-[link2:relationship {type:"co-worker"}]->(n3)
  , (n3)-[link3:relationship {type:"friend"}]->(n4)
  , (n1)-[link4:relationship {type:"co-worker"}]->(n5)
  , (n5)-[link5:relationship {type:"friend"}]->(n6)
  , (n1)-[link:relationship {type:"friend"}]->(n4)
;

Now let’s find the Longest path using PATTERN MATCHING

MATCH path = ALL  (a)-[links]->{1,}(b)
RETURN a, b, PATH_LENGTH(path) as path_length
ORDER BY path_length DESC limit 1

This will result in:

Next let’s add use Linear Composition to add another Pattern Matching query to identify the all the paths between Saqib and Uroosa


MATCH path = ALL  (a)-[links]->{1,}(b)
RETURN a, b, PATH_LENGTH(path) as path_length, path
ORDER BY path_length DESC limit 1
NEXT
MATCH path = ALL (a)-[links]->{1,}(b)
RETURN table(a.name, b.name, pedges(path));

This will result in

Observe that GQL processes results of pattern matching in sequential, or pipelined way where the output of each operation in a sequence serves as the input for the next operation. GQL standard refers to this as Linear Statement Composition or simple Linear Composition.

Comments

One response to “Linear Composition in GQL”

  1. […] can also use the Linear Composition in GQL to sum up the Ownership percentages (0.15 + 0.1224) as […]

Leave a Reply to Aggregating Edge Weights in GQL – GQL Cancel reply

Your email address will not be published. Required fields are marked *