MCPcopy Index your code
hub / github.com/chmp/serde_arrow

github.com/chmp/serde_arrow @v0.14.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.14.2 ↗ · + Follow
2,059 symbols 7,292 edges 157 files 94 documented · 5%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

Crate info | API docs | Example | Related packages & performance | Status | License | Changes | Contributing

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 such as Polars to query engines such as 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

The following examples assume that serde_arrow is added to the Cargo.toml file and its features are configured. serde_arrow supports different arrow and arrow2 versions. The relevant one can be selected by specifying the correct feature (e.g., arrow-51 to support arrow=51). See here for more details.

The following examples use the following Rust structure and example records

#[derive(Serialize, Deserialize)]
struct Record {
    a: f32,
    b: i32,
}

let records = vec![
    Record { a: 1.0, b: 1 },
    Record { a: 2.0, b: 2 },
    Record { a: 3.0, b: 3 },
];

Serialize to arrow RecordBatch

use arrow::datatypes::FieldRef;
use serde_arrow::schema::{SchemaLike, TracingOptions};

// Determine Arrow schema
let fields = Vec::<FieldRef>::from_type::<Record>(TracingOptions::default())?;

// Build a record batch
let batch = serde_arrow::to_record_batch(&fields, &records)?;

This RecordBatch can now be written to disk using ArrowWriter from the parquet crate.

use use parquet::arrow::ArrowWriter;

let file = File::create("example.pq")?;
let mut writer = ArrowWriter::try_new(file, batch.schema(), None)?;
writer.write(&batch)?;
writer.close()?;

Usage from python

The written files can be read in Python via

# using polars
>>> import polars as pl
>>> pl.read_parquet("example.pq")
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ f32 ┆ i32 │
╞═════╪═════╡
│ 1.0 ┆ 1   │
│ 2.0 ┆ 2   │
│ 3.0 ┆ 3   │
└─────┴─────┘

# using pandas
>>> import pandas as pd
>>> pd.read_parquet("example.pq")
     a  b
0  1.0  1
1  2.0  2
2  3.0  3

Related packages & Performance

  • arrow: the JSON component of the official Arrow package supports serializing objects that support serialize via the Decoder 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.
  • arrow-convert: a port of arrow2-convert to arrow-rs
  • typed-arrow: derive based converter of Rust structs to arrow

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::to serde_arrow::to arrow_json::Rea
arrow2_convert::TryIntoArrow 54.01 1.00 0.31 0.30 0.14
serde_arrow::to_arrow2 173.84 3.22 1.00 0.98 0.46
serde_arrow::to_arrow 177.92 3.29 1.02 1.00 0.47
arrow_json::ReaderBuilder 378.48 7.01 2.18 2.13 1.00

complex_common_serialize(1000000)

label time [ms] arrow2_convert: serde_arrow::to serde_arrow::to arrow_json::Rea
arrow2_convert::TryIntoArrow 576.81 1.00 0.34 0.33 0.16
serde_arrow::to_arrow2 1701.46 2.95 1.00 0.97 0.46
serde_arrow::to_arrow 1748.89 3.03 1.03 1.00 0.48
arrow_json::ReaderBuilder 3676.51 6.37 2.16 2.10 1.00

primitives_serialize(100000)

label time [ms] arrow2_convert: serde_arrow::to serde_arrow::to arrow_json::Rea
arrow2_convert::TryIntoArrow 15.83 1.00 0.51 0.36 0.12
serde_arrow::to_arrow2 30.90 1.95 1.00 0.70 0.23
serde_arrow::to_arrow 43.96 2.78 1.42 1.00 0.33
arrow_json::ReaderBuilder 133.97 8.46 4.34 3.05 1.00

primitives_serialize(1000000)

label time [ms] arrow2_convert: serde_arrow::to serde_arrow::to arrow_json::Rea
arrow2_convert::TryIntoArrow 153.07 1.00 0.47 0.35 0.11
serde_arrow::to_arrow2 327.32 2.14 1.00 0.74 0.23
serde_arrow::to_arrow 440.39 2.88 1.35 1.00 0.31
arrow_json::ReaderBuilder 1429.31 9.34 4.37 3.25 1.00

License

Copyright (c) 2021 - 2024 Christopher Prohm and contributors

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.

Extension points exported contracts — how you extend this code

Context (Interface)
An object that offers additional context to an error [56 implementers]
serde_arrow/src/internal/error.rs
SchemaLike (Interface)
Instances of `SerdeArrowSchema` can be directly serialized and deserialized. The format is that described in [`SchemaLik [5 …
serde_arrow/src/internal/schema/mod.rs
SeqArrayExt (Interface)
An array that models a sequence As some sequence arrays, e.g., `ListArrays`, can contain arbitrarily nested subarrays, [4 …
serde_arrow/src/internal/utils/array_ext.rs
RandomAccessDeserializer (Interface)
(no doc) [19 implementers]
serde_arrow/src/internal/deserialization/random_access_deserializer.rs
Integer (Interface)
(no doc) [8 implementers]
serde_arrow/src/internal/deserialization/integer_deserializer.rs

Core symbols most depended-on inside this repo

serialize
called by 342
serde_arrow/src/test_with_arrow/impls/utils.rs
with_schema
called by 330
serde_arrow/src/test_with_arrow/impls/utils.rs
unwrap
called by 255
serde_arrow/src/test_with_arrow/impls/arrow_time.rs
deserialize
called by 215
serde_arrow/src/test_with_arrow/impls/utils.rs
ctx
called by 186
serde_arrow/src/internal/error.rs
try_
called by 148
serde_arrow/src/internal/error.rs
trace_schema_from_samples
called by 132
serde_arrow/src/test_with_arrow/impls/utils.rs
len
called by 87
serde_arrow/src/internal/utils/array_view_ext.rs

Shape

Method 1,100
Function 689
Class 206
Interface 35
Enum 29

Languages

Rust99%
Python1%

Modules by API surface

serde_arrow/src/internal/utils/value.rs102 symbols
serde_arrow/src/internal/deserialization/random_access_deserializer.rs63 symbols
serde_arrow/src/internal/schema/from_samples/mod.rs52 symbols
serde_arrow/src/internal/schema/tracer.rs50 symbols
serde_arrow/src/internal/schema/from_type/mod.rs47 symbols
serde_arrow/src/internal/deserializer.rs46 symbols
serde_arrow/src/test_with_arrow/impls/arrow_floats.rs41 symbols
serde_arrow/src/internal/chrono.rs40 symbols
serde_arrow/src/internal/serializer.rs39 symbols
serde_arrow/src/internal/serialization/array_builder.rs37 symbols
serde_arrow/src/internal/deserialization/array_deserializer.rs35 symbols
serde_arrow/src/internal/serialization/float_builder.rs34 symbols

For agents

$ claude mcp add serde_arrow \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact