Thread: array_reverse()
Hi all Before I go ahead and try to write a decent quality version: is there any chance an array_reverse() function (in C) would be accepted into Pg mainline? If not, I probably won't bother cleaning up my current awful-hack quality module. It's a slow operation to perform in SQL or PL/PgSQL even with 8.4's array_agg(...) and generate_subscripts(...) and given how many languages seem to provide built-in support it seems to be an operation people need quite a bit. -- Craig Ringer
On Mon, Nov 2, 2009 at 1:49 AM, Craig Ringer <craig@postnewspapers.com.au> wrote: > > Hi all > > Before I go ahead and try to write a decent quality version: is there > any chance an array_reverse() function (in C) would be accepted into Pg > mainline? If not, I probably won't bother cleaning up my current > awful-hack quality module. > > It's a slow operation to perform in SQL or PL/PgSQL even with 8.4's > array_agg(...) and generate_subscripts(...) and given how many languages > seem to provide built-in support it seems to be an operation people need > quite a bit. IMO, we should have both that and a string reverse() function. I just bumped into the same problem yesterday and was surprised we didn't have it already. merlin
Craig Ringer <craig@postnewspapers.com.au> writes: > Before I go ahead and try to write a decent quality version: is there > any chance an array_reverse() function (in C) would be accepted into Pg > mainline? What would it mean for a multi-dimensional array? regards, tom lane
On Mon, Nov 2, 2009 at 9:20 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote: > Craig Ringer <craig@postnewspapers.com.au> writes: >> Before I go ahead and try to write a decent quality version: is there >> any chance an array_reverse() function (in C) would be accepted into Pg >> mainline? > > What would it mean for a multi-dimensional array? Couple of possibilities?: *) Could raise an error for ndim != 1 *) could reverse top dimension only only (I like this the best), so that: reverse(array[array[1,2], array[3,4]]) -> {{3,4},{1,2}}; *) maybe overload the above so that: reverse(array[array[1,2], array[3,4]], array[1]) -> {{2,1},{3,4}}; reverse(array[array[1,2], array[3,4]], array[2]) -> {{1,2},{4,3}}; The second term would be int array of 1 to ndim -1 dimensions, so you can re-arrange an array slice without restacking it. merlin
On Mon, Nov 02, 2009 at 09:20:38AM -0500, Tom Lane wrote: > Craig Ringer <craig@postnewspapers.com.au> writes: > > Before I go ahead and try to write a decent quality version: is there > > any chance an array_reverse() function (in C) would be accepted into Pg > > mainline? > > What would it mean for a multi-dimensional array? Rotating the array by 180 degrees in every dimension would give the same answer for 1-dimensional arrays and give sensible answers for higher dimensional arrays. For example: 1,2,3 == 3,2,1 [1 2 3 [9 8 7 4 5 6 == 6 5 4 7 8 9] 3 2 1] I think higher dimensionality would work, it's just a bit fiddly to draw. -- Sam http://samason.me.uk/
On Mon, Nov 2, 2009 at 9:51 AM, Sam Mason <sam@samason.me.uk> wrote: > On Mon, Nov 02, 2009 at 09:20:38AM -0500, Tom Lane wrote: >> Craig Ringer <craig@postnewspapers.com.au> writes: >> > Before I go ahead and try to write a decent quality version: is there >> > any chance an array_reverse() function (in C) would be accepted into Pg >> > mainline? >> >> What would it mean for a multi-dimensional array? > > Rotating the array by 180 degrees in every dimension would give the same > answer for 1-dimensional arrays and give sensible answers for higher > dimensional arrays. For example: > > 1,2,3 == 3,2,1 > > [1 2 3 [9 8 7 > 4 5 6 == 6 5 4 > 7 8 9] 3 2 1] > > I think higher dimensionality would work, it's just a bit fiddly to > draw. do you think that's the typical case, or is it more common to want to reverse a particular slice? (I like your idea actually, because if you need a slice you can slice notation one out...better than what I posted above). merlin
Sam Mason <sam@samason.me.uk> writes: > On Mon, Nov 02, 2009 at 09:20:38AM -0500, Tom Lane wrote: >> What would it mean for a multi-dimensional array? > Rotating the array by 180 degrees in every dimension would give the same > answer for 1-dimensional arrays and give sensible answers for higher > dimensional arrays. The easy implementation at the C level would be to reverse the storage order of the elements (and then do something-or-other with the dimension info, in case the array isn't square). Your diagram suggests that this might be the same thing as what you suggest, but I can't work it out for lack of caffeine. regards, tom lane
On Mon, Nov 02, 2009 at 09:55:20AM -0500, Merlin Moncure wrote: > On Mon, Nov 2, 2009 at 9:51 AM, Sam Mason <sam@samason.me.uk> wrote: > > On Mon, Nov 02, 2009 at 09:20:38AM -0500, Tom Lane wrote: > >> Craig Ringer <craig@postnewspapers.com.au> writes: > >> > Before I go ahead and try to write a decent quality version: is there > >> > any chance an array_reverse() function (in C) would be accepted into Pg > >> > mainline? > >> > >> What would it mean for a multi-dimensional array? > > > > Rotating the array by 180 degrees in every dimension would give the same > > answer for 1-dimensional arrays and give sensible answers for higher > > dimensional arrays. > do you think that's the typical case, or is it more common to want to > reverse a particular slice? I hadn't thought about that; I'm normally more concerned about making the general case (i.e. most complicated) behave sensibly, with the common cases being optimizations. The problem I was solving was making 1d arrays consistent with higher dimensional ones. As far as I can tell, if the spec is just to rotate by 180 degrees then the implementation is pretty easy; just run through all the elements writing them out in reverse order. Sizes and number of dimensions can be completely ignored. -- Sam http://samason.me.uk/
On Mon, Nov 02, 2009 at 10:03:49AM -0500, Tom Lane wrote: > Sam Mason <sam@samason.me.uk> writes: > > Rotating the array by 180 degrees in every dimension would give the same > > answer for 1-dimensional arrays and give sensible answers for higher > > dimensional arrays. > > The easy implementation at the C level would be to reverse the storage > order of the elements (and then do something-or-other with the dimension > info, in case the array isn't square). Your diagram suggests that this > might be the same thing as what you suggest, but I can't work it out for > lack of caffeine. Yes, it is. I hadn't worked out the code before. Implementation should be very easy at C level. Not sure how NULLs are implemented in arrays, they may require a bit more care. -- Sam http://samason.me.uk/