diff options
author | Peter Eisentraut <peter_e@gmx.net> | 2008-11-12 13:09:28 +0000 |
---|---|---|
committer | Peter Eisentraut <peter_e@gmx.net> | 2008-11-12 13:09:28 +0000 |
commit | f98f6ee0641e87c6ecc2524f5d0a8b54924ffd14 (patch) | |
tree | a192c5bf9cfea3c4581f16f19ec7f6e8245f3c2a /src/backend/utils/adt/arrayfuncs.c | |
parent | 4c22564471e2724bcc62bc2c61ece796c946f16e (diff) | |
download | postgresql-f98f6ee0641e87c6ecc2524f5d0a8b54924ffd14.tar.gz postgresql-f98f6ee0641e87c6ecc2524f5d0a8b54924ffd14.zip |
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
Diffstat (limited to 'src/backend/utils/adt/arrayfuncs.c')
-rw-r--r-- | src/backend/utils/adt/arrayfuncs.c | 30 |
1 files changed, 29 insertions, 1 deletions
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 $ * *------------------------------------------------------------------------- */ @@ -1641,6 +1641,34 @@ array_upper(PG_FUNCTION_ARGS) } /* + * 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 * the referenced item as a Datum. Note that for a pass-by-reference |