Skip to content

Commit

Permalink
Fix maps as well
Browse files Browse the repository at this point in the history
  • Loading branch information
chmp committed Oct 22, 2024
1 parent d11f139 commit 54638c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions serde_arrow/src/internal/deserialization/map_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ pub struct MapDeserializer<'a> {
impl<'a> MapDeserializer<'a> {
pub fn new(
path: String,
key: ArrayDeserializer<'a>,
value: ArrayDeserializer<'a>,
mut key: ArrayDeserializer<'a>,
mut value: ArrayDeserializer<'a>,
offsets: &'a [i32],
validity: Option<BitsWithOffset<'a>>,
) -> Result<Self> {
check_supported_list_layout(validity, offsets)?;

// skip any unused values up front
for _ in 0..usize::try_from(offsets[0])? {
key.deserialize_ignored_any(serde::de::IgnoredAny)?;
value.deserialize_ignored_any(serde::de::IgnoredAny)?;
}

Ok(Self {
path,
key: Box::new(key),
Expand Down
24 changes: 24 additions & 0 deletions serde_arrow/src/test_with_arrow/issue_248_sliced_lists.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::HashMap;

use crate::_impl::arrow::datatypes::FieldRef;
use crate::internal::testing::hash_map;
use crate::{
self as serde_arrow,
schema::{SchemaLike, TracingOptions},
Expand Down Expand Up @@ -41,3 +44,24 @@ fn vec_of_strings() {
let actual: Vec<Struct> = serde_arrow::from_record_batch(&batch).unwrap();
assert_eq!(data[5..10], actual);
}

#[test]
fn map_of_strings() {
#[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq)]
struct Struct {
string: HashMap<String, String>,
}
let data = (1..=10)
.map(|x| Struct {
string: hash_map!(x.to_string() => x.to_string()),
})
.collect::<Vec<_>>();
let fields =
Vec::<FieldRef>::from_type::<Struct>(TracingOptions::default().map_as_struct(false))
.unwrap();
let batch = serde_arrow::to_record_batch(&fields, &data).unwrap();
let batch = batch.slice(5, 5);

let actual: Vec<Struct> = serde_arrow::from_record_batch(&batch).unwrap();
assert_eq!(data[5..10], actual);
}

0 comments on commit 54638c0

Please sign in to comment.