Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit01f7808

Browse files
committed
Add a cardinality function for arrays.
Unlike our other array functions, this considers the total number ofelements across all dimensions, and returns 0 rather than NULL when thearray has no elements. But it seems that both of those behaviors arealmost universally disliked, so hopefully that's OK.Marko Tiikkaja, reviewed by Dean Rasheed and Pavel Stehule
1 parent033b234 commit01f7808

File tree

8 files changed

+93
-1
lines changed

8 files changed

+93
-1
lines changed

‎doc/src/sgml/array.sgml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,19 @@ SELECT array_length(schedule, 1) FROM sal_emp WHERE name = 'Carol';
337337
--------------
338338
2
339339
(1 row)
340+
</programlisting>
341+
342+
<function>cardinality</function> returns the total number of elements in an
343+
array across all dimensions. It is effectively the number of rows a call to
344+
<function>unnest</function> would yield:
345+
346+
<programlisting>
347+
SELECT cardinality(schedule) FROM sal_emp WHERE name = 'Carol';
348+
349+
cardinality
350+
-------------
351+
4
352+
(1 row)
340353
</programlisting>
341354
</para>
342355
</sect2>

‎doc/src/sgml/func.sgml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11008,6 +11008,9 @@ SELECT NULLIF(value, '(none)') ...
1100811008
<indexterm>
1100911009
<primary>array_upper</primary>
1101011010
</indexterm>
11011+
<indexterm>
11012+
<primary>cardinality</primary>
11013+
</indexterm>
1101111014
<indexterm>
1101211015
<primary>string_to_array</primary>
1101311016
</indexterm>
@@ -11164,6 +11167,17 @@ SELECT NULLIF(value, '(none)') ...
1116411167
<entry><literal>array_upper(ARRAY[1,8,3,7], 1)</literal></entry>
1116511168
<entry><literal>4</literal></entry>
1116611169
</row>
11170+
<row>
11171+
<entry>
11172+
<literal>
11173+
<function>cardinality</function>(<type>anyarray</type>)
11174+
</literal>
11175+
</entry>
11176+
<entry><type>int</type></entry>
11177+
<entry>returns the total number of elements in the array, or 0 if the array is empty</entry>
11178+
<entry><literal>cardinality(ARRAY[[1,2],[3,4]])</literal></entry>
11179+
<entry><literal>4</literal></entry>
11180+
</row>
1116711181
<row>
1116811182
<entry>
1116911183
<literal>

‎src/backend/utils/adt/arrayfuncs.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,18 @@ array_length(PG_FUNCTION_ARGS)
17391739
PG_RETURN_INT32(result);
17401740
}
17411741

1742+
/*
1743+
* array_cardinality:
1744+
*returns the total number of elements in an array
1745+
*/
1746+
Datum
1747+
array_cardinality(PG_FUNCTION_ARGS)
1748+
{
1749+
ArrayType*v=PG_GETARG_ARRAYTYPE_P(0);
1750+
PG_RETURN_INT32(ArrayGetNItems(ARR_NDIM(v),ARR_DIMS(v)));
1751+
}
1752+
1753+
17421754
/*
17431755
* array_ref :
17441756
* This routine takes an array pointer and a subscript array and returns

‎src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO201312231
56+
#defineCATALOG_VERSION_NO201401211
5757

5858
#endif

‎src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ DATA(insert OID = 2092 ( array_upper PGNSP PGUID 12 1 0 0 0 f f f f t f i 2
840840
DESCR("array upper dimension");
841841
DATA(insertOID=2176 (array_lengthPGNSPPGUID121000fffftfi2023"2277 23"_null__null__null__null_array_length_null__null__null_ ));
842842
DESCR("array length");
843+
DATA(insertOID=3179 (cardinalityPGNSPPGUID121000fffftfi1023"2277"_null__null__null__null_array_cardinality_null__null__null_ ));
844+
DESCR("array cardinality");
843845
DATA(insertOID=378 (array_appendPGNSPPGUID121000ffffffi202277"2277 2283"_null__null__null__null_array_push_null__null__null_ ));
844846
DESCR("append element onto end of array");
845847
DATA(insertOID=379 (array_prependPGNSPPGUID121000ffffffi202277"2283 2277"_null__null__null__null_array_push_null__null__null_ ));

‎src/include/utils/array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ extern Datum array_dims(PG_FUNCTION_ARGS);
204204
externDatumarray_lower(PG_FUNCTION_ARGS);
205205
externDatumarray_upper(PG_FUNCTION_ARGS);
206206
externDatumarray_length(PG_FUNCTION_ARGS);
207+
externDatumarray_cardinality(PG_FUNCTION_ARGS);
207208
externDatumarray_larger(PG_FUNCTION_ARGS);
208209
externDatumarray_smaller(PG_FUNCTION_ARGS);
209210
externDatumgenerate_subscripts(PG_FUNCTION_ARGS);

‎src/test/regress/expected/arrays.out

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,48 @@ select array_length(array[[1,2,3], [4,5,6]], 3);
14551455

14561456
(1 row)
14571457

1458+
select cardinality(NULL::int[]);
1459+
cardinality
1460+
-------------
1461+
1462+
(1 row)
1463+
1464+
select cardinality('{}'::int[]);
1465+
cardinality
1466+
-------------
1467+
0
1468+
(1 row)
1469+
1470+
select cardinality(array[1,2,3]);
1471+
cardinality
1472+
-------------
1473+
3
1474+
(1 row)
1475+
1476+
select cardinality('[2:4]={5,6,7}'::int[]);
1477+
cardinality
1478+
-------------
1479+
3
1480+
(1 row)
1481+
1482+
select cardinality('{{1,2}}'::int[]);
1483+
cardinality
1484+
-------------
1485+
2
1486+
(1 row)
1487+
1488+
select cardinality('{{1,2},{3,4},{5,6}}'::int[]);
1489+
cardinality
1490+
-------------
1491+
6
1492+
(1 row)
1493+
1494+
select cardinality('{{{1}},{{2,3},{3,4}}}'::int[]);
1495+
cardinality
1496+
-------------
1497+
8
1498+
(1 row)
1499+
14581500
select array_agg(unique1) from (select unique1 from tenk1 where unique1 < 15 order by unique1) ss;
14591501
array_agg
14601502
--------------------------------------

‎src/test/regress/sql/arrays.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@ select array_length(array[[1,2,3], [4,5,6]], 1);
419419
select array_length(array[[1,2,3], [4,5,6]],2);
420420
select array_length(array[[1,2,3], [4,5,6]],3);
421421

422+
select cardinality(NULL::int[]);
423+
select cardinality('{}'::int[]);
424+
select cardinality(array[1,2,3]);
425+
select cardinality('[2:4]={5,6,7}'::int[]);
426+
select cardinality('{{1,2}}'::int[]);
427+
select cardinality('{{1,2},{3,4},{5,6}}'::int[]);
428+
select cardinality('{{{1}},{{2,3},{3,4}}}'::int[]);
429+
422430
select array_agg(unique1)from (select unique1from tenk1where unique1<15order by unique1) ss;
423431
select array_agg(ten)from (select tenfrom tenk1where unique1<15order by unique1) ss;
424432
select array_agg(nullif(ten,4))from (select tenfrom tenk1where unique1<15order by unique1) ss;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp