Skip to content

Commit 903d892

Browse files
authored
Code cleanup of array value functions (#2583)
1 parent 45fb919 commit 903d892

File tree

7 files changed

+98
-19
lines changed

7 files changed

+98
-19
lines changed

arrow/src/array/array_binary.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,15 @@ impl<OffsetSize: OffsetSizeTrait> GenericBinaryArray<OffsetSize> {
9999
}
100100

101101
/// Returns the element at index `i` as bytes slice
102+
/// # Panics
103+
/// Panics if index `i` is out of bounds.
102104
pub fn value(&self, i: usize) -> &[u8] {
103-
assert!(i < self.data.len(), "BinaryArray out of bounds access");
105+
assert!(
106+
i < self.data.len(),
107+
"Trying to access an element at index {} from a BinaryArray of length {}",
108+
i,
109+
self.len()
110+
);
104111
//Soundness: length checked above, offset buffer length is 1 larger than logical array length
105112
let end = unsafe { self.value_offsets().get_unchecked(i + 1) };
106113
let start = unsafe { self.value_offsets().get_unchecked(i) };
@@ -806,7 +813,9 @@ mod tests {
806813
}
807814

808815
#[test]
809-
#[should_panic(expected = "BinaryArray out of bounds access")]
816+
#[should_panic(
817+
expected = "Trying to access an element at index 4 from a BinaryArray of length 3"
818+
)]
810819
fn test_binary_array_get_value_index_out_of_bound() {
811820
let values: [u8; 12] =
812821
[104, 101, 108, 108, 111, 112, 97, 114, 113, 117, 101, 116];

arrow/src/array/array_boolean.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,15 @@ impl BooleanArray {
115115
}
116116

117117
/// Returns the boolean value at index `i`.
118-
///
119-
/// Panics of offset `i` is out of bounds
118+
/// # Panics
119+
/// Panics if index `i` is out of bounds
120120
pub fn value(&self, i: usize) -> bool {
121-
assert!(i < self.len());
121+
assert!(
122+
i < self.len(),
123+
"Trying to access an element at index {} from a BooleanArray of length {}",
124+
i,
125+
self.len()
126+
);
122127
// Safety:
123128
// `i < self.len()
124129
unsafe { self.value_unchecked(i) }
@@ -389,6 +394,17 @@ mod tests {
389394
}
390395
}
391396

397+
#[test]
398+
#[should_panic(
399+
expected = "Trying to access an element at index 4 from a BooleanArray of length 3"
400+
)]
401+
fn test_fixed_size_binary_array_get_value_index_out_of_bound() {
402+
let v = vec![Some(true), None, Some(false)];
403+
let array = v.into_iter().collect::<BooleanArray>();
404+
405+
array.value(4);
406+
}
407+
392408
#[test]
393409
#[should_panic(expected = "BooleanArray data should contain a single buffer only \
394410
(values buffer)")]

arrow/src/array/array_decimal.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,15 @@ impl<T: DecimalType> DecimalArray<T> {
108108
}
109109

110110
/// Returns the element at index `i`.
111+
/// # Panics
112+
/// Panics if index `i` is out of bounds.
111113
pub fn value(&self, i: usize) -> Decimal<T> {
112-
assert!(i < self.data().len(), "Out of bounds access");
114+
assert!(
115+
i < self.data().len(),
116+
"Trying to access an element at index {} from a DecimalArray of length {}",
117+
i,
118+
self.len()
119+
);
113120

114121
unsafe { self.value_unchecked(i) }
115122
}
@@ -952,4 +959,14 @@ mod tests {
952959
assert_eq!(101_i128, array.value(2).into());
953960
assert!(!array.is_null(2));
954961
}
962+
963+
#[test]
964+
#[should_panic(
965+
expected = "Trying to access an element at index 4 from a DecimalArray of length 3"
966+
)]
967+
fn test_fixed_size_binary_array_get_value_index_out_of_bound() {
968+
let array = Decimal128Array::from_iter_values(vec![-100, 0, 101].into_iter());
969+
970+
array.value(4);
971+
}
955972
}

