Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Fixed growable of dictionaries negative keys (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored and jorgecarleitao committed Nov 7, 2021
1 parent d952a29 commit 51c7579
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/array/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use iterator::*;
pub use mutable::*;

use super::{new_empty_array, primitive::PrimitiveArray, Array};
use crate::scalar::NullScalar;

/// Trait denoting [`NativeType`]s that can be used as keys of a dictionary.
pub trait DictionaryKey: NativeType + num_traits::NumCast + num_traits::FromPrimitive {
Expand Down Expand Up @@ -140,8 +141,12 @@ impl<K: DictionaryKey> DictionaryArray<K> {
/// Returns the value of the [`DictionaryArray`] at position `i`.
#[inline]
pub fn value(&self, index: usize) -> Box<dyn Scalar> {
let index = self.keys.value(index).to_usize().unwrap();
new_scalar(self.values.as_ref(), index)
if self.keys.is_null(index) {
Box::new(NullScalar::new())
} else {
let index = self.keys.value(index).to_usize().unwrap();
new_scalar(self.values.as_ref(), index)
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/array/growable/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ impl<'a, T: DictionaryKey> Growable<'a> for GrowableDictionary<'a, T> {
self.key_values.extend(
values
.iter()
.map(|x| T::from_usize(offset + x.to_usize().unwrap()).unwrap()),
// `.unwrap_or(0)` because this operation does not check for null values, which may contain any key.
.map(|x| T::from_usize(offset + x.to_usize().unwrap_or(0)).unwrap()),
);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/it/array/growable/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use arrow2::array::growable::{Growable, GrowableDictionary};
use arrow2::array::*;
use arrow2::datatypes::DataType;
use arrow2::error::Result;

#[test]
Expand Down Expand Up @@ -29,6 +30,25 @@ fn test_single() -> Result<()> {
Ok(())
}

#[test]
fn test_negative_keys() {
let vals = vec![Some("a"), Some("b"), Some("c")];
let keys = vec![0, 1, 2, -1];

let keys = PrimitiveArray::from_data(
DataType::Int32,
keys.into(),
Some(vec![true, true, true, false].into()),
);

let arr = DictionaryArray::from_data(keys, Arc::new(Utf8Array::<i32>::from(vals)));
// check that we don't panic with negative keys to usize conversion
let mut growable = GrowableDictionary::new(&[&arr], false, 0);
growable.extend(0, 0, 4);
let out: DictionaryArray<i32> = growable.into();
assert_eq!(out, arr);
}

#[test]
fn test_multi() -> Result<()> {
let mut original_data1 = vec![Some("a"), Some("b"), None, Some("a")];
Expand Down

0 comments on commit 51c7579

Please sign in to comment.