4

Assuming I have a customer graph in apache AGE, and I make the following query:

    SELECT *
    FROM cypher("customers",
    $$
        MATCH (p:person)
        RETURN p.first_name, p.last_name
    $$
    ) as (first_name agtype, last_name agtype)

I get the following result:

     first_name  | last_name 
    -------------+-----------
     John        | Doe
     Matthew     | Martinez
     Melissa     | Moore

How do I determine the data type of these 2 columns? I know that internally AGE keeps track of different types (like int, float etc), but to postgres these columns are all of type 'agtype'.

Looking at the above example, I already know that first_name and last_name are VARCHAR. So if I wanted to CAST first_name from 'agtype' to VARCHAR I could, but is there any way to determine this without having to guess the type?

For example, if a query from AGE returns a column zip_code, and the zip_code property is stored internally in AGE as an INT, how would I CAST agtype zip_code to the correct type instead of having to guess between INT and VARCHAR?

moeed865
  • 304
  • 1
  • 6

6 Answers6

0

It doesn't seem like there is an existing function that outputs the datatype as of now; however, you can find a workaround such as:

SELECT *
FROM cypher('customers', $$
    MATCH (p:person)
    RETURN p.first_name, toInteger(p.first_name) IS NULL
$$) AS (first_name agtype, is_string agtype);

Which results in the output:

first_name | is_string
------------+-----------
 "John"     | true
 "Matthew"  | true
 "Melissa"  | true

This should work if you are just trying to differentiate between an integer and something else (for example a varchar), which can be implemented in the zip code example you have given.

Ken W.
  • 397
  • 1
  • 13
0

The agtype data type in Apache AGE is an internal data representation used by the engine. It encapsulates various data types within a single data structure, similar to JSON. However, from the perspective of the PostgreSQL interface in Apache AGE, the agtype data type appears as a single type, and the underlying data types are not directly accessible or exposed.

In the Apache AGE source code, the handling and interpretation of the agtype data type are implemented internally within the engine. The agtype values are processed and manipulated using custom logic specific to Apache AGE. The actual implementation details of the agtype data type are located in various parts of the source code, including components related to query execution, data storage, and serialization.

If you are interested in exploring the Apache AGE source code, you can find it on the official Apache AGE GitHub repository: https://github.com/apache/age

0

Agtype datatype here in apache age represents complex structures. You can use agetype_typeof function to determine the datatype of the specific column. Below is its example:

SELECT
    agtype_typeof(first_name) AS firstnametype,
    agtype_typeof(last_name) AS lastnametype
FROM cypher("customers",
    $$
        MATCH (c:customer)
        RETURN c.first_name, c.last_name
    $$
) as (first_name agtype, last_name agtype);

In the above query agetype_typeof will return the datatype of agetype value. In above case it would return string for varchar values.

Talha Munir
  • 121
  • 5
0

Currently there is no direct function to get the datatype on its own but you can use the following cypher queries:

SELECT * FROM cypher('MATCH (n) RETURN n.name') AS result;

You can skip the as result part as it just provides an alias to the results of the query. This will return the 'name' property of all nodes in the graph. For examples' sake you can use age or date to get something that will not be a string.

SELECT column_name, typname FROM graph_result_metadata('type') AS meta(column_name text, typname text);

Then you can use this query to create a column result which will return the datatypes. The query above will give you a column with the property and its datatype.

The datatypes usually used are boolean, string, integer, float.

Shanzay
  • 19
  • 3
0

The agtype data type utilized in Apache AGE functions as an internal data representation within the engine.

This is an example that could be beneficial in your case:

SELECT
agtype_typeof(name_agtype) AS name_datatype,
FROM (
SELECT
    name::agtype AS name_agtype,

FROM cypher("people",
    $$
        MATCH (p:person)
        RETURN p.name
    $$
  )
) AS result;

I hope this will help you. Good luck!

-1

Try using apoc.meta.type feature.

An example,

CREATE (p:Person { first_name: 'John', last_name: 'Doe' }) RETURN apoc.meta.types(properties(p))

Try using this method with your queries by wrapping them around this.