diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index e272b8b6ee..61460f3c0f 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,4 +1,4 @@ - + Functions and Operators @@ -9373,6 +9373,17 @@ SELECT NULLIF(value, '(none)') ... array_cat(ARRAY[1,2,3], ARRAY[4,5]) {1,2,3,4,5} + + + + array_ndims(anyarray) + + + int + returns the number of dimensions of the array + array_ndims(ARRAY[[1,2,3], [4,5,6]]) + 2 + diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index 5f3356f560..97dc3dc6c8 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.147 2008/07/21 04:47:00 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.148 2008/11/04 14:49:11 petere Exp $ * *------------------------------------------------------------------------- */ @@ -1530,6 +1530,22 @@ array_send(PG_FUNCTION_ARGS) PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } +/* + * array_ndims : + * returns the number of dimensions of the array pointed to by "v" + */ +Datum +array_ndims(PG_FUNCTION_ARGS) +{ + ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); + + /* Sanity check: does it look like an array at all? */ + if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) + PG_RETURN_NULL(); + + PG_RETURN_INT32(ARR_NDIM(v)); +} + /* * array_dims : * returns the dimensions of the array pointed to by "v", as a "text" diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index c41fcb0570..58bda0f16d 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -37,7 +37,7 @@ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.501 2008/11/03 17:51:13 tgl Exp $ + * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.502 2008/11/04 14:49:11 petere Exp $ * *------------------------------------------------------------------------- */ @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 200811031 +#define CATALOG_VERSION_NO 200811041 #endif diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index cbf2c28e79..ea686c319d 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.523 2008/11/03 21:09:17 tgl Exp $ + * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.524 2008/11/04 14:49:11 petere Exp $ * * NOTES * 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 DESCR("array less than or equal"); 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_ )); DESCR("array greater than or equal"); +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_ )); 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_ )); DESCR("array dimensions"); 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_ )); diff --git a/src/include/utils/array.h b/src/include/utils/array.h index 9efa78e6f3..6bbc46e13b 100644 --- a/src/include/utils/array.h +++ b/src/include/utils/array.h @@ -49,7 +49,7 @@ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/utils/array.h,v 1.68 2008/07/16 00:48:54 momjian Exp $ + * $PostgreSQL: pgsql/src/include/utils/array.h,v 1.69 2008/11/04 14:49:12 petere Exp $ * *------------------------------------------------------------------------- */ @@ -195,6 +195,7 @@ extern Datum btarraycmp(PG_FUNCTION_ARGS); extern Datum arrayoverlap(PG_FUNCTION_ARGS); extern Datum arraycontains(PG_FUNCTION_ARGS); extern Datum arraycontained(PG_FUNCTION_ARGS); +extern Datum array_ndims(PG_FUNCTION_ARGS); extern Datum array_dims(PG_FUNCTION_ARGS); extern Datum array_lower(PG_FUNCTION_ARGS); extern Datum array_upper(PG_FUNCTION_ARGS); diff --git a/src/test/regress/expected/arrays.out b/src/test/regress/expected/arrays.out index bcf5280b14..e429995fc8 100644 --- a/src/test/regress/expected/arrays.out +++ b/src/test/regress/expected/arrays.out @@ -68,6 +68,15 @@ SELECT a[1:3], {} | {} | {foo,bar} | {} (3 rows) +SELECT array_ndims(a) AS a,array_ndims(b) AS b,array_ndims(c) AS c + FROM arrtest; + a | b | c +---+---+--- + 1 | 3 | + 1 | 2 | 1 + | 1 | 1 +(3 rows) + SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c FROM arrtest; a | b | c diff --git a/src/test/regress/sql/arrays.sql b/src/test/regress/sql/arrays.sql index 868ee4afda..54f8ab53e5 100644 --- a/src/test/regress/sql/arrays.sql +++ b/src/test/regress/sql/arrays.sql @@ -53,6 +53,9 @@ SELECT a[1:3], d[1:1][1:2] FROM arrtest; +SELECT array_ndims(a) AS a,array_ndims(b) AS b,array_ndims(c) AS c + FROM arrtest; + SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c FROM arrtest;