Re: Support for negative index values in array fetching

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Florian Pflug <fgp(at)phlo(dot)org>, "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 10:27:33
Message-ID: AANLkTi=mEpOmXsF3sZBd_RU7qWR+ZDqpCZ1ijoHeQbQQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello

2011/1/5 Peter Eisentraut <peter_e(at)gmx(dot)net>:
> On sön, 2011-01-02 at 12:47 +0100, Florian Pflug wrote:
>> > Here's a patch to add support for negative index values in fetching elements from an array.
>>

negative arguments for array can be really strange

>> That won't work. In SQL, array indices don't necessarily start with 0 (or 1, or *any*
>> single value).
>
> FYI, this is true for PostgreSQL, but not in SQL in general.  In the
> standard, array indexes go from 1 to N.
>
>> 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)
>
> Perhaps some variants for splice vs. scalar.

Itakagi has a function trim_array in
http://archives.postgresql.org/message-id/AANLkTinrRubdSSWvqO481sL0EyGz830=mFKAdK_knfgZ@mail.gmail.com
patch. It's similar to array_first.

I understand to a missing functionality for FIFO or LIFO queues
implementation based on array. There can be function that reduce a
array to first or last n items, and functions that returns first or
last items.

some like array_first(array, items), array_last(array, items),
array_remove_first(array, items), array_remove_last(array, items)

or some similar

Pavel

>
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Florian Pflug 2011-01-05 11:28:23 Re: Support for negative index values in array fetching
Previous Message Shigeru HANADA 2011-01-05 10:24:18 Re: SQL/MED - core functionality