Skip to content

Commit

Permalink
Fix panic when hashing empty FixedSizeList Array (#13533)
Browse files Browse the repository at this point in the history
* Fix panic when hashing empty FixedSizeList Array

Previously it would panic due to division by zero.

* simplify code
  • Loading branch information
findepi authored Nov 24, 2024
1 parent 1a6e9f5 commit 789390e
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions datafusion/common/src/hash_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,16 @@ fn hash_fixed_list_array(
hashes_buffer: &mut [u64],
) -> Result<()> {
let values = Arc::clone(array.values());
let value_len = array.value_length();
let offset_size = value_len as usize / array.len();
let value_length = array.value_length() as usize;
let nulls = array.nulls();
let mut values_hashes = vec![0u64; values.len()];
create_hashes(&[values], random_state, &mut values_hashes)?;
if let Some(nulls) = nulls {
for i in 0..array.len() {
if nulls.is_valid(i) {
let hash = &mut hashes_buffer[i];
for values_hash in &values_hashes[i * offset_size..(i + 1) * offset_size]
for values_hash in
&values_hashes[i * value_length..(i + 1) * value_length]
{
*hash = combine_hashes(*hash, *values_hash);
}
Expand All @@ -339,7 +339,7 @@ fn hash_fixed_list_array(
} else {
for i in 0..array.len() {
let hash = &mut hashes_buffer[i];
for values_hash in &values_hashes[i * offset_size..(i + 1) * offset_size] {
for values_hash in &values_hashes[i * value_length..(i + 1) * value_length] {
*hash = combine_hashes(*hash, *values_hash);
}
}
Expand Down Expand Up @@ -454,6 +454,16 @@ mod tests {
Ok(())
}

#[test]
fn create_hashes_for_empty_fixed_size_lit() -> Result<()> {
let empty_array = FixedSizeListBuilder::new(StringBuilder::new(), 1).finish();
let random_state = RandomState::with_seeds(0, 0, 0, 0);
let hashes_buff = &mut vec![0; 0];
let hashes = create_hashes(&[Arc::new(empty_array)], &random_state, hashes_buff)?;
assert_eq!(hashes, &Vec::<u64>::new());
Ok(())
}

#[test]
fn create_hashes_for_float_arrays() -> Result<()> {
let f32_arr = Arc::new(Float32Array::from(vec![0.12, 0.5, 1f32, 444.7]));
Expand Down

0 comments on commit 789390e

Please sign in to comment.