On Mon, Oct 21, 2024 at 2:36 PM Aleksander Alekseev <aleksan...@timescale.com> wrote: > > Hi, > > Recently I wanted to call array_reverse() and discovered that we still > don't have it. I'm not the first one who encountered this limitation. > array_reverse() was requested at least since 2009 [1] and the > workaround on PostgreSQL Wiki is dated 2010 [2]. > > The proposed patch adds this function. Only the first dimension of the > array is reversed, for consistency with the existing functions such as > array_shuffle() [3]. > > Examples: > > =# SELECT array_reverse(ARRAY[1,2,3,NULL]); > array_reverse > --------------- > {NULL,3,2,1} > > =# SELECT array_reverse(ARRAY[[1,2],[3,4],[5,6]]); > array_reverse > --------------------- > {{5,6},{3,4},{1,2}} > > Thoughts?
Looks useful. Glancing quickly at the code I have a comment + + /* If the target array is empty, exit fast */ + if (ndim < 1 || dims[0] < 1) + return construct_empty_array(elmtyp); This is taken care by the caller, at least the ndim < 1 case. + /* + * There is no point in reversing empty arrays or arrays with less than + * two items. + */ + if (ARR_NDIM(array) < 1 || ARR_DIMS(array)[0] < 2) + PG_RETURN_ARRAYTYPE_P(array); But it returns the input array as is. I think it should at least make a new copy of input array. -- Best Wishes, Ashutosh Bapat