Re: Support for negative index values in array fetching

Поиск
Список
Период
Сортировка
От Florian Pflug
Тема Re: Support for negative index values in array fetching
Дата
Msg-id BEE34D42-CF41-48A9-9359-031B689CE01B@phlo.org
обсуждение исходный текст
Ответ на Re: Support for negative index values in array fetching  (Peter Eisentraut <peter_e@gmx.net>)
Ответы Re: Support for negative index values in array fetching  (Pavel Stehule <pavel.stehule@gmail.com>)
Список pgsql-hackers
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[])

which would return the element indexed by <indices>, where positive indices are assumed to
be relative to the respective dimension's lower bound and negative indices to the
upper bound + 1.

For slices, we could additionally have

array_relative(array anyarray, indices_start int[], indices_end int[])

best regards,
Florian Pflug






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

Предыдущее
От: Pavel Stehule
Дата:
Сообщение: Re: Support for negative index values in array fetching
Следующее
От: Itagaki Takahiro
Дата:
Сообщение: Re: SQL/MED - core functionality