Skip to content

Commit 4d1d79c

Browse files
authored
feat: coerce fixed size binary to binary view (#7431)
1 parent ab45719 commit 4d1d79c

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

arrow-cast/src/cast/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
213213

214214
(Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
215215
(LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
216-
(FixedSizeBinary(_), Binary | LargeBinary) => true,
216+
(FixedSizeBinary(_), Binary | LargeBinary | BinaryView) => true,
217217
(
218218
Utf8 | LargeUtf8 | Utf8View,
219219
Binary
@@ -1192,6 +1192,7 @@ pub fn cast_with_options(
11921192
(FixedSizeBinary(size), _) => match to_type {
11931193
Binary => cast_fixed_size_binary_to_binary::<i32>(array, *size),
11941194
LargeBinary => cast_fixed_size_binary_to_binary::<i64>(array, *size),
1195+
BinaryView => cast_fixed_size_binary_to_binary_view(array, *size),
11951196
_ => Err(ArrowError::CastError(format!(
11961197
"Casting from {from_type:?} to {to_type:?} not supported",
11971198
))),
@@ -2327,6 +2328,27 @@ fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
23272328
Ok(Arc::new(builder.finish()))
23282329
}
23292330

2331+
fn cast_fixed_size_binary_to_binary_view(
2332+
array: &dyn Array,
2333+
_byte_width: i32,
2334+
) -> Result<ArrayRef, ArrowError> {
2335+
let array = array
2336+
.as_any()
2337+
.downcast_ref::<FixedSizeBinaryArray>()
2338+
.unwrap();
2339+
2340+
let mut builder = BinaryViewBuilder::with_capacity(array.len());
2341+
for i in 0..array.len() {
2342+
if array.is_null(i) {
2343+
builder.append_null();
2344+
} else {
2345+
builder.append_value(array.value(i));
2346+
}
2347+
}
2348+
2349+
Ok(Arc::new(builder.finish()))
2350+
}
2351+
23302352
/// Helper function to cast from one `ByteArrayType` to another and vice versa.
23312353
/// If the target one (e.g., `LargeUtf8`) is too large for the source array it will return an Error.
23322354
fn cast_byte_container<FROM, TO>(array: &dyn Array) -> Result<ArrayRef, ArrowError>
@@ -4847,6 +4869,12 @@ mod tests {
48474869
assert_eq!(bytes_1, down_cast.value(0));
48484870
assert_eq!(bytes_2, down_cast.value(1));
48494871
assert!(down_cast.is_null(2));
4872+
4873+
let array_ref = cast(&a1, &DataType::BinaryView).unwrap();
4874+
let down_cast = array_ref.as_binary_view();
4875+
assert_eq!(bytes_1, down_cast.value(0));
4876+
assert_eq!(bytes_2, down_cast.value(1));
4877+
assert!(down_cast.is_null(2));
48504878
}
48514879

48524880
#[test]

0 commit comments

Comments
 (0)