-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New helper for sticking Serde-encodable data into arrow #2004
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
91faa17
New helper for sticking Serde-encodable data into arrow
jleibs e3fb5ae
Taplo
jleibs 7cdc4af
Dep leak from cherry-pick
jleibs 7ac1896
Docstring
jleibs 57cce2a
Shorter docstring and profiles
jleibs c62b73a
Merge branch 'main' into jleibs/serde_field
jleibs ee3fef1
Add rmp error to the ExternalFormat error
jleibs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use arrow2::{ | ||
array::{BinaryArray, MutableBinaryArray, TryPush}, | ||
datatypes::DataType, | ||
}; | ||
use arrow2_convert::{deserialize::ArrowDeserialize, field::ArrowField, serialize::ArrowSerialize}; | ||
|
||
/// Helper for storing arbitrary serde-compatible types in an arrow2 [`BinaryArray`] field | ||
/// | ||
/// Use as: | ||
/// ``` | ||
/// use re_log_types::serde_field::SerdeField; | ||
/// use arrow2_convert::{ArrowDeserialize, ArrowField, ArrowSerialize, field::ArrowField}; | ||
/// use arrow2::datatypes::{DataType, Field}; | ||
/// | ||
/// #[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)] | ||
/// struct SomeStruct { | ||
/// foo: String, | ||
/// bar: u32, | ||
/// } | ||
/// | ||
/// #[derive(Clone, Debug, PartialEq, ArrowField, ArrowSerialize, ArrowDeserialize)] | ||
/// #[arrow_field(transparent)] | ||
/// struct SomeStructArrow(#[arrow_field(type = "SerdeField<SomeStruct>")] SomeStruct); | ||
/// | ||
/// assert_eq!(SomeStructArrow::data_type(), DataType::Binary); | ||
/// ``` | ||
pub struct SerdeField<T>(std::marker::PhantomData<T>); | ||
|
||
impl<T> ArrowField for SerdeField<T> | ||
where | ||
T: serde::ser::Serialize + serde::de::DeserializeOwned, | ||
{ | ||
type Type = T; | ||
|
||
#[inline] | ||
fn data_type() -> DataType { | ||
arrow2::datatypes::DataType::Binary | ||
} | ||
} | ||
|
||
impl<T> ArrowSerialize for SerdeField<T> | ||
where | ||
T: serde::ser::Serialize + serde::de::DeserializeOwned, | ||
{ | ||
type MutableArrayType = MutableBinaryArray<i32>; | ||
#[inline] | ||
fn new_array() -> Self::MutableArrayType { | ||
MutableBinaryArray::new() | ||
} | ||
|
||
fn arrow_serialize( | ||
v: &<Self as ArrowField>::Type, | ||
array: &mut Self::MutableArrayType, | ||
) -> arrow2::error::Result<()> { | ||
crate::profile_function!(); | ||
let mut buf = Vec::new(); | ||
rmp_serde::encode::write_named(&mut buf, v).map_err(|_err| { | ||
// TODO(jleibs): Could not get re_error::format() to work here | ||
jleibs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
arrow2::error::Error::ExternalFormat("Could not encode as rmp".to_owned()) | ||
})?; | ||
array.try_push(Some(buf)) | ||
} | ||
} | ||
|
||
impl<T> ArrowDeserialize for SerdeField<T> | ||
where | ||
T: serde::ser::Serialize + serde::de::DeserializeOwned, | ||
{ | ||
type ArrayType = BinaryArray<i32>; | ||
|
||
#[inline] | ||
fn arrow_deserialize(v: <&Self::ArrayType as IntoIterator>::Item) -> Option<T> { | ||
crate::profile_function!(); | ||
v.and_then(|v| rmp_serde::from_slice::<T>(v).ok()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::SerdeField; | ||
use arrow2_convert::{ArrowDeserialize, ArrowField, ArrowSerialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)] | ||
struct SomeStruct { | ||
foo: String, | ||
bar: u32, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, ArrowField, ArrowSerialize, ArrowDeserialize)] | ||
struct SomeArrowStruct { | ||
#[arrow_field(type = "SerdeField<SomeStruct>")] | ||
field: SomeStruct, | ||
} | ||
|
||
impl From<SomeStruct> for SomeArrowStruct { | ||
fn from(s: SomeStruct) -> Self { | ||
Self { field: s } | ||
} | ||
} | ||
|
||
#[test] | ||
fn round_trip_serdefield() { | ||
use arrow2_convert::{deserialize::TryIntoCollection, serialize::TryIntoArrow}; | ||
|
||
let data: [SomeArrowStruct; 2] = [ | ||
SomeStruct { | ||
foo: "hello".into(), | ||
bar: 42, | ||
} | ||
.into(), | ||
SomeStruct { | ||
foo: "world".into(), | ||
bar: 1983, | ||
} | ||
.into(), | ||
]; | ||
let array: Box<dyn arrow2::array::Array> = data.try_into_arrow().unwrap(); | ||
let ret: Vec<SomeArrowStruct> = array.try_into_collection().unwrap(); | ||
assert_eq!(&data, ret.as_slice()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder how we should handle the fact that we use MsgPack as the "codec". There are alternatives, like
bincode
, we could be using, and might want to switch too in the future.We could, for instance, add an
enum Codec { MsgPack = 1 }
and put that as the first byte of the encoded array.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the "right" thing to do in theory is to add this to the arrow meta-data for the field. We've been wanting to use that for some other stuff as well so this might be a good opportunity to investigate the plumbing.