Re: [PATCH] Add array_reverse() function

From: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
To: Aleksander Alekseev <aleksander(at)timescale(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Add array_reverse() function
Date: 2024-10-21 14:11:44
Message-ID: CAExHW5vMAHAdcJvAPNhopH_jf3PTZVtFysKr4ix+KvbD9GUD3Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Oct 21, 2024 at 2:36 PM Aleksander Alekseev
<aleksander(at)timescale(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2024-10-21 14:25:18 Re: ECPG Refactor: move sqlca variable in ecpg_log()
Previous Message David G. Johnston 2024-10-21 13:55:13 Re: Question about VACUUM behavior with sub-transactions in stored procedures