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

Store StructArray entries in MapArray #4085

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
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
30 changes: 9 additions & 21 deletions arrow-array/src/array/map_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ pub struct MapArray {
data_type: DataType,
nulls: Option<NullBuffer>,
/// The [`StructArray`] that is the direct child of this array
entries: ArrayRef,
/// The first child of `entries`, the "keys" of this MapArray
keys: ArrayRef,
/// The second child of `entries`, the "values" of this MapArray
values: ArrayRef,
entries: StructArray,
/// The start and end offsets of each entry
value_offsets: OffsetBuffer<i32>,
}
Expand All @@ -54,35 +50,34 @@ impl MapArray {

/// Returns a reference to the keys of this map
pub fn keys(&self) -> &ArrayRef {
&self.keys
self.entries.column(0)
}

/// Returns a reference to the values of this map
pub fn values(&self) -> &ArrayRef {
&self.values
self.entries.column(1)
}

/// Returns a reference to the [`StructArray`] entries of this map
pub fn entries(&self) -> &ArrayRef {
pub fn entries(&self) -> &StructArray {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, but I think is significantly easier to understand, especially since it was already documented to be a StructArray 😅

&self.entries
}

/// Returns the data type of the map's keys.
pub fn key_type(&self) -> &DataType {
self.keys.data_type()
self.keys().data_type()
}

/// Returns the data type of the map's values.
pub fn value_type(&self) -> &DataType {
self.values.data_type()
self.values().data_type()
}

/// Returns ith value of this map array.
///
/// This is a [`StructArray`] containing two fields
/// # Safety
/// Caller must ensure that the index is within the array bounds
pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {
pub unsafe fn value_unchecked(&self, i: usize) -> StructArray {
let end = *self.value_offsets().get_unchecked(i + 1);
let start = *self.value_offsets().get_unchecked(i);
self.entries
Expand All @@ -92,7 +87,7 @@ impl MapArray {
/// Returns ith value of this map array.
///
/// This is a [`StructArray`] containing two fields
pub fn value(&self, i: usize) -> ArrayRef {
pub fn value(&self, i: usize) -> StructArray {
let end = self.value_offsets()[i + 1] as usize;
let start = self.value_offsets()[i] as usize;
self.entries.slice(start, end - start)
Expand All @@ -117,8 +112,6 @@ impl MapArray {
data_type: self.data_type.clone(),
nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
entries: self.entries.clone(),
keys: self.keys.clone(),
values: self.values.clone(),
value_offsets: self.value_offsets.slice(offset, length),
}
}
Expand Down Expand Up @@ -181,10 +174,7 @@ impl MapArray {
entries.data_type()
)));
}

let keys = make_array(entries.child_data()[0].clone());
let values = make_array(entries.child_data()[1].clone());
let entries = make_array(entries);
let entries = entries.into();

// SAFETY:
// ArrayData is valid, and verified type above
Expand All @@ -194,8 +184,6 @@ impl MapArray {
data_type: data.data_type().clone(),
nulls: data.nulls().cloned(),
entries,
keys,
values,
value_offsets,
})
}
Expand Down
9 changes: 4 additions & 5 deletions parquet/src/arrow/arrow_writer/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl LevelInfoBuilder {
}

/// Given an `array`, write the level data for the elements in `range`
fn write(&mut self, array: &ArrayRef, range: Range<usize>) {
fn write(&mut self, array: &dyn Array, range: Range<usize>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is necessary because MapArray::entries now returns StructArray instead of &ArrayRef

match array.data_type() {
d if is_leaf(d) => self.write_leaf(array, range),
DataType::Dictionary(_, v) if is_leaf(v.as_ref()) => {
Expand Down Expand Up @@ -225,7 +225,7 @@ impl LevelInfoBuilder {
&mut self,
offsets: &[O],
nulls: Option<&NullBuffer>,
values: &ArrayRef,
values: &dyn Array,
range: Range<usize>,
) {
let (child, ctx) = match self {
Expand Down Expand Up @@ -372,7 +372,7 @@ impl LevelInfoBuilder {
}

/// Write a primitive array, as defined by [`is_leaf`]
fn write_leaf(&mut self, array: &ArrayRef, range: Range<usize>) {
fn write_leaf(&mut self, array: &dyn Array, range: Range<usize>) {
let info = match self {
Self::Primitive(info) => info,
_ => unreachable!(),
Expand Down Expand Up @@ -918,12 +918,11 @@ mod tests {
assert_eq!(a_list_data.null_count(), 1);

let a = ListArray::from(a_list_data);
let values = Arc::new(a) as _;

let item_field = Field::new("item", a_list_type, true);
let mut builder =
LevelInfoBuilder::try_new(&item_field, Default::default()).unwrap();
builder.write(&values, 2..4);
builder.write(&a, 2..4);
let levels = builder.finish();

assert_eq!(levels.len(), 1);
Expand Down