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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for slumba-1.1.17-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c08f8005689443832bb94265581127b977ef061e1a84f8e3ffbaa78dd2c26e4 |
|
MD5 | 7c4a5e934cacd4b94c5b730643c7fe1a |
|
BLAKE2b-256 | 52c7b88c0fb51cf6cf751d8d5228a97812b75b757779d7481eff15377acb3cdf |
Hashes for slumba-1.1.17-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94b667553245b9ac8f71b403962fde09f01c3ef2060116d98d7b4b2598a9456b |
|
MD5 | b46744d1e8fa8c2d7765edbe3fd57e3d |
|
BLAKE2b-256 | 30bf0d9e6b39b713a16b163ec1f322e55f76b3bbe3ceb1f1e85f576d451cd4af |
Hashes for slumba-1.1.17-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3c11fd12dc99959c27d12662b8e1dfd22618a4f3237fc5fd1e50b17f0375be1 |
|
MD5 | 97aabbb09824ce01a3db8af939e11f8b |
|
BLAKE2b-256 | 7279966a85c52eb42d628623295957c1aff6fa95ba26c56bf9f7af24c7bfeae2 |
Hashes for slumba-1.1.17-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04aeecf55ded6881553559e1eb6ad5418bb27b25f614f432033ae9e14cdaba20 |
|
MD5 | d14722604fe7abf7731d600ae88f6ad2 |
|
BLAKE2b-256 | 3e48eb50351afa3226f6053056a0bf5e5d29960ccbebd321323fa463c4eba8f7 |
Hashes for slumba-1.1.17-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c0230d2c609a0bbb176f28aeb20b195b44aee55e3f58f232b5ff0f77b7aaf11 |
|
MD5 | 48f0c60961b3672d2afbeb2ef570d72c |
|
BLAKE2b-256 | a95c3e4f72a6371b295c5e28052339fd74e507539c4c23fbfbb479938ae46b53 |
Hashes for slumba-1.1.17-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 257cc344df538b05c1b10c869de5155091e44e237733c3e57fefe2e9078e6dd2 |
|
MD5 | a5b0877335cc5ac7b0c15618bb24baf5 |
|
BLAKE2b-256 | ad2d91d5601c8c2ac9c7152e746da29c35e51525890981499618b8cd11fa60fe |
Hashes for slumba-1.1.17-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d9ee95d596315a7c35f0ecc69e67477e4018b03fc7e123626d454ad18e67e55 |
|
MD5 | 71e758b697a7cc94f0e095bbbf8c8bb6 |
|
BLAKE2b-256 | d4b4a988955574dfed03d3c1d4fa0564d1590fa04908434786edac6fcc332117 |
Hashes for slumba-1.1.17-cp37-cp37m-manylinux_2_31_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a04abb9ea9b3e4274677f1732c49cbf6e4e35073e91ed1fbb2721832d0d0c71b |
|
MD5 | 58c0d9ece1f1d2bbfcf7ebc97f37209c |
|
BLAKE2b-256 | 9561f36c9d4dfae656211ef465d0f4f6220578d0b9b72896346bd2cc525eee33 |
Hashes for slumba-1.1.17-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61ae1dec73b0b1ba201b56bd9ba8647f852721557a6d92c85f809c70fa853530 |
|
MD5 | 34a941d7779ae3cf394f30a5dde60874 |
|
BLAKE2b-256 | b03e38508c5bb6bd906c6d6a81cf33bd061f33998c4a2e721fd71acd8c9f8f96 |