Skip to main content

Build and run queries against data

Project description

DataFusion in Python

This is a Python library that binds to Apache Arrow in-memory query engine DataFusion.

Like pyspark, it allows you to build a plan through SQL or a DataFrame API against in-memory data, parquet or CSV files, run it in a multi-threaded environment, and obtain the result back in Python.

It also allows you to use UDFs and UDAFs for complex operations.

The major advantage of this library over other execution engines is that this library achieves zero-copy between Python and its execution engine: there is no cost in using UDFs, UDAFs, and collecting the results to Python apart from having to lock the GIL when running those operations.

Its query engine, DataFusion, is written in Rust, which makes strong assumptions about thread safety and lack of memory leaks.

Technically, zero-copy is achieved via the c data interface.

How to use it

Simple usage:

import datafusion
import pyarrow

# an alias
f = datafusion.functions

# create a context
ctx = datafusion.ExecutionContext()

# create a RecordBatch and a new DataFrame from it
batch = pyarrow.RecordBatch.from_arrays(
    [pyarrow.array([1, 2, 3]), pyarrow.array([4, 5, 6])],
    names=["a", "b"],
)
df = ctx.create_dataframe([[batch]])

# create a new statement
df = df.select(
    f.col("a") + f.col("b"),
    f.col("a") - f.col("b"),
)

# execute and collect the first (and only) batch
result = df.collect()[0]

assert result.column(0) == pyarrow.array([5, 7, 9])
assert result.column(1) == pyarrow.array([-3, -3, -3])

UDFs

def is_null(array: pyarrow.Array) -> pyarrow.Array:
    return array.is_null()

udf = f.udf(is_null, [pyarrow.int64()], pyarrow.bool_())

df = df.select(udf(f.col("a")))

UDAF

import pyarrow
import pyarrow.compute


class Accumulator:
    """
    Interface of a user-defined accumulation.
    """
    def __init__(self):
        self._sum = pyarrow.scalar(0.0)

    def to_scalars(self) -> [pyarrow.Scalar]:
        return [self._sum]

    def update(self, values: pyarrow.Array) -> None:
        # not nice since pyarrow scalars can't be summed yet. This breaks on `None`
        self._sum = pyarrow.scalar(self._sum.as_py() + pyarrow.compute.sum(values).as_py())

    def merge(self, states: pyarrow.Array) -> None:
        # not nice since pyarrow scalars can't be summed yet. This breaks on `None`
        self._sum = pyarrow.scalar(self._sum.as_py() + pyarrow.compute.sum(states).as_py())

    def evaluate(self) -> pyarrow.Scalar:
        return self._sum


df = ...

udaf = f.udaf(Accumulator, pyarrow.float64(), pyarrow.float64(), [pyarrow.float64()])

df = df.aggregate(
    [],
    [udaf(f.col("a"))]
)

How to install

pip install datafusion

How to develop

This assumes that you have rust and cargo installed. We use the workflow recommended by pyo3 and maturin.

Bootstrap:

# fetch this repo
git clone git@github.com:jorgecarleitao/datafusion-python.git

cd datafusion-python

# prepare development environment (used to build wheel / install in development)
python -m venv venv
venv/bin/pip install maturin==0.8.2 toml==0.10.1

Whenever rust code changes (your changes or via git pull):

venv/bin/maturin develop
venv/bin/python -m unittest discover tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datafusion-0.2.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distributions

datafusion-0.2.0-cp39-cp39-manylinux2010_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

datafusion-0.2.0-cp38-none-win_amd64.whl (3.5 MB view details)

Uploaded CPython 3.8Windows x86-64

datafusion-0.2.0-cp38-cp38-manylinux2010_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

datafusion-0.2.0-cp38-cp38-macosx_10_7_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

datafusion-0.2.0-cp37-none-win_amd64.whl (3.5 MB view details)

Uploaded CPython 3.7Windows x86-64

datafusion-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

datafusion-0.2.0-cp37-cp37m-macosx_10_7_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.7mmacOS 10.7+ x86-64

datafusion-0.2.0-cp36-none-win_amd64.whl (3.5 MB view details)

Uploaded CPython 3.6Windows x86-64

datafusion-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

datafusion-0.2.0-cp36-cp36m-macosx_10_7_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.6mmacOS 10.7+ x86-64

File details

