From f98f6ee0641e87c6ecc2524f5d0a8b54924ffd14 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 12 Nov 2008 13:09:28 +0000 Subject: array_length() function, and for SQL compatibility also cardinality() function as a special case. This version still has the suspicious behavior of returning null for an empty array (rather than zero), but this may need a wholesale revision of empty array behavior, currently under discussion. Jim Nasby, Robert Haas, Peter Eisentraut --- src/backend/utils/adt/arrayfuncs.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/backend/utils/adt/arrayfuncs.c') diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index 97dc3dc6c83..9d2b036897b 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.148 2008/11/04 14:49:11 petere Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.149 2008/11/12 13:09:27 petere Exp $ * *------------------------------------------------------------------------- */ @@ -1640,6 +1640,34 @@ array_upper(PG_FUNCTION_ARGS) PG_RETURN_INT32(result); } +/* + * array_length : + * returns the length, of the dimension requested, for + * the array pointed to by "v", as an int4 + */ +Datum +array_length(PG_FUNCTION_ARGS) +{ + ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); + int reqdim = PG_GETARG_INT32(1); + int *dimv; + int result; + + /* Sanity check: does it look like an array at all? */ + if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) + PG_RETURN_NULL(); + + /* Sanity check: was the requested dim valid */ + if (reqdim <= 0 || reqdim > ARR_NDIM(v)) + PG_RETURN_NULL(); + + dimv = ARR_DIMS(v); + + result = dimv[reqdim - 1]; + + PG_RETURN_INT32(result); +} + /* * array_ref : * This routine takes an array pointer and a subscript array and returns -- cgit v1.2.3