4.11. TQL CALL Clause¶
The CALL clause executes procedures from a catalog of available procedures. Procedures optionally take arguments as inputs and either produce resulting arguments or modify the system through side effects. Procedures that have output arguments produce rows of results where the output arguments define the columns of the rows. The YIELD sub-clause defines which output parameters of the procedure are included in the result and their order. The syntax for the CALL clause is
CALL procedure_name([input_param][, input_param...])
YIELD output_param [, output_param...]
The YIELD sub-clause is usually required. The CALL clause must also usually be followed by a RETURN or WITH statement. Here are a couple of examples of valid CALL usage.
CALL procedure_name(input_param1, input_param2)
YIELD output_param1
RETURN output_param1
CALL procedure_name(input_param1)
YIELD output_param1, output_param2
WITH output_param1, output_param2
WHERE output_param1 > 7
RETURN output_param1, output_param2
The one exception to requiring YIELD and RETURN or WITH clauses is a stand-alone CALL clause. In this case, the RETURN clause or both the YIELD and RETURN clauses may be omitted, and all the outputs of the procedure are returned in the default order. Examples are
CALL procedure_name(input_param1, input_param2)
CALL procedure_name(input_param1, input_param2)
YIELD output_param1
A CALL can also be followed by a WHERE clause to filter the results.
CALL procedure_name(input_param1)
YIELD output_param1, output_param2
WHERE output_param1 > 7
RETURN output_param1, output_param2
4.11.1. Whole Graph Algorithms¶
xGT supports the following whole graph algorithms:
Breadth First Search
PageRank
One common concept used in the graph algorithms is the graph on which an algorithm operates. In xGT a graph is a collection of vertex frames and edge frames that are connected. A vertex frame without any attached edge frames would just be a bunch of isolated vertices so can’t be in a graph. An edge frame can’t be in a graph if one of its source or target vertex frames is not included. Graphs are defined by a list of edge frames. The source and target vertex frames of each of the edge frames are included in the graph. The frames in the graph will often be all connected, but it is possible to have multiple separate components.
Another common concept used in the graph algorithms is a unique representation of a vertex across all frames. The pair of a frame name and a vertex key are used to uniquely represent a vertex.
All graph algorithms take two parameters.
The first describes the graph on which the algorithm operates, and the second is a map giving any additional parameters to the algorithm.
Here is an example call for page_rank()
:
CALL page_rank("graph_namespace",
{ tolerance : .0001 })
YIELD frame, key, rank
RETURN frame, key, rank
INTO Results
The first parameter can be either a string literal representing a namespace or a list of string literals representing a list of edge frames. When a namespace name is given, all of the edge frames the user has access to in the namespace along with their source and target vertex frames the user has access to in the namespace are used to define the graph. If one of the source or target vertex frames of an edge frame is not in the given namespace or the user doesn’t have access to the source or target vertex frame, that edge frame is not included. The example above shows calling a whole graph algorithm giving a namespace to define the graph.
When a list of edge frames is given, those frames along with their source and target vertex frames are used to define a graph. If a user gives an edge frame they don’t have access to or if the user doesn’t have access to one of the source or target vertex frames, a security error is thrown. Passing an empty list of edge frames causes all edge frames the user has access to in all namespaces in the system along with their source and target vertex frames to be used to define the graph. As before if the user doesn’t have access to the source or target vertex frame of one of the edge frames, that edge frame is not included. This example shows calling a whole graph algorithm giving a list of edge frames to define the graph.
CALL breadth_first_search(["Graph__Edge1", "Graph__Edge2"],
{ sources : [["Graph__Vertex1", "0"]],
depth : 2 })
YIELD frame, key, depth, parent_frame, parent_key
RETURN frame, key, depth, parent_frame, parent_key
INTO Results
4.11.1.1. Breadth First Search¶
The algorithm breadth_first_search()
performs a breadth first search on a graph.
This algorithm starts from one or more source vertices and visits all the vertices reachable from the source vertices by traversing incident edges, expanding out a level of neighbors at a time.
The algorithm supports the following input parameters given in the map:
Name |
Type |
Default |
Description |
---|---|---|---|
sources |
List of lists of string |
N/A |
A list of frame name and vertex key pairs representing the source vertices. |
max_depth |
Integer |
No limit |
The depth at which to stop the search. A depth of 1 indicates one level out from the sources. |
direction |
String |
forward |
The direction to perform the search. Valid values are “forward”, “reverse”, or “undirected”. |
The “sources” parameter is required. All other parameters are optional. The “sources” parameter is a list of lists of string where each sublist has two elements: a frame name and a vertex key. Note that the vertex key must be given as a string regardless of the type of the key. The “direction” parameter indicates the direction the search traverses incident edges. The default is the “forward” direction which traverses outgoing edges of vertices. The “reverse” direction traverses incoming edges of vertices. The “undirected” direction traverses both outgoing and incoming edges of vertices essentially treating the graph as if it were undirected.
Breadth first search has the following output parameters:
Name |
Type |
Description |
---|---|---|
frame |
String |
Name of the frame of a found vertex. |
key |
Original key type or string |
Key of a found vertex. |
depth |
Integer |
Depth at which the vertex was found. |
parent_frame |
String |
Name of the frame of the parent of a found vertex. |
parent_key |
Original key type or string |
Key of the parent of a found vertex. |
The breadth first search algorithm outputs the vertices it finds along with the depth the vertex was found at and the parent vertex. The type of the keys in the results is dependent on the types of the keys in the input vertex frames. If the types of the keys of all the input vertex frames is the same, then that type is used for the output key type. Otherwise, string is used as the output key type.
4.11.1.2. PageRank¶
The algorithm page_rank()
assigns a score to each vertex in the graph based on the number of incoming edges to the vertex and the scores of the sources of those edges.
This is the well-known PageRank algorithm developed by Larry Page.
The algorithm supports the following optional input parameters given in the map:
Name |
Type |
Default |
Description |
---|---|---|---|
damping_factor |
Float |
0.85 |
The damping factor used by PageRank. |
max_iterations |
Integer |
20 |
The maximum number of iterations before stopping the algorithm. |
tolerance |
Float |
0.0001 |
The algorithm stops after the norm of the difference between the score vectors of successive iterations is less than this value. |
norm |
String |
infinity |
The norm used to decide when to stop the algorithm. Valid values are “L1”, “L2”, and “infinity”. |
sources |
List of lists of string |
Empty list |
A list of frame name and vertex key pairs representing the set of personalized vertices. |
The “damping_factor” parameter is the standard PageRank damping factor that balances between visiting an adjacent vertex or teleporting to a random vertex. PageRank is an iterative algorithm. When the iteration stops is determined by a combination of the “max_iterations”, “tolerance”, and “norm” parameters. The iteration stops when either the maximum number of iterations has been reached or the norm of the difference between the score vectors of successive iterations is less than the tolerance. Each iteration generates a score vector that contains a score for each vertex in the graph. Taking the norm of the difference between successive score vectors gives a value to how much the last iteration improved the scores, and the algorithm stops if this value is less than a tolerance. The standard “L1”, “L2”, and “infinity” norms are supported. The “sources” parameter is used to perform personalized PageRank which restricts the vertices visited by a teleportation to the set of vertices given.
PageRank has the following output parameters:
Name |
Type |
Description |
---|---|---|
frame |
String |
Name of the frame of a vertex. |
key |
Original key type or string |
Key of a vertex. |
rank |
Float |
PageRank of a vertex. |
The PageRank algorithm outputs all the vertices in the graph along with the PageRank score for each vertex. The type of the keys in the results is dependent on the types of the keys in the input vertex frames. If the types of the keys of all the input vertex frames is the same, then that type is used for the output key type. Otherwise, string is used as the output key type.