Lists: | pgsql-hackers |
---|
From: | Aleksander Alekseev <aleksander(at)timescale(dot)com> |
---|---|
To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
Subject: | [PATCH] Add array_reverse() function |
Date: | 2024-10-21 09:06:29 |
Message-ID: | CAJ7c6TMpeO_ke+QGOaAx9xdJuxa7r=49-anMh3G5476e3CX1CA@mail.gmail.com |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Lists: | pgsql-hackers |
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?
[1]: http://postgr.es/m/4AEE80FC.7010608%40postnewspapers.com.au
[2]: https://wiki.postgresql.org/wiki/Array_reverse
[3]: https://www.postgresql.org/docs/current/functions-array.html
--
Best regards,
Aleksander Alekseev
Attachment | Content-Type | Size |
---|---|---|
v1-0001-Add-array_reverse-function.patch | application/octet-stream | 7.0 KB |
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: | Whole Thread | Raw Message | Download mbox | Resend email |
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
From: | "Joel Jacobson" <joel(at)compiler(dot)org> |
---|---|
To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
Subject: | Re: [PATCH] Add array_reverse() function |
Date: | 2024-10-21 14:37:52 |
Message-ID: | [email protected] |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Lists: | pgsql-hackers |
On Mon, Oct 21, 2024, at 11:06, Aleksander Alekseev 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].
+1
I've needed this many times.
/Joel
From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com> |
Cc: | Aleksander Alekseev <aleksander(at)timescale(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
Subject: | Re: [PATCH] Add array_reverse() function |
Date: | 2024-10-21 16:00:30 |
Message-ID: | [email protected] |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Lists: | pgsql-hackers |
Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com> writes:
> On Mon, Oct 21, 2024 at 2:36 PM Aleksander Alekseev
> <aleksander(at)timescale(dot)com> wrote:
> + /*
> + * 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.
I don't think that's really necessary. We have other functions that
will return an input value unchanged without copying it. A
longstanding example is array_larger. Also, this code looks to be
copied from array_shuffle.
regards, tom lane
From: | Aleksander Alekseev <aleksander(at)timescale(dot)com> |
---|---|
To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
Cc: | Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Subject: | Re: [PATCH] Add array_reverse() function |
Date: | 2024-10-22 13:27:30 |
Message-ID: | CAJ7c6TMuPDMp2KxORDLT9+2Ggu47ye0X_VGyGYB5_F3NM-kb4A@mail.gmail.com |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Lists: | pgsql-hackers |
Hi everyone,
Thanks for the feedback!
> > But it returns the input array as is. I think it should at least make
> > a new copy of input array.
>
> I don't think that's really necessary. We have other functions that
> will return an input value unchanged without copying it. A
> longstanding example is array_larger. Also, this code looks to be
> copied from array_shuffle.
It was indeed copied from array_shuffle().
Makes me wonder if we should have an utility function which would
contain common code for array_shuffle() and array_reverse().
Considering inconveniences such an approach caused in case of common
code for text and bytea types [1] I choose to copy the code and modify
it for the task.
> +
> + /* 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.
Agree. Here is the corrected patch.
--
Best regards,
Aleksander Alekseev
Attachment | Content-Type | Size |
---|---|---|
v2-0001-Add-array_reverse-function.patch | application/x-patch | 7.1 KB |
From: | Пополитов Владлен <v(dot)popolitov(at)postgrespro(dot)ru> |
---|---|
To: | "Aleksander Alekseev" <aleksander(at)timescale(dot)com> |
Cc: | "PostgreSQL Hackers" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, "Ashutosh Bapat" <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Subject: | Re: [PATCH] Add array_reverse() function |
Date: | 2024-10-30 19:11:20 |
Message-ID: | 1bd9d1-67228500-4f-2fa60e40@14545352 |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Lists: | pgsql-hackers |
On Tuesday, October 22, 2024 16:27 MSK, Aleksander Alekseev <aleksander(at)timescale(dot)com> wrote:
Hi everyone,
Thanks for the feedback!
> > But it returns the input array as is. I think it should at least make
> > a new copy of input array.
>
> I don't think that's really necessary. We have other functions that
> will return an input value unchanged without copying it. A
> longstanding example is array_larger. Also, this code looks to be
> copied from array_shuffle.
It was indeed copied from array_shuffle().
Makes me wonder if we should have an utility function which would
contain common code for array_shuffle() and array_reverse().
Considering inconveniences such an approach caused in case of common
code for text and bytea types [1] I choose to copy the code and modify
it for the task.
> +
> + /* 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.
Agree. Here is the corrected patch.
--
Best regards,
Aleksander Alekseev
Hi!
Content.
The proposed function waited long to be implemented and it will be very
useful. Personally I used slow workaround, when I needed the reverse of
the array.
Run.
This patch applies cleanly to HEAD. All regression tests pass
successfully against the patch.
Format.
The code formatted according to The Code Guidelines
Documentation.
The documentation is updated, the description of the function is added.
From my point of view, it would be better to mention, that function returns
updated array (does not updates it in place, as a reader can understand),
but other array returning functions in the documentation has the same
style (silently assume: reverse == return reversed array).
Conclusion.
+1 for commiter review
Best regards,
Vladlen Popolitov
postgrespro.com
--
Best regards,
Vladlen Popolitov.
From: | Michael Paquier <michael(at)paquier(dot)xyz> |
---|---|
To: | Пополитов Владлен <v(dot)popolitov(at)postgrespro(dot)ru> |
Cc: | Aleksander Alekseev <aleksander(at)timescale(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Subject: | Re: [PATCH] Add array_reverse() function |
Date: | 2024-10-31 02:10:09 |
Message-ID: | [email protected] |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Lists: | pgsql-hackers |
On Wed, Oct 30, 2024 at 10:11:20PM +0300, Пополитов Владлен wrote:
> Makes me wonder if we should have an utility function which would
> contain common code for array_shuffle() and array_reverse().
> Considering inconveniences such an approach caused in case of common
> code for text and bytea types [1] I choose to copy the code and modify
> it for the task.
We are talking about 20 lines copied over to reverse the order of
these elements that require their own inner for the element lookups
and manipulations. FWIW, I can live with this burden rather than
invent a routine that encapsulates both as this has also the
disadvantage of hiding more internals across more layers.
The abstraction with array_reverse_n() is unnecessary, but you have an
argument with the consistency and the shuffling inner routine.
Perhaps somebody will reuse that to invent a function that works on a
subset, where the comment on top array_reverse_n() about the type
caching still works. Anyway, no objections to what the patch defines
and does.
So it looks rather OK seen from here, as you are proposing.
--
Michael
From: | Vladlen Popolitov <v(dot)popolitov(at)postgrespro(dot)ru> |
---|---|
To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
Cc: | Aleksander Alekseev <afiskon(at)gmail(dot)com> |
Subject: | Re: [PATCH] Add array_reverse() function |
Date: | 2024-10-31 06:37:31 |
Message-ID: | 173035665129.1164.3833125953041709640.pgcf@coridan.postgresql.org |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Lists: | pgsql-hackers |
The following review has been posted through the commitfest application:
make installcheck-world: tested, failed
Implements feature: tested, failed
Spec compliant: tested, failed
Documentation: tested, failed
Content.
The proposed function waited long to be implemented and it will be very
useful. Personally I used slow workaround, when I needed the reverse of
the array.
Run.
This patch applies cleanly to HEAD. All regression tests pass
successfully against the patch.
Format.
The code formatted according to The Code Guidelines
Documentation.
The documentation is updated, the description of the function is added.
From my point of view, it would be better to mention, that function returns
updated array (does not updates it in place, as a reader can understand),
but other array returning functions in the documentation has the same
style (silently assume: reverse == return reversed array).
Conclusion.
+1 for commiter review
From: | Michael Paquier <michael(at)paquier(dot)xyz> |
---|---|
To: | Пополитов Владлен <v(dot)popolitov(at)postgrespro(dot)ru> |
Cc: | Aleksander Alekseev <aleksander(at)timescale(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Subject: | Re: [PATCH] Add array_reverse() function |
Date: | 2024-11-01 01:34:18 |
Message-ID: | [email protected] |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Lists: | pgsql-hackers |
On Thu, Oct 31, 2024 at 11:10:09AM +0900, Michael Paquier wrote:
> So it looks rather OK seen from here, as you are proposing.
After a second lookup, bumped CATALOG_VERSION_NO, then applied.
--
Michael