Best alternative for array_length function?

1,517 views
Skip to first unread message

Keaton Adams

unread,
Jan 5, 2017, 5:10:51 PM1/5/17
to Greenplum Users
So no array_length function appears to be available in Greenplum.  I checked the docs and do not see it in the list.  Does anyone have a good, readily available alternative?

gpadmin=# SELECT array_length(array[1,2,3], 1);
ERROR:  function array_length(integer[], integer) does not exist


This is what I have come up with so far:

gpadmin=# select array[1,2,3];
  array
---------
 {1,2,3}
(1 row)

gpadmin=# select array_to_string(array[1,2,3],'');
 array_to_string
-----------------
 123
(1 row)

gpadmin=# select length(array_to_string(array[1,2,3],''));
 length
--------
      3
(1 row)

gpadmin=# select length(array_to_string(array[1,2,4,6],''));
 length
--------
      4
(1 row)


Thanks.



Louis Mugnano

unread,
Jan 5, 2017, 6:01:08 PM1/5/17
to Keaton Adams, Greenplum Users
You can use array_upper to get the length of the array.  Lousy name but that is what it does:

gpadmin=# SELECT array_upper(array[1,2,3,5,10],1);

 array_upper 
-------------
           5


--
You received this message because you are subscribed to the Google Groups "Greenplum Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gpdb-users+unsubscribe@greenplum.org.
To post to this group, send email to gpdb-...@greenplum.org.
Visit this group at https://groups.google.com/a/greenplum.org/group/gpdb-users/.
For more options, visit https://groups.google.com/a/greenplum.org/d/optout.

Keaton Adams

unread,
Jan 5, 2017, 6:42:21 PM1/5/17
to Greenplum Users
That is interesting. So "upper" is the "upper bound" of the array dimension. Okay.


array_upper(anyarray, int) int returns upper bound of the requested array dimension array_upper(ARRAY[1,2,3,4], 1) 4


gpadmin=# SELECT array_upper(array[1,2,3,5,10],1);
 array_upper
-------------
           5
(1 row)

gpadmin=# select array_upper(array['a','b','c','d'],1);
 array_upper
-------------
           4
(1 row)


Thanks again.
Reply all
Reply to author
Forward
0 new messages