Repeating a 1-Hop Pattern

A common pattern is the two-step "Friend of a Friend". Or, how many entities might receive a message if it is passed up to three times? Do you have any known change of connections to a celebrity?

GSQL pattern matching makes it easy to express such variable-length patterns which repeat a single-hop. Everything else stays the same as introduced in the previous section, except we append an asterisk (or Kleene star for you regular expressionists) and an optional min..max range to an edge pattern.

  • (E*) means edge type E repeats any number of times (including zero!)

  • (E*1..3) means edge type E occurs one to three times.

Below are more illustrative examples:

  • 1-hop star pattern — repetition of an edge pattern 0 or more times

    1. FROM X:x - (E1*) - Y:y

    2. FROM X:x - (E2>*) - Y:y

    3. FROM X:x - (<E3*) - Y:y

    4. FROM X:x - (_*) - Y:y

      • Any undirected edge can be chosen at each repetition.

    5. FROM X:x - (_>*) - Y:y

      • Any right-directed edge can be chosen at each repetition.

    6. FROM X:x - (<_*) - Y:y

      • Any left-directed edge can be chosen at each repetition.

    7. FROM X:x - ((E1|E2>|<E3)*) - Y:y

      • Either E1, E2> or <E3 can be chosen at each repetition.

  • 1-hop star pattern with bounds

    1. FROM X:x - (E1*2..) - Y:y

      • Lower bounds only. There is a chain of at least 2 E1 edges.

    2. FROM X:x - (E2>*..3) - Y:y

      • Upper bounds only. There is a chain of between 0 and 3 E2 edges.

    3. FROM X:x - (<E3*3..5) - Y:y

      • Both Lower and Upper bounds. There is a chain of 3 to 5 E3 edges.

    4. FROM X:x - ((E1|E2>|<E3)*3) - Y:y

      • Exact bound. There is a chain of exactly 3 edges, where each edge is either E1, E2>, or <E3.

Remarks

  • No alias allowed for edge with Kleene star An edge alias may not be used when a Kleene star is used. The reason is that when there are a variable number of edges, we cannot associate or bind the alias to a specific edge in the pattern.

  • Shortest path semantics When an edge is repeated with a Kleene star, only the shortest matching occurrences are selected. See the example below:

In Figure 2, for Pattern 1 - (E>*) - 4, any of the following paths reach 4 from 1.

  • 1->2->3->4

  • 1->2->3->5->6->2->3->4

  • any path that goes through the cycle 2->3->5->6->2 two or more times and jumps out at 3.

The first path is shorter than the rest; it is considered the only match.

Examples of Variable Hop Queries

In this tutorial, we will use the new Interpreted Mode for GSQL, introduced in TigerGraph 2.4. Interpreted mode lets us skip the INSTALL step, and even to run a query as soon as we create it, to offer a more interactive experience. These one-step interpreted queries are unnamed (anonymous) and parameterless, just like SQL.

Example 1. Find the direct or indirect superclass (including the self class) of the TagClass whose name is "TennisPlayer".

You can copy the above GSQL script to a file named example1.gsql, and invoke this script file in a Linux shell.

Note below that the starting vertex s, whose name is TennisPlayer, is also a match, using a path with zero hops.

Example 2. Find the immediate superclass of the TagClass whose name is "TennisPlayer". (This is equivalent to a 1-hop non-repeating pattern.)

You can copy the above GSQL script to a file named example2.gsql, and invoke this script file in a Linux shell.

Example 3. Find the 1 to 2 hops direct and indirect superclasses of the TagClass whose name is "TennisPlayer".

You can copy the above GSQL script to a file named example3.gsql, and invoke this script file in a Linux shell.

Example 4. Find the superclasses within 2 hops of the TagClass whose name is "TennisPlayer".

You can copy the above GSQL script to a file named example4.gsql, and invoke this script file in a Linux shell.

Example 5. Find the superclasses at least one hop from the TagClass whose name is "TennisPlayer".

You can copy the above GSQL script to a file named example5.gsql, and invoke this script file in a Linux shell.

Example 6. Find the 3 most recent comments that are liked or created by Viktor Akhiezer, and the total number of comments related to (created or liked by) Viktor Akhiezer.

You can copy the above GSQL script to a file named example6.gsql, and invoke this script file in a Linux shell.