DEV Community

Talha Munir 🇵🇸
Talha Munir 🇵🇸

Posted on • Updated on

Composite Data types part 1

Composite Data types:

In this article we will discuss the composite datatypes in Apache age. Following are some of the composite data types in age with example queries.

List:

A literal list is created using brackets and separating the elements in the list with commas. A list can also hole null values, unlike when a null is an independent value, it will appear as the word null in a list.

Query:

The below query returns the list

SELECT *
FROM cypher('graph_name', $$
    WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
    RETURN lst
$$) AS (lst agtype);
Enter fullscreen mode Exit fullscreen mode

Using null in the query:
It will return the list containing null.

SELECT *
FROM cypher('graph_name', $$
    WITH [null] as lst
    RETURN lst
$$) AS (lst agtype);
Enter fullscreen mode Exit fullscreen mode

Accessing elements:

To access individual elements in the list, we use the square brackets again. This will extract from the start index and move up to but not till the end index.
The below query will return the 3rd element of the list.

SELECT *
FROM cypher('graph_name', $$
    WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
    RETURN lst[3]
$$) AS (element agtype);
Enter fullscreen mode Exit fullscreen mode

Map elements in the list:

You can also contains map elements in the list like below:

SELECT *
FROM cypher('graph_name', $$
   WITH [0, {status: 'teacher'}, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
    RETURN lst
$$) AS (map_value agtype);
Enter fullscreen mode Exit fullscreen mode

In order to access such map elements you can access them using the key attribute of that map. For example below code will output the value of status which is teacher here.

SELECT *
FROM cypher('graph_name', $$
   WITH [0, {status: 'teacher'}, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
    RETURN lst[1].status
$$) AS (map_value agtype);
Enter fullscreen mode Exit fullscreen mode

Negative index access:

You can use negative numbers as index which will start from the end of the list.

SELECT *
FROM cypher('graph_name', $$
    WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
    RETURN lst[-3]
$$) AS (element agtype);
Enter fullscreen mode Exit fullscreen mode

Above query will return the 3rd item from the end of the list i.e. 8

Index ranges:

You can also indicate ranges inside the brackets to return item in that range of the list.

SELECT *
FROM cypher('graph_name', $$
    WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
    RETURN lst[0..3]
$$) AS (element agtype);
Enter fullscreen mode Exit fullscreen mode

Above query will return elements at index 0, 1 and 2.

Negative index ranges:

You can also use negative ranges in the indexes. For example the below query will print the first 5 elements in the list.

SELECT *
FROM cypher('graph_name', $$
    WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
    RETURN lst[0..-5]
$$) AS (lst agtype);
Enter fullscreen mode Exit fullscreen mode

Positive Slices:

The positive slices can be used to obtain the number of elements in the list. The below query will print first 4 elements in the list.

SELECT *
FROM cypher('graph_name', $$
    WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
    RETURN lst[..4]
$$) AS (lst agtype);
Enter fullscreen mode Exit fullscreen mode

References:

You can follow for more content on age website and github:

  1. Apache age
  2. Apache age Github

Top comments (0)