DEV Community

Talha Munir 🇵🇸
Talha Munir 🇵🇸

Posted on

Numeric functions in Apache Age

In this article we will discuss numeric functions in Apache age. Following provides the description of the function, it's syntax, what will it return and a sample query for the stated function.

rand:

Description

Rand() function in age will return a random floating point number with in the range of 0 to 1. The numbers returned follows an approximate uniform distribution.

Syntax:

rand()

Returns:

The method will return a floating point number.

Query:

SELECT *
FROM cypher('your_graph_name', $$
    RETURN rand()
$$) as (randomNumber agtype);
Enter fullscreen mode Exit fullscreen mode

abs:

Description

abs() returns the absolute value of the number provided.

Syntax:

abs(expression)
here expression can be any numeric expression.

Returns:

The method will return an absolute value.

Query:

SELECT *
FROM cypher('your_graph_name', $$
    MATCH (a), (e) WHERE a.name = 'Usman' AND e.name = 'Talha'
    RETURN a.age, e.age, abs(a.age - e.age)
$$) as (usman_age agtype, talha_age agtype, difference agtype);
Enter fullscreen mode Exit fullscreen mode

The above query will return the ages of usman and talha and then return the difference of their ages.

ceil:

Description

ceil function will return the smallest floating point number greater than or equal to the given number.

Syntax:

ceil(numeric expression)
here numeric expression can be any numbers or expression of numbers.

Returns:

The method will return a floating point number.

Query:

SELECT *
FROM cypher('your_graph_name', $$
    RETURN ceil(0.1)
$$) as (ceil_value agtype);
Enter fullscreen mode Exit fullscreen mode

The above query will return 1 as the ceil of 0.1

floor:

Description

The function will return the greatest floating point number that is less than or equal to the given number.

Syntax:

ceil(numeric expression)
here numeric expression can be any numbers or expression of numbers.

Returns:

The method will return a floating point number.

Query:

SELECT *
FROM cypher('your_graph_name', $$
    RETURN floor(0.1)
$$) as (flr agtype);
Enter fullscreen mode Exit fullscreen mode

The above query will return 0 as a floor of 0.1.

Round:

Description

The method will return the rounded value of the given number to the nearest integer

Syntax:

round(expression)
here numeric expression can be any numbers or expression of numbers.

Returns:

The method will return a floating point number.

Query:

SELECT *
FROM cypher('your_graph_name', $$
    RETURN round(3.141592)
$$) as (rounded_value agtype);
Enter fullscreen mode Exit fullscreen mode

The above query will return 3.0 as rounded value of the given 3.141592.

Sign:

Description

The function will return an signed number for the number provided.

  • It will return 0 if the number is zero.
  • It will return 1 if the number is positive.
  • It will return -1 if the number is negative.

Syntax:

sign(a numeric expression)

Returns:

The method will return an integer.

Query:

SELECT *
FROM cypher('your_graph_name', $$
    RETURN sign(-17), sign(0.1), sign(0)
$$) as (negative_sign agtype, positive_sign agtype, zero_sign agtype);
Enter fullscreen mode Exit fullscreen mode

The above query will return the -1, 1 and zero as the signum of given values i.e. -17, 0.1 and 0 respectively.

References:

You can view more on Github and documentation of Apache age by following these links:

  1. https://age.apache.org/age-manual/master/intro/overview.html
  2. https://github.com/apache/age

Top comments (0)