Re: array_length(anyarray)

Поиск
Список
Период
Сортировка
От Merlin Moncure
Тема Re: array_length(anyarray)
Дата
Msg-id CAHyXU0xmqirCzkXDFg2T-5maFtkrqFbsByZzwZNkCpkqAt_RWw@mail.gmail.com
обсуждение исходный текст
Ответ на Re: array_length(anyarray)  (Dean Rasheed <dean.a.rasheed@gmail.com>)
Ответы Re: array_length(anyarray)  (Marko Tiikkaja <marko@joh.to>)
Список pgsql-hackers
On Fri, Jan 10, 2014 at 2:04 AM, Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
> On 10 January 2014 00:36, Marko Tiikkaja <marko@joh.to> wrote:
>> On 1/10/14, 1:20 AM, Merlin Moncure wrote:
>>>
>>> I'm piling on: it's not clear at all to me why you've special cased
>>> this to lower_bound=1.  First of all, there are other reasons to check
>>> length than iteration.
>>
>
> Yes, I agree. A length function that returned 0 for empty arrays would
> be far from useless.
>
>>
>> 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.

right -- exactly.  or, 'ORDER BY cardinatility(nodes)', etc etc.
Furthermore, we already have pretty good support for iteration with
arrays via unnest().  What's needed for better iteration support (IMO)
is a function that does what unnest does but returns an array on
indexes (one per dimsension) -- a generalization of the
_pg_expandarray function.  Lets' say 'unnest_dims'.  'unnest_dims' is
non-trivial to code in user land while 'array_length' is an extremely
trivial wrapper to array_upper().

cardinality() (which is much better name for the function IMSNHO)
gives a*b*c values say for a 3d array also does something non-trivial
*particularly in the case of offset arrays*.

On Fri, Jan 10, 2014 at 3:36 AM, Marko Tiikkaja <marko@joh.to> wrote:
> I guess what I truly want is a less generic type that's like an array, but always one-dimensional with a lower bound
of1.
 

Your function would be the only one in the array API that implemented
special behaviors like that.  That's suggests to me that the less
generic function belongs in user land, not in the core array API.

merlin



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Marko Tiikkaja
Дата:
Сообщение: Re: array_length(anyarray)
Следующее
От: Dean Rasheed
Дата:
Сообщение: Re: [PATCH] Negative Transition Aggregate Functions (WIP)