GithubHelp home page GithubHelp logo

thehyve / pg_bitcount Goto Github PK

View Code? Open in Web Editor NEW
8.0 11.0 3.0 63 KB

PostgreSQL extension providing a bitcount function.

License: MIT License

Makefile 0.74% C 94.70% C++ 1.64% Shell 2.91%
postgresql bitset extension

pg_bitcount's Introduction

pg_bitcount

Build Status

PostgreSQL extension providing a bitcount function and an aggregate function that aggregates integers in a bit string.

Installation

Using Docker

Docker images of PostgreSQL with the pg_bitcount extension available can be found at Docker Hub. To start a Docker container with PostgreSQL 11 and expose the database server port at port 9432:

docker run --detach --publish 9432:5432 thehyve/pg_bitcount:11

From Debian packages

Install the extension for PostgreSQL 11 in Debian or Ubuntu:

wget https://github.com/thehyve/pg_bitcount/releases/download/0.0.3-2/postgresql-11-pg-bitcount_0.0.3-2_amd64.deb && \
sudo dpkg -i postgresql-11-pg-bitcount_0.0.3-2_amd64.deb

From source

Dependencies

The PostgreSQL extension build tool needs to be installed.

# For Ubuntu/Debian:
sudo apt install postgresql-server-dev-all

Build

Install the extension into the PostgreSQL library directory:

wget https://github.com/thehyve/pg_bitcount/archive/0.0.3-2.zip && \
unzip 0.0.3-2.zip && \
cd pg_bitcount-0.0.3-2 && \
make && sudo make install

Usage

-- Register the extension in PostgreSQL
create extension pg_bitcount version '0.0.3';

-- Use the pg_bitcount function
select public.pg_bitcount(127::bit(8)); -- 7
select public.pg_bitcount(B'101010101'); -- 5 
select public.pg_bitcount((17^15)::bigint::bit(128) << 64 | (17^14)::bigint::bit(128)); -- 58

-- Use the pg_int_to_bit_agg aggregate using a bit string of size 24
select public.pg_bitcount(public.pg_int_to_bit_agg(i::int, 24)) from (select generate_series(2, 8) as i) data; -- 7

List of functions

Schema Name Result data type Argument data types Type
public pg_bitcount integer bit normal
public pg_int_to_bit_agg bit integer, integer agg

Development

Build the extension

make

Tests

# Run performance tests (comparing four different implementations)
make performance_tester && ./performance_tester

Run database tests

# Create database role for testing
sudo -u postgres psql -p 5434 -c "create role ${USER} with login superuser" 
# Execute checks
PGPORT=5434 make installcheck

Acknowledgements

The website Bit Twiddling Hacks by Sean Eron Anderson provides a valuable resource for bit counting algorithms. From our comparison, the table lookup methods appears to be the fastest (function bitcount_dictionary in bitcount.c.

We use pop_count from the collection of Portable Snippets of C code to avoid dependency on a particular compiler.

License

Copyright © 2018–2021   The Hyve

The pg_count extension is licensed under the MIT License. See the file LICENSE.

pg_bitcount's People

Contributors

gijskant avatar phozzy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pg_bitcount's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.