9 releases

0.6.8 Jun 10, 2024
0.6.7 May 1, 2024
0.6.6 Apr 21, 2024
0.6.3 Mar 29, 2024

#171 in Rust patterns

Download history 286/week @ 2024-03-24 227/week @ 2024-03-31 29/week @ 2024-04-07 152/week @ 2024-04-14 560/week @ 2024-04-21 523/week @ 2024-04-28 143/week @ 2024-05-05 373/week @ 2024-05-12 417/week @ 2024-05-19 330/week @ 2024-05-26 559/week @ 2024-06-02 1988/week @ 2024-06-09 2523/week @ 2024-06-16

5,409 downloads per month

Apache-2.0 OR MIT

60KB
1.5K SLoC

arrow_convert

Provides an API on top of arrow-rs to convert between rust types and Arrow. This repository was ported from the directly converted from arrow2-convert library for use with arrow-rs.

The Arrow ecosystem provides many ways to convert between Arrow and other popular formats across several languages. This project aims to serve the need for rust-centric data pipelines to easily convert to/from Arrow with strong typing and arbitrary nesting.

Example

The example below performs a round trip conversion of a struct with a single field.

Please see the complex_example.rs for usage of the full functionality.

/// Simple example

use arrow::array::{Array, ArrayRef};
use arrow_convert::{deserialize::TryIntoCollection, serialize::TryIntoArrow, ArrowField, ArrowSerialize, ArrowDeserialize};

#[derive(Debug, Clone, PartialEq, ArrowField, ArrowSerialize, ArrowDeserialize)]
pub struct Foo {
    name: String,
}

fn main() {
    // an item
    let original_array = [
        Foo { name: "hello".to_string() },
        Foo { name: "one more".to_string() },
        Foo { name: "good bye".to_string() },
    ];

    // serialize to an arrow array. try_into_arrow() is enabled by the TryIntoArrow trait
    let arrow_array: ArrayRef = original_array.try_into_arrow().unwrap();

    // which can be cast to an Arrow StructArray and be used for all kinds of IPC, FFI, etc.
    // supported by `arrow`
    let struct_array= arrow_array.as_any().downcast_ref::<arrow::array::StructArray>().unwrap();
    assert_eq!(struct_array.len(), 3);

    // deserialize back to our original vector via TryIntoCollection trait.
    let round_trip_array: Vec<Foo> = arrow_array.try_into_collection().unwrap();
    assert_eq!(round_trip_array, original_array);
}

API

Types that implement the ArrowField, ArrowSerialize and ArrowDeserialize traits can be converted to/from Arrow via the try_into_arrow and the try_into_collection methods.

The ArrowField, ArrowSerialize and ArrowDeserialize derive macros can be used to generate implementations of these traits for structs and enums. Custom implementations can also be defined for any type that needs to convert to/from Arrow by manually implementing the traits.

For serializing to arrow, TryIntoArrow::try_into_arrow can be used to serialize any iterable into an arrow::Array or a arrow::Chunk. arrow::Array represents the in-memory Arrow layout. arrow::Chunk represents a column group and can be used with arrow API for other functionality such converting to parquet and arrow flight RPC.

For deserializing from arrow, the TryIntoCollection::try_into_collection can be used to deserialize from an arrow::Array representation into any container that implements FromIterator.

Default implementations

Default implementations of the above traits are provided for the following:

Enums

Enums are still an experimental feature and need to be integrated tested. Rust enum arrays are converted to a Arrow::UnionArray. Some additional notes on enums:

  • Rust unit variants are represented using as the bool data type.

i128

i128 represents a decimal number and requires the precision and scale to be specified to be used as an Arrow data type. The precision and scale can be specified by using a type override via the I128 type.

For example to use i128 as a field in a struct:

use arrow_convert::field::I128;
use arrow_convert::ArrowField;

#[derive(Debug, ArrowField)]
struct S {
    #[arrow_field(type = "I128<32, 32>")]
    field: i128,
}

A vec<i128> can be converted. to/from arrow by using the arrow_serialize_to_mutable_array and arrow_array_deserialize_iterator_as_type methods.

use arrow::array::{Array, ArrayBuilder, ArrayRef};
use arrow_convert::serialize::arrow_serialize_to_mutable_array;
use arrow_convert::deserialize::arrow_array_deserialize_iterator_as_type;
use arrow_convert::field::I128;
use std::borrow::Borrow;
use std::sync::Arc;

fn convert_i128() {
    let original_array = vec![1 as i128, 2, 3];
    let b: ArrayRef = Arc::new(arrow_serialize_to_mutable_array::<_, I128<32,32>, _>(
        &original_array).unwrap().finish());
    let round_trip: Vec<i128> = arrow_array_deserialize_iterator_as_type::<_, I128<32,32>>(
        b.borrow()).unwrap().collect();
    assert_eq!(original_array, round_trip);
}

Nested Option Types

Since the Arrow format only supports one level of validity, nested option types such as Option<Option<T>>, after serialization to Arrow, will lose any intermediate nesting of None values. For example, Some(None) will be serialized to None,

Missing Features

  • Support for generics, slices and reference is currently missing.

This is not an exhaustive list. Please open an issue if you need a feature.

Memory

Pass-thru conversions perform a single memory copy. Deserialization performs a copy from arrow to the destination. Serialization performs a copy from the source to arrow. In-place deserialization is theoretically possible but currently not supported.

Internals

Similarities with Serde

The design is inspired by serde. The ArrowSerialize and ArrowDeserialize are analogs of serde's Serialize and Deserialize respectively.

However unlike serde's traits provide an exhaustive and flexible mapping to the serde data model, arrow_convert's traits provide a much more narrower mapping to arrow's data structures.

Specifically, the ArrowSerialize trait provides the logic to serialize a type to the corresponding arrow::array::ArrayBuilder. The ArrowDeserialize trait deserializes a type from the corresponding arrow::array::ArrowArray.

Workarounds

Features such as partial implementation specialization and generic associated types (currently only available in nightly builds) can greatly simplify the underlying implementation.

For example custom types need to explicitly enable Vec serialization via the arrow_enable_vec_for_type macro on the primitive type. This is needed since Vec is a special type in Arrow, but without implementation specialization there's no way to special-case it.

Availability of generaic associated types would simplify the implementation for large and fixed types, since a generic ArrayBuilder can be defined. Ideally for code reusability, we wouldn’t have to reimplement ArrowSerialize and ArrowDeserialize for large and fixed size types since the primitive types are the same. However, this requires the trait functions to take a generic bounded mutable array as an argument instead of a single array type. This requires the ArrowSerialize and ArrowDeserialize implementations to be able to specify the bounds as part of the associated type, which is not possible without generic associated types.

As a result, we’re forced to sacrifice code reusability and introduce a little bit of complexity by providing separate ArrowSerialize and ArrowDeserialize implementations for large and fixed size types via placeholder structures. This also requires introducing the Type associated type to ArrowField so that the arrow type can be overriden via a macro field attribute without affecting the actual type.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~13–22MB
~287K SLoC