Skip to content

feat: coerce fixed size binary to binary view #7431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 29 additions & 1 deletion arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
))),
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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]
Expand Down