Skip to content
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

Fix MapBuilder example #3246

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 16 additions & 89 deletions arrow-array/src/builder/map_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,32 @@ use std::sync::Arc;

/// Creates a new `MapBuilder`
/// ```
/// use arrow_array::builder::{MapBuilder, Int32Builder, StringBuilder};
/// use arrow_array::{StringArray, Int32Array};
/// use std::sync::Arc;
/// # use arrow_array::builder::{Int32Builder, MapBuilder, StringBuilder};
/// # use arrow_array::{Int32Array, StringArray};
///
/// let string_builder = StringBuilder::new();
/// let int_builder = Int32Builder::with_capacity(4);
///
/// // Construct `[{"joe": 1}, {"blogs": 2, "foo": 4}, {}, null]`
/// let mut builder = MapBuilder::new(None, string_builder, int_builder);
///
/// let string_builder = builder.keys();
/// string_builder.append_value("joe");
/// string_builder.append_value("n1");
/// string_builder.append_value("n2");
/// string_builder.append_value("mark");
///
/// let int_builder = builder.values();
/// int_builder.append_value(1);
/// int_builder.append_value(2);
/// int_builder.append_null();
/// int_builder.append_value(4);
/// builder.keys().append_value("joe");
/// builder.values().append_value(1);
/// builder.append(true).unwrap();
///
/// builder.keys().append_value("blogs");
/// builder.values().append_value(2);
/// builder.keys().append_value("foo");
/// builder.values().append_value(4);
/// builder.append(true).unwrap();
/// builder.append(false).unwrap();
/// builder.append(true).unwrap();
/// builder.append(false).unwrap();
///
/// let array = builder.finish();
/// assert_eq!(array.value_offsets(), &[0, 1, 3, 3, 3]);
/// assert_eq!(*array.values(), Int32Array::from(vec![1, 2, 4]));
/// assert_eq!(*array.keys(), StringArray::from(vec!["joe", "blogs", "foo"]));
///
/// let arr = builder.finish();
/// assert_eq!(
/// *arr.values(),
/// Int32Array::from(vec![Some(1), Some(2), None, Some(4)])
/// );
/// assert_eq!(
/// *arr.keys(),
/// StringArray::from(vec![Some("joe"), Some("n1"), Some("n2"), Some("mark")])
/// );
/// ```
#[derive(Debug)]
pub struct MapBuilder<K: ArrayBuilder, V: ArrayBuilder> {
Expand Down Expand Up @@ -91,7 +83,6 @@ impl Default for MapFieldNames {
}
}

#[allow(dead_code)]
impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> {
/// Creates a new `MapBuilder`
pub fn new(
Expand Down Expand Up @@ -264,67 +255,3 @@ impl<K: ArrayBuilder, V: ArrayBuilder> ArrayBuilder for MapBuilder<K, V> {
self
}
}

#[cfg(test)]
mod tests {
use super::*;
use arrow_buffer::Buffer;
use arrow_data::Bitmap;

use crate::builder::{Int32Builder, StringBuilder};

// TODO: add a test that finishes building, after designing a spec-compliant
// way of inserting values to the map.
// A map's values shouldn't be repeated within a slot

#[test]
fn test_map_array_builder() {
let string_builder = StringBuilder::new();
let int_builder = Int32Builder::with_capacity(4);

let mut builder = MapBuilder::new(None, string_builder, int_builder);

let string_builder = builder.keys();
string_builder.append_value("joe");
string_builder.append_value("n1");
string_builder.append_value("n2");
string_builder.append_value("mark");

let int_builder = builder.values();
int_builder.append_value(1);
int_builder.append_value(2);
int_builder.append_null();
int_builder.append_value(4);

builder.append(true).unwrap();
builder.append(false).unwrap();
builder.append(true).unwrap();

let arr = builder.finish();

let map_data = arr.data();
assert_eq!(3, map_data.len());
assert_eq!(1, map_data.null_count());
assert_eq!(
Some(&Bitmap::from(Buffer::from(&[5_u8]))),
map_data.null_bitmap()
);

let expected_string_data = ArrayData::builder(DataType::Utf8)
.len(4)
.add_buffer(Buffer::from_slice_ref([0, 3, 5, 7, 11]))
.add_buffer(Buffer::from_slice_ref(b"joen1n2mark"))
.build()
.unwrap();

let expected_int_data = ArrayData::builder(DataType::Int32)
.len(4)
.null_bit_buffer(Some(Buffer::from_slice_ref([11_u8])))
.add_buffer(Buffer::from_slice_ref([1, 2, 0, 4]))
.build()
.unwrap();

assert_eq!(&expected_string_data, arr.keys().data());
assert_eq!(&expected_int_data, arr.values().data());
}
}