This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 4d1d79c49f feat: coerce fixed size binary to binary view (#7431)
4d1d79c49f is described below
commit 4d1d79c49f788b787f162a8f73b2ae1f4a4f3a26
Author: Chen Chongchen <[email protected]>
AuthorDate: Thu May 8 04:32:42 2025 +0800
feat: coerce fixed size binary to binary view (#7431)
---
arrow-cast/src/cast/mod.rs | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/arrow-cast/src/cast/mod.rs b/arrow-cast/src/cast/mod.rs
index a5f5fddb85..b317dabd5d 100644
--- a/arrow-cast/src/cast/mod.rs
+++ b/arrow-cast/src/cast/mod.rs
@@ -213,7 +213,7 @@ pub fn can_cast_types(from_type: &DataType, to_type:
&DataType) -> bool {
(Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) |
BinaryView | Utf8View ) => true,
(LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) |
BinaryView | Utf8View ) => true,
- (FixedSizeBinary(_), Binary | LargeBinary) => true,
+ (FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
(
Utf8 | LargeUtf8 | Utf8View,
Binary
@@ -1192,6 +1192,7 @@ pub fn cast_with_options(
(FixedSizeBinary(size), _) => match to_type {
Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array,
*size),
+ BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
_ => Err(ArrowError::CastError(format!(
"Casting from {from_type:?} to {to_type:?} not supported",
))),
@@ -2327,6 +2328,27 @@ fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
Ok(Arc::new(builder.finish()))
}
+fn cast_fixed_size_binary_to_binary_view(
+ array: &dyn Array,
+ _byte_width: i32,
+) -> Result<ArrayRef, ArrowError> {
+ let array = array
+ .as_any()
+ .downcast_ref::<FixedSizeBinaryArray>()
+ .unwrap();
+
+ let mut builder = BinaryViewBuilder::with_capacity(array.len());
+ 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 one `ByteArrayType` to another and vice versa.
/// If the target one (e.g., `LargeUtf8`) is too large for the source array it
will return an Error.
fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef,
ArrowError>
@@ -4847,6 +4869,12 @@ mod tests {
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::BinaryView).unwrap();
+ let down_cast = array_ref.as_binary_view();
+ assert_eq!(bytes_1, down_cast.value(0));
+ assert_eq!(bytes_2, down_cast.value(1));
+ assert!(down_cast.is_null(2));
}
#[test]