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

From Marko Tiikkaja
Subject Re: array_length(anyarray)
Date
Msg-id 52CFBF2B.90605@joh.to
Whole thread Raw
In response to Re: array_length(anyarray)  (Dean Rasheed <dean.a.rasheed@gmail.com>)
List pgsql-hackers
On 1/10/14, 9:04 AM, Dean Rasheed wrote:
> On 10 January 2014 00:36, Marko Tiikkaja <marko@joh.to> wrote:
>>
>> 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

But this is what I don't understand: why do you care whether there's 
less than 5 elements in the array, but you don't care about how they're 
organized?  '[2:3]={1,2}'::int[] and '{{1},{2}}'::int[] both give the 
same result when unnest()ed, sure, but why do you want to accept such 
crap as input if you just want a list of elements?

I guess what I truly want is a less generic type that's like an array, 
but always one-dimensional with a lower bound of 1.  There's too much 
garbage that can be passed to a function taking an array as an input 
right now.


Regards,
Marko Tiikkaja



pgsql-hackers by date:

Previous
From: David Rowley
Date:
Subject: Re: [PATCH] Negative Transition Aggregate Functions (WIP)
Next
From: Merlin Moncure
Date:
Subject: Re: array_length(anyarray)