Skip to content
Open
Changes from all 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
71 changes: 71 additions & 0 deletions arrow-select/src/interleave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub fn interleave(
return Ok(new_empty_array(data_type));
}

validate_interleave_indices(values, indices)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the cost of this separate pass will be quite high, other kernels don't do this (or make it optional).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Would you prefer making this optional, similar to take bounds checking, or keeping the fast path unchanged and documenting that invalid indices may panic?


downcast_primitive! {
data_type => (primitive_helper, values, indices, data_type),
DataType::Utf8 => interleave_bytes::<Utf8Type>(values, indices),
Expand Down Expand Up @@ -122,6 +124,29 @@ pub fn interleave(
}
}

fn validate_interleave_indices(
values: &[&dyn Array],
indices: &[(usize, usize)],
) -> Result<(), ArrowError> {
for (index, &(array_idx, row_idx)) in indices.iter().enumerate() {
let array = values.get(array_idx).ok_or_else(|| {
ArrowError::InvalidArgumentError(format!(
"interleave index {index} references array {array_idx}, but only {} arrays were provided",
values.len()
))
})?;

if row_idx >= array.len() {
return Err(ArrowError::InvalidArgumentError(format!(
"interleave index {index} references row {row_idx} of array {array_idx}, but the array has length {}",
array.len()
)));
}
}

Ok(())
}

/// Common functionality for interleaving arrays
///
/// T is the concrete Array type
Expand Down Expand Up @@ -889,6 +914,32 @@ mod tests {
assert_eq!(v.data_type(), &DataType::Int32);
}

#[test]
fn test_interleave_out_of_bounds_array_index() {
let a = Int32Array::from_iter_values([1, 2, 3, 4]);

let err = interleave(&[&a], &[(1, 0)]).unwrap_err();

assert!(matches!(
err,
ArrowError::InvalidArgumentError(message)
if message.contains("references array 1")
));
}

#[test]
fn test_interleave_out_of_bounds_row_index() {
let a = Int32Array::from_iter_values([1, 2, 3, 4]);

let err = interleave(&[&a], &[(0, 4)]).unwrap_err();

assert!(matches!(
err,
ArrowError::InvalidArgumentError(message)
if message.contains("references row 4")
));
}

#[test]
fn test_strings() {
let a = StringArray::from_iter_values(["a", "b", "c"]);
Expand Down Expand Up @@ -1536,6 +1587,26 @@ mod tests {
);
}

#[test]
fn test_interleave_views_out_of_bounds_row_index() {
let values = StringArray::from_iter_values([
"hello",
"world_long_string_not_inlined",
"foo",
"bar",
"baz",
]);
let view = StringViewArray::from(&values);

let err = interleave(&[&view], &[(0, 5)]).unwrap_err();

assert!(matches!(
err,
ArrowError::InvalidArgumentError(message)
if message.contains("references row 5")
));
}

#[test]
fn test_interleave_views_with_nulls() {
let values = StringArray::from_iter([
Expand Down
Loading