Enhancing the create_complete_graph Function in Apache AGE

markgomer

Marco Aurélio Silva de Souza Júnior

Posted on July 27, 2023

Enhancing the create_complete_graph Function in Apache AGE

In this blog post, we're going to discuss an upgrade to the create_complete_graph function in Apache AGE, continuing our series. This function generates a complete graph with the given parameters, such as the graph name, number of nodes, and edge label. We'll be enhancing the function to add support for pre-modeled starting properties when creating nodes and edges.

To understand this modification, let's first dive into the create_complete_graph function in its initial state. The function takes in the following parameters:

  1. 'graph_name': The name of the graph.
  2. 'no_of_nodes': The number of nodes to create.
  3. 'edge_label': The label for the edges.
  4. 'node_label': The label for the nodes (optional).

In the existing code, node properties and edge properties are initialized as empty by default:

agtype *props = create_empty_agtype();
Enter fullscreen mode Exit fullscreen mode

Then, these empty properties are passed to both insert_vertex_simple and insert_edge_simple functions while creating vertices and edges.

Modifications

Now, we'll modify the function to accept two additional parameters: 'edge_properties' and 'node_properties'. This will allow us to provide initial properties for edges and nodes while creating them.

Here are the steps to implement this enhancement:

  • Add Parameters: First, we need to add two more parameters in the age--1.3.0.sql file:
CREATE FUNCTION ag_catalog.create_complete_graph(
    graph_name name, 
    number_of_nodes int, 
    edge_label name, 
    edge_properties agtype = NULL,
    node_label name = NULL,
    node_properties agtype = NULL)
RETURNS void
LANGUAGE c
CALLED ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';
Enter fullscreen mode Exit fullscreen mode
  • Declare Properties: Next, declare two agtype variables, one for edge_properties and one for node_properties. Initialize them to NULL.
agtype* edge_properties = NULL;
agtype* node_properties = NULL;
Enter fullscreen mode Exit fullscreen mode
  • Assign Properties: Within the function, we will assign values to edge_properties and node_properties only if they are not NULL.

For edge_properties:

if(!PG_ARGISNULL(3))
{
    edge_properties = (agtype*)PG_GETARG_ARRAYTYPE_P(3);
}
Enter fullscreen mode Exit fullscreen mode

For node_properties:

if(!PG_ARGISNULL(5))
{
    node_properties = (agtype*)PG_GETARG_ARRAYTYPE_P(5);
}
Enter fullscreen mode Exit fullscreen mode
  • Modify Vertex and Edge Creation: Replace the props variable in insert_vertex_simple and insert_edge_simple function calls with node_properties and edge_properties respectively.

For vertex creation:

insert_vertex_simple(
    graph_id,
    vtx_name_str,
    object_graph_id,
    node_properties);
Enter fullscreen mode Exit fullscreen mode

For edge creation:

insert_edge_simple(graph_id, edge_name_str,
                   object_graph_id, start_vertex_graph_id,
                   end_vertex_graph_id, edge_properties);
Enter fullscreen mode Exit fullscreen mode

And that's it! You have now successfully added the ability to include pre-modeled starting properties for nodes and edges in the create_complete_graph function. This will allow developers to add their own properties to nodes and edges upon creation, making the function more flexible and customizable.

To call the function now you can put properties as agtype in the parameters, like this:

SELECT * FROM create_complete_graph(
    'test_graph', 5,
    'edges_label', '{"edge_property":"test"}'::agtype,
    'vertices_label', '{"node_property":"test"}'::agtype);
Enter fullscreen mode Exit fullscreen mode

Remember to compile the modified extension and install it in your PostgreSQL server to make use of these enhancements.


I make these posts in order to guide people into the development of a new technology. If you find anything incorrect, I urge you to comment below so I can fix it. Thanks!


Check Apache AGE: https://age.apache.org/.

Overview — Apache AGE master documentation: https://age.apache.org/age-manual/master/intro/overview.html.

GitHub - apache/age: https://github.com/apache/age

💖 💪 🙅 🚩
markgomer

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related