apache-ageopencypher

Is it possible to update the type of property in Apache AGE?


Suppose I have an Apache AGE graph database 'books' in which some vertices have the pages property stored in different types, as shown in the examples below:

SELECT * FROM cypher('books', $$      
CREATE (v:Book {title: 'A book', pages: 10})
RETURN v $$) as (v agtype);
                                                v                                                 
--------------------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Book", "properties": {"pages": 10, "title": "A book"}}::vertex
(1 row)

SELECT * FROM cypher('books', $$
CREATE (v:Book {title: 'Another book', pages: '10'})
RETURN v $$) as (v agtype);
                                                    v                                                     
----------------------------------------------------------------------------------------------------------
 {"id": 844424930131970, "label": "Book", "properties": {"pages": "10", "title": "Another book"}}::vertex
(1 row)

Is there a way to update all occurrences of pages of type string to type integer? In other words, is there a way to update the type of all properties of a particular type?


Solution

  • Yes, It is possible to change the data type of property values for vertices and edges.

    Integer To String:

    SELECT * FROM cypher('books', $$
      MATCH (v:Book) 
      SET v.pages = tostring(v.pages)
      RETURN v
    $$) AS (res agtype);
                                                    res                             
                        
    ----------------------------------------------------------------------------------------------------
     {"id": 844424930131969, "label": "Book", "properties": {"pages": "10", "title": "A book"}}::vertex
    

    String To Integer:

    SELECT * FROM cypher('books', $$
      MATCH (v:Book) 
      SET v.pages = (v.pages)::integer
      RETURN n
    $$) AS (res agtype);
                                                    res                             
                        
    --------------------------------------------------------------------------------------------------
     {"id": 844424930131969, "label": "Book", "properties": {"pages": 10, "title": "A book"}}::vertex
    (1 row)
    

    You can use typecasting for converting one data type to other using :: operator.

    Make sure you use (v.pages)::int rather than v.pages::int to avoid unexpected results because the :: has more precedence than . operator