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.13-cp39-cp39-win_amd64.whl (64.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

slumba-1.1.13-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.13-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.13-cp38-cp38-win_amd64.whl (64.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

slumba-1.1.13-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.13-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.13-cp37-cp37m-win_amd64.whl (64.5 kB view details)

Uploaded CPython 3.7m Windows x86-64

slumba-1.1.13-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.1.13-cp37-cp37m-macosx_10_15_x86_64.whl (54.8 kB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

Details for the file slumba-1.1.13-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: slumba-1.1.13-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 64.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.9.6 Windows/10

File hashes

Hashes for slumba-1.1.13-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 113f416f1928fa2ff2a1fb389ff0722a65248273ef0e29df7405c223efe671e2
MD5 8d2fca44bc5722ede6e70e1282ce00cf
BLAKE2b-256 25d273575f7802454d4720bd1a772b08184b00fa87698346a92f5f481506d4df

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: slumba-1.1.13-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.13-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 fea7d7be55d29dd8b79f466ff846b7318ce55292d6caad35b12b5b6062a5aa0b
MD5 e303ebcc4bba0d40a3361c7c53868daa
BLAKE2b-256 162156eb0c6d73c255b28e7910211d9fc1c2f8aeeab9ff06d1e00ce70af1a1bc

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for slumba-1.1.13-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 b9c0f3c314f9d75bbfb1ebb5f78643e0e009b6ef5730a4298fe5abfcc7c470e4
MD5 750cbfab40d5219d4a1f4f6c37ba7fda
BLAKE2b-256 0b55954aa6e41a432d9fde7b3ca594970073d2bb952a67d9e94cb627be6eeed1

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.1.13-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: slumba-1.1.13-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 64.1 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.10 Windows/10

File hashes

Hashes for slumba-1.1.13-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 0f7a96d35597bc94754ca69c4c219289660777d43e42dc08e419981884c77280
MD5 1c8f19b8a29e915a5fd6ee30a3e497d2
BLAKE2b-256 2c1c69999c7b842c2b12b33cb88ddb22e23977bf590018c703d6faf9f19300d0

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: slumba-1.1.13-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.13-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 be83771349cf92ab9ca83caa96c425647735072e27c15a98bcf323f915799d62
MD5 a5fc49a3765b3b9f53d6aa5d29c89994
BLAKE2b-256 af3f49661c578a0d6e49569ff41042b7726d5762cf9ddd428ff57c95b18fbb7b

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for slumba-1.1.13-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 df5e11bac5848c4c25dc8ee3daea2cb3245b357f9a79e7afdd2c56db4ea7c317
MD5 90142dd1bb1f1c320354ab7fee3fdda8
BLAKE2b-256 0666b855cf6daa383f645e447bfade961fb481403157c80f763bfc673ee86aff

See more details on using hashes here.

Provenance

File details

Details for the file slumba-1.1.13-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: slumba-1.1.13-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 64.5 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.7.10 Windows/10

File hashes

Hashes for slumba-1.1.13-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 58c7591c2bff5e9cb3eeb9dc8b09641d37f65b8f6a67a4790af7de7992321f8e
MD5 a12b556926db5cb0ce7906d2c30cb061
BLAKE2b-256 fbc17c5c1feb17ef952b27203f337424cf0b74c186b6418e67ad171be2367364

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for slumba-1.1.13-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 eedd1f22dec48098d218ec7358302d0cd72ec3fcb114b477e64bb8552c258de7
MD5 4ec5c19abb5a4303e1a454822979520f
BLAKE2b-256 e4782f52e781e6690ab772df1c15e83104ee704bc8aab10748c6881b6296e5db

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for slumba-1.1.13-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 49e8b91772ffc9544fd71d987307abddc737be257a1c239eb98b679333583724
MD5 ddb20beb1c773c773d65106537a72267
BLAKE2b-256 8b6cb7ae072253dc2d14a6216321c1036e513252e2730d3183529312316bf1b7

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