Re: array_length(anyarray)

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Marko Tiikkaja <marko(at)joh(dot)to>
Cc: 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-19 13:12:10
Message-ID: CAEZATCXoYtskFPAs+f7nf4RfQSHNc0=e9dN-mnKy0iAmJB5g+Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 19 January 2014 11:43, Marko Tiikkaja <marko(at)joh(dot)to> wrote:
>
>
> On 1/19/14, 9:12 AM, Dean Rasheed wrote:
>>
>> On 18 January 2014 03:07, Marko Tiikkaja <marko(at)joh(dot)to> wrote:
>>>
>>> Here's the patch as promised. Thoughts?
>>>
>>
>> A couple of points:
>>
>> The answer for empty (zero dimensional) arrays is wrong --- you need
>> special case handling for this case to return 0.
>
>
> How embarrassing. I don't know why I removed that check or how I didn't
> catch the clearly wrong answer in the test output.
>
>
>> In fact why not
>> simply use ArrayGetNItems()?
>
>
> Even better. Changed.
>
>
>> In the docs, in the table of array functions, I think it would
>> probably be useful to make the entry for array_length say "see also
>> cardinality", otherwise people might just stop reading there. I
>> suspect that in over 90% of cases, cardinality will be the more
>> appropriate function to use rather than array_length.
>
>
> I don't see this as a huge improvement, but even worse, I don't see a way to
> naturally fit it into the description.
>

Hmm. Looking at that page in the docs, it currently doesn't even
mention that array_length returns NULL for empty arrays, or more
generally for arrays that don't have the requested dimension. To
someone unfamiliar with postgresql arrays, that could lead to a nasty
surprise.

How about having the array_length docs say something like

returns the length of the requested array dimension, or NULL if the
array is empty or does not have the requested dimension. To get the
total number of array elements across all dimensions, use
<function>cardinality</>.

If we did that, we should probably also add the "or NULL if the array
is empty or does not have the requested dimension" clause to the
array_upper and array_lower docs, and "or NULL if the array is empty"
to the array_dims and array_ndims docs.

That might seem overly pedantic, but it's quite annoying when API
documentation doesn't fully specify the behaviour, and you're forced
to use trial-and-error to find out how the functions behave.

Regards,
Dean

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Marko Tiikkaja 2014-01-19 13:24:18 Re: array_length(anyarray)
Previous Message Magnus Hagander 2014-01-19 12:27:38 Re: Feature request: Logging SSL connections