Skip to content
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

Added casting from Binary/LargeBinary to Utf8View #6592

Merged
merged 2 commits into from
Oct 24, 2024
Merged
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
22 changes: 20 additions & 2 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
DataType::is_integer(to_type) || DataType::is_floating(to_type) || to_type == &Utf8 || to_type == &LargeUtf8
}

(Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView) => true,
(LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView) => true,
(Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
(LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
(FixedSizeBinary(_), Binary | LargeBinary) => true,
(
Utf8 | LargeUtf8 | Utf8View,
Expand Down Expand Up @@ -1399,6 +1399,9 @@ pub fn cast_with_options(
cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
}
BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
Utf8View => Ok(Arc::new(StringViewArray::from(
cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
))),
_ => Err(ArrowError::CastError(format!(
"Casting from {from_type:?} to {to_type:?} not supported",
))),
Expand All @@ -1414,6 +1417,10 @@ pub fn cast_with_options(
cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
}
BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
Utf8View => {
let array = cast_binary_to_string::<i64>(array, cast_options)?;
Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
}
_ => Err(ArrowError::CastError(format!(
"Casting from {from_type:?} to {to_type:?} not supported",
))),
Expand Down Expand Up @@ -5368,14 +5375,25 @@ mod tests {
{
let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);

assert!(can_cast_types(
binary_array.data_type(),
&DataType::Utf8View
));

assert!(can_cast_types(
binary_array.data_type(),
&DataType::BinaryView
));

let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked that this is invoked with both BinaryArray and LargeBinaryArray

assert_eq!(string_view_array.data_type(), &DataType::Utf8View);

let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);

let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
assert_eq!(string_view_array.as_ref(), &expect_string_view_array);

let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
}
Expand Down
Loading