On 19 December 2013 08:05, Pavel Stehule <pavel.steh...@gmail.com> wrote: > > > > 2013/12/19 David Fetter <da...@fetter.org> >> >> On Wed, Dec 18, 2013 at 09:27:54PM +0100, Marko Tiikkaja wrote: >> > Hi, >> > >> > Attached is a patch to add support for array_length(anyarray), which >> > only works for one-dimensional arrays, returns 0 for empty arrays >> > and complains if the array's lower bound isn't 1. In other words, >> > does the right thing when used with the arrays people use 99% of the >> > time. >> >> +1 for adding this. > > > +1 >
I think that having 2 functions called array_length() that each behave differently for empty arrays would just lead to confusion. The SQL standard defines a function called cardinality() that returns the number of elements of a collection (array or multiset), so why don't we call it that? > length should be irrelevant to fact so array starts from 1, 0 or anything > else Yes, this should just return the number of elements, and 0 for an empty array. How it should behave for multi-dimensional arrays is less clear, but I'd argue that it should return the total number of elements, i.e. cardinality('{{1,2},{3,4}}'::int[][]) = 4. That would make it consistent with the choices we've already made for unnest() and ordinality: - cardinality(foo) = (select count(*) from unnest(foo)). - unnest with ordinality would always result in ordinals in the range [1, cardinality]. Regards, Dean -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers