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
44 changes: 26 additions & 18 deletions arrow-array/src/array/fixed_size_binary_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,31 @@ impl FixedSizeBinaryArray {
ArrowError::InvalidArgumentError(format!("Size cannot be negative, got {size}"))
})?;

let len = if s == 0 {
if !values.is_empty() {
return Err(ArrowError::InvalidArgumentError(
"Buffer cannot have non-zero length if the item size is zero".to_owned(),
));
let len = match values.len().checked_div(s) {
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.

what is the clippy lint that is triggered?

To be honest I found this formulation harder to read than the previous one

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.

its going to be clippy::manual_checked_ops which I think will be part of 1.95, I just ran into it because I was building with nightly.

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.

obviously this is a really nitpicky thing, glad to close this PR and just punt on it to whenever it becomes an actual issue

Some(len) => {
if let Some(n) = nulls.as_ref() {
if n.len() != len {
return Err(ArrowError::InvalidArgumentError(format!(
"Incorrect length of null buffer for FixedSizeBinaryArray, expected {} got {}",
len,
n.len(),
)));
}
}

len
}
None => {
if !values.is_empty() {
return Err(ArrowError::InvalidArgumentError(
"Buffer cannot have non-zero length if the item size is zero".to_owned(),
));
}

// If the item size is zero, try to determine the length from the null buffer
nulls.as_ref().map(|n| n.len()).unwrap_or(0)
} else {
values.len() / s
};
if let Some(n) = nulls.as_ref() {
if n.len() != len {
return Err(ArrowError::InvalidArgumentError(format!(
"Incorrect length of null buffer for FixedSizeBinaryArray, expected {} got {}",
len,
n.len(),
)));
// If the item size is zero, try to determine the length from the null buffer
nulls.as_ref().map(|n| n.len()).unwrap_or(0)
}
}
};

Ok(Self {
data_type,
Expand Down Expand Up @@ -1032,10 +1036,14 @@ mod tests {

let zero_sized = FixedSizeBinaryArray::new(0, Buffer::default(), None);
assert_eq!(zero_sized.len(), 0);
assert_eq!(zero_sized.null_count(), 0);
assert_eq!(zero_sized.values().len(), 0);

let nulls = NullBuffer::new_null(3);
let zero_sized_with_nulls = FixedSizeBinaryArray::new(0, Buffer::default(), Some(nulls));
assert_eq!(zero_sized_with_nulls.len(), 3);
assert_eq!(zero_sized_with_nulls.null_count(), 3);
assert_eq!(zero_sized_with_nulls.values().len(), 0);

let zero_sized_with_non_empty_buffer_err =
FixedSizeBinaryArray::try_new(0, buffer, None).unwrap_err();
Expand Down
Loading