Skip to content

Commit

Permalink
Add DictionaryArray::key function (#1912)
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Jun 20, 2022
1 parent 853a405 commit 9059cbf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions arrow/src/array/array_dictionary.rs
Expand Up @@ -169,6 +169,17 @@ impl<'a, K: ArrowPrimitiveType> DictionaryArray<K> {
.iter()
.map(|key| key.map(|k| k.to_usize().expect("Dictionary index not usize")))
}

/// Return the value of `keys` (the dictionary key) at index `i`,
/// cast to `usize`, `None` if the value at `i` is `NULL`.
pub fn key(&self, i: usize) -> Option<usize> {
self.keys.is_valid(i).then(|| {
self.keys
.value(i)
.to_usize()
.expect("Dictionary index not usize")
})
}
}

/// Constructs a `DictionaryArray` from an array data reference.
Expand Down Expand Up @@ -534,6 +545,17 @@ mod tests {
assert!(iter.next().is_none());
}

#[test]
fn test_dictionary_key() {
let keys = Int8Array::from(vec![Some(2), None, Some(1)]);
let values = StringArray::from(vec!["foo", "bar", "baz", "blarg"]);

let array = DictionaryArray::try_new(&keys, &values).unwrap();
assert_eq!(array.key(0), Some(2));
assert_eq!(array.key(1), None);
assert_eq!(array.key(2), Some(1));
}

#[test]
fn test_try_new() {
let values: StringArray = [Some("foo"), Some("bar"), Some("baz")]
Expand Down

0 comments on commit 9059cbf

Please sign in to comment.