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

Fixed growable of dictionaries negative keys #582

Merged
merged 4 commits into from
Nov 7, 2021
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
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:
Expand Down Expand Up @@ -127,8 +128,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())
jorgecarleitao marked this conversation as resolved.
Show resolved Hide resolved
} 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()),
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
);
}

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