Skip to content

Commit f18a35c

Browse files
authored
Merge branch 'main' into zstd-window-size
2 parents 5bb5cf4 + 7dbe58a commit f18a35c

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

arrow-string/src/length.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ pub fn length(array: &dyn Array) -> Result<ArrayRef, ArrowError> {
124124

125125
/// Returns an array of Int32/Int64 denoting the number of bits in each value in the array.
126126
///
127-
/// * this only accepts StringArray/Utf8, LargeString/LargeUtf8, BinaryArray and LargeBinaryArray,
127+
/// * this only accepts StringArray/Utf8, LargeString/LargeUtf8, StringViewArray/Utf8View,
128+
/// BinaryArray, LargeBinaryArray, BinaryViewArray, and FixedSizeBinaryArray,
128129
/// or DictionaryArray with above Arrays as values
129130
/// * bit_length of null is null.
130131
/// * bit_length is in number of bits
@@ -167,6 +168,18 @@ pub fn bit_length(array: &dyn Array) -> Result<ArrayRef, ArrowError> {
167168
vec![*len * 8; array.len()].into(),
168169
array.nulls().cloned(),
169170
)?)),
171+
DataType::BinaryView => {
172+
let list = array.as_binary_view();
173+
let values = list
174+
.views()
175+
.iter()
176+
.map(|view| (*view as i32).wrapping_mul(8))
177+
.collect();
178+
Ok(Arc::new(Int32Array::try_new(
179+
values,
180+
array.nulls().cloned(),
181+
)?))
182+
}
170183
other => Err(ArrowError::ComputeError(format!(
171184
"bit_length not supported for {other:?}"
172185
))),
@@ -586,6 +599,36 @@ mod tests {
586599
length_binary_helper!(i64, Int64Array, bit_length, value, expected)
587600
}
588601

602+
#[test]
603+
fn bit_length_binary_view() {
604+
let value: Vec<&[u8]> = vec![
605+
b"zero",
606+
&[0xff, 0xf8],
607+
b"two",
608+
b"this is a longer string to test binary array with",
609+
];
610+
let expected: Vec<i32> = vec![32, 16, 24, 392];
611+
612+
let array = BinaryViewArray::from(value);
613+
let result = bit_length(&array).unwrap();
614+
let result = result.as_any().downcast_ref::<Int32Array>().unwrap();
615+
let expected: Int32Array = expected.into();
616+
assert_eq!(&expected, result);
617+
}
618+
619+
#[test]
620+
fn bit_length_null_binary_view() {
621+
let value: Vec<Option<&[u8]>> =
622+
vec![Some(b"one"), None, Some(b"three"), Some(&[0xff, 0xf8])];
623+
let expected: Vec<Option<i32>> = vec![Some(24), None, Some(40), Some(16)];
624+
625+
let array = BinaryViewArray::from(value);
626+
let result = bit_length(&array).unwrap();
627+
let result = result.as_any().downcast_ref::<Int32Array>().unwrap();
628+
let expected: Int32Array = expected.into();
629+
assert_eq!(&expected, result);
630+
}
631+
589632
fn bit_length_null_cases() -> Vec<(Vec<OptionStr>, usize, Vec<Option<i32>>)> {
590633
vec![(
591634
vec![Some("one"), None, Some("three"), Some("four")],

0 commit comments

Comments
 (0)