Exploring Numeric Functions in Apache AGE: A Comprehensive Tutorial
Tags: postgres, apacheage, database
Published: 2023-06-24 22:21:55

In this tutorial, we will delve into the numeric functions offered by Apache AGE. These functions can be utilized in our Cypher queries to enhance their functionality and calculations.


**What is Apache AGE ?**

Apache AGE is a graph database system that is built on top of PostgreSQL. It allows users to store, manage, and analyze large-scale graph data in a highly performant and scalable way.

Apache AGE combines the benefits of a powerful relational database management system (PostgreSQL) with the flexibility and scalability of a graph database. This means that users can store their data in a relational format and also perform graph-based queries and analyses on the data.

**Prerequisites**

1. PostgreSQL Version 11 or 12: Make sure you have PostgreSQL installed on your machine. Apache AGE relies on PostgreSQL, so it's important to have either version 11 or 12.

2. Apache AGE Installation and Configuration: Install and configure Apache AGE to seamlessly integrate with your PostgreSQL installation. If you haven't installed PostgreSQL and Apache AGE yet, we recommend following our comprehensive step-by-step guide for easy installation on Windows. You can access the tutorial [here](https://dev.to/omarsaad/step-by-step-guide-to-install-postgresql-apache-age-on-windows-28gf). This guide will walk you through the entire process and ensure a smooth setup.


**Numeric Functions in Apache AGE**

**rand()**

The `rand()` function in Apache AGE generates a random floating-point number within the range of 0 (inclusive) to 1 (exclusive), denoted as [0, 1). The numbers produced by this function follow an approximate uniform distribution.

```cypher
SELECT *
FROM cypher('graph_name', $$
    RETURN rand()
$$) AS (r agtype);
```

The above query will return a random number.

**abs()**

The `abs()` function in Apache AGE calculates and returns the absolute value of a given number.

Here's an example of using the `abs()` function in a Cypher query within Apache AGE:

```cypher
SELECT *
FROM cypher('graph_name', $$
    RETURN abs(-5)
$$) AS (result agtype);
```

The above query will return the absolute value which is `5`.

**ceil()**

The `ceil()` function returns the smallest floating-point number that is greater than or equal to the given number and equal to a mathematical integer.

Here's an example using the `ceil()` function in a Cypher query within Apache AGE:

```cypher
SELECT *
FROM cypher('graph_name', $$
    RETURN ceil(0.1) 
$$) AS (ceil agtype);
```

The ceiling of 0.1 is returned which is 1.

**floor()**

The `floor()` function  returns the greatest floating-point number that is less than or equal to the given number and equal to a mathematical integer.

Here's an example using the `floor()` function in a Cypher query within Apache AGE:

```cypher
SELECT *
FROM cypher('graph_name', $$
    RETURN floor(0.1) 
$$) AS (flr agtype);
```

The floor of 0.1 is returned which is 0.

**round()**

The `round()` function returns the value of the given number rounded to the nearest integer.

Here's an example using the `round()` function in a Cypher query within Apache AGE:

```cypher
SELECT *
FROM cypher('graph_name', $$
    RETURN round(3.141592) 
$$) AS (rounded_value agtype);
```

In the above query 3.0 is returned.

**sign()**

The `sign()` function in Apache AGE returns the sign of a given number. It indicates whether the number is positive, negative, or zero.

Here's an example using the `sign()` function in a Cypher query within Apache AGE:

```cypher
SELECT *
FROM cypher('graph_name', $$
    RETURN sign(-5) 
$$) AS (sign_value agtype);
```


The `sign()` function returns the following values:
- 1 if the number is positive
- -1 if the number is negative
- 0 if the number is zero


In this tutorial, we explored various numeric functions available in Apache AGE and learned how to incorporate them into our Cypher queries. We covered essential functions such as `rand()`, `abs()`, `ceil()`, `floor()`, `round()`, and `sign()`, each serving a distinct purpose in performing calculations and manipulating numeric values. By leveraging these functions, we can enhance the capabilities of our Cypher queries within Apache AGE, enabling us to work with random numbers, absolute values, rounding, ceiling, floor, and signum operations. With this newfound knowledge, you are now equipped to utilize these numeric functions effectively to analyze and manipulate numerical data in your Apache AGE graph databases.



**References**
[Apache AGE documentation](https://age.apache.org/age-manual/master/index.html)
[Apache AGE Github](https://github.com/apache/age)

**Contribute to Apache AGE**

[Apache AGE Github](https://github.com/apache/age)
[Apache AGE Viewer Github](https://github.com/apache/age-viewer)