1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-29 10:41:53 +03:00

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
This commit is contained in:
Peter Eisentraut
2008-11-12 13:09:28 +00:00
parent 4c22564471
commit f98f6ee064
8 changed files with 135 additions and 7 deletions

View File

@ -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