Re: general purpose array_sort
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: Junwang Zhao <zhjwpku@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>, "David G. Johnston" <david.g.johnston@gmail.com>,
"andreas@proxel.se" <andreas@proxel.se>, Robert Haas <robertmhaas@gmail.com>, Amit Langote <amitlangote09@gmail.com>
Date: 2024-09-28T14:40:00Z
Lists: pgsql-hackers
Attachments
- array_sort_changes.no-cfbot (application/octet-stream)
On Sat, Sep 28, 2024 at 7:52 PM Junwang Zhao <zhjwpku@gmail.com> wrote:
>
> PFA v2, use COLLATE keyword to supply the collation suggested by
> Andreas offlist.
>
this is better. otherwise we need extra care to handle case like:
SELECT array_sort('{1,3,5,2,4,6}'::int[] COLLATE "pg_c_utf8");
+ <row>
+ <entry role="func_table_entry"><para role="func_signature">
+ <indexterm>
+ <primary>array_sort</primary>
+ </indexterm>
+ <function>array_sort</function> ( <type>anyarray</type>
<optional>, <parameter>dir</parameter> </optional>)
+ <returnvalue>anyarray</returnvalue>
+ </para>
+ <para>
+ Sorts the array in either ascending or descending order.
+ <parameter>dir</parameter> must be <literal>asc</literal>
+ or <literal>desc</literal>. The array must be empty or one-dimensional.
+ </para>
+ <para>
+ <literal>array_sort(ARRAY[1,2,5,6,3,4])</literal>
+ <returnvalue>{1,2,3,4,5,6}</returnvalue>
+ </para></entry>
+ </row>
I am confused with <parameter>dir</parameter>. I guess you want to say
"direction"
But here, I think <parameter>sort_asc</parameter> would be more appropriate?
<parameter>dir</parameter> can have only two potential values, make it
as a boolean would be more easier?
you didn't mention information: "by default, it will sort by
ascending order; the sort collation by default is using the array
element type's collation"
tuplesort_begin_datum can do null-first, null-last, so the
one-dimension array can allow null values.
Based on the above and others, I did some refactoring, feel free to take it.
my changes, changed the function signature, so you need to pay
attention to sql test file.
Commits
-
Introduce a SQL-callable function array_sort(anyarray).
- 6c12ae09f5a5 18.0 landed
-
Fix ARRAY_SUBLINK and ARRAY[] for int2vector and oidvector input.
- 4618045bee4a 18.0 cited
-
Re-implement the ereport() macro using __VA_ARGS__.
- e3a87b4991cc 13.0 cited