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.11-cp39-cp39-manylinux_2_31_x86_64.whl (64.1 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

slumba-1.1.11-cp39-cp39-macosx_10_15_x86_64.whl (55.4 kB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

slumba-1.1.11-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.1.11-cp38-cp38-macosx_10_15_x86_64.whl (55.2 kB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

slumba-1.1.11-cp37-cp37m-manylinux_2_31_x86_64.whl (63.3 kB view details)

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

slumba-1.1.11-cp37-cp37m-macosx_10_15_x86_64.whl (54.9 kB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: slumba-1.1.11-cp39-cp39-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 64.1 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.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for slumba-1.1.11-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 1485b5cffc2548cafe88c34df7becded1e38b1ccf57532992d4435317f581e46
MD5 b1f064b1a63bcafec7f471d1378b5644
BLAKE2b-256 c2142971f30d659079bde41ec8e48f548a017d09731416d1dc57db793d42f41d

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.1.11-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: slumba-1.1.11-cp39-cp39-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 55.4 kB
  • Tags: CPython 3.9, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for slumba-1.1.11-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 94310aaa356604ee887c7d824fc1421797873c36e52a25603ca013176491b982
MD5 7624c8b15c36b8d92ca7b7c01c737a17
BLAKE2b-256 c1525659ca558325c75ff68202d89f75e045ab6569a34a4c17382306344b5a8c

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: slumba-1.1.11-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.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.11

File hashes

Hashes for slumba-1.1.11-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 1f49d24ccbce4c80aaad976ff7a440a1a49bf7069e155bb22ac67d9d787311ed
MD5 f843e67b239707810181786797bf7405
BLAKE2b-256 3c781c020f86978075dcdb918a4c448035f4bf96ab8b76b0212295dd8fecb291

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.1.11-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: slumba-1.1.11-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 55.2 kB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.11

File hashes

Hashes for slumba-1.1.11-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 5b6f0af6258fc1d067defdeba2b6dcd33d04cd3daad804783c977759c429a2c4
MD5 de7df2e351d86ec65ca79b37898e40f4
BLAKE2b-256 83b194d4e74c796b4d5fd556ee61c5ff807bee493a814bbd04cd59c809707b77

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: slumba-1.1.11-cp37-cp37m-manylinux_2_31_x86_64.whl
  • Upload date:
  • Size: 63.3 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.4 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.1.11-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 e853373066e0eaa0af834ce337771b82b97e9fc371679aeeb63bb63223951842
MD5 df448ee7000d2a9f4c3dc47e9e9eaac7
BLAKE2b-256 789ee6a983ae3ec2b72b5b2a9a82f4151df6a088a1c099eec54eb86be2b05a46

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.1.11-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: slumba-1.1.11-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 54.9 kB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 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.1.11-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 d3ff0137ad45204638ecd220746d4e8adf6376340fcf6e29f1f63a80d1bb2506
MD5 aa11c8a1ad33ad56832965f26c3c209a
BLAKE2b-256 7b6cf3c0766266a021ecacea733995d668ba95aa8edeee2e5e0df58153935844

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