Hi,

Attached is a patch to add support for array_length(anyarray), which only works for one-dimensional arrays, returns 0 for empty arrays and complains if the array's lower bound isn't 1. In other words, does the right thing when used with the arrays people use 99% of the time.

I'll add this to the next commit fest, but feel free to discuss it before that.



Regards,
Marko Tiikkaja
*** a/doc/src/sgml/array.sgml
--- b/doc/src/sgml/array.sgml
***************
*** 338,343 **** SELECT array_length(schedule, 1) FROM sal_emp WHERE name = 
'Carol';
--- 338,354 ----
              2
  (1 row)
  </programlisting>
+ 
+  The single-argument overload of <function>array_length</function> can be used
+  to get the length of a one-dimensional array:
+ <programlisting>
+ SELECT array_length(schedule) FROM sal_emp WHERE name = 'Carol';
+ 
+  array_length
+ --------------
+             2
+ (1 row)
+ </programlisting>
   </para>
   </sect2>
  
*** a/doc/src/sgml/func.sgml
--- b/doc/src/sgml/func.sgml
***************
*** 11096,11101 **** SELECT NULLIF(value, '(none)') ...
--- 11096,11112 ----
         <row>
          <entry>
           <literal>
+           <function>array_length</function>(<type>anyarray</type>)
+          </literal>
+         </entry>
+         <entry><type>int</type></entry>
+         <entry>returns the length of the array (array must be 
one-dimensional)</entry>
+         <entry><literal>array_length(array[1,2,3])</literal></entry>
+         <entry><literal>3</literal></entry>
+        </row>
+        <row>
+         <entry>
+          <literal>
            <function>array_lower</function>(<type>anyarray</type>, 
<type>int</type>)
           </literal>
          </entry>
*** a/src/backend/utils/adt/arrayfuncs.c
--- b/src/backend/utils/adt/arrayfuncs.c
***************
*** 1740,1745 **** array_length(PG_FUNCTION_ARGS)
--- 1740,1779 ----
  }
  
  /*
+  * array_length_single:
+  *            Returns the length of a single-dimensional array.  The array 
must be
+  *            single-dimensional or empty and its lower bound must be 1.
+  */
+ Datum
+ array_length_single(PG_FUNCTION_ARGS)
+ {
+       ArrayType  *v = PG_GETARG_ARRAYTYPE_P(0);
+       int                     result;
+       int                *lb;
+       int                *dimv;
+ 
+       /* empty array */
+       if (ARR_NDIM(v) == 0)
+               PG_RETURN_INT32(0);
+ 
+       if (ARR_NDIM(v) != 1)
+               ereport(ERROR, 
+                               (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+                                errmsg("input array is not 
single-dimensional")));
+ 
+       lb = ARR_LBOUND(v);
+       if (lb[0] != 1)
+               ereport(ERROR,
+                               (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+                                errmsg("lower bound of array is not 1")));
+ 
+       dimv = ARR_DIMS(v);
+       result = dimv[0];
+       PG_RETURN_INT32(result);
+ }
+ 
+ 
+ /*
   * array_ref :
   *      This routine takes an array pointer and a subscript array and returns
   *      the referenced item as a Datum.  Note that for a pass-by-reference
*** a/src/include/catalog/pg_proc.h
--- b/src/include/catalog/pg_proc.h
***************
*** 840,845 **** DATA(insert OID = 2092 (  array_upper     PGNSP PGUID 12 1 0 0 
0 f f f f t f i 2
--- 840,847 ----
  DESCR("array upper dimension");
  DATA(insert OID = 2176 (  array_length           PGNSP PGUID 12 1 0 0 0 f f f 
f t f i 2 0 23 "2277 23" _null_ _null_ _null_ _null_ array_length _null_ _null_ 
_null_ ));
  DESCR("array length");
+ DATA(insert OID = 3179 (  array_length           PGNSP PGUID 12 1 0 0 0 f f f 
f t f i 1 0 23 "2277" _null_ _null_ _null_ _null_ array_length_single _null_ 
_null_ _null_ ));
+ DESCR("array length");
  DATA(insert OID = 378 (  array_append    PGNSP PGUID 12 1 0 0 0 f f f f f f i 
2 0 2277 "2277 2283" _null_ _null_ _null_ _null_ array_push _null_ _null_ 
_null_ ));
  DESCR("append element onto end of array");
  DATA(insert OID = 379 (  array_prepend           PGNSP PGUID 12 1 0 0 0 f f f 
f f f i 2 0 2277 "2283 2277" _null_ _null_ _null_ _null_ array_push _null_ 
_null_ _null_ ));
*** a/src/include/utils/array.h
--- b/src/include/utils/array.h
***************
*** 204,209 **** extern Datum array_dims(PG_FUNCTION_ARGS);
--- 204,210 ----
  extern Datum array_lower(PG_FUNCTION_ARGS);
  extern Datum array_upper(PG_FUNCTION_ARGS);
  extern Datum array_length(PG_FUNCTION_ARGS);
+ extern Datum array_length_single(PG_FUNCTION_ARGS);
  extern Datum array_larger(PG_FUNCTION_ARGS);
  extern Datum array_smaller(PG_FUNCTION_ARGS);
  extern Datum generate_subscripts(PG_FUNCTION_ARGS);
*** a/src/test/regress/expected/arrays.out
--- b/src/test/regress/expected/arrays.out
***************
*** 1455,1460 **** select array_length(array[[1,2,3], [4,5,6]], 3);
--- 1455,1482 ----
               
  (1 row)
  
+ select array_length(NULL::int[]);
+  array_length 
+ --------------
+              
+ (1 row)
+ 
+ select array_length(array[1,2,3]);
+  array_length 
+ --------------
+             3
+ (1 row)
+ 
+ select array_length('{}'::int[]);
+  array_length 
+ --------------
+             0
+ (1 row)
+ 
+ select array_length('[2:4]={5,6,7}'::int[]);
+ ERROR:  lower bound of array is not 1
+ select array_length('{{1,2}}'::int[]);
+ ERROR:  input array is not single-dimensional
  select array_agg(unique1) from (select unique1 from tenk1 where unique1 < 15 
order by unique1) ss;
                array_agg               
  --------------------------------------
*** a/src/test/regress/sql/arrays.sql
--- b/src/test/regress/sql/arrays.sql
***************
*** 419,424 **** select array_length(array[[1,2,3], [4,5,6]], 1);
--- 419,430 ----
  select array_length(array[[1,2,3], [4,5,6]], 2);
  select array_length(array[[1,2,3], [4,5,6]], 3);
  
+ select array_length(NULL::int[]);
+ select array_length(array[1,2,3]);
+ select array_length('{}'::int[]);
+ select array_length('[2:4]={5,6,7}'::int[]);
+ select array_length('{{1,2}}'::int[]);
+ 
  select array_agg(unique1) from (select unique1 from tenk1 where unique1 < 15 
order by unique1) ss;
  select array_agg(ten) from (select ten from tenk1 where unique1 < 15 order by 
unique1) ss;
  select array_agg(nullif(ten, 4)) from (select ten from tenk1 where unique1 < 
15 order by unique1) ss;
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to