From the answer of the question How to use array_agg() for varchar[],
We can create a custom aggregate function to aggregate n-dimensional arrays in Postgres like:
CREATE AGGREGATE array_agg_mult (anyarray) (
SFUNC = array_cat
,STYPE = anyarray
,INITCOND = '{}'
);
A constrain is that the values have to share the same array extents and same length, handling empty values and different lengths doesn't work.
From the answer:
There is no way around that, the array type does not allow such a mismatch in Postgres. You could pad your arrays with NULL values so that all dimensions have matching extents.
I have the rows like
------ arrayfield
----- {1},
----- {},
----- {abc}
array_agg_mult(ARRAY[arrayfield]) AS customarray
I am expecting a aggregate resule like {{1},NULL,{abc}}
but it will throw
ERROR: cannot concatenate incompatible arrays
DETAIL: Arrays with differing element dimensions are not compatible for concatenation.
Is there any way that we can add the padding values in the custom function ?
I found the issue is when the array length is different. {a},{null},{1}
will aggregate, but {a,b},{},{1}
will not.
So I need a query where I can add NULL elements to existing arrays.
One solution is to append two NULL always (2 is the max length going to be in that filed) array_cat(arr, ARRAY[NULL,NULL])
and trim the array to length 2:
{1} --> {1,NULL,NULL} --> {1,NULL}
{NULL} --> {NULL,NULL,NULL} --> {NULL,NULL}
{abc, def} --> {abc,def,NULL,NULL} --> {abc, def}
But I can't figure out the syntax.