Skip to content

Commit

Permalink
Add entries to MapBuilder to return both key and value array buil…
Browse files Browse the repository at this point in the history
…ders (#5218)

* Add keys_and_values to MapBuilder to return both key and value array builders

* Rename to `entries`.
  • Loading branch information
viirya authored Dec 18, 2023
1 parent 9a1e8b5 commit 9e060dc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions arrow-array/src/builder/map_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> {
&mut self.value_builder
}

/// Returns both the key and value array builders of the map
pub fn entries(&mut self) -> (&mut K, &mut V) {
(&mut self.key_builder, &mut self.value_builder)
}

/// Finish the current map array slot
///
/// Returns an error if the key and values builders are in an inconsistent state.
Expand Down
51 changes: 51 additions & 0 deletions arrow/tests/array_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,57 @@ fn test_map_nulls_append() {
assert_eq!(result, expected_list_data);
}

#[test]
fn test_map_keys_values_append() {
let mut builder = MapBuilder::<Int64Builder, Int64Builder>::new(
None,
Int64Builder::with_capacity(32),
Int64Builder::with_capacity(32),
);
let (keys, values) = builder.entries();
keys.append_slice(&[1, 2, 3]);
values.append_slice(&[1, 3, 4]);
builder.append(true).unwrap();

let (keys, values) = builder.entries();
keys.append_slice(&[4, 5]);
values.append_slice(&[4, 6]);
builder.append(true).unwrap();

builder.append(false).unwrap();

let map = builder.finish();
assert!(map.is_null(2));

let first = map.value(0);
let keys = first
.column(0)
.as_any()
.downcast_ref::<Int64Array>()
.unwrap();
let values = first
.column(1)
.as_any()
.downcast_ref::<Int64Array>()
.unwrap();
assert_eq!(keys, &Int64Array::from(vec![Some(1), Some(2), Some(3)]));
assert_eq!(values, &Int64Array::from(vec![Some(1), Some(3), Some(4)]));

let second = map.value(1);
let keys = second
.column(0)
.as_any()
.downcast_ref::<Int64Array>()
.unwrap();
let values = second
.column(1)
.as_any()
.downcast_ref::<Int64Array>()
.unwrap();
assert_eq!(keys, &Int64Array::from(vec![Some(4), Some(5)]));
assert_eq!(values, &Int64Array::from(vec![Some(4), Some(6)]));
}

#[test]
fn test_list_of_strings_append() {
// [["alpha", "beta", None]]
Expand Down

0 comments on commit 9e060dc

Please sign in to comment.