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

From Dean Rasheed
Subject Re: array_length(anyarray)
Date
Msg-id CAEZATCWuf1y7e=JUiVxgG1cE3cJqxQBEmWJC641Vn_uXdva+jw@mail.gmail.com
Whole thread Raw
In response to Re: array_length(anyarray)  (Marko Tiikkaja <marko@joh.to>)
Responses Re: array_length(anyarray)
List pgsql-hackers
On 18 January 2014 03:07, Marko Tiikkaja <marko@joh.to> wrote:
> On 1/12/14, 5:53 AM, I wrote:
>>
>> On 1/9/14, 2:57 PM, Dean Rasheed wrote:
>>>
>>> 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].
>>
>>
>> Ignoring my proposal, this seems like the most reasonable option.  I'll
>> send an updated patch along these lines.
>
>
> 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. In fact why not
simply use ArrayGetNItems()?

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.

Regards,
Dean



pgsql-hackers by date:

Previous
From: David Rowley
Date:
Subject: Re: [PATCH] Negative Transition Aggregate Functions (WIP)
Next
From: Tom Lane
Date:
Subject: Re: Re: Patch to add support of "IF NOT EXISTS" to others "CREATE" statements