Skip to content

Commit

Permalink
*: add cheap validate and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwener committed Mar 22, 2022
1 parent 388b703 commit 61963ba
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions arrow/src/array/array_dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ use super::{
make_array, Array, ArrayData, ArrayRef, PrimitiveArray, PrimitiveBuilder,
StringArray, StringBuilder, StringDictionaryBuilder,
};
use crate::datatypes::ArrowNativeType;
use crate::datatypes::{ArrowDictionaryKeyType, ArrowPrimitiveType, DataType};
use crate::datatypes::{
ArrowDictionaryKeyType, ArrowNativeType, ArrowPrimitiveType, DataType,
};
use crate::error::Result;

/// A dictionary array where each element is a single value indexed by an integer key.
Expand Down Expand Up @@ -96,8 +97,8 @@ impl<'a, K: ArrowPrimitiveType> DictionaryArray<K> {
Box::new(values.data_type().clone()),
);

// Note: This does more work than necessary by rebuilding /
// revalidating all the data
// Note: This use the ArrayDataBuilder::build_unchecked and afterwards
// call the new function which only validates that the keys are in bounds.
let mut data = ArrayData::builder(dict_data_type)
.len(keys.len())
.add_buffer(keys.data().buffers()[0].clone())
Expand All @@ -114,6 +115,7 @@ impl<'a, K: ArrowPrimitiveType> DictionaryArray<K> {

let array = unsafe { data.build_unchecked() };

array.validate()?;
array.validate_dictionary_offset()?;

Ok(array.into())
Expand Down Expand Up @@ -312,8 +314,8 @@ impl<T: ArrowPrimitiveType> fmt::Debug for DictionaryArray<T> {
mod tests {
use super::*;

use crate::array::Int8Array;
use crate::datatypes::Int16Type;
use crate::array::{Float32Array, Int8Array};
use crate::datatypes::{Float32Type, Int16Type};
use crate::{
array::Int16DictionaryArray, array::PrimitiveDictionaryBuilder,
datatypes::DataType,
Expand Down Expand Up @@ -578,4 +580,12 @@ mod tests {
let keys: Int32Array = [Some(-100)].into_iter().collect();
DictionaryArray::<Int32Type>::try_new(&keys, &values).unwrap();
}

#[test]
#[should_panic(expected = "Dictionary values must be integer, but was Float32")]
fn test_try_wrong_dictionary_key_type() {
let values: StringArray = [Some("foo"), Some("bar")].into_iter().collect();
let keys: Float32Array = [Some(0_f32), None, Some(3_f32)].into_iter().collect();
DictionaryArray::<Float32Type>::try_new(&keys, &values).unwrap();
}
}

0 comments on commit 61963ba

Please sign in to comment.