Re: array_length(anyarray) - Mailing list pgsql-hackers

From Dean Rasheed
Subject Re: array_length(anyarray)
Date
Msg-id CAEZATCWEm6Ej8a7_Zu4XbtBq0kWrQvBhz=x5Fn+iB11+sAL96w@mail.gmail.com
Whole thread Raw
In response to Re: array_length(anyarray)  (Marko Tiikkaja <marko@joh.to>)
Responses Re: array_length(anyarray)  (Marko Tiikkaja <marko@joh.to>)
Re: array_length(anyarray)  (Merlin Moncure <mmoncure@gmail.com>)
List pgsql-hackers
On 10 January 2014 00:36, Marko Tiikkaja <marko@joh.to> wrote:
> On 1/10/14, 1:20 AM, Merlin Moncure wrote:
>>
>> I'm piling on: it's not clear at all to me why you've special cased
>> this to lower_bound=1.  First of all, there are other reasons to check
>> length than iteration.
>

Yes, I agree. A length function that returned 0 for empty arrays would
be far from useless.

>
> Can you point me to some examples?
>

The example I see all the time is code like

if array_length(nodes, 1) < 5 then   ... do something ...

then you realise (or not as the case may be) that this doesn't work
for empty arrays, and have to remember to wrap it in a coalesce call.

Simply being able to write

if cardinality(nodes) < 5 then  ... do something ...

is not just shorter, easier to type and easier to read, it is far less
likely to be the source of subtle bugs.

Regards,
Dean



pgsql-hackers by date:

Previous
From: Dean Rasheed
Date:
Subject: Re: WIP patch (v2) for updatable security barrier views
Next
From: David Rowley
Date:
Subject: Re: [PATCH] Negative Transition Aggregate Functions (WIP)