Version: 7.0.0

Apache AGE (incubating) for openGauss ​

Before You Start ​

Apache AGE is used by creating an extension.

bash
create extension age;

After AGE is installed, the ag_catalog schema is created by default. All built-in data types and functions of AGE are stored under ag_catalog. Therefore, when using AGE, especially when executing Cypher statements, you need to first run the following command:

bash
SET search_path TO ag_catalog;

You also need to run the load 'age' command to ensure that all hooks of the AGE extension are loaded for graph data integrity.

bash
load 'age';

NOTE

Before creating and using the AGE extension, you need to disable the thread pool and set enable_thread_pool = off.

1. Graph Operations ​

A graph consists of a set of vertices and edges, where each vertex and edge has a property map. A vertex is the basic object of a graph, which can exist independently of anything else in the graph. An edge creates a directed connection between two vertices.

1.1 Creating a Graph ​

To create a graph, use create_graph(graph_name):

bash
SELECT * FROM ag_catalog.create_graph('graph_name');

1.2 Deleting a Graph ​

To delete a graph, use drop_graph(graph_name, cascade).

  1. The first parameter graph_name is the graph to be deleted, and the second parameter cascade is a boolean value indicating whether to delete graph label and data.
  2. It is recommended to set cascade to true; otherwise, all contents in the graph must be manually deleted using SQL DDL commands
bash
SELECT * FROM ag_catalog.drop_graph('graph_name', true);

1.3 Creating Vertex Labels in a Graph ​

To create a vertex label in a graph, use create_vlabel(graph_name, label_name).

bash
SELECT * FROM ag_catalog.create_vlabel('graph_name','label_name');

Whenever a vertex label is created using the create_vlabel() function, a corresponding table named <label_name> is generated within the namespace new_graph. The same behavior applies to create_elabel() for creating edge labels. These two functions are not mandatory. if a label table does not already exist when creating vertices or edges via Cypher, it will be created automatically.

1.4 Creating Edge Labels in a Graph ​

To create a graph, use create_elabel(graph_name, label_name).

bash
SELECT * FROM ag_catalog.create_elabel('graph_name','label_name');

2. Graph Storage ​

2.1 Graph ​

After a graph is created, the kernel creates a schema with the same name as the graph. Meanwhile, a record is inserted into the ag_catalog.ag_graph table, marking the newly created schema aas the storage location for the graph data.

bash
SELECT create_graph('new_graph');

NOTICE:  graph "new_graph" has been created
 create_graph 
--------------

(1 row)

SELECT * FROM ag_catalog.ag_graph;

   name    | namespace 
-----------+-----------
 new_graph | new_graph
(1 row)

Vertices and Edges in a Graph ​

bash
-- After a graph is created, two tables, _ag_label_vertex and _ag_label_edge, are created in the schema corresponding to the graph as the default vertex table and edge table. Meanwhile, two records are inserted into ag_catalog.ag_label to associate the vertex table and edge table with the schema.
SELECT * FROM ag_catalog.ag_label;

       name       | graph | id | kind |          relation          
------------------+-------+----+------+----------------------------
 _ag_label_vertex | 68484 |  1 | v    | new_graph._ag_label_vertex 
 _ag_label_edge   | 68484 |  2 | e    | new_graph._ag_label_edge   
(2 rows)

-- Create a vertex table.
SELECT create_vlabel('new_graph', 'Person');
NOTICE:  VLabel "Person" has been created
 create_vlabel 
---------------
 
(1 row)

-- After a vertex table is created, a record is inserted into the ag_catalog.ag_label table to associate the vertex table with the graph. The kind 'v' represents a vertex table and the kind 'e' represents an edge table.
SELECT * FROM ag_catalog.ag_label;
       name       | graph | id | kind |          relation          
------------------+-------+----+------+----------------------------
 _ag_label_vertex | 68484 |  1 | v    | new_graph._ag_label_vertex 
 _ag_label_edge   | 68484 |  2 | e    | new_graph._ag_label_edge   
 Person           | 68484 |  3 | v    | new_graph."Person"         
(3 rows)

3. Cypher Query ​

Cypher statements cannot be executed directly in the database.Instead, they must be passed as arguments to the cypher() function, which then returns a SETOF records.

3.1 Introduction to cypher() ​

cypher(graph_name, query_string, parameters)

graph_name is the graph to query. query_string is the Cypher statement. parameters is optional and can only be used with Prepared Statements; otherwise, an error will be thrown.

bash
SELECT * FROM cypher('graph_name', $$ 
/* Cypher Query Here */ 
$$) AS (result1 agtype, result2 agtype);

Note

  1. (result1 agtype, result2 agtype) after AS, i.e., SETOF records, must match the number of return values in the Cypher statement.
  2. SELECT * FROM cypher cannot be written as SELECT cypher.
  3. Before executing the statement, execute load 'age' and set search_path = ag_catalog.

Data Types and Functions ​

The following provides a brief description of the data types, Cypher statements, and functions included in AGE.

1 Data Types ​

AGE creates two data types: graphid and agtype. graphid represents the unique ID identifier for vertices and edges. agtype is the core data type of AGE. For more details, refer to https://age.apache.org/age-manual/master/intro/types.html.

1.1 Simple Data Types ​

Simple data types include Null, Integer, Float, Numeric, Bool, and String.

1.2 Composite Data Types ​

Composite data types include List and Map.

  • List operations
No.Supported OperationDescription
1List in generalOrdinary list
2NULL in a ListList with null
3Access Individual ElementsAccess an element of a list
4MapElements in ListsList elements containing map structures
5Accessing Map Elements in ListsAccess the value of a map within a list element
6Negative Index AccessNegative index access
7Index RangesIndex ranges
8Negative Index RangesNegative index ranges
9Positive SlicesPositive slice
10Negative SlicesNegative slice
  • Map operations
No.Supported OperationDescription
1Literal Maps with SimpleDataTypesOrdinary map type
2Literal Maps with composite Data TypesMap containing composite data types
3Property Access of a mapMap property access
4Accessing List Elements in MapsList element access in a map

1.3 Simple Entities ​

Simple entity types include GraphId, Labels, and Properties, and simple entity types can further compose Vertex, Edge, and Composite Entities.

1.4 Vertex ​

A Vertex is the fundamental building block of a graph, representing a node.

1.5 Edge ​

An Edge is the basic building block of a graph, representing an edge.

1.6 Composite Entities ​

A Path composed of Vertex and Edge.

2 Cypher Statements ​

For detailed information on AGE's support for Cypher statements, refer to https://age.apache.org/age-manual/master/clauses/match.html.

2.1 Match ​

No.Supported OperationDescription
1get all verticesObtain all vertices
2get all vertices with a labelObtain all vertices of a certain label type
3related verticesObtain related vertices (neighbor vertices) through edges
4match with labelsFilter related vertices by label.
5Outgoing EdgesOutgoing edges support
6Directed Edges and variableDirected edges and variable support
7Match on edge typeFilter edges by label type
8Match on edge type and use a variableFilter edges by label type and set a variable
9Multiple EdgesMultiple edge matching
10Variable Length EdgesVariable-length path matching

2.2 WITH ​

No.Supported OperationDescription
1Filter on aggregate function resultsFilter results through aggregate functions
2Sort results before using collect on themSort before collect
3Limit branching of a path searchMatch paths, limit them to a certain number, and then use these paths as a basis for matching again

2.3 SKIP ​

NumberSupported OperationDescription
1skip first three rowsSkip the first three rows
2Return middle tow rowsReturn the middle tow rows, working with limit
3Using an expression with SKIP to return a subset of the rowsUse an expression with SKIP to return a subset of rows

2.4 LIMIT ​

No.Supported OperationDescription
1Return a subset of the rowsReturn a subset of the query results
2Using an expression with LIMIT to return a subset of the rowsUse an expression with LIMIT to return a subset of rows

2.5 Return ​

No.Supported OperationDescription
1Return nodesReturn the queried nodes
2Return edgesReturn the queried edges
3Return propertyReturn the property of a node or an edge
4Return all elementsReturn all elements
5Variable with uncommon charactersSupport variables with uncommon characters
6Aliasing a fieldAlias a return value
7unique resultsReturn value of distinct

2.6 ORDER BY ​

#Supported OperationDescription
1Order nodes by propertySort nodes by a single property
2Order nodes by multiple propertiesSort nodes by multiple properties
3Order nodes in descending orderSort nodes in descending order
4Ordering nullSort null values in ascending order

2.7 CREATE ​

No.Supported OperationDescription
1Create single vertexCreate a single vertex
2Create multiple verticesCreate multiple vertices
3Create a vertex with a labelCreate a vertex with a label
4Create vertex and add labels and propertiesCreate a vertex with labels and properties
5Return create nodeCreate and return a vertex
6Create an edge between two nodesCreate an edge
7Create an edge and set propertiesCreate an edge and set properties
8Create a full pathCreate a full path

2.8 SET ​

