tustvold commented on code in PR #3961:
URL: https://github.com/apache/arrow-rs/pull/3961#discussion_r1150824930
##########
arrow-cast/src/cast.rs:
##########
@@ -3390,6 +3405,67 @@ fn cast_binary_to_string<O: OffsetSizeTrait>(
}
}
+/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to
'FixedSizeBinaryArray'.
+fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
+ array: &dyn Array,
+ byte_width: i32,
+) -> Result<ArrayRef, ArrowError> {
+ let array = array
+ .as_any()
+ .downcast_ref::<GenericBinaryArray<O>>()
+ .unwrap();
+
+ let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(),
byte_width);
+
+ for i in 0..array.len() {
+ if array.is_null(i) {
+ builder.append_null();
+ } else {
+ builder.append_value(array.value(i))?;
Review Comment:
I think this should handle `CastOptions::safe` and insert nulls instead of
erroring if `safe = true`
##########
arrow-cast/src/cast.rs:
##########
@@ -3390,6 +3405,67 @@ fn cast_binary_to_string<O: OffsetSizeTrait>(
}
}
+/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to
'FixedSizeBinaryArray'.
+fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
+ array: &dyn Array,
+ byte_width: i32,
+) -> Result<ArrayRef, ArrowError> {
+ let array = array
+ .as_any()
+ .downcast_ref::<GenericBinaryArray<O>>()
+ .unwrap();
Review Comment:
```suggestion
let array = array.as_binary::<O>();
```
##########
arrow-cast/src/cast.rs:
##########
@@ -3390,6 +3405,67 @@ fn cast_binary_to_string<O: OffsetSizeTrait>(
}
}
+/// Helper function to cast from one `BinaryArray` or 'LargeBinaryArray' to
'FixedSizeBinaryArray'.
+fn cast_binary_to_fixed_size_binary<O: OffsetSizeTrait>(
+ array: &dyn Array,
+ byte_width: i32,
+) -> Result<ArrayRef, ArrowError> {
+ let array = array
+ .as_any()
+ .downcast_ref::<GenericBinaryArray<O>>()
+ .unwrap();
+
+ let mut builder = FixedSizeBinaryBuilder::with_capacity(array.len(),
byte_width);
+
+ for i in 0..array.len() {
+ if array.is_null(i) {
+ builder.append_null();
+ } else {
+ builder.append_value(array.value(i))?;
+ }
+ }
+
+ Ok(Arc::new(builder.finish()))
+}
+
+/// Helper function to cast from 'FixedSizeBinaryArray' to one `BinaryArray`
or 'LargeBinaryArray'.
+/// If the target one is too large for the source array it will return an
Error.
+fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
+ array: &dyn Array,
+ to_type: &DataType,
+ byte_width: i32,
+) -> Result<ArrayRef, ArrowError> {
+ let array = array
+ .as_any()
+ .downcast_ref::<FixedSizeBinaryArray>()
+ .unwrap();
+
+ let offsets: i128 = byte_width as i128 * array.len() as i128;
+
+ let is_binary = matches!(to_type, DataType::Binary);
Review Comment:
Could we change this to match on `GenericBinaryType<O>::DATA_TYPE` instead
##########
arrow-cast/src/cast.rs:
##########
@@ -5295,6 +5371,72 @@ mod tests {
);
}
+ #[test]
+ fn test_cast_binary_to_fixed_size_binary() {
+ let bytes_1 = "Hiiii".as_bytes();
+ let bytes_2 = "Hello".as_bytes();
+
+ let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
+ let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
+ let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
+
+ let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
+ let down_cast = array_ref
+ .as_any()
+ .downcast_ref::<FixedSizeBinaryArray>()
+ .unwrap();
+ assert_eq!(bytes_1, down_cast.value(0));
+ assert_eq!(bytes_2, down_cast.value(1));
+ assert!(down_cast.is_null(2));
+
+ let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
+ let down_cast = array_ref
+ .as_any()
+ .downcast_ref::<FixedSizeBinaryArray>()
+ .unwrap();
+ assert_eq!(bytes_1, down_cast.value(0));
+ assert_eq!(bytes_2, down_cast.value(1));
+ assert!(down_cast.is_null(2));
+
+ // test error cases when the length of binary are not same
+ let bytes_1 = "Hi".as_bytes();
+ let bytes_2 = "Hello".as_bytes();
+
+ let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
+ let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
+ let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
+
+ let array_ref = cast(&a1, &DataType::FixedSizeBinary(5));
+ assert!(array_ref.is_err());
+
+ let array_ref = cast(&a2, &DataType::FixedSizeBinary(5));
+ assert!(array_ref.is_err());
+ }
+
+ #[test]
+ fn test_fixed_size_binary_to_binary() {
+ let bytes_1 = "Hiiii".as_bytes();
+ 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 array_ref = cast(&a1, &DataType::Binary).unwrap();
+ let down_cast =
array_ref.as_any().downcast_ref::<BinaryArray>().unwrap();
Review Comment:
```suggestion
let down_cast = array_ref.as_binary::<i32>();
```
##########
arrow-cast/src/cast.rs:
##########
@@ -5295,6 +5371,72 @@ mod tests {
);
}
+ #[test]
+ fn test_cast_binary_to_fixed_size_binary() {
+ let bytes_1 = "Hiiii".as_bytes();
+ let bytes_2 = "Hello".as_bytes();
+
+ let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
+ let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
+ let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
+
+ let array_ref = cast(&a1, &DataType::FixedSizeBinary(5)).unwrap();
+ let down_cast = array_ref
+ .as_any()
+ .downcast_ref::<FixedSizeBinaryArray>()
+ .unwrap();
+ assert_eq!(bytes_1, down_cast.value(0));
+ assert_eq!(bytes_2, down_cast.value(1));
+ assert!(down_cast.is_null(2));
+
+ let array_ref = cast(&a2, &DataType::FixedSizeBinary(5)).unwrap();
+ let down_cast = array_ref
+ .as_any()
+ .downcast_ref::<FixedSizeBinaryArray>()
+ .unwrap();
+ assert_eq!(bytes_1, down_cast.value(0));
+ assert_eq!(bytes_2, down_cast.value(1));
+ assert!(down_cast.is_null(2));
+
+ // test error cases when the length of binary are not same
+ let bytes_1 = "Hi".as_bytes();
+ let bytes_2 = "Hello".as_bytes();
+
+ let binary_data = vec![Some(bytes_1), Some(bytes_2), None];
+ let a1 = Arc::new(BinaryArray::from(binary_data.clone())) as ArrayRef;
+ let a2 = Arc::new(LargeBinaryArray::from(binary_data)) as ArrayRef;
+
+ let array_ref = cast(&a1, &DataType::FixedSizeBinary(5));
+ assert!(array_ref.is_err());
+
+ let array_ref = cast(&a2, &DataType::FixedSizeBinary(5));
+ assert!(array_ref.is_err());
+ }
+
+ #[test]
+ fn test_fixed_size_binary_to_binary() {
+ let bytes_1 = "Hiiii".as_bytes();
+ 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 array_ref = cast(&a1, &DataType::Binary).unwrap();
+ let down_cast =
array_ref.as_any().downcast_ref::<BinaryArray>().unwrap();
+ assert_eq!(bytes_1, down_cast.value(0));
+ assert_eq!(bytes_2, down_cast.value(1));
+ assert!(down_cast.is_null(2));
+
+ let array_ref = cast(&a1, &DataType::LargeBinary).unwrap();
+ let down_cast = array_ref
+ .as_any()
+ .downcast_ref::<LargeBinaryArray>()
+ .unwrap();
Review Comment:
```suggestion
let down_cast = array_ref.as_binary::<i64>();
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]