Skip to main content

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.

from typing import Optional

from slumba import sqlite_udf


@sqlite_udf
def add_one(x: Optional[int]) -> Optional[int]:
    """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 typing import Optional

from numba.experimental import jitclass

from slumba import sqlite_udaf


@sqlite_udaf
@jitclass
class Avg:
    total: float
    count: int

    def __init__(self):
        self.total = 0.0
        self.count = 0

    def step(self, value: Optional[float]) -> None:
        if value is not None:
            self.total += value
            self.count += 1

    def finalize(self) -> Optional[float]:
        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:

from typing import Optional

from numba.experimental import jitclass

from slumba import sqlite_udaf


@sqlite_udaf
@jitclass
class WinAvg:  # pragma: no cover
    total: float
    count: int

    def __init__(self) -> None:
        self.total = 0.0
        self.count = 0

    def step(self, value: Optional[float]) -> None:
        if value is not None:
            self.total += value
            self.count += 1

    def finalize(self) -> Optional[float]:
        count = self.count
        if count:
            return self.total / count
        return None

    def value(self) -> Optional[float]:
        return self.finalize()

    def inverse(self, value: Optional[float]) -> None:
        if value is not None:
            self.total -= value
            self.count -= 1

Calling your aggregate function

Similar to scalar functions, we register the function with a sqlite3.Connection object:

>>> import sqlite3
>>> from slumba import create_aggregate, create_function
>>> con = sqlite3.connect(":memory:")
>>> create_function(con, "add_one", 1, add_one)
>>> con.execute("SELECT add_one(1)").fetchall()
[(2,)]

Release files for slumba 3.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slumba 3.1.0
File Size Uploaded
slumba-3.1.0.tar.gz 18.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slumba 3.1.0
File Interpreter ABI Platform
slumba-3.1.0-py3-none-any.whl Python 3 none any Details

Total release size:38.3 kB

Release files / slumba-3.1.0.tar.gz

Download URL slumba-3.1.0.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f198b2d8adaeba036070767cbd980bb39d6320e2f4f52db316149bc34bf54a3c
BLAKE2b-256 checksum
How to use checksums
d8e42149ea4683b6ab1362f7bec7c437a25b4ea9cc052744652c02c2e9adbca3
Upload date
Uploaded using Trusted Publishing?
What is 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.2 CPython/3.9.6

Release files / slumba-3.1.0-py3-none-any.whl

Download URL slumba-3.1.0-py3-none-any.whl
Size 19.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a59be7f3764be47e2a4737dbf8bd380259534ab8338b0b472dd596238a204e72
BLAKE2b-256 checksum
How to use checksums
335d5c48663d7b9ad29354241938082ee964a1b035bd136d03c7f5a7d9e6a15b
Upload date
Uploaded using Trusted Publishing?
What is 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.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

3.1.0 This release

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.17

9 release files

1.1.15

3 release files

1.1.13

9 release files

1.1.11

6 release files

1.0.6

2 release files

1.0.3

5 release files

0.0.8

1 release file

0.0.7

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page