No.Supported OperationDescription
1Set a propertySet a single property
2Return created vertexReturn the modified vertex
3Remove a propertyRemove a property
4Set multiple properties using one SET clauseSet multiple properties

2.9 REMOVE ​

NameSupported OperationDescription
1Remove a propertyRemove a property

2.10 DELETE ​

No.Supported OperationDescription
1Delete single vertexDelete a single vertex
2Delete all vertices and edgesDelete all vertices and edges
3Delete edges onlyDelete edges
4Return a deleted vertexReturn a deleted vertex

3 Functions ​

Functions primarily involve operations on agtype and the generation of expressions. For details, refer to https://age.apache.org/age-manual/master/functions/predicate_functions.html.

3.1 Predicate Functions ​

No.Supported OperationDescription
1Exists(Property)Check whether a property exists
2Exists(Path)Check whether a query path exists

3.2 Scalar Functions ​

No.Supported OperationDescription
1idReturn the ID of a vertex or an edge
2start_idReturn the ID of the starting vertex of an edge
3end_idReturn the ID of the ending vertex of an edge
4typeReturn the string representation of the edge type
5propertiesReturn an agtype map containing all properties of a vertex or an edge. If the argument is already a map, return it unchanged
6headReturn the first element in an agtype list
7lastReturn the last element in an agtype list
8lengthReturn the length of a path
9sizeReturn the length of a list
10startNodeReturn the starting node of an edge
11endNodeReturn the ending node of an edge
12timestampReturn the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC
13toBooleanConvert a string value to a boolean value
14toFloatConvert an integer or string value to a floating-point number
15toIntegerConvert a floating-point or string value to an integer value
15coalesceReturn the first non-null value in the given expression list

3.3 List Functions ​

No.Supported OperationDescription
1keysReturn a list containing the string representation of all property names of a vertex, edge, or map
2rangeReturn a list containing all integer values within the range bounded by a start value and end value
3labelsReturn a list containing the string representation of all labels of a node
4relationshipsReturn a list containing all relationships in a path
5nodesReturn a list containing all vertices in a path

3.4 Numeric Functions ​

No.Supported OperationDescription
1randReturn a random floating-point number in the range from 0 (inclusive) to 1 (exclusive); i.e., [0,1)
2absReturn the absolute value of the given number
3ceilReturn the smallest floating-point number that is greater than or equal to the given number and equal to a mathematical integer
4floorReturn the largest floating-point number that is less than or equal to the given number and equal to a mathematical integer
5roundReturn the value of the given number rounded to the nearest integer
6signReturn the sign of the given number

3.5 Logarithmic Functions ​

No.Supported OperationDescription
1eReturn the base of the natural logarithm
2sqrtReturn the square root of a number
3expReturn e^n, where e is the base of the natural logarithm and n is the value of the argument expression
4logReturn the natural logarithm of a number
5log10Return the common logarithm (base 10) of a number

3.6 Trigonometric Functions ​

No.Supported OperationDescription
1degreesConvert radians to degrees
2radiansConvert degrees to radians
3piReturn the mathematical constant pi
4sinReturn the sine of a number
5cosReturn the cosine of a number
6tanReturn the tangent of a number
7cotReturn the cotangent of a number
8asinReturn the arcsine of a number
9acosReturn the arccosine of a number
10atanReturn the arctangent of a number
11atan2Return the arctangent of a set of coordinates in radians

3.7 String Functions ​

#Supported OperationDescription
1replaceReturn returns a string in which all occurrences of a specified string in the original string have been replaced by another (specified) string
2splitReturn a list of strings obtained by splitting the original string based on matches of a given delimiter
3leftReturn a string containing the specified number of leftmost characters of the original string
4rightReturn a string containing the specified number of rightmost characters of the original string
5substringReturn a substring of the original string, starting at a 0-based index and with a specified length
6rTrimReturn the original string with trailing whitespace removed
7lTrimReturn the original string with leading whitespace removed
8trimReturn the original string with leading and trailing whitespace removed
9toLowerReturn the original string in lowercase
10toUpperReturn the original string in uppercase
11reverseReturn a string in which the order of all characters in the original string has been reversed

3.8 Aggregation Functions ​

No.Supported OperationDescription
1minReturn the minimum value in a set of values
2maxReturn the maximum value in a set of values
3stDevReturn the standard deviation of the given values in a group
4stDevPReturn the standard deviation of the given values in a group
5percentileContReturn the percentile of the given values in a group
6percentileDiscReturn the percentile of the given values in a group
7countReturn the number of values or records
8avgReturn the average of a set of numeric values
9sumReturn the sum of a set of numeric values