Skip to content

Commit

Permalink
fix: ensure take_fixed_size_list can handle null indices (#5170)
Browse files Browse the repository at this point in the history
* fix: ensure take_fixed_size_list can handle null indices

* chore: apply clippy suggestion

* Apply suggestions from code review

Co-authored-by: Will Jones <[email protected]>

* Applying suggetions from review

* Using a builder, per review suggestion

* Apply suggestions from code review

Co-authored-by: Will Jones <[email protected]>

* Cast length to usize to avoid compile error

---------

Co-authored-by: Will Jones <[email protected]>
  • Loading branch information
westonpace and wjones127 authored Dec 6, 2023
1 parent 46bbd7d commit f4bad68
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions arrow-select/src/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use std::sync::Arc;

use arrow_array::builder::BufferBuilder;
use arrow_array::builder::{BufferBuilder, UInt32Builder};
use arrow_array::cast::AsArray;
use arrow_array::types::*;
use arrow_array::*;
Expand Down Expand Up @@ -689,7 +689,7 @@ fn take_value_indices_from_fixed_size_list<IndexType>(
where
IndexType: ArrowPrimitiveType,
{
let mut values = vec![];
let mut values = UInt32Builder::with_capacity(length as usize * indices.len());

for i in 0..indices.len() {
if indices.is_valid(i) {
Expand All @@ -699,11 +699,16 @@ where
.ok_or_else(|| ArrowError::ComputeError("Cast to usize failed".to_string()))?;
let start = list.value_offset(index) as <UInt32Type as ArrowPrimitiveType>::Native;

values.extend(start..start + length);
// Safety: Range always has known length.
unsafe {
values.append_trusted_len_iter(start..start + length);
}
} else {
values.append_nulls(length as usize);
}
}

Ok(PrimitiveArray::<UInt32Type>::from(values))
Ok(values.finish())
}

/// To avoid generating take implementations for every index type, instead we
Expand Down Expand Up @@ -1985,6 +1990,23 @@ mod tests {
assert_eq!(&values, &[Some(23), Some(4), None, None])
}

#[test]
fn test_take_fixed_size_list_null_indices() {
let indices = Int32Array::from_iter([Some(0), None]);
let values = Arc::new(Int32Array::from(vec![0, 1, 2, 3]));
let arr_field = Arc::new(Field::new("item", values.data_type().clone(), true));
let values = FixedSizeListArray::try_new(arr_field, 2, values, None).unwrap();

let r = take(&values, &indices, None).unwrap();
let values = r
.as_fixed_size_list()
.values()
.as_primitive::<Int32Type>()
.into_iter()
.collect::<Vec<_>>();
assert_eq!(values, &[Some(0), Some(1), None, None])
}

#[test]
fn test_take_bytes_null_indices() {
let indices = Int32Array::new(
Expand Down

0 comments on commit f4bad68

Please sign in to comment.