Details for the file datafusion-0.2.0.tar.gz.

File metadata

  • Download URL: datafusion-0.2.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3c1849319336771fffc4310f0cebeb6bce0937ffc4bb3e4a937943177bd0ab78
MD5 8430fefc13f6a47ffd2e913f0fdcfec1
BLAKE2b-256 50a7093c2785922dc16911702fea2d2e4dbdb66f800c61573ab5808d309a26b2

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp39-cp39-manylinux2010_x86_64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp39-cp39-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 5.1 MB
  • Tags: CPython 3.9, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 51ac8f97eb57855da00fa7c2796bee4fb260d438af0a8ebe891821adf2576932
MD5 9d76c8735c56bf9af6209d6a10ab05f9
BLAKE2b-256 4b040a88a703a93532b8fab67197775263dba16044b00c71d8843de45c5020b2

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp38-none-win_amd64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 3.5 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 75547f95885a3c3b129a12899c4bb82947460e9ccc943c0b9e6d5ad883e4efbb
MD5 7a33370259b1a5075e9b31cc9236a1b7
BLAKE2b-256 b42c3b759e8a2528aa229490055198a81be8117356634c13ee6bd1872b389049

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 5.1 MB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 9f48789bbacc5018143af054c437211078a65f96f03a9d6e4b8f7117e98e4bfe
MD5 50ae1d4e05e06362b93a1964f2a41b25
BLAKE2b-256 5a50b280ecd107c0a89811f4ba446c26dc8b37dbd19f3b13d2943e430bc572f2

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp38-cp38-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 3.6 MB
  • Tags: CPython 3.8, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 59f34824be7daf4200420238340d6d40b26ce2187343c8edf279a00db94f9b27
MD5 be27dc741775d842b71e8b064faaaefc
BLAKE2b-256 328a485b6e1092c4751c22c51e92a480184767315be9c702e9029c44093e1b92

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp37-none-win_amd64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 3.5 MB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 1487750bd6325fbce85029257bb7bb942f58af34e68f64de3eef0cec54536c79
MD5 6c8673c6ac1a35c3910491510a875637
BLAKE2b-256 b840b2a89b51a183f2babc412695d3e050ba638e70e7ba0e5849af29b7508979

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 5.1 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 4c80fff07ec8d7196ae67a9a43f36fea9529d608e560b84bc92abc439983048d
MD5 f9233a4c01f8453c942e898a7a1dd932
BLAKE2b-256 9b5296a1d2b9524e4b3b16fccbe28b055ad48b2d27089fe486a2a41aec08ffa1

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp37-cp37m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 3.6 MB
  • Tags: CPython 3.7m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 2d449c7e0ce995e2ae5d02de937f77034cfd25ef289025c4346f4a94b70efe28
MD5 4400d8cd03077d7c05b3dde8a206dcb7
BLAKE2b-256 7c9f5f4eea7fc3ae87d5f71891619a48d79a4d39e966bab6a4bd530d0d78a600

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp36-none-win_amd64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp36-none-win_amd64.whl
  • Upload date:
  • Size: 3.5 MB
  • Tags: CPython 3.6, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 fded7d46d26245a5f0474b43fd3c36262ad156f13ef4b79f4d851ea5fb5a2892
MD5 c5373f7fa452b0f56abb8d8413df2190
BLAKE2b-256 9a3f91fcd37a31ed27df1bd49ee85593ebfb8d0ddad457ebc1358134b22acfa3

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 5.1 MB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 e17f0295bf6a9457e213da7ad7fba0938e33985723c160be33dc2d2d50fd9eec
MD5 508bcafd2e0a4f2a9c5139a55f6d99f5
BLAKE2b-256 bbd993d865ca075f7e5792aae7bc360b72ceca3c3125d4b34b2a33525de1f8dc

See more details on using hashes here.

File details

Details for the file datafusion-0.2.0-cp36-cp36m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: datafusion-0.2.0-cp36-cp36m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 3.6 MB
  • Tags: CPython 3.6m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for datafusion-0.2.0-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 d47bfdb7e5e3bebf4b79904a230be70e16bef2c3b2245c9f986dfa46f03b9a85
MD5 e98ca6524569344045ad2ad72e27f225
BLAKE2b-256 ee507d3d5e6e690b5843e8933ae2c09edfa642e05d04c3e614a311a7e5527747

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page