Re: Support for negative index values in array fetching - Mailing list pgsql-hackers

From Pavel Stehule
Subject Re: Support for negative index values in array fetching
Date
Msg-id AANLkTikOYyi0U6BAkJzk2QioNJW2rqxijmNY0kaZOCz4@mail.gmail.com
Whole thread Raw
In response to Re: Support for negative index values in array fetching  (Florian Pflug <fgp@phlo.org>)
Responses Re: Support for negative index values in array fetching  (Florian Pflug <fgp@phlo.org>)
List pgsql-hackers
2011/1/5 Florian Pflug <fgp@phlo.org>:
> On Jan5, 2011, at 13:08 , Pavel Stehule wrote:
>> 2011/1/5 Florian Pflug <fgp@phlo.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).

Sorry, but It isn't too intuitive. Minimally for me. Why you don't
thinking about simple functions with only positive arguments. There
are only four combinations. I don't think we must have only one super
function.

we need functionality for:

a) get first n items
b) get items without last n items
c) get last n items
d) skip first n items

I think so this functionality is relative important, so we can use a
richer api.

Maybe we thinking about different use cases.

Pavel

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


pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: pg_upgrade patches applied
Next
From: Tom Lane
Date:
Subject: Re: pg_upgrade patches applied