Skip to content

Commit

Permalink
Fix DictionaryArray::normalized_keys (#4788)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Sep 7, 2023
1 parent 6fdbc26 commit d657498
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 10 additions & 1 deletion arrow-array/src/array/dictionary_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ impl<K: ArrowDictionaryKeyType> AnyDictionaryArray for DictionaryArray<K> {
let v_len = self.values().len();
assert_ne!(v_len, 0);
let iter = self.keys().values().iter();
iter.map(|x| x.as_usize().min(v_len)).collect()
iter.map(|x| x.as_usize().min(v_len - 1)).collect()
}

fn with_values(&self, values: ArrayRef) -> ArrayRef {
Expand Down Expand Up @@ -1385,4 +1385,13 @@ mod tests {
.collect();
assert_eq!(values, &[Some(50), None, None, Some(2)])
}

#[test]
fn test_normalized_keys() {
let values = vec![132, 0, 1].into();
let nulls = NullBuffer::from(vec![false, true, true]);
let keys = Int32Array::new(values, Some(nulls));
let dictionary = DictionaryArray::new(keys, Arc::new(Int32Array::new_null(2)));
assert_eq!(&dictionary.normalized_keys(), &[1, 0, 1])
}
}
14 changes: 13 additions & 1 deletion arrow-ord/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ impl<'a> ArrayOrd for &'a FixedSizeBinaryArray {
mod tests {
use std::sync::Arc;

use arrow_array::{DictionaryArray, Int32Array, Scalar};
use arrow_array::{DictionaryArray, Int32Array, Scalar, StringArray};

use super::*;

Expand Down Expand Up @@ -708,4 +708,16 @@ mod tests {
let r = eq(&b, &a).unwrap();
assert_eq!(r.len(), 0);
}

#[test]
fn test_dictionary_nulls() {
let values = StringArray::from(vec![Some("us-west"), Some("us-east")]);
let nulls = NullBuffer::from(vec![false, true, true]);

let key_values = vec![100i32, 1i32, 0i32].into();
let keys = Int32Array::new(key_values, Some(nulls));
let col = DictionaryArray::try_new(keys, Arc::new(values)).unwrap();

neq(&col.slice(0, col.len() - 1), &col.slice(1, col.len() - 1)).unwrap();
}
}

0 comments on commit d657498

Please sign in to comment.