Skip to content
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
39 changes: 24 additions & 15 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,9 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
}
(Struct(_), _) => false,
(_, Struct(_)) => false,
(_, Boolean) => {
DataType::is_integer(from_type)
|| DataType::is_floating(from_type)
|| from_type == &Utf8View
|| from_type == &Utf8
|| from_type == &LargeUtf8
}
(Boolean, _) => {
DataType::is_integer(to_type)
|| DataType::is_floating(to_type)
|| to_type == &Utf8View
|| to_type == &Utf8
|| to_type == &LargeUtf8
}

(_, Boolean) => from_type.is_integer() || from_type.is_floating() || from_type.is_string(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Little refactor using new is_string()

(Boolean, _) => to_type.is_integer() || to_type.is_floating() || to_type.is_string(),

(Binary, LargeBinary | Utf8 | LargeUtf8 | FixedSizeBinary(_) | BinaryView | Utf8View) => {
true
Expand Down Expand Up @@ -284,7 +273,7 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
) => true,
(Utf8 | LargeUtf8, Utf8View) => true,
(BinaryView, Binary | LargeBinary | Utf8 | LargeUtf8 | Utf8View) => true,
(Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
(Utf8View | Utf8 | LargeUtf8, _) => to_type.is_numeric(),
(_, Utf8 | Utf8View | LargeUtf8) => from_type.is_primitive(),

(_, Binary | LargeBinary) => from_type.is_integer(),
Expand Down Expand Up @@ -1217,6 +1206,7 @@ pub fn cast_with_options(
Int16 => parse_string::<Int16Type, i32>(array, cast_options),
Int32 => parse_string::<Int32Type, i32>(array, cast_options),
Int64 => parse_string::<Int64Type, i32>(array, cast_options),
Float16 => parse_string::<Float16Type, i32>(array, cast_options),
Float32 => parse_string::<Float32Type, i32>(array, cast_options),
Float64 => parse_string::<Float64Type, i32>(array, cast_options),
Date32 => parse_string::<Date32Type, i32>(array, cast_options),
Expand Down Expand Up @@ -1279,6 +1269,7 @@ pub fn cast_with_options(
Int16 => parse_string_view::<Int16Type>(array, cast_options),
Int32 => parse_string_view::<Int32Type>(array, cast_options),
Int64 => parse_string_view::<Int64Type>(array, cast_options),
Float16 => parse_string_view::<Float16Type>(array, cast_options),
Float32 => parse_string_view::<Float32Type>(array, cast_options),
Float64 => parse_string_view::<Float64Type>(array, cast_options),
Date32 => parse_string_view::<Date32Type>(array, cast_options),
Expand Down Expand Up @@ -1330,6 +1321,7 @@ pub fn cast_with_options(
Int16 => parse_string::<Int16Type, i64>(array, cast_options),
Int32 => parse_string::<Int32Type, i64>(array, cast_options),
Int64 => parse_string::<Int64Type, i64>(array, cast_options),
Float16 => parse_string::<Float16Type, i64>(array, cast_options),
Float32 => parse_string::<Float32Type, i64>(array, cast_options),
Float64 => parse_string::<Float64Type, i64>(array, cast_options),
Date32 => parse_string::<Date32Type, i64>(array, cast_options),
Expand Down Expand Up @@ -4433,6 +4425,23 @@ mod tests {
assert_eq!(8.9, c.value(3));
}

#[test]
fn test_cast_string_to_f16() {
let arrays = [
Arc::new(StringViewArray::from(vec!["3", "4.56", "seven", "8.9"])) as ArrayRef,
Arc::new(StringArray::from(vec!["3", "4.56", "seven", "8.9"])),
Arc::new(LargeStringArray::from(vec!["3", "4.56", "seven", "8.9"])),
];
for array in arrays {
let b = cast(&array, &DataType::Float16).unwrap();
let c = b.as_primitive::<Float16Type>();
assert_eq!(half::f16::from_f32(3.0), c.value(0));
assert_eq!(half::f16::from_f32(4.56), c.value(1));
assert!(!c.is_valid(2));
assert_eq!(half::f16::from_f32(8.9), c.value(3));
}
}

#[test]
fn test_cast_utf8view_to_decimal128() {
let array = StringViewArray::from(vec![None, Some("4"), Some("5.6"), Some("7.89")]);
Expand Down
Loading