diff options
author | Peter Eisentraut <peter@eisentraut.org> | 2021-03-23 08:45:51 +0100 |
---|---|---|
committer | Peter Eisentraut <peter@eisentraut.org> | 2021-03-23 10:13:58 +0100 |
commit | a6715af1e72da289474011be1e2d536f991eda34 (patch) | |
tree | 48ae8ef2c5c858baf43611b8a65c4ec22cbe47bf /src/backend/utils/adt/varbit.c | |
parent | 5aed6a1fc214913de9ac69c1717dc64a2483e16d (diff) |
Add bit_count SQL function
This function for bit and bytea counts the set bits in the bit or byte
string. Internally, we use the existing popcount functionality.
For the name, after some discussion, we settled on bit_count, which
also exists with this meaning in MySQL, Java, and Python.
Author: David Fetter <david@fetter.org>
Discussion: https://www.postgresql.org/message-id/flat/20201230105535.GJ13234@fetter.org
Diffstat (limited to 'src/backend/utils/adt/varbit.c')
-rw-r--r-- | src/backend/utils/adt/varbit.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/backend/utils/adt/varbit.c b/src/backend/utils/adt/varbit.c index 2235866244d..0d0c0fd9f3c 100644 --- a/src/backend/utils/adt/varbit.c +++ b/src/backend/utils/adt/varbit.c @@ -36,6 +36,7 @@ #include "libpq/pqformat.h" #include "nodes/nodeFuncs.h" #include "nodes/supportnodes.h" +#include "port/pg_bitutils.h" #include "utils/array.h" #include "utils/builtins.h" #include "utils/varbit.h" @@ -1202,6 +1203,19 @@ bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl) } /* + * bit_count + * + * Returns the number of bits set in a bit string. + */ +Datum +bit_bit_count(PG_FUNCTION_ARGS) +{ + VarBit *arg = PG_GETARG_VARBIT_P(0); + + PG_RETURN_INT64(pg_popcount((char *) VARBITS(arg), VARBITBYTES(arg))); +} + +/* * bitlength, bitoctetlength * Return the length of a bit string */ |