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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

slumba-1.1.15-cp39-cp39-manylinux_2_31_x86_64.whl (63.3 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

slumba-1.1.15-cp38-cp38-manylinux_2_31_x86_64.whl (63.6 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.31+ x86-64

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

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

File details

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

File metadata

  • Download URL: slumba-1.1.15-cp39-cp39-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 63.3 kB
  • Tags: CPython 3.9, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.9.6 Linux/5.8.0-1039-azure

File hashes

Hashes for slumba-1.1.15-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 0d2b5fd7f318f005a90b015391830eb9957d2a53f25dc54dc78a75eb8ac395f7
MD5 672daed7fe1d1207f1c50bbac0ed0647
BLAKE2b-256 9bc8c1d3409fe53d00cfa42407fc5167516af27825b852b9fef8e6009a8ec0c4

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: slumba-1.1.15-cp38-cp38-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 63.6 kB
  • Tags: CPython 3.8, manylinux: glibc 2.31+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.8.0-1039-azure

File hashes

Hashes for slumba-1.1.15-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 7ded3914c7917fac939766aa9588496eaf12a08f4e941b800c410dd5e490ae65
MD5 add7dddb49582ba305c45d7a753e25ca
BLAKE2b-256 1f9d3abd6b9128d6bfa1d7498bd73afe776d8c4792699612e81d8955afdf3acf

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for slumba-1.1.15-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 3dea800376a0987e5efebf9b8c138b70c489b8f2818a2fcb899c7bc7f15dd177
MD5 eb6a335dc22cab18c6d9450a63da9a93
BLAKE2b-256 65793ce39fe4d877f1ba0c0972c1be0da24db7d75e3aa4ea51edbf22b428c95e

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