5.4 Vertex and Edge Identifiers

You can uniquely identify each vertex and edge in a SQL property graph with the VERTEX_ID and EDGE_ID functions, respectively, in a SQL graph query.

Graph element identifiers are based on the key value defined for the graph element tables. Therefore, it is important to note the following:

  • Graphs in TRUSTED mode may produce duplicate identifiers for different vertices if some key columns do not have a UNIQUE constraint.
  • Graphs in ENFORCED mode are guaranteed to always produce unique identifiers.

The VERTEX_ID and EDGE_ID functions can be used in any expression appearing in the COLUMNS or WHERE clause in a SQL graph query.

Note:

In order to use the VERTEX_ID and EDGE_ID functions, you must have READ or SELECT privilege on the property graph. In addition, for each vertex or edge table from which the vertex or edge IDs are requested, ensure at least one of the following:
  • Each of its key columns is directly exposed as a property.
  • You have READ or SELECT privilege on its underlying database table.

The input to the VERTEX_ID function is a single vertex graph pattern variable coming from a matched vertex pattern as shown:

MATCH (v) COLUMNS(VERTEX_ID(v) AS v_id)

Similarly, the EDGE_ID function takes as input a single edge graph pattern variable coming from a matched edge pattern as shown:

MATCH (v1)-[e]->(v2) COLUMNS(EDGE_ID(e) AS e_id)

The output of these functions is a vertex or an edge identifier of JSON data type. The following shows an example of a JSON output describing the vertex identifier:

{
  "GRAPH_OWNER": "GRAPHUSER",
  "GRAPH_NAME": "STUDENTS_GRAPH",
  "ELEM_TABLE": "PERSONS",
  "KEY_VALUE": {
    "PERSON_ID": 1
  }
}

In the preceding JSON output:

  • GRAPH_OWNER: Owner of the property graph object
  • GRAPH_NAME: Name of the property graph object
  • ELEM_TABLE: Name of the vertex table
  • KEY_VALUE: Name and value of the key column

The same list of JSON output fields apply to an edge identifier also. However, the ELEM_TABLE field represents the name of an edge table. Also, all operations that can be performed on a JSON data type can be performed on the vertex and edge identifiers.

If the referenced element variable is not bound to a graph element, then the VERTEX_ID and EDGE_ID functions return the null value.

See Example 5-25 for more information.

VERTEX_EQUAL and EDGE_EQUAL Predicates

The VERTEX_EQUAL and EDGE_EQUAL predicates can be used to, respectively, compare two vertex and edge identifiers and return TRUE if they are equal.

The inputs to the VERTEX_EQUAL predicate are two vertex graph pattern variables. Similarly for EDGE_EQUAL, both inputs must be edge graph pattern variables. These predicates can be used in the WHERE clause in a SQL graph query.

If at least one of the referenced element variables is not bound to a graph element, then the predicates evaluate to the null value. Otherwise, they evaluate to TRUE or FALSE.

See Example 5-26 for more information.