age icon indicating copy to clipboard operation
age copied to clipboard

Agtype array casting

Open ilyalasy opened this issue 4 years ago • 4 comments

Let's say I'm running following query from example:

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

When trying to cast to int[] following error occurs: ERROR: cannot cast type agtype to integer[] for column "lst"

Is it supposed to work, or there is another way to cast agtype array to postgres array?

ilyalasy avatar Aug 18 '21 07:08 ilyalasy

Hello there.

wonder if i exactly give what you just want to.

If you try just like this,

WITH result as (
SELECT *
FROM cypher('test', $$
WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
RETURN lst
$$) AS ( lst agtype ) 
)select 
string_to_array(ltrim(rtrim(array_to_string(array_agg(lst), ','), ']'),'['),',')::int[] from result;

You'll got this result

     string_to_array
--------------------------
 {0,1,2,3,4,5,6,7,8,9,10}
(1 row)

AGE support type casting restrictively. You can find in http://age.incubator.apache.org/docs/Apache_AGE_Guide.pdf which type can be casted.

Is this what you wanted? If not, just leave a comment plz.

seo-kw avatar Aug 19 '21 09:08 seo-kw

Hello there.

wonder if i exactly give what you just want to.

If you try just like this,

WITH result as (
SELECT *
FROM cypher('test', $$
WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
RETURN lst
$$) AS ( lst agtype ) 
)select 
string_to_array(ltrim(rtrim(array_to_string(array_agg(lst), ','), ']'),'['),',')::int[] from result;

You'll got this result

     string_to_array
--------------------------
 {0,1,2,3,4,5,6,7,8,9,10}
(1 row)

AGE support type casting restrictively. You can find in http://age.incubator.apache.org/docs/Apache_AGE_Guide.pdf which type can be casted.

Is this what you wanted? If not, just leave a comment plz.

Hello, yes the result is what I wanted, but the implementation seems like a duct tape, I think it should be possible to do that in a more obvious and intuitive way.

ilyalasy avatar Aug 19 '21 12:08 ilyalasy

yup. It seems like a duct tape. lol I'm agree with you.

As you seen in pdf above, Agtype can be casted to 3 ways varchar(char), bool, float.

So i just choosed string conversion ( like a duct tape )

WITH source as (
SELECT *
FROM cypher('test', $$
WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst
RETURN lst
$$) AS ( lst varchar ) 
)select string_to_array(substr(lst, 2, length(lst) - 2), ',')::int[] from source;

also result is same as above.

     string_to_array
--------------------------
 {0,1,2,3,4,5,6,7,8,9,10}
(1 row)

I cannnot help but it is still like a ducktape.

But, well, I think, maybe it will better than above

seo-kw avatar Aug 19 '21 14:08 seo-kw

Hi,

The patch mentioned in this issue solves the immediate problem of casting agtype to a 4 byte integer array. We should probably support type casting to a 2 byte and 8 byte integer array.

JoshInnis avatar Feb 08 '22 06:02 JoshInnis