aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/arrayfuncs.c
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2014-01-21 12:38:53 -0500
committerRobert Haas <rhaas@postgresql.org>2014-01-21 12:38:53 -0500
commit01f7808b3eafcae1f6077f2f61e13b4c132ccd47 (patch)
tree744e328b7dddbdd7294e341f6165d58e51024cdd /src/backend/utils/adt/arrayfuncs.c
parent033b2343fae9d8c9df124cde62087dcb481c9c5e (diff)
downloadpostgresql-01f7808b3eafcae1f6077f2f61e13b4c132ccd47.tar.gz
postgresql-01f7808b3eafcae1f6077f2f61e13b4c132ccd47.zip
Add a cardinality function for arrays.
Unlike our other array functions, this considers the total number of elements across all dimensions, and returns 0 rather than NULL when the array has no elements. But it seems that both of those behaviors are almost universally disliked, so hopefully that's OK. Marko Tiikkaja, reviewed by Dean Rasheed and Pavel Stehule
Diffstat (limited to 'src/backend/utils/adt/arrayfuncs.c')
-rw-r--r--src/backend/utils/adt/arrayfuncs.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c
index d52101663a1..311d0c22f06 100644
--- a/src/backend/utils/adt/arrayfuncs.c
+++ b/src/backend/utils/adt/arrayfuncs.c
@@ -1740,6 +1740,18 @@ array_length(PG_FUNCTION_ARGS)
}
/*
+ * array_cardinality:
+ * returns the total number of elements in an array
+ */
+Datum
+array_cardinality(PG_FUNCTION_ARGS)
+{
+ ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
+ PG_RETURN_INT32(ArrayGetNItems(ARR_NDIM(v), ARR_DIMS(v)));
+}
+
+
+/*
* 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