DEV Community

WALEED SHAHID
WALEED SHAHID

Posted on

Blog Post 2: More Scalar Functions in Apache Age

In the previous blog post, we discussed some scalar functions in Apache Age and their usage. In this post, let's continue our exploration of scalar functions and their capabilities in Apache Age.

4. type()

The type() function returns the string representation of the edge type. It takes an expression that evaluates to an edge as an argument and returns an agtype string representing the edge type.

Syntax: type(edge)

Consider the following query:

SELECT *
FROM cypher('graph_name', $$
    MATCH ()-[e]->()
    RETURN type(e)
$$) as (type agtype);
Enter fullscreen mode Exit fullscreen mode

Results:

  type
---------
  knows
  likes
  works
Enter fullscreen mode Exit fullscreen mode

The query retrieves the edge types of all edges in the graph and returns the results as agtype strings.

5. properties()

The properties() function returns an agtype map containing all the properties of a vertex or edge. It takes an expression that returns a vertex, edge, or agtype map as an argument and returns an agtype map representing the properties.

Syntax: properties(element)

Consider the following query:

SELECT *
FROM cypher('graph_name', $$
    MATCH (a)
    RETURN properties(a)
$$) as (props agtype);
Enter fullscreen mode Exit fullscreen mode

Results:

                props
-------------------------------------
 {name: 'Alice', age: 30, city: 'NY'}
 {name: 'Bob', age: 25, city: 'LA'}
 {name: 'Charlie', age: 35, city: 'SF'}

Enter fullscreen mode Exit fullscreen mode

The query retrieves the properties of all vertices in the graph and returns the results as agtype maps.

6. head()

The head() function returns the first 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 first element of the list.

Syntax: head(list)

Consider the following query:

SELECT *
FROM cypher('graph_name', $$
   MATCH (a)
   WHERE a.name = 'Eskil'
   RETURN a.array, head(a.array)
$$) as (lst agtype, lst_head agtype);
Enter fullscreen mode Exit fullscreen mode

Results:

      lst      | lst_head
----------------+----------
 [1, 2, 3, 4]   | 1
 [5, 6, 7, 8]   | 5
 [9, 10, 11, 12]| 9
Enter fullscreen mode Exit fullscreen mode

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

These scalar functions provide powerful capabilities for extracting and manipulating data in Apache Age. By leveraging these functions, developers and data scientists can perform advanced graph operations and gain valuable insights from the graph structure.

Stay tuned for the next blog post, where we will explore more scalar functions in Apache Age!

Top comments (0)