Skip to content

Commit

Permalink
Fix #26 Support FixedSizeVec
Browse files Browse the repository at this point in the history
  • Loading branch information
ncpenke committed Mar 6, 2022
1 parent 229551b commit 0acad8e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ The following features are supported:
- These can be used via the "override" attribute. Please see the [complex_example.rs](./arrow2_convert/tests/complex_example.rs) for usage.
- Fixed size types:
- [`FixedSizeBinary`]
- [`FixedSizeList`]
- This is supported for a fixed size `Vec<T>` via the `FixedSizeVec` type override.
- Note: nesting of [`FixedSizeList`] is not supported.

The following are not yet supported.

- [`FixedSizeList`]
- Rust enums, slices, references

Note: This is not an exclusive list. Please see the repo issues for current work in progress and add proposals for features that would be useful for your project.
Expand Down
3 changes: 2 additions & 1 deletion arrow2_convert/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ repository = "https://github.com/DataEngineeringLabs/arrow2-convert/arrow2_conve
description = "Convert between nested rust types and Arrow with arrow2"

[dependencies]
arrow2 = { version = "0.9.1", default-features = false }
# Temporary until next arrow2 release
arrow2 = { git = "https://github.com/jorgecarleitao/arrow2", rev = "81bfad", default_features = false }
arrow2_convert_derive = { version = "0.1.0", path = "../arrow2_convert_derive", optional = true }
chrono = { version = "0.4", default_features = false, features = ["std"] }
err-derive = "0.3"
Expand Down
10 changes: 4 additions & 6 deletions arrow2_convert/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,22 +279,20 @@ where

#[inline]
fn new_array() -> Self::MutableArrayType {
Self::MutableArrayType::new(
Self::MutableArrayType::new_with_field(
<T as ArrowSerialize>::new_array(),
"item",
<T as ArrowField>::is_nullable(),
SIZE
)
}

fn arrow_serialize(_v: &<Self as ArrowField>::Type, _array: &mut Self::MutableArrayType) -> arrow2::error::Result<()> {
// TODO: neeed mutable values from arrow2::MutableFixedSizeListArray
/*
fn arrow_serialize(v: &<Self as ArrowField>::Type, array: &mut Self::MutableArrayType) -> arrow2::error::Result<()> {
let values = array.mut_values();
for i in v.iter() {
<T as ArrowSerialize>::arrow_serialize(i, values)?;
}
array.try_push_valid()
*/
unimplemented!()
}
}

Expand Down
9 changes: 7 additions & 2 deletions arrow2_convert/tests/complex_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use arrow2_convert::ArrowField;
use arrow2_convert::deserialize::{arrow_array_deserialize_iterator, TryIntoCollection};
use arrow2_convert::field::{LargeBinary, LargeString, LargeVec, FixedSizeBinary};
use arrow2_convert::field::{LargeBinary, LargeString, LargeVec, FixedSizeBinary, FixedSizeVec};
use arrow2_convert::serialize::TryIntoArrow;
use arrow2::array::*;
use std::borrow::Borrow;
Expand Down Expand Up @@ -52,6 +52,9 @@ pub struct Root {
// large vec
#[arrow_field(override="LargeVec<i64>")]
large_vec: Vec<i64>,
// fixed size vec
#[arrow_field(override="FixedSizeVec<i64, 3>")]
fixed_size_vec: Vec<i64>,
}

#[derive(Debug, Clone, PartialEq, ArrowField)]
Expand Down Expand Up @@ -158,6 +161,7 @@ fn item1() -> Root {
fixed_size_binary: b"aaa".to_vec(),
large_string: "abcdefg".to_string(),
large_vec: vec![1, 2, 3, 4],
fixed_size_vec: vec![10, 20, 30],
}
}

Expand Down Expand Up @@ -203,6 +207,7 @@ fn item2() -> Root {
fixed_size_binary: b"bbb".to_vec(),
large_string: "abdefag".to_string(),
large_vec: vec![5, 4, 3, 2],
fixed_size_vec: vec![11, 21, 32],
}
}

Expand All @@ -219,7 +224,7 @@ fn test_round_trip() -> arrow2::error::Result<()> {
assert_eq!(struct_array.len(), 2);

let values = struct_array.values();
assert_eq!(values.len(), 20);
assert_eq!(values.len(), 21);
assert_eq!(struct_array.len(), 2);

// can iterate one struct at a time without collecting
Expand Down
17 changes: 16 additions & 1 deletion arrow2_convert/tests/test_round_trip.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use arrow2_convert::{ArrowField,field::{LargeString,LargeVec,FixedSizeBinary}};
use arrow2_convert::{ArrowField,field::{LargeString,LargeVec,FixedSizeBinary,FixedSizeVec}};
use arrow2_convert::deserialize::*;
use arrow2_convert::field::LargeBinary;
use arrow2_convert::serialize::*;
Expand Down Expand Up @@ -127,3 +127,18 @@ fn test_large_vec_nested()
let round_trip: Vec<Vec<Vec<u8>>> = b.try_into_collection_as_type::<LargeVec<LargeBinary>>().unwrap();
assert_eq!(round_trip, strs);
}

#[test]
fn test_fixed_size_vec()
{
let ints = vec![vec![1, 2, 3]];
let b: Box<dyn Array> = ints.try_into_arrow_as_type::<FixedSizeVec<i32, 3>>().unwrap();
assert_eq!(b.data_type(),
&DataType::FixedSizeList(Box::new(Field::new(
"item",
DataType::Int32,
false)),
3));
let round_trip: Vec<Vec<i32>> = b.try_into_collection_as_type::<FixedSizeVec<i32, 3>>().unwrap();
assert_eq!(round_trip, ints);
}

0 comments on commit 0acad8e

Please sign in to comment.