Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions arrow-array/src/array/fixed_size_binary_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,22 +679,42 @@ impl From<FixedSizeListArray> for FixedSizeBinaryArray {
}
}

impl From<Vec<Option<&[u8]>>> for FixedSizeBinaryArray {
fn from(v: Vec<Option<&[u8]>>) -> Self {
impl TryFrom<Vec<Option<&[u8]>>> for FixedSizeBinaryArray {
type Error = ArrowError;

fn try_from(v: Vec<Option<&[u8]>>) -> Result<Self, Self::Error> {
#[allow(deprecated)]
Self::try_from_sparse_iter(v.into_iter()).unwrap()
Self::try_from_sparse_iter(v.into_iter())
}
}

impl TryFrom<Vec<&[u8]>> for FixedSizeBinaryArray {
type Error = ArrowError;

fn try_from(v: Vec<&[u8]>) -> Result<Self, Self::Error> {
Self::try_from_iter(v.into_iter())
}
}

impl From<Vec<&[u8]>> for FixedSizeBinaryArray {
fn from(v: Vec<&[u8]>) -> Self {
Self::try_from_iter(v.into_iter()).unwrap()
impl<const N: usize> TryFrom<Vec<Option<&[u8; N]>>> for FixedSizeBinaryArray {
type Error = ArrowError;

fn try_from(v: Vec<Option<&[u8; N]>>) -> Result<Self, Self::Error> {
N.try_into()
.map_err(|_| {
ArrowError::InvalidArgumentError(format!(
"FixedSizeBinaryArray value length exceeds i32, got {N}"
))
})
.and_then(|x| Self::try_from_sparse_iter_with_size(v.into_iter(), x))
}
}

impl<const N: usize> From<Vec<&[u8; N]>> for FixedSizeBinaryArray {
fn from(v: Vec<&[u8; N]>) -> Self {
Self::try_from_iter(v.into_iter()).unwrap()
impl<const N: usize> TryFrom<Vec<&[u8; N]>> for FixedSizeBinaryArray {
type Error = ArrowError;

fn try_from(v: Vec<&[u8; N]>) -> Result<Self, Self::Error> {
Self::try_from_iter(v.into_iter())
}
}

Expand Down Expand Up @@ -1009,7 +1029,7 @@ mod tests {
#[test]
fn test_fixed_size_binary_array_from_vec() {
let values = vec!["one".as_bytes(), b"two", b"six", b"ten"];
let array = FixedSizeBinaryArray::from(values);
let array = FixedSizeBinaryArray::try_from(values).unwrap();
assert_eq!(array.len(), 4);
assert_eq!(array.null_count(), 0);
assert_eq!(array.logical_null_count(), 0);
Expand All @@ -1024,10 +1044,9 @@ mod tests {
}

#[test]
#[should_panic(expected = "Nested array size mismatch: one is 3, and the other is 5")]
fn test_fixed_size_binary_array_from_vec_incorrect_length() {
let values = vec!["one".as_bytes(), b"two", b"three", b"four"];
let _ = FixedSizeBinaryArray::from(values);
assert!(FixedSizeBinaryArray::try_from(values).is_err());
}

#[test]
Expand All @@ -1039,7 +1058,7 @@ mod tests {
Some(b"six"),
Some(b"ten"),
];
let array = FixedSizeBinaryArray::from(values);
let array = FixedSizeBinaryArray::try_from(values).unwrap();
assert_eq!(array.len(), 5);
assert_eq!(array.value(0), b"one");
assert_eq!(array.value(1), b"two");
Expand All @@ -1053,7 +1072,6 @@ mod tests {
}

#[test]
#[should_panic(expected = "Nested array size mismatch: one is 3, and the other is 5")]
fn test_fixed_size_binary_array_from_opt_vec_incorrect_length() {
let values = vec![
Some("one".as_bytes()),
Expand All @@ -1062,7 +1080,7 @@ mod tests {
Some(b"three"),
Some(b"four"),
];
let _ = FixedSizeBinaryArray::from(values);
assert!(FixedSizeBinaryArray::try_from(values).is_err());
}

#[test]
Expand Down Expand Up @@ -1098,7 +1116,7 @@ mod tests {
)]
fn test_fixed_size_binary_array_get_value_index_out_of_bound() {
let values = vec![Some("one".as_bytes()), Some(b"two"), None];
let array = FixedSizeBinaryArray::from(values);
let array = FixedSizeBinaryArray::try_from(values).unwrap();

array.value(4);
}
Expand Down
4 changes: 2 additions & 2 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5724,7 +5724,7 @@ mod tests {
let bytes_2 = "Hello".as_bytes();

let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;

let array_ref = cast(&a1, &DataType::Binary).unwrap();
let down_cast = array_ref.as_binary::<i32>();
Expand All @@ -5751,7 +5751,7 @@ mod tests {
let bytes_2 = "Hello".as_bytes();

let binary_data = vec![Some(bytes_1), Some(bytes_2), Some(bytes_1), None];
let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;
let a1 = Arc::new(FixedSizeBinaryArray::try_from(binary_data.clone()).unwrap()) as ArrayRef;

let cast_type = DataType::Dictionary(
Box::new(DataType::Int8),
Expand Down
6 changes: 4 additions & 2 deletions arrow-ord/src/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,8 @@ mod tests {
expected
);

let fsb_array = FixedSizeBinaryArray::from(vec![&[0u8], &[0u8], &[0u8], &[1u8]]);
let fsb_array =
FixedSizeBinaryArray::try_from(vec![&[0u8], &[0u8], &[0u8], &[1u8]]).unwrap();
let scalar = FixedSizeBinaryArray::new_scalar([1u8]);
let expected = BooleanArray::from(vec![Some(false), Some(false), Some(false), Some(true)]);
assert_eq!(crate::cmp::eq(&fsb_array, &scalar).unwrap(), expected);
Expand All @@ -1824,7 +1825,8 @@ mod tests {
expected
);

let fsb_array = FixedSizeBinaryArray::from(vec![&[0u8], &[0u8], &[0u8], &[1u8]]);
let fsb_array =
FixedSizeBinaryArray::try_from(vec![&[0u8], &[0u8], &[0u8], &[1u8]]).unwrap();
let scalar = FixedSizeBinaryArray::new_scalar([1u8]);
let expected = BooleanArray::from(vec![Some(true), Some(true), Some(true), Some(false)]);
assert_eq!(crate::cmp::neq(&fsb_array, &scalar).unwrap(), expected);
Expand Down
2 changes: 1 addition & 1 deletion arrow-select/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ mod tests {
let v2 = [3_u8, 4];
let v3 = [5_u8, 6];
let v = vec![&v1, &v2, &v3];
let a = FixedSizeBinaryArray::from(v);
let a = FixedSizeBinaryArray::try_from(v).unwrap();
let b = BooleanArray::from(vec![true, false, true]);
let c = filter(&a, &b).unwrap();
let d = c
Expand Down
28 changes: 17 additions & 11 deletions arrow-string/src/concat_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,30 +505,33 @@ mod tests {

#[test]
fn test_fixed_size_binary_concat() {
let left = FixedSizeBinaryArray::from(vec![Some(b"foo" as &[u8]), Some(b"bar"), None]);
let right = FixedSizeBinaryArray::from(vec![None, Some(b"yyy" as &[u8]), Some(b"zzz")]);
let left = FixedSizeBinaryArray::try_from(vec![Some(b"foo" as &[u8]), Some(b"bar"), None])
.unwrap();
let right = FixedSizeBinaryArray::try_from(vec![None, Some(b"yyy" as &[u8]), Some(b"zzz")])
.unwrap();

let output = concat_elements_fixed_size_binary(&left, &right).unwrap();

let expected = FixedSizeBinaryArray::from(vec![None, Some(b"baryyy" as &[u8]), None]);
let expected =
FixedSizeBinaryArray::try_from(vec![None, Some(b"baryyy" as &[u8]), None]).unwrap();
assert_eq!(output, expected);
}

#[test]
fn test_fixed_size_binary_concat_no_null() {
let left = FixedSizeBinaryArray::from(vec![b"ab" as &[u8], b"cd"]);
let right = FixedSizeBinaryArray::from(vec![b"12" as &[u8], b"34"]);
let left = FixedSizeBinaryArray::try_from(vec![b"ab" as &[u8], b"cd"]).unwrap();
let right = FixedSizeBinaryArray::try_from(vec![b"12" as &[u8], b"34"]).unwrap();

let output = concat_elements_fixed_size_binary(&left, &right).unwrap();

let expected = FixedSizeBinaryArray::from(vec![b"ab12" as &[u8], b"cd34"]);
let expected = FixedSizeBinaryArray::try_from(vec![b"ab12" as &[u8], b"cd34"]).unwrap();
assert_eq!(output, expected);
}

#[test]
fn test_fixed_size_binary_concat_error() {
let left = FixedSizeBinaryArray::from(vec![b"ab" as &[u8], b"cd"]);
let right = FixedSizeBinaryArray::from(vec![b"12" as &[u8]]);
let left = FixedSizeBinaryArray::try_from(vec![b"ab" as &[u8], b"cd"]).unwrap();
let right = FixedSizeBinaryArray::try_from(vec![b"12" as &[u8]]).unwrap();

let output = concat_elements_fixed_size_binary(&left, &right);
assert_eq!(
Expand Down Expand Up @@ -673,13 +676,16 @@ mod tests {
assert_eq!(output, expected);

// test for FixedSizeBinaryArray
let left = FixedSizeBinaryArray::from(vec![Some(b"foo" as &[u8]), Some(b"bar"), None]);
let right = FixedSizeBinaryArray::from(vec![None, Some(b"yyy" as &[u8]), Some(b"zzz")]);
let left = FixedSizeBinaryArray::try_from(vec![Some(b"foo" as &[u8]), Some(b"bar"), None])
.unwrap();
let right = FixedSizeBinaryArray::try_from(vec![None, Some(b"yyy" as &[u8]), Some(b"zzz")])
.unwrap();
let output: FixedSizeBinaryArray = concat_elements_dyn(&left, &right)
.unwrap()
.into_data()
.into();
let expected = FixedSizeBinaryArray::from(vec![None, Some(b"baryyy" as &[u8]), None]);
let expected =
FixedSizeBinaryArray::try_from(vec![None, Some(b"baryyy" as &[u8]), None]).unwrap();
assert_eq!(output, expected);
}

Expand Down
19 changes: 17 additions & 2 deletions arrow-string/src/substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,21 @@ mod tests {
};
}

/// A helper macro to test the substring functions for array types only implementing TryFrom.
macro_rules! do_test_tryfrom {
($cases:expr, $array_ty:ty, $substring_fn:ident) => {
$cases
.into_iter()
.for_each(|(array, start, length, expected)| {
let array = <$array_ty>::try_from(array).unwrap();
let result = $substring_fn(&array, start, length).unwrap();
let result = result.as_any().downcast_ref::<$array_ty>().unwrap();
let expected = <$array_ty>::try_from(expected).unwrap();
assert_eq!(&expected, result);
})
};
}

fn with_nulls_generic_binary<O: OffsetSizeTrait>() {
let input = vec![
Some("hello".as_bytes()),
Expand Down Expand Up @@ -591,7 +606,7 @@ mod tests {
(-3, Some(4), input.clone())
);

do_test!(
do_test_tryfrom!(
[&base_case[..], &cases[..]].concat(),
FixedSizeBinaryArray,
substring
Expand Down Expand Up @@ -630,7 +645,7 @@ mod tests {
(-3, Some(4), input.clone())
);

do_test!(
do_test_tryfrom!(
[&base_case[..], &cases[..]].concat(),
FixedSizeBinaryArray,
substring
Expand Down
3 changes: 2 additions & 1 deletion parquet/tests/arrow_reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ fn make_bytearray_batch(
.iter()
.map(|value| Some(value.as_slice()))
.collect::<Vec<_>>()
.into();
.try_into()
.unwrap();
let service_large_binary: LargeBinaryArray = large_binary_values.iter().map(Some).collect();

let schema = Schema::new(vec![
Expand Down
Loading