Skip to content

Commit 254aecb

Browse files
committed
ADD array_ndims function
Author: Robert Haas <robertmhaas@gmail.com>
1 parent 9beb9e7 commit 254aecb

File tree

7 files changed

+47
-6
lines changed

7 files changed

+47
-6
lines changed

doc/src/sgml/func.sgml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.454 2008/11/04 00:59:45 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.455 2008/11/04 14:49:11 petere Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -9373,6 +9373,17 @@ SELECT NULLIF(value, '(none)') ...
93739373
<entry><literal>array_cat(ARRAY[1,2,3], ARRAY[4,5])</literal></entry>
93749374
<entry><literal>{1,2,3,4,5}</literal></entry>
93759375
</row>
9376+
<row>
9377+
<entry>
9378+
<literal>
9379+
<function>array_ndims</function>(<type>anyarray</type>)
9380+
</literal>
9381+
</entry>
9382+
<entry><type>int</type></entry>
9383+
<entry>returns the number of dimensions of the array</entry>
9384+
<entry><literal>array_ndims(ARRAY[[1,2,3], [4,5,6]])</literal></entry>
9385+
<entry><literal>2</literal></entry>
9386+
</row>
93769387
<row>
93779388
<entry>
93789389
<literal>

src/backend/utils/adt/arrayfuncs.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.147 2008/07/21 04:47:00 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.148 2008/11/04 14:49:11 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1530,6 +1530,22 @@ array_send(PG_FUNCTION_ARGS)
15301530
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
15311531
}
15321532

1533+
/*
1534+
* array_ndims :
1535+
* returns the number of dimensions of the array pointed to by "v"
1536+
*/
1537+
Datum
1538+
array_ndims(PG_FUNCTION_ARGS)
1539+
{
1540+
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
1541+
1542+
/* Sanity check: does it look like an array at all? */
1543+
if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM)
1544+
PG_RETURN_NULL();
1545+
1546+
PG_RETURN_INT32(ARR_NDIM(v));
1547+
}
1548+
15331549
/*
15341550
* array_dims :
15351551
* returns the dimensions of the array pointed to by "v", as a "text"

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.501 2008/11/03 17:51:13 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.502 2008/11/04 14:49:11 petere Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200811031
56+
#define CATALOG_VERSION_NO 200811041
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.523 2008/11/03 21:09:17 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.524 2008/11/04 14:49:11 petere Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -985,6 +985,7 @@ DATA(insert OID = 393 ( array_le PGNSP PGUID 12 1 0 0 f f t f i 2 16 "2277
985985
DESCR("array less than or equal");
986986
DATA(insert OID = 396 ( array_ge PGNSP PGUID 12 1 0 0 f f t f i 2 16 "2277 2277" _null_ _null_ _null_ array_ge _null_ _null_ _null_ ));
987987
DESCR("array greater than or equal");
988+
DATA(insert OID = 748 ( array_ndims PGNSP PGUID 12 1 0 0 f f t f i 1 23 "2277" _null_ _null_ _null_ array_ndims _null_ _null_ _null_ ));
988989
DATA(insert OID = 747 ( array_dims PGNSP PGUID 12 1 0 0 f f t f i 1 25 "2277" _null_ _null_ _null_ array_dims _null_ _null_ _null_ ));
989990
DESCR("array dimensions");
990991
DATA(insert OID = 750 ( array_in PGNSP PGUID 12 1 0 0 f f t f s 3 2277 "2275 26 23" _null_ _null_ _null_ array_in _null_ _null_ _null_ ));

src/include/utils/array.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
5050
* Portions Copyright (c) 1994, Regents of the University of California
5151
*
52-
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.68 2008/07/16 00:48:54 momjian Exp $
52+
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.69 2008/11/04 14:49:12 petere Exp $
5353
*
5454
*-------------------------------------------------------------------------
5555
*/
@@ -195,6 +195,7 @@ extern Datum btarraycmp(PG_FUNCTION_ARGS);
195195
extern Datum arrayoverlap(PG_FUNCTION_ARGS);
196196
extern Datum arraycontains(PG_FUNCTION_ARGS);
197197
extern Datum arraycontained(PG_FUNCTION_ARGS);
198+
extern Datum array_ndims(PG_FUNCTION_ARGS);
198199
extern Datum array_dims(PG_FUNCTION_ARGS);
199200
extern Datum array_lower(PG_FUNCTION_ARGS);
200201
extern Datum array_upper(PG_FUNCTION_ARGS);

src/test/regress/expected/arrays.out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ SELECT a[1:3],
6868
{} | {} | {foo,bar} | {}
6969
(3 rows)
7070

71+
SELECT array_ndims(a) AS a,array_ndims(b) AS b,array_ndims(c) AS c
72+
FROM arrtest;
73+
a | b | c
74+
---+---+---
75+
1 | 3 |
76+
1 | 2 | 1
77+
| 1 | 1
78+
(3 rows)
79+
7180
SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c
7281
FROM arrtest;
7382
a | b | c

src/test/regress/sql/arrays.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ SELECT a[1:3],
5353
d[1:1][1:2]
5454
FROM arrtest;
5555

56+
SELECT array_ndims(a) AS a,array_ndims(b) AS b,array_ndims(c) AS c
57+
FROM arrtest;
58+
5659
SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c
5760
FROM arrtest;
5861

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy