All Projects → cpcloud → numbsql

cpcloud / numbsql

Licence: Apache-2.0 License
Run Numba compiled functions into SQLite

Programming Languages

python
139335 projects - #7 most used programming language
Nix
1067 projects
Jupyter Notebook
11667 projects
shell
77523 projects

Projects that are alternatives of or similar to numbsql

PySDM
Pythonic particle-based (super-droplet) warm-rain/aqueous-chemistry cloud microphysics package with box, parcel & 1D/2D prescribed-flow examples in Python, Julia and Matlab
Stars: ✭ 26 (-23.53%)
Mutual labels:  numba
bookshelf-json-columns
Parse JSON columns with Bookshelf.js
Stars: ✭ 64 (+88.24%)
Mutual labels:  sqlite
sqlite-amalgamation
The SQLite amalgamation mirror with cmake
Stars: ✭ 72 (+111.76%)
Mutual labels:  sqlite
sync-client
SyncProxy javascript client with support for all major embedded databases (IndexedDB, SQLite, WebSQL, LokiJS...)
Stars: ✭ 30 (-11.76%)
Mutual labels:  sqlite
reactnative-typescript
Playground and evolution of learnings done in react native with typescript
Stars: ✭ 28 (-17.65%)
Mutual labels:  sqlite
winmerge2011
Fork of WinMerge which has a different set of features
Stars: ✭ 36 (+5.88%)
Mutual labels:  sqlite
grblas
Python wrapper around GraphBLAS
Stars: ✭ 22 (-35.29%)
Mutual labels:  numba
deutschland
Free open public domain football data for Germany (Deutschland) incl. Deutsche Bundesliga, 2. Bundesliga, 3. Liga, DFB Pokal etc.
Stars: ✭ 64 (+88.24%)
Mutual labels:  sqlite
acid-store
A library for secure, deduplicated, transactional, and verifiable data storage
Stars: ✭ 48 (+41.18%)
Mutual labels:  sqlite
couchwarehouse
Data warehouse for CouchDB
Stars: ✭ 41 (+20.59%)
Mutual labels:  sqlite
database
Simple and easy go database micro framework
Stars: ✭ 12 (-64.71%)
Mutual labels:  sqlite
closql
Store EIEIO objects using EmacSQL
Stars: ✭ 23 (-32.35%)
Mutual labels:  sqlite
Zipcodes
A simple library for querying U.S. zipcodes.
Stars: ✭ 63 (+85.29%)
Mutual labels:  sqlite
database
Database Abstraction Layer, Schema Introspection, Schema Generation, Query Builders
Stars: ✭ 25 (-26.47%)
Mutual labels:  sqlite
FlashPaper
One-time encrypted password/secret sharing
Stars: ✭ 85 (+150%)
Mutual labels:  sqlite
microlight
A fully IndieWeb-compatible PHP blogging engine
Stars: ✭ 35 (+2.94%)
Mutual labels:  sqlite
HighLite
An SQLite ORM for Android with automatic database migrations built with annotation processing
Stars: ✭ 77 (+126.47%)
Mutual labels:  sqlite
EFCore.Sqlite.NodaTime
Adds support for NodaTime types when using SQLite with Entity Framework Core.
Stars: ✭ 21 (-38.24%)
Mutual labels:  sqlite
react-native-quick-sqlite
Fast SQLite for react-native.
Stars: ✭ 239 (+602.94%)
Mutual labels:  sqlite
astro
Astro allows rapid and clean development of {Extract, Load, Transform} workflows using Python and SQL, powered by Apache Airflow.
Stars: ✭ 79 (+132.35%)
Mutual labels:  sqlite

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 numbsql 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 numbsql aggregates is that they require two decorators: numba.experimental.jit_class and numbsql.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 numbsql 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 numbsql 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 numbsql 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,)]
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].