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 08:12:50
Message-ID: CAEZATCWuf1y7e=JUiVxgG1cE3cJqxQBEmWJC641Vn_uXdva+jw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 18 January 2014 03:07, Marko Tiikkaja <marko(at)joh(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2014-01-19 08:22:10 Re: Re: Patch to add support of "IF NOT EXISTS" to others "CREATE" statements
Previous Message David Rowley 2014-01-19 08:01:30 Re: [PATCH] Negative Transition Aggregate Functions (WIP)