rluvaton commented on code in PR #10121:
URL: https://github.com/apache/arrow-rs/pull/10121#discussion_r3397100891
##########
arrow-string/src/length.rs:
##########
@@ -340,6 +346,76 @@ mod tests {
length_list_helper!(i64, Int64Array, Float32Type, value, result)
}
+ fn convert_vec_to_map<K, V, KeyArray, ValueArray>(
+ input: Vec<Option<HashMap<K, Option<V>>>>,
+ ) -> MapArray
+ where
+ K: Clone,
+ V: Clone,
+ Vec<K>: Into<KeyArray>,
+ Vec<Option<V>>: Into<ValueArray>,
+ KeyArray: Array + 'static,
+ ValueArray: Array + 'static,
+ {
+ let offsets = OffsetBuffer::<i32>::from_lengths(
+ input.iter().map(|v| v.as_ref().map_or(0, |m| m.len())),
+ );
+ let nulls = NullBuffer::from_iter(input.iter().map(|v| v.is_some()));
+ let nulls = Some(nulls).filter(|b| b.null_count() > 0);
+ let keys = input
+ .iter()
+ .flatten()
+ .flat_map(|v| v.keys())
+ .cloned()
+ .collect::<Vec<K>>();
+ let values = input
+ .iter()
+ .flatten()
+ .flat_map(|v| v.values())
+ .cloned()
+ .collect::<Vec<Option<V>>>();
+
+ let keys_array = keys.into();
+ let values_array = values.into();
+
+ let entries = StructArray::new(
+ Fields::from(vec![
+ Field::new("keys", keys_array.data_type().clone(), false),
+ Field::new(
+ "values",
+ values_array.data_type().clone(),
+ values_array.is_nullable(),
+ ),
+ ]),
+ vec![Arc::new(keys_array), Arc::new(values_array)],
+ None,
+ );
+
+ MapArray::new(
+ Arc::new(Field::new("entries", entries.data_type().clone(),
false)),
+ offsets,
+ entries,
+ nulls,
+ false,
+ )
+ }
Review Comment:
Created this helper so the test will be cleaner, maybe I will add it to
`MapArray`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]