DEV Community

Talha Munir 🇵🇸
Talha Munir 🇵🇸

Posted on • Edited on

1

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more