Re: Support for negative index values in array fetching

From: Florian Pflug <fgp(at)phlo(dot)org>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, "Valtonen, Hannu" <hannu(dot)valtonen(at)hut(dot)fi>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Support for negative index values in array fetching
Date: 2011-01-05 13:41:35
Message-ID: 61546526-1975-46E5-9563-0390B77C883C@phlo.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Jan5, 2011, at 13:08 , Pavel Stehule wrote:
> 2011/1/5 Florian Pflug <fgp(at)phlo(dot)org>:
>> On Jan5, 2011, at 10:25 , Peter Eisentraut wrote:
>>> On sön, 2011-01-02 at 12:47 +0100, Florian Pflug wrote:
>>>> The only way around that would be to introduce magic constants "lower", "upper" that
>>>> can be used within index expressions and evaluate to the indexed dimension's lower
>>>> and upper bound. You'd then use
>>>>
>>>> my_array[upper], my_array[upper-1], ...
>>>>
>>>> to refer to the last, second-to-last, ... element in the array. Actually doing this
>>>> could get pretty messy, though - not sure if it's really worth the effort...
>>>
>>> How about just some functions:
>>>
>>> array_first(array, dim)
>>> array_last(array, dim)
>>
>>
>> You image these to return the actual element, not the first and last index value, right?
>> Because we already have array_lower() and array_upper() which return the lower and upper
>> index bound for a certain dimension.
>> (http://www.postgresql.org/docs/9.0/interactive/functions-array.htm)
>>
>> A more general solution would be a function
>>
>> array_relative(array anyarray, indices int[])
>>
>
> I don't think so this design helps. instead maintaining a data array,
> you should to maintain a indices array.

How so? You'd still be able to get the last element by simply writing

array_relative(some_array, array[-1]).

Or, if we made the function variadic, by writing

array_relative(some_array, -1).

It's essentially what the OP proposed, but with the function array_relative() in place of
the indexing operator [].

best regards,
Florian Pflug

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Csaba Nagy 2011-01-05 13:43:06 Re: estimating # of distinct values
Previous Message Magnus Hagander 2011-01-05 13:35:32 Re: system views for walsender activity