Skip to content

Commit

Permalink
Add support for BinaryView in arrow_string::length (#6359)
Browse files Browse the repository at this point in the history
* Add support for BinaryView in arrow_string::length

* Adding a longer binary value to binary view test.
  • Loading branch information
Omega359 authored Sep 10, 2024
1 parent 7a5155c commit 704f90b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions arrow-string/src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ pub fn length(array: &dyn Array) -> Result<ArrayRef, ArrowError> {
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::<Vec<_>>();
Ok(Arc::new(PrimitiveArray::<Int32Type>::new(
v.into(),
list.nulls().cloned(),
)))
}
other => Err(ArrowError::ComputeError(format!(
"length not supported for {other:?}"
))),
Expand Down Expand Up @@ -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<i32> = vec![4, 2, 3, 49];

let array = BinaryViewArray::from(value);
let result = length(&array).unwrap();
let result = result.as_any().downcast_ref::<Int32Array>().unwrap();
let expected: Int32Array = expected.into();
assert_eq!(&expected, result);
}

#[test]
fn length_test_list() {
let value = vec![
Expand Down

0 comments on commit 704f90b

Please sign in to comment.