Skip to content
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
27 changes: 11 additions & 16 deletions arrow-array/src/array/dictionary_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
};
use arrow_buffer::bit_util::set_bit;
use arrow_buffer::buffer::NullBuffer;
use arrow_buffer::{ArrowNativeType, BooleanBuffer, BooleanBufferBuilder};
use arrow_buffer::{ArrowNativeType, BooleanBuffer, BooleanBufferBuilder, ScalarBuffer};
use arrow_data::ArrayData;
use arrow_schema::{ArrowError, DataType};
use std::any::Any;
Expand Down Expand Up @@ -580,21 +580,25 @@ impl<K: ArrowDictionaryKeyType> DictionaryArray<K> {
}
}

/// Constructs a `DictionaryArray` from an array data reference.
/// Constructs a `DictionaryArray` from an `ArrayData`
impl<T: ArrowDictionaryKeyType> From<ArrayData> for DictionaryArray<T> {
fn from(data: ArrayData) -> Self {
let (data_type, len, nulls, offset, mut buffers, mut child_data) = data.into_parts();

assert_eq!(
data.buffers().len(),
buffers.len(),
1,
"DictionaryArray data should contain a single buffer only (keys)."
);
let buffer = buffers.pop().expect("checked above");
assert_eq!(
data.child_data().len(),
child_data.len(),
1,
"DictionaryArray should contain a single child array (values)."
);
let cd = child_data.pop().expect("checked above");

if let DataType::Dictionary(key_data_type, _) = data.data_type() {
if let DataType::Dictionary(key_data_type, _) = &data_type {
assert_eq!(
&T::DATA_TYPE,
key_data_type.as_ref(),
Expand All @@ -603,19 +607,10 @@ impl<T: ArrowDictionaryKeyType> From<ArrayData> for DictionaryArray<T> {
key_data_type
);

let values = make_array(data.child_data()[0].clone());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

saves a clone here of both the child data and also a DataType::Drop

let data_type = data.data_type().clone();
let values = make_array(cd);

// create a zero-copy of the keys' data
// SAFETY:
// ArrayData is valid and verified type above

let keys = PrimitiveArray::<T>::from(unsafe {
data.into_builder()
.data_type(T::DATA_TYPE)
.child_data(vec![])
.build_unchecked()
});
let keys = PrimitiveArray::<T>::new(ScalarBuffer::new(buffer, offset, len), nulls);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bonus removal of unsafe


Self {
data_type,
Expand Down
Loading