1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Introduce a SQL-callable function array_sort(anyarray).

Create a function that will sort the elements of an array
according to the element type's sort order.  If the array
has more than one dimension, the sub-arrays of the first
dimension are sorted per normal array-comparison rules,
leaving their contents alone.

In support of this, add pg_type.typarray to the set of fields
cached by the typcache.

Author: Junwang Zhao <zhjwpku@gmail.com>
Co-authored-by: Jian He <jian.universality@gmail.com>
Reviewed-by: Aleksander Alekseev <aleksander@timescale.com>
Discussion: https://postgr.es/m/CAEG8a3J41a4dpw_-F94fF-JPRXYxw-GfsgoGotKcjs9LVfEEvw@mail.gmail.com
This commit is contained in:
Tom Lane
2025-04-01 18:03:55 -04:00
parent 6da2ba1d8a
commit 6c12ae09f5
11 changed files with 426 additions and 1 deletions

View File

@ -20741,6 +20741,42 @@ SELECT NULLIF(value, '(none)') ...
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>array_sort</primary>
</indexterm>
<function>array_sort</function> (
<parameter>array</parameter> <type>anyarray</type>
<optional>, <parameter>descending</parameter> <type>boolean</type>
<optional>, <parameter>nulls_first</parameter> <type>boolean</type>
</optional></optional> )
<returnvalue>anyarray</returnvalue>
</para>
<para>
Sorts the first dimension of the array.
The sort order is determined by the default sort ordering of the
array's element type; however, if the element type is collatable,
the collation to use can be specified by adding
a <literal>COLLATE</literal> clause to
the <parameter>array</parameter> argument.
</para>
<para>
If <parameter>descending</parameter> is true then sort in
descending order, otherwise ascending order. If omitted, the
default is ascending order.
If <parameter>nulls_first</parameter> is true then nulls appear
before non-null values, otherwise nulls appear after non-null
values.
If omitted, <parameter>nulls_first</parameter> is taken to have
the same value as <parameter>descending</parameter>.
</para>
<para>
<literal>array_sort(ARRAY[[2,4],[2,1],[6,5]])</literal>
<returnvalue>{{2,1},{2,4},{6,5}}</returnvalue>
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm id="function-array-to-string">