ADD array_ndims function
authorPeter Eisentraut <[email protected]>
Tue, 4 Nov 2008 14:49:12 +0000 (14:49 +0000)
committerPeter Eisentraut <[email protected]>
Tue, 4 Nov 2008 14:49:12 +0000 (14:49 +0000)
Author: Robert Haas <[email protected]>

doc/src/sgml/func.sgml
src/backend/utils/adt/arrayfuncs.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.h
src/include/utils/array.h
src/test/regress/expected/arrays.out
src/test/regress/sql/arrays.sql

index 855528a1e392b85dbc65d82891957fe43d4dded7..f257238b998f1b43054272f06c9cff34f658a66c 100644 (file)
@@ -9373,6 +9373,17 @@ SELECT NULLIF(value, '(none)') ...
         <entry><literal>array_cat(ARRAY[1,2,3], ARRAY[4,5])</literal></entry>
         <entry><literal>{1,2,3,4,5}</literal></entry>
        </row>
+       <row>
+        <entry>
+         <literal>
+          <function>array_ndims</function>(<type>anyarray</type>)
+         </literal>
+        </entry>
+        <entry><type>int</type></entry>
+        <entry>returns the number of dimensions of the array</entry>
+        <entry><literal>array_ndims(ARRAY[[1,2,3], [4,5,6]])</literal></entry>
+        <entry><literal>2</literal></entry>
+       </row>
        <row>
         <entry>
          <literal>
index 98f0f0c22215042582ad1a6fe6e912d543d11baa..497d0663892c862bb62ca88fd188f4d142a01dfb 100644 (file)
@@ -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"
index c3fda2165b7dd34149a004140247ccb179d217e4..a88d830e7e8a1bff364dcbe207ac6cfc2db8f4d9 100644 (file)
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     200811031
+#define CATALOG_VERSION_NO     200811041
 
 #endif
index 8e1e869a8abb9e8b6e6dea8a2a87329f718f388e..fe20b01e75c26347e84f9cf2ccc9075799cec887 100644 (file)
@@ -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_ ));
index 05323f083cd82857db0e395fe41c5b84a672503e..500966de6508eb29129e5680e19a915ca401b2e5 100644 (file)
@@ -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);
index bcf5280b147ff2b4dbe574cbd5315451306eb743..e429995fc81d3ee6b7730fecc7bb11758005f3b5 100644 (file)
@@ -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   
index 868ee4afda75c8c4c64e94aa487a42f78081385c..54f8ab53e54eac8061a6ef2868cb34c023e08c46 100644 (file)
@@ -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;