Skip to main content

JITted SQLite user-defined functions and aggregates

Project description

Put some Numba in your SQLite

Fair Warning

This library does unsafe things like pass around function pointer addresses as integers. Use at your own risk.

If you're unfamiliar with why passing function pointers' addresses around as integers might be unsafe, then you shouldn't use this library.

Requirements

  • Python >=3.7
  • numba

Use nix-shell from the repository to avoid dependency hell.

Installation

  • poetry install

Examples

Scalar Functions

These are almost the same as decorating a Python function with numba.jit. In the case of sqlite_udf a signature is required.

from slumba import sqlite_udf
from numba import int64


@sqlite_udf(optional(int64)(optional(int64)))
def add_one(x):
    """Add one to `x` if `x` is not NULL."""

    if x is not None:
        return x + 1
    return None

Aggregate Functions

These follow the API of the Python standard library's sqlite3.Connection.create_aggregate method. The difference with slumba aggregates is that they require two decorators: numba.experimental.jit_class and slumba.sqlite_udaf. Let's define the avg (arithmetic mean) function for 64-bit floating point numbers.

from numba import int64, float64
from numba.experimental import jit_class
from slumba import sqlite_udaf


@sqlite_udaf(optional(float64)(optional(float64)))
@jit_class(dict(total=float64, count=int64))
class Avg:
    def __init__(self):
        self.total = 0.0
        self.count = 0

    def step(self, value):
        if value is not None:
            self.total += value
            self.count += 1

    def finalize(self):
        if not self.count:
            return None
        return self.total / self.count

Window Functions

You can also define window functions for use with SQLite's OVER construct:

@sqlite_udaf(optional(float64)(optional(float64)))
@jitclass(dict(total=float64, count=int64))
class WinAvg:  # pragma: no cover
    def __init__(self):
        self.total = 0.0
        self.count = 0

    def step(self, value):
        if value is not None:
            self.total += value
            self.count += 1

    def finalize(self):
        count = self.count
        if count:
            return self.total / count
        return None

    def value(self):
        return self.finalize()

    def inverse(self, value):
        if value is not None:
            self.total -= value
            self.count -= 1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slumba-1.0.3.tar.gz (12.4 kB view details)

Uploaded Source

Built Distributions

slumba-1.0.3-cp39-cp39-manylinux_2_31_x86_64.whl (64.0 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

slumba-1.0.3-cp38-cp38-manylinux_2_31_x86_64.whl (63.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.31+ x86-64

slumba-1.0.3-cp37-cp37m-manylinux_2_31_x86_64.whl (63.2 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.31+ x86-64

slumba-1.0.3-cp37-cp37m-manylinux_2_28_x86_64.whl (61.1 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.28+ x86-64

File details

Details for the file slumba-1.0.3.tar.gz.

File metadata

  • Download URL: slumba-1.0.3.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.7.11

File hashes

Hashes for slumba-1.0.3.tar.gz
Algorithm Hash digest
SHA256 c17db0ebb691a83914702b8b1fe326be18365d17d3afb07aec86546f6ce76e9a
MD5 a9c1bf74ca9a4a2dcdf7e59df655a86b
BLAKE2b-256 833f9ecc79f43640f56a4a06c275c7367a6db3ef33c04f0f6408468994cdb4bf

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.0.3-cp39-cp39-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: slumba-1.0.3-cp39-cp39-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 64.0 kB
  • Tags: CPython 3.9, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for slumba-1.0.3-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 1df232eb1e4f707245467531e5236d8cd1eb461a05dcd8fbe06a80802bd4403c
MD5 fe13afdc63441138eedbe8ecf63b7cb7
BLAKE2b-256 1249d850a0c49dd27dadef1be09be3be1d543deb6d88cb521de360ef8a59e23a

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.0.3-cp38-cp38-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: slumba-1.0.3-cp38-cp38-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 63.7 kB
  • Tags: CPython 3.8, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for slumba-1.0.3-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 06c775666b4a8c9999d4a05268772a5a2577bcca921a796438bccf1ab92e65a5
MD5 66717561dd86161d63f651e7795fe467
BLAKE2b-256 38e7e88f9e621bcf1674cdb1b8017f502a6ed3c5ea9927bcc88630f1c9603cad

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.0.3-cp37-cp37m-manylinux_2_31_x86_64.whl.

File metadata

  • Download URL: slumba-1.0.3-cp37-cp37m-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 63.2 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for slumba-1.0.3-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 d32fb8d1872f7d0c897545e29a39a76ae6cba3be0e39a52650f57ba250afcce3
MD5 c54f2dde74088b15b2a8f6405b892a19
BLAKE2b-256 dd46c634164448a13a0e2ff81abcb5ded4f4d4371b6e307ba52874bf15ecaef2

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.0.3-cp37-cp37m-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: slumba-1.0.3-cp37-cp37m-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 61.1 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.7.11

File hashes

Hashes for slumba-1.0.3-cp37-cp37m-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bdaa9292ef8ffc969fbbcd4cf4b05b9a7b53a3c97ac7cd0ae3ea94c3df12e4fe
MD5 d03f739fa693e2d9d110a7033d1251b4
BLAKE2b-256 9241240b8205651edce3cf4d44d5c2d1d351188ef7ccc514ca67b1b44df5f5cc

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page