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 2 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
2 changes: 1 addition & 1 deletion src/array/growable/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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()),
.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
19 changes: 19 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,24 @@ 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 out = arrow2::compute::concat::concatenate(&[&arr]).unwrap();
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
let out = out.as_any().downcast_ref::<DictionaryArray<i32>>().unwrap();
assert_eq!(out, &arr);
}

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