Skip to content

Commit a9b91b6

Browse files
alambRich-T-kid
authored andcommitted
[arrow-array] Use consistent value_length name in FixedSizeBinaryArray (apache#9905)
# Which issue does this PR close? - Part of apache#9906 - First follow on to apache#9872 # Rationale for this change While trying to avoid overflows due to using i32 arithmetic in FixedSizeBinaryArray, I found the use of the term `size` in parameters to be confusing when the field name is called `value_length` # What changes are included in this PR? Change several parameter / variable names to `value_length` to keep the code consistent # Are these changes tested? By CI # Are there any user-facing changes? No this is an internal code refactor
1 parent 2ef8bc1 commit a9b91b6

1 file changed

Lines changed: 79 additions & 71 deletions

File tree

arrow-array/src/array/fixed_size_binary_array.rs

Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -96,44 +96,48 @@ pub struct FixedSizeBinaryArray {
9696
}
9797

9898
impl FixedSizeBinaryArray {
99-
/// Create a new [`FixedSizeBinaryArray`] with `size` element size, panicking on failure
99+
/// Create a new [`FixedSizeBinaryArray`] with `value_length` bytes per element, panicking on
100+
/// failure
100101
///
101102
/// # Panics
102103
///
103104
/// Panics if [`Self::try_new`] returns an error
104-
pub fn new(size: i32, values: Buffer, nulls: Option<NullBuffer>) -> Self {
105-
Self::try_new(size, values, nulls).unwrap()
105+
pub fn new(value_length: i32, values: Buffer, nulls: Option<NullBuffer>) -> Self {
106+
Self::try_new(value_length, values, nulls).unwrap()
106107
}
107108

108109
/// Create a new [`Scalar`] from `value`
109110
pub fn new_scalar(value: impl AsRef<[u8]>) -> Scalar<Self> {
110111
let v = value.as_ref();
111-
let size = i32::try_from(v.len()).expect("FixedSizeBinaryArray value length exceeds i32");
112-
Scalar::new(Self::new(size, Buffer::from(v), None))
112+
let value_length =
113+
i32::try_from(v.len()).expect("FixedSizeBinaryArray value length exceeds i32");
114+
Scalar::new(Self::new(value_length, Buffer::from(v), None))
113115
}
114116

115117
/// Create a new [`FixedSizeBinaryArray`] from the provided parts, returning an error on failure
116118
///
117-
/// Creating an arrow with `size == 0` will try to get the length from the null buffer. If
118-
/// no null buffer is provided, the resulting array will have length zero.
119+
/// Creating an array with `value_length == 0` will try to get the length from the null
120+
/// buffer. If no null buffer is provided, the resulting array will have length zero.
119121
///
120122
/// # Errors
121123
///
122-
/// * `size < 0`
123-
/// * `values.len() / size != nulls.len()`
124-
/// * `size == 0 && values.len() != 0`
125-
/// * `len * size > i32::MAX`
124+
/// * `value_length < 0`
125+
/// * `values.len() / value_length != nulls.len()`
126+
/// * `value_length == 0 && values.len() != 0`
127+
/// * `len * value_length > i32::MAX`
126128
pub fn try_new(
127-
size: i32,
129+
value_length: i32,
128130
values: Buffer,
129131
nulls: Option<NullBuffer>,
130132
) -> Result<Self, ArrowError> {
131-
let data_type = DataType::FixedSizeBinary(size);
132-
let s = size.to_usize().ok_or_else(|| {
133-
ArrowError::InvalidArgumentError(format!("Size cannot be negative, got {size}"))
133+
let data_type = DataType::FixedSizeBinary(value_length);
134+
let value_size = value_length.to_usize().ok_or_else(|| {
135+
ArrowError::InvalidArgumentError(format!(
136+
"Value length cannot be negative, got {value_length}"
137+
))
134138
})?;
135139

136-
let len = match values.len().checked_div(s) {
140+
let len = match values.len().checked_div(value_size) {
137141
Some(len) => {
138142
if let Some(n) = nulls.as_ref() {
139143
if n.len() != len {
@@ -150,21 +154,21 @@ impl FixedSizeBinaryArray {
150154
None => {
151155
if !values.is_empty() {
152156
return Err(ArrowError::InvalidArgumentError(
153-
"Buffer cannot have non-zero length if the item size is zero".to_owned(),
157+
"Buffer cannot have non-zero length if the value length is zero".to_owned(),
154158
));
155159
}
156160

157-
// If the item size is zero, try to determine the length from the null buffer
161+
// If the value length is zero, try to determine the length from the null buffer
158162
nulls.as_ref().map(|n| n.len()).unwrap_or(0)
159163
}
160164
};
161165

162-
Self::validate_lengths(s, len)?;
166+
Self::validate_lengths(value_size, len)?;
163167

164168
Ok(Self {
165169
data_type,
166170
value_data: values,
167-
value_length: size,
171+
value_length,
168172
nulls,
169173
len,
170174
})
@@ -203,21 +207,21 @@ impl FixedSizeBinaryArray {
203207
///
204208
/// Panics if
205209
///
206-
/// * `size < 0`
207-
/// * `size * len` would overflow `usize`
208-
/// * `size * len > i32::MAX`
209-
/// * `size * len * 8` would overflow `usize`
210-
pub fn new_null(size: i32, len: usize) -> Self {
210+
/// * `value_length < 0`
211+
/// * `value_length * len` would overflow `usize`
212+
/// * `value_length * len > i32::MAX`
213+
/// * `value_length * len * 8` would overflow `usize`
214+
pub fn new_null(value_length: i32, len: usize) -> Self {
211215
const BITS_IN_A_BYTE: usize = 8;
212-
let size_usize = size.to_usize().unwrap();
213-
Self::validate_lengths(size_usize, len).unwrap();
214-
let capacity_in_bytes = size_usize.checked_mul(len).unwrap();
216+
let value_size = value_length.to_usize().unwrap();
217+
Self::validate_lengths(value_size, len).unwrap();
218+
let capacity_in_bytes = value_size.checked_mul(len).unwrap();
215219
let capacity_in_bits = capacity_in_bytes.checked_mul(BITS_IN_A_BYTE).unwrap();
216220
Self {
217-
data_type: DataType::FixedSizeBinary(size),
221+
data_type: DataType::FixedSizeBinary(value_length),
218222
value_data: MutableBuffer::new_null(capacity_in_bits).into(),
219223
nulls: Some(NullBuffer::new_null(len)),
220-
value_length: size,
224+
value_length,
221225
len,
222226
}
223227
}
@@ -351,7 +355,7 @@ impl FixedSizeBinaryArray {
351355
U: AsRef<[u8]>,
352356
{
353357
let mut len = 0;
354-
let mut size = None;
358+
let mut value_size = None;
355359
let mut byte = 0;
356360

357361
let iter_size_hint = iter.size_hint().0;
@@ -369,7 +373,7 @@ impl FixedSizeBinaryArray {
369373

370374
if let Some(slice) = item {
371375
let slice = slice.as_ref();
372-
if let Some(size) = size {
376+
if let Some(size) = value_size {
373377
if size != slice.len() {
374378
return Err(ArrowError::InvalidArgumentError(format!(
375379
"Nested array size mismatch: one is {}, and the other is {}",
@@ -379,7 +383,7 @@ impl FixedSizeBinaryArray {
379383
}
380384
} else {
381385
let len = slice.len();
382-
size = Some(len);
386+
value_size = Some(len);
383387
// Now that we know how large each element is we can reserve
384388
// sufficient capacity in the underlying mutable buffer for
385389
// the data.
@@ -396,7 +400,7 @@ impl FixedSizeBinaryArray {
396400
}
397401
bit_util::set_bit(null_buf.as_slice_mut(), len);
398402
buffer.extend_from_slice(slice);
399-
} else if let Some(size) = size {
403+
} else if let Some(size) = value_size {
400404
buffer.extend_zeros(size);
401405
} else {
402406
prepend += 1;
@@ -415,27 +419,27 @@ impl FixedSizeBinaryArray {
415419

416420
let nulls = NullBuffer::from_unsliced_buffer(null_buf, len);
417421

418-
let size = size.unwrap_or(0);
419-
Self::validate_lengths(size, len)?;
420-
let size = size.try_into().map_err(|_| {
422+
let value_size = value_size.unwrap_or(0);
423+
Self::validate_lengths(value_size, len)?;
424+
let value_length = value_size.try_into().map_err(|_| {
421425
ArrowError::InvalidArgumentError(format!(
422-
"FixedSizeBinaryArray value length exceeds i32, got {size}"
426+
"FixedSizeBinaryArray value length exceeds i32, got {value_size}"
423427
))
424428
})?;
425429
Ok(Self {
426-
data_type: DataType::FixedSizeBinary(size),
430+
data_type: DataType::FixedSizeBinary(value_length),
427431
value_data: buffer.into(),
428432
nulls,
429-
value_length: size,
433+
value_length,
430434
len,
431435
})
432436
}
433437

434438
/// Create an array from an iterable argument of sparse byte slices.
435439
/// Sparsity means that items returned by the iterator are optional, i.e input argument can
436440
/// contain `None` items. In cases where the iterator returns only `None` values, this
437-
/// also takes a size parameter to ensure that the a valid FixedSizeBinaryArray is still
438-
/// created.
441+
/// also takes a `value_length` parameter to ensure that a valid
442+
/// [`FixedSizeBinaryArray`] is still created.
439443
///
440444
/// # Examples
441445
///
@@ -455,22 +459,27 @@ impl FixedSizeBinaryArray {
455459
/// # Errors
456460
///
457461
/// Returns error if argument has length zero, or sizes of nested slices don't match.
458-
pub fn try_from_sparse_iter_with_size<T, U>(mut iter: T, size: i32) -> Result<Self, ArrowError>
462+
pub fn try_from_sparse_iter_with_size<T, U>(
463+
mut iter: T,
464+
value_length: i32,
465+
) -> Result<Self, ArrowError>
459466
where
460467
T: Iterator<Item = Option<U>>,
461468
U: AsRef<[u8]>,
462469
{
463-
let size_usize = size.to_usize().ok_or_else(|| {
464-
ArrowError::InvalidArgumentError(format!("Size cannot be negative, got {size}"))
470+
let value_size = value_length.to_usize().ok_or_else(|| {
471+
ArrowError::InvalidArgumentError(format!(
472+
"Value length cannot be negative, got {value_length}"
473+
))
465474
})?;
466475
let mut len = 0;
467476
let mut byte = 0;
468477

469478
let iter_size_hint = iter.size_hint().0;
470479
let mut null_buf = MutableBuffer::new(bit_util::ceil(iter_size_hint, 8));
471-
let capacity = iter_size_hint.checked_mul(size_usize).ok_or_else(|| {
480+
let capacity = iter_size_hint.checked_mul(value_size).ok_or_else(|| {
472481
ArrowError::InvalidArgumentError(format!(
473-
"FixedSizeBinaryArray error: value size {size_usize} * len hint {iter_size_hint} exceeds usize"
482+
"FixedSizeBinaryArray error: value size {value_size} * len hint {iter_size_hint} exceeds usize"
474483
))
475484
})?;
476485
let mut buffer = MutableBuffer::new(capacity);
@@ -485,18 +494,18 @@ impl FixedSizeBinaryArray {
485494

486495
if let Some(slice) = item {
487496
let slice = slice.as_ref();
488-
if size_usize != slice.len() {
497+
if value_size != slice.len() {
489498
return Err(ArrowError::InvalidArgumentError(format!(
490499
"Nested array size mismatch: one is {}, and the other is {}",
491-
size,
500+
value_length,
492501
slice.len()
493502
)));
494503
}
495504

496505
bit_util::set_bit(null_buf.as_slice_mut(), len);
497506
buffer.extend_from_slice(slice);
498507
} else {
499-
buffer.extend_zeros(size_usize);
508+
buffer.extend_zeros(value_size);
500509
}
501510

502511
len += 1;
@@ -505,14 +514,14 @@ impl FixedSizeBinaryArray {
505514
})?;
506515

507516
let nulls = NullBuffer::from_unsliced_buffer(null_buf, len);
508-
Self::validate_lengths(size_usize, len)?;
517+
Self::validate_lengths(value_size, len)?;
509518

510519
Ok(Self {
511-
data_type: DataType::FixedSizeBinary(size),
520+
data_type: DataType::FixedSizeBinary(value_length),
512521
value_data: buffer.into(),
513522
nulls,
514523
len,
515-
value_length: size,
524+
value_length,
516525
})
517526
}
518527

@@ -539,23 +548,22 @@ impl FixedSizeBinaryArray {
539548
U: AsRef<[u8]>,
540549
{
541550
let mut len = 0;
542-
let mut size = None;
551+
let mut value_size = None;
543552
let iter_size_hint = iter.size_hint().0;
544553
let mut buffer = MutableBuffer::new(0);
545554

546555
iter.try_for_each(|item| -> Result<(), ArrowError> {
547556
let slice = item.as_ref();
548-
if let Some(size) = size {
549-
if size != slice.len() {
557+
if let Some(value_size) = value_size {
558+
if value_size != slice.len() {
550559
return Err(ArrowError::InvalidArgumentError(format!(
551-
"Nested array size mismatch: one is {}, and the other is {}",
552-
size,
560+
"Nested array size mismatch: one is {value_size}, and the other is {}",
553561
slice.len()
554562
)));
555563
}
556564
} else {
557565
let len = slice.len();
558-
size = Some(len);
566+
value_size = Some(len);
559567
if let Some(capacity) = iter_size_hint.checked_mul(len) {
560568
buffer.reserve(capacity);
561569
}
@@ -574,18 +582,18 @@ impl FixedSizeBinaryArray {
574582
));
575583
}
576584

577-
let size = size.unwrap_or(0);
578-
Self::validate_lengths(size, len)?;
579-
let size = size.try_into().map_err(|_| {
585+
let value_size = value_size.unwrap_or(0);
586+
Self::validate_lengths(value_size, len)?;
587+
let value_length = value_size.try_into().map_err(|_| {
580588
ArrowError::InvalidArgumentError(format!(
581-
"FixedSizeBinaryArray value length exceeds i32, got {size}"
589+
"FixedSizeBinaryArray value length exceeds i32, got {value_size}"
582590
))
583591
})?;
584592
Ok(Self {
585-
data_type: DataType::FixedSizeBinary(size),
593+
data_type: DataType::FixedSizeBinary(value_length),
586594
value_data: buffer.into(),
587595
nulls: None,
588-
value_length: size,
596+
value_length,
589597
len,
590598
})
591599
}
@@ -615,14 +623,14 @@ impl From<ArrayData> for FixedSizeBinaryArray {
615623
_ => panic!("Expected data type to be FixedSizeBinary"),
616624
};
617625

618-
let size = value_length
626+
let value_size = value_length
619627
.to_usize()
620628
.expect("FixedSizeBinaryArray value length must be non-negative");
621-
Self::validate_lengths(size, len)
629+
Self::validate_lengths(value_size, len)
622630
.expect("FixedSizeBinaryArray offsets must fit within i32");
623631
let value_data = buffers[0].slice_with_length(
624-
offset.checked_mul(size).expect("offset overflow"),
625-
len.checked_mul(size).expect("length overflow"),
632+
offset.checked_mul(value_size).expect("offset overflow"),
633+
len.checked_mul(value_size).expect("length overflow"),
626634
);
627635

628636
Self {
@@ -1157,7 +1165,7 @@ mod tests {
11571165

11581166
assert_eq!(
11591167
err.to_string(),
1160-
"Invalid argument error: Size cannot be negative, got -1"
1168+
"Invalid argument error: Value length cannot be negative, got -1"
11611169
);
11621170

11631171
let nulls = NullBuffer::new_null(3);
@@ -1182,7 +1190,7 @@ mod tests {
11821190
FixedSizeBinaryArray::try_new(0, buffer, None).unwrap_err();
11831191
assert_eq!(
11841192
zero_sized_with_non_empty_buffer_err.to_string(),
1185-
"Invalid argument error: Buffer cannot have non-zero length if the item size is zero"
1193+
"Invalid argument error: Buffer cannot have non-zero length if the value length is zero"
11861194
);
11871195
}
11881196
}

0 commit comments

Comments
 (0)