#serde

serde_arrow

convert sequences of structs / maps to arrow tables

15 unstable releases (7 breaking)

0.8.0-rc.2 Aug 14, 2023
0.8.0-rc.1 Jul 8, 2023
0.7.1 May 29, 2023
0.6.0-rc.1 Mar 25, 2023
0.2.0 Aug 4, 2021

#191 in Encoding

Download history 106/week @ 2023-06-02 123/week @ 2023-06-09 87/week @ 2023-06-16 116/week @ 2023-06-23 129/week @ 2023-06-30 92/week @ 2023-07-07 77/week @ 2023-07-14 124/week @ 2023-07-21 28/week @ 2023-07-28 59/week @ 2023-08-04 117/week @ 2023-08-11 78/week @ 2023-08-18 326/week @ 2023-08-25 229/week @ 2023-09-01 187/week @ 2023-09-08 193/week @ 2023-09-15

965 downloads per month
Used in combee

MIT license

575KB
14K SLoC

serde_arrow - convert sequences Rust objects to Arrow arrays and back again

[Crate info] | [API docs] | Changes | Example | Related packages & performance | How does it work? | Status | Development | License

Warning: this package is in an experiment at the moment.

The arrow in-memory format is a powerful way to work with data frame like structures. The surrounding ecosystem includes a rich set of libraries, ranging from data frames via Polars to query engines via DataFusion. However, the API of the underlying Rust crates can be at times cumbersome to use due to the statically typed nature of Rust.

serde_arrow, offers a simple way to convert Rust objects into Arrow arrays and back. serde_arrow relies on the Serde package to interpret Rust objects. Therefore, adding support for serde_arrow to custom types is as easy as using Serde's derive macros.

In the Rust ecosystem there are two competing implementations of the arrow in-memory format. serde_arrow supports both arrow and arrow2 for schema tracing, serialization from Rust structs to arrays, and deserialization from arrays to Rust structs.

Example

#[derive(Serialize)]
struct Item {
    a: f32,
    b: i32,
    point: Point,
}

#[derive(Serialize)]
struct Point(f32, f32);

let items = vec![
    Item { a: 1.0, b: 1, point: Point(0.0, 1.0) },
    Item { a: 2.0, b: 2, point: Point(2.0, 3.0) },
    // ...
];

// detect the field types and convert the items to arrays
use serde_arrow::arrow2::{serialize_into_fields, serialize_into_arrays};

let fields = serialize_into_fields(&items, TracingOptions::default())?;
let arrays = serialize_into_arrays(&fields, &items)?;

These arrays can now be written to disk using the helper method defined in the arrow2 guide. For parquet:

use arrow2::{chunk::Chunk, datatypes::Schema};

// see https://jorgecarleitao.github.io/arrow2/io/parquet_write.html
write_chunk(
    "example.pq",
    Schema::from(fields),
    Chunk::new(arrays),
)?;

The written file can now be read in Python via

# using polars
import polars as pl
pl.read_parquet("example.pq")

# using pandas
import pandas as pd
pd.read_parquet("example.pq")
  • arrow: the JSON component of the official Arrow package supports serializing objects that support serialize via the RawDecoder object. It supports primitives types, structs and lists
  • arrow2-convert: adds derive macros to convert objects from and to arrow2 arrays. It supports primitive types, structs, lists, and chrono's date time types. Enum support is experimental according to the Readme. If performance is the main objective, arrow2-convert is a good choice as it has no or minimal overhead over building the arrays manually.

The different implementation have the following performance differences, when compared to arrow2-convert:

Time

The detailed runtimes of the benchmarks are listed below.

complex_common_serialize(100000)

label time [ms] arrow2_convert serde_arrow arrow
arrow2_convert 53.14 1.00 0.15 0.03
serde_arrow 348.85 6.57 1.00 0.20
arrow 1703.18 32.05 4.88 1.00

complex_common_serialize(1000000)

label time [ms] arrow2_convert serde_arrow arrow
arrow2_convert 478.85 1.00 0.18 0.05
serde_arrow 2711.26 5.66 1.00 0.31
arrow 8729.60 18.23 3.22 1.00

primitives_serialize(100000)

label time [ms] arrow2_convert serde_arrow arrow
arrow2_convert 13.64 1.00 0.19 0.07
serde_arrow 73.32 5.38 1.00 0.38
arrow 192.76 14.13 2.63 1.00

primitives_serialize(1000000)

label time [ms] arrow2_convert serde_arrow arrow
arrow2_convert 178.98 1.00 0.25 0.09
serde_arrow 716.76 4.00 1.00 0.36
arrow 2005.61 11.21 2.80 1.00

Development

All common tasks are bundled in the x.py script:

# format the code and run tests
python x.py precommit

Run python x.py --help for details. The script only uses standard Python modules can can be run without installing further packages.

License

Copyright (c) 2021 - 2023 Christopher Prohm

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Dependencies

~1–33MB
~483K SLoC