Re: array_length(anyarray)

From: Marko Tiikkaja <marko(at)joh(dot)to>
To: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, Florian Pflug <fgp(at)phlo(dot)org>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, David Fetter <david(at)fetter(dot)org>, PostGreSql hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length(anyarray)
Date: 2014-01-10 09:36:43
Message-ID: 52CFBF2B.90605@joh.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 1/10/14, 9:04 AM, Dean Rasheed wrote:
> On 10 January 2014 00:36, Marko Tiikkaja <marko(at)joh(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Merlin Moncure 2014-01-10 09:41:34 Re: array_length(anyarray)
Previous Message David Rowley 2014-01-10 08:34:55 Re: [PATCH] Negative Transition Aggregate Functions (WIP)