Skip to content

Commit 91114cf

Browse files
paddyhorannevi-me
authored andcommitted
ARROW-7559: [Rust] Incorrect index check assertion in StringArray and BinaryArray
Closes #6180 from paddyhoran/string-array-bug and squashes the following commits: 286bf7f <Paddy Horan> Fixed lints. fd8a26b <Paddy Horan> Fixed bug in check in `StringArray` and `BinaryArray` Authored-by: Paddy Horan <[email protected]> Signed-off-by: Neville Dipale <[email protected]>
1 parent 469e9cb commit 91114cf

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

rust/arrow/src/array/array.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,10 +1069,7 @@ pub struct FixedSizeBinaryArray {
10691069
impl BinaryArray {
10701070
/// Returns the element at index `i` as a byte slice.
10711071
pub fn value(&self, i: usize) -> &[u8] {
1072-
assert!(
1073-
i + self.offset() < self.data.len(),
1074-
"BinaryArray out of bounds access"
1075-
);
1072+
assert!(i < self.data.len(), "BinaryArray out of bounds access");
10761073
let offset = i.checked_add(self.data.offset()).unwrap();
10771074
unsafe {
10781075
let pos = self.value_offset_at(offset);
@@ -1119,10 +1116,7 @@ impl BinaryArray {
11191116
impl StringArray {
11201117
/// Returns the element at index `i` as a string slice.
11211118
pub fn value(&self, i: usize) -> &str {
1122-
assert!(
1123-
i + self.offset() < self.data.len(),
1124-
"StringArray out of bounds access"
1125-
);
1119+
assert!(i < self.data.len(), "StringArray out of bounds access");
11261120
let offset = i.checked_add(self.data.offset()).unwrap();
11271121
unsafe {
11281122
let pos = self.value_offset_at(offset);
@@ -2442,6 +2436,36 @@ mod tests {
24422436
}
24432437
}
24442438

2439+
#[test]
2440+
fn test_nested_string_array() {
2441+
let string_builder = StringBuilder::new(3);
2442+
let mut list_of_string_builder = ListBuilder::new(string_builder);
2443+
2444+
list_of_string_builder.values().append_value("foo").unwrap();
2445+
list_of_string_builder.values().append_value("bar").unwrap();
2446+
list_of_string_builder.append(true).unwrap();
2447+
2448+
list_of_string_builder
2449+
.values()
2450+
.append_value("foobar")
2451+
.unwrap();
2452+
list_of_string_builder.append(true).unwrap();
2453+
let list_of_strings = list_of_string_builder.finish();
2454+
2455+
assert_eq!(list_of_strings.len(), 2);
2456+
2457+
let first_slot = list_of_strings.value(0);
2458+
let first_list = first_slot.as_any().downcast_ref::<StringArray>().unwrap();
2459+
assert_eq!(first_list.len(), 2);
2460+
assert_eq!(first_list.value(0), "foo");
2461+
assert_eq!(first_list.value(1), "bar");
2462+
2463+
let second_slot = list_of_strings.value(1);
2464+
let second_list = second_slot.as_any().downcast_ref::<StringArray>().unwrap();
2465+
assert_eq!(second_list.len(), 1);
2466+
assert_eq!(second_list.value(0), "foobar");
2467+
}
2468+
24452469
#[test]
24462470
#[should_panic(
24472471
expected = "BinaryArray can only be created from List<u8> arrays, mismatched \

0 commit comments

Comments
 (0)