skip to Main Content

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?

2

Answers


  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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search