Skip to content

Graph types: GRAPH_OBJECT and PATH

View as Markdown

CoraDB has two data types for graph queries. Both are used together with MATCH and the graph functions.

Type Value Created by
PATH An ordered sequence of vertices and the edges between them A path variable in a MATCH clause
GRAPH_OBJECT A reference to one vertex or edge, of any vertex or edge table CAST of a vertex or edge

The examples use the e-commerce sample graph, in which customers like products (likes edges from customer to product). A path of two likes edges, matched without direction, leads from a product to a customer who liked it and on to another product that customer liked.

A PATH value is created by a path variable in a MATCH clause, written path_variable = pattern. It contains the vertices and edges matched by the pattern, in order. See Path variables.

  • A path exists only within the query that matches it. It cannot be created with a literal, stored in a table column (PATH is not defined), or passed between queries.
  • Read its parts with nodes() and relationships(), which return LISTs, and measure it with path_size(). path_comp() collects values from matching paths without a FROM MATCH clause.
SELECT p2.name AS other_product,
size(nodes(pth)) AS vertex_count,
size(relationships(pth)) AS edge_count,
path_size(pth) AS path_size
FROM MATCH pth = (p1:product)-[l:likes{2,2}]-(p2:product)
WHERE p1.name = 'Vegie-spread' AND p1 <> p2
ORDER BY 1
LIMIT 3;
other_product vertex_count edge_count path_size
Abominable Hoodie 3 2 2
Aeon Capri 3 2 2
Arcadio Gym Short 3 2 2

Each path has three vertices (product, customer, product) and two edges.

A GRAPH_OBJECT value refers to a vertex or an edge without fixing its table. It lets you handle the elements of a path, which can come from different vertex and edge tables, in one list.

  • A GRAPH_OBJECT is obtained with CAST: CAST(v AS GRAPH_OBJECT) for one element, or CAST(nodes(pth) AS SEQUENCE(GRAPH_OBJECT)) for the vertices of a path. With list_comp() each element can be cast separately.
  • Internally it is an object identifier (OID). Drivers return it as an opaque reference, and corasql prints it as an OID such as (0|3585|1); OIDs differ from database to database.
  • Read the properties of the element with property_of(), giving the table the element belongs to.
  • A table column can be declared as GRAPH_OBJECT. It stores the reference, not a copy of the vertex or edge.

The following query reads the first, second and last vertex of each path through GRAPH_OBJECT values, using the three ways to convert them:

SELECT property_of(CAST(first(nodes(pth)) AS GRAPH_OBJECT), name, product) AS product,
property_of(at(CAST(nodes(pth) AS SEQUENCE(GRAPH_OBJECT)), 1), company_name, customer) AS liked_by,
property_of(last(list_comp(x IN nodes(pth) | CAST(x AS GRAPH_OBJECT))), name, product) AS also_liked
FROM MATCH pth = (p1:product)-[l:likes{2,2}]-(p2:product)
WHERE p1.name = 'Vegie-spread' AND p1 <> p2
ORDER BY 2, 3
LIMIT 2;
product liked_by also_liked
Vegie-spread Alvarado LLC Lono Yoga Short
Vegie-spread Alvarado LLC Neve Studio Dance Jacket