Skip to content

Commit 1295b00

Browse files
ngli-menglimealamb
authored
Added casting from Binary/LargeBinary to Utf8View (#6592)
* Added casting from Binary and LargeBinary to Utf8View, along with testing for it. * Avoid overflow --------- Co-authored-by: nglime <[email protected]> Co-authored-by: Andrew Lamb <[email protected]>
1 parent f86668c commit 1295b00

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

arrow-cast/src/cast/mod.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
206206
DataType::is_integer(to_type) || DataType::is_floating(to_type) || to_type == &Utf8 || to_type == &LargeUtf8
207207
}
208208

209-
(Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView) => true,
210-
(LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView) => true,
209+
(Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
210+
(LargeBinary, Binary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View ) => true,
211211
(FixedSizeBinary(_), Binary | LargeBinary) => true,
212212
(
213213
Utf8 | LargeUtf8 | Utf8View,
@@ -1411,6 +1411,9 @@ pub fn cast_with_options(
14111411
cast_binary_to_fixed_size_binary::<i32>(array, *size, cast_options)
14121412
}
14131413
BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i32>()))),
1414+
Utf8View => Ok(Arc::new(StringViewArray::from(
1415+
cast_binary_to_string::<i32>(array, cast_options)?.as_string::<i32>(),
1416+
))),
14141417
_ => Err(ArrowError::CastError(format!(
14151418
"Casting from {from_type:?} to {to_type:?} not supported",
14161419
))),
@@ -1426,6 +1429,10 @@ pub fn cast_with_options(
14261429
cast_binary_to_fixed_size_binary::<i64>(array, *size, cast_options)
14271430
}
14281431
BinaryView => Ok(Arc::new(BinaryViewArray::from(array.as_binary::<i64>()))),
1432+
Utf8View => {
1433+
let array = cast_binary_to_string::<i64>(array, cast_options)?;
1434+
Ok(Arc::new(StringViewArray::from(array.as_string::<i64>())))
1435+
}
14291436
_ => Err(ArrowError::CastError(format!(
14301437
"Casting from {from_type:?} to {to_type:?} not supported",
14311438
))),
@@ -5534,14 +5541,25 @@ mod tests {
55345541
{
55355542
let binary_array = GenericBinaryArray::<O>::from_iter(VIEW_TEST_DATA);
55365543

5544+
assert!(can_cast_types(
5545+
binary_array.data_type(),
5546+
&DataType::Utf8View
5547+
));
5548+
55375549
assert!(can_cast_types(
55385550
binary_array.data_type(),
55395551
&DataType::BinaryView
55405552
));
55415553

5554+
let string_view_array = cast(&binary_array, &DataType::Utf8View).unwrap();
5555+
assert_eq!(string_view_array.data_type(), &DataType::Utf8View);
5556+
55425557
let binary_view_array = cast(&binary_array, &DataType::BinaryView).unwrap();
55435558
assert_eq!(binary_view_array.data_type(), &DataType::BinaryView);
55445559

5560+
let expect_string_view_array = StringViewArray::from_iter(VIEW_TEST_DATA);
5561+
assert_eq!(string_view_array.as_ref(), &expect_string_view_array);
5562+
55455563
let expect_binary_view_array = BinaryViewArray::from_iter(VIEW_TEST_DATA);
55465564
assert_eq!(binary_view_array.as_ref(), &expect_binary_view_array);
55475565
}

0 commit comments

Comments
 (0)