From 704f90bbf541896387bed030e12a39be308047e8 Mon Sep 17 00:00:00 2001 From: Bruce Ritchie Date: Tue, 10 Sep 2024 06:30:03 -0400 Subject: [PATCH] Add support for BinaryView in arrow_string::length (#6359) * Add support for BinaryView in arrow_string::length * Adding a longer binary value to binary view test. --- arrow-string/src/length.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/arrow-string/src/length.rs b/arrow-string/src/length.rs index 479c444edce2..82fb2e0d109b 100644 --- a/arrow-string/src/length.rs +++ b/arrow-string/src/length.rs @@ -93,6 +93,14 @@ pub fn length(array: &dyn Array) -> Result { DataType::FixedSizeBinary(len) | DataType::FixedSizeList(_, len) => Ok(Arc::new( Int32Array::new(vec![*len; array.len()].into(), array.nulls().cloned()), )), + DataType::BinaryView => { + let list = array.as_binary_view(); + let v = list.views().iter().map(|v| *v as i32).collect::>(); + Ok(Arc::new(PrimitiveArray::::new( + v.into(), + list.nulls().cloned(), + ))) + } other => Err(ArrowError::ComputeError(format!( "length not supported for {other:?}" ))), @@ -253,6 +261,23 @@ mod tests { length_binary_helper!(i64, Int64Array, length, value, result) } + #[test] + fn length_test_binary_view() { + let value: Vec<&[u8]> = vec![ + b"zero", + &[0xff, 0xf8], + b"two", + b"this is a longer string to test binary array with", + ]; + let expected: Vec = vec![4, 2, 3, 49]; + + let array = BinaryViewArray::from(value); + let result = length(&array).unwrap(); + let result = result.as_any().downcast_ref::().unwrap(); + let expected: Int32Array = expected.into(); + assert_eq!(&expected, result); + } + #[test] fn length_test_list() { let value = vec![