WALEED SHAHID

Posted on

# Blog Post 3: Exploration of Scalar Functions in Apache Age

In the previous blog posts, we discussed several scalar functions in Apache Age and their applications. In this final blog post, let's continue our exploration of scalar functions and conclude our journey through Apache Age.

## 7. last()

The last() function returns the last element in an agtype list. It takes an expression that returns a list as an argument and returns the type of the value returned, which is the type of the last element of the list.

Syntax: last(list)

Consider the following query:

``````SELECT *
FROM cypher('graph_name', \$\$
MATCH (a)
WHERE a.name = 'Eskil'
RETURN a.array, last(a.array)
\$\$) as (lst agtype, lst_tail agtype);
``````

Results:

``````      lst      | lst_tail
----------------+----------
[1, 2, 3, 4]   | 4
[5, 6, 7, 8]   | 8
[9, 10, 11, 12]| 12
``````

The query retrieves a list from a vertex and returns the list itself and its last element.

## 8. length()

The length() function returns the length of a path. It takes an expression that returns a path as an argument and returns an agtype integer representing the length of the path.

Syntax: length(path)

Consider the following query:

``````SELECT *
FROM cypher('graph_name', \$\$
MATCH p = (a)-[]->(b)-[]->(c)
WHERE a.name = 'Alice'
RETURN length(p)
\$\$) as (length_of_path agtype);
``````

Results:

``````length_of_path
----------------
2
3
4
``````

The query retrieves all paths of length 2 or more from vertex Alice and returns the length of each path.

## 9. size()

The size() function returns the length of a list. It takes an expression that returns a list as an argument and returns an agtype integer representing the length of the list.

Syntax: size(list)

Consider the following query:

``````SELECT *
FROM cypher('graph_name', \$\$
RETURN size(['Alice', 'Bob', 'Charlie'])
\$\$) as (size_of_list agtype);
``````

Results:

`````` size_of_list
-------------
3
``````

The query returns the length of the given list, which is 3 in this case.

With these additional scalar functions, developers and data scientists can further enhance their graph querying and analysis capabilities in Apache Age.

We conclude our exploration of scalar functions in Apache Age. We hope you found these blog posts informative and useful for understanding the power and versatility of scalar functions in Apache Age.

## Previous Blogs

Blog post 1 on Scalar Functions

Blog post 2 on Scalar Functions