Enhancing the create_complete_graph Function in Apache AGE
Tags: apacheage, postgres
Published: 2023-07-27 13:27:38

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:

```C
agtype *props = create_empty_agtype();
```

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:

```sql
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';
```

- **Declare Properties:** Next, declare two `agtype` variables, one for `edge_properties` and one for `node_properties`. Initialize them to `NULL`.

```C
agtype* edge_properties = NULL;
agtype* node_properties = NULL;
```

- **Assign Properties:** Within the function, we will assign values to `edge_properties` and `node_properties` only if they are not NULL.

For `edge_properties`:

```C
if(!PG_ARGISNULL(3))
{
    edge_properties = (agtype*)PG_GETARG_ARRAYTYPE_P(3);
}
```

For `node_properties`:

```C
if(!PG_ARGISNULL(5))
{
    node_properties = (agtype*)PG_GETARG_ARRAYTYPE_P(5);
}
```

- **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:

```C
insert_vertex_simple(
    graph_id,
    vtx_name_str,
    object_graph_id,
    node_properties);
```

For edge creation:

```C
insert_edge_simple(graph_id, edge_name_str,
                   object_graph_id, start_vertex_graph_id,
                   end_vertex_graph_id, edge_properties);
```

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:

```sql
SELECT * FROM create_complete_graph(
    'test_graph', 5,
    'edges_label', '{"edge_property":"test"}'::agtype,
    'vertices_label', '{"node_property":"test"}'::agtype);
```

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