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/varlena.c | |
parent | 5aed6a1fc214913de9ac69c1717dc64a2483e16d (diff) | |
download | postgresql-a6715af1e72da289474011be1e2d536f991eda34.tar.gz postgresql-a6715af1e72da289474011be1e2d536f991eda34.zip |
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/varlena.c')
-rw-r--r-- | src/backend/utils/adt/varlena.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 0bc345aa4d3..640e3fd4c04 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -3441,6 +3441,17 @@ bytea_overlay(bytea *t1, bytea *t2, int sp, int sl) } /* + * bit_count + */ +Datum +bytea_bit_count(PG_FUNCTION_ARGS) +{ + bytea *t1 = PG_GETARG_BYTEA_PP(0); + + PG_RETURN_INT64(pg_popcount(VARDATA_ANY(t1), VARSIZE_ANY_EXHDR(t1))); +} + +/* * byteapos - * Return the position of the specified substring. * Implements the SQL POSITION() function. |