arrow/src/array/array_fixed_size_binary.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ pub struct FixedSizeBinaryArray {
6060

6161
impl FixedSizeBinaryArray {
6262
/// Returns the element at index `i` as a byte slice.
63+
/// # Panics
64+
/// Panics if index `i` is out of bounds.
6365
pub fn value(&self, i: usize) -> &[u8] {
6466
assert!(
6567
i < self.data.len(),
66-
"FixedSizeBinaryArray out of bounds access"
68+
"Trying to access an element at index {} from a FixedSizeBinaryArray of length {}",
69+
i,
70+
self.len()
6771
);
6872
let offset = i + self.data.offset();
6973
unsafe {
@@ -672,4 +676,15 @@ mod tests {
672676
// Should not panic
673677
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(item)]).unwrap();
674678
}
679+
680+
#[test]
681+
#[should_panic(
682+
expected = "Trying to access an element at index 4 from a FixedSizeBinaryArray of length 3"
683+
)]
684+
fn test_fixed_size_binary_array_get_value_index_out_of_bound() {
685+
let values = vec![Some("one".as_bytes()), Some(b"two"), None];
686+
let array = FixedSizeBinaryArray::from(values);
687+
688+
array.value(4);
689+
}
675690
}

arrow/src/array/array_primitive.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,16 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
106106
}
107107

108108
/// Returns the primitive value at index `i`.
109-
///
110-
/// Panics of offset `i` is out of bounds
109+
/// # Panics
110+
/// Panics if index `i` is out of bounds
111111
#[inline]
112112
pub fn value(&self, i: usize) -> T::Native {
113-
assert!(i < self.len());
113+
assert!(
114+
i < self.len(),
115+
"Trying to access an element at index {} from a PrimitiveArray of length {}",
116+
i,
117+
self.len()
118+
);
114119
unsafe { self.value_unchecked(i) }
115120
}
116121

@@ -1128,4 +1133,14 @@ mod tests {
11281133
assert_eq!(2, b.value(0));
11291134
assert_eq!(15, b.value(1));
11301135
}
1136+
1137+
#[test]
1138+
#[should_panic(
1139+
expected = "Trying to access an element at index 4 from a PrimitiveArray of length 3"
1140+
)]
1141+
fn test_string_array_get_value_index_out_of_bound() {
1142+
let array: Int8Array = [10_i8, 11, 12].into_iter().collect();
1143+
1144+
array.value(4);
1145+
}
11311146
}

arrow/src/array/array_string.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,16 @@ impl<OffsetSize: OffsetSizeTrait> GenericStringArray<OffsetSize> {
113113
}
114114

115115
/// Returns the element at index `i` as &str
116+
/// # Panics
117+
/// Panics if index `i` is out of bounds.
116118
#[inline]
117119
pub fn value(&self, i: usize) -> &str {
118-
assert!(i < self.data.len(), "StringArray out of bounds access");
120+
assert!(
121+
i < self.data.len(),
122+
"Trying to access an element at index {} from a StringArray of length {}",
123+
i,
124+
self.len()
125+
);
119126
// Safety:
120127
// `i < self.data.len()
121128
unsafe { self.value_unchecked(i) }
@@ -521,7 +528,9 @@ mod tests {
521528
}
522529

523530
#[test]
524-
#[should_panic(expected = "StringArray out of bounds access")]
531+
#[should_panic(
532+
expected = "Trying to access an element at index 4 from a StringArray of length 3"
533+
)]
525534
fn test_string_array_get_value_index_out_of_bound() {
526535
let values: [u8; 12] = [
527536
b'h', b'e', b'l', b'l', b'o', b'p', b'a', b'r', b'q', b'u', b'e', b't',

arrow/src/array/array_union.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,12 @@ impl UnionArray {
261261
}
262262
}
263263

264-
/// Returns the array's value at `index`.
265-
///
264+
/// Returns the array's value at index `i`.
266265
/// # Panics
267-
///
268-
/// Panics if `index` is greater than the length of the array.
269-
pub fn value(&self, index: usize) -> ArrayRef {
270-
let type_id = self.type_id(index);
271-
let value_offset = self.value_offset(index) as usize;
266+
/// Panics if index `i` is out of bounds
267+
pub fn value(&self, i: usize) -> ArrayRef {
268+
let type_id = self.type_id(i);
269+
let value_offset = self.value_offset(i) as usize;
272270
let child_data = self.boxed_fields[type_id as usize].clone();
273271
child_data.slice(value_offset, 1)
274272
}

0 commit comments

Comments
 (0)