Skip to main content

Polars io-plugin for reading and writing avro files

Project description

polars-avro

build pypi docs

A polars io plugin for reading and writing avro files.

Polars is deprecating support for reading and writing avro files, and this plugin fills in support. Currently it's about 7x slower at reading avro files and up to 20x slower at writing files.

The reason it's slower is beause this uses the apache rust library, which is fully complaint, but does a lot of unnecessary memory allocation and object creation that the polars implementaiton avoids. However, this is likely not a bottlneck, so the benefits of the standard implementation seem to outweight the added computation.

In exchange for speed you get:

  1. future proof - this won't get deprecated
  2. robust support - the current polars avro implementation has bugs with non-contiguous data frames
  3. better coverage - this supports reading map types as lists
  4. scan support - this can scan and push down predicates by chunk

Python Usage

from polars_avro import scan_avro, read_avro, write_avro

lazy = scan_avro(path)
frame = read_avro(path)
write_avro(frame, path)

Rust Usage

There are two main objects exported in rust: AvroScanner for creating an iterator of DataFrames from polars ScanSources, and sink_avro for writing an iterator of DataFrames to a Writeable.

use polars_avro::{AvroScanner, sink_avro, WriteOptions};

let scanner = AvroScanner::new_from_sources(
    &ScanSources::Paths(...),
    false, // expand globs
    None,  // cloud options
).unwrap()

sink_avro(
    scanner.into_iter(
        1024, // batch size
        None, // columns to select
    ).map(Result::unwrap),
    ..., // impl Write
    WriteOptions::default(),
).unwrap();

ℹ️ Avro supports writing with a fire compression schemes. In rust these features need to be enabled manually, e.g. apache-avro/bzip to enable bzip2 compression. Decompression is handled automatically.

Idiosyncorcies

Avro and Arrow don't align fully, and polars only supports a subset of arrow. This library tries to allow you to serialize tow avro and deserialize from avro. Trying to do both means that many types will change at each pass due to the way serde works.

  1. Avro only supports time with at most microsecond resolution, polars only supports time with nanosecond resolution, so writing times values truncates them. You must explicitely allow this behavior.
  2. Avro fixed types don't support storing null values in the individual bytes so while a fixed type can be read into a u8 array, it must be serialized back as a list of i32s. This may be addressed with polars support for arrow fixedlengthbinary, but that seems unlikely.

Development

Rust

Standard cargo commands will build and test the rust library.

Python

The python library is built with uv and maturin. Run the following to compile rust for use by python:

For local rust development, run

uv run maturin develop -m Cargo.toml

to build a local copy of the rust interface. Add -r if you want to trust the benchmark results.

Testing

cargo fmt --check
cargo clippy --all-features
cargo test
uv run ruff format --check
uv run ruff check
uv run pyright
uv run pytest

Benchmarks

cargo +nightly bench
uv run pytest

ℹ️ For python benchmarks, make sure you've compiled in release mode: uv run maturin develop -m Cargo.toml -r

Releasing

rm -rf dist
uv build --sdist
uv run maturin build -r -o dist --target aarch64-apple-darwin
uv run maturin build -r -o dist --target aarch64-unknown-linux-gnu --zig
uv publish --username __token__

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

polars_avro-0.3.0.tar.gz (130.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polars_avro-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (29.6 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

polars_avro-0.3.0-cp39-abi3-macosx_11_0_arm64.whl (34.9 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file polars_avro-0.3.0.tar.gz.

File metadata

  • Download URL: polars_avro-0.3.0.tar.gz
  • Upload date:
  • Size: 130.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.28

File hashes

Hashes for polars_avro-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5c742d7ca49172dbe051a9dd89b040c28c5f42b877363eb2e54e9aab329a3ffd
MD5 c44f3109b84e9dcf29f817cf6eb65f3a
BLAKE2b-256 43a99144d3592e7c596f19cbef4e7192f407001077d9aabffe62d2ffe4c98c38

See more details on using hashes here.

File details

Details for the file polars_avro-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_avro-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2437c01626205715346792a3eaca946ae9c02b39b83acbcc0344b5b79d7415df
MD5 319ad2340cf2e725e56fa91d43d0b9ba
BLAKE2b-256 3987a93900ed72540712ddedab40ae9e6cab7c9feb14f023441278657c81c6d0

See more details on using hashes here.

File details

Details for the file polars_avro-0.3.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_avro-0.3.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 661dbc7a24ddac2b100ba77591effd167909dc808fd7eb0f4c245d72bb6e41ab
MD5 2172beb56b53c45ad7f5dba0db91b666
BLAKE2b-256 a197f3204c16a21a5db5422ac9f24f9cbdbdc1a618c5dd9d1bec50c4ffcec890

See more details on using hashes here.

Supported by

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