Re: array_length(anyarray)

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Marko Tiikkaja <marko(at)joh(dot)to>
Cc: PostGreSql hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: array_length(anyarray)
Date: 2013-12-18 21:32:46
Message-ID: 52B2147E.9040501@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On 12/18/2013 04:19 PM, Marko Tiikkaja wrote:
> On 2013-12-18 22:13, Andrew Dunstan wrote:
>> On 12/18/2013 03:27 PM, Marko Tiikkaja wrote:
>>> Attached is a patch to add support for array_length(anyarray), which
>>> only works for one-dimensional arrays, returns 0 for empty arrays and
>>> complains if the array's lower bound isn't 1. In other words, does
>>> the right thing when used with the arrays people use 99% of the time.
>>
>> Why the heck would it complain if the lower bound isn't 1?
>
> Because then you're free to assume that the first element is [1] and
> the last one is [array_length()]. Which is what 99% of the code using
> array_length(anyarray, int) does anyway.
>
>
>

You're not really free to assume it - you'll need an exception handler
for the other-than-1 case, or your code might blow up.

This seems to be codifying a bad pattern, which should be using
array_lower() and array_upper() instead.

cheers

andrew

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Marko Tiikkaja 2013-12-18 21:38:38 Re: array_length(anyarray)
Previous Message Marko Tiikkaja 2013-12-18 21:31:12 Re: array_length(anyarray)