Skip to content

Commit 182c7a9

Browse files
authored
Fix clippy warning in fixed_size_binary_array.rs (#9712)
# Which issue does this PR close? - Closes #NNN. # Rationale for this change This is a future lint, so I took to the opportunity to make this it clear that the length validation only needs to happen in one case. # What changes are included in this PR? Minor changes to validation in `FixedSizeBinaryArray::try_new`. # Are these changes tested? Added some assertions to existing tests. # Are there any user-facing changes? None
1 parent 9ed4c6f commit 182c7a9

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

arrow-array/src/array/fixed_size_binary_array.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,31 @@ impl FixedSizeBinaryArray {
9494
ArrowError::InvalidArgumentError(format!("Size cannot be negative, got {size}"))
9595
})?;
9696

97-
let len = if s == 0 {
98-
if !values.is_empty() {
99-
return Err(ArrowError::InvalidArgumentError(
100-
"Buffer cannot have non-zero length if the item size is zero".to_owned(),
101-
));
97+
let len = match values.len().checked_div(s) {
98+
Some(len) => {
99+
if let Some(n) = nulls.as_ref() {
100+
if n.len() != len {
101+
return Err(ArrowError::InvalidArgumentError(format!(
102+
"Incorrect length of null buffer for FixedSizeBinaryArray, expected {} got {}",
103+
len,
104+
n.len(),
105+
)));
106+
}
107+
}
108+
109+
len
102110
}
111+
None => {
112+
if !values.is_empty() {
113+
return Err(ArrowError::InvalidArgumentError(
114+
"Buffer cannot have non-zero length if the item size is zero".to_owned(),
115+
));
116+
}
103117

104-
// If the item size is zero, try to determine the length from the null buffer
105-
nulls.as_ref().map(|n| n.len()).unwrap_or(0)
106-
} else {
107-
values.len() / s
108-
};
109-
if let Some(n) = nulls.as_ref() {
110-
if n.len() != len {
111-
return Err(ArrowError::InvalidArgumentError(format!(
112-
"Incorrect length of null buffer for FixedSizeBinaryArray, expected {} got {}",
113-
len,
114-
n.len(),
115-
)));
118+
// If the item size is zero, try to determine the length from the null buffer
119+
nulls.as_ref().map(|n| n.len()).unwrap_or(0)
116120
}
117-
}
121+
};
118122

119123
Ok(Self {
120124
data_type,
@@ -1032,10 +1036,14 @@ mod tests {
10321036

10331037
let zero_sized = FixedSizeBinaryArray::new(0, Buffer::default(), None);
10341038
assert_eq!(zero_sized.len(), 0);
1039+
assert_eq!(zero_sized.null_count(), 0);
1040+
assert_eq!(zero_sized.values().len(), 0);
10351041

10361042
let nulls = NullBuffer::new_null(3);
10371043
let zero_sized_with_nulls = FixedSizeBinaryArray::new(0, Buffer::default(), Some(nulls));
10381044
assert_eq!(zero_sized_with_nulls.len(), 3);
1045+
assert_eq!(zero_sized_with_nulls.null_count(), 3);
1046+
assert_eq!(zero_sized_with_nulls.values().len(), 0);
10391047

10401048
let zero_sized_with_non_empty_buffer_err =
10411049
FixedSizeBinaryArray::try_new(0, buffer, None).unwrap_err();

0 commit comments

Comments
 (0)