Skip to content

Commit

Permalink
remove casting other type to null type (apache#1942)
Browse files Browse the repository at this point in the history
  • Loading branch information
liukun4515 authored and ovr committed Aug 15, 2022
1 parent 669c218 commit ce035c0
Showing 1 changed file with 12 additions and 94 deletions.
106 changes: 12 additions & 94 deletions arrow/src/compute/kernels/cast.rs
Expand Up @@ -105,39 +105,7 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
| FixedSizeList(_, _)
| Struct(_)
| Map(_, _)
| Dictionary(_, _),
)
| (
Boolean
| Int8
| UInt8
| Int16
| UInt16
| Int32
| UInt32
| Float32
| Date32
| Time32(_)
| Int64
| UInt64
| Float64
| Date64
| Timestamp(_, _)
| Time64(_)
| Duration(_)
| Interval(_)
| FixedSizeBinary(_)
| Binary
| Utf8
| LargeBinary
| LargeUtf8
| List(_)
| LargeList(_)
| FixedSizeList(_, _)
| Struct(_)
| Map(_, _)
| Dictionary(_, _),
Null,
| Dictionary(_, _)
) => true,
(Decimal(_, _), _) => false,
(_, Decimal(_, _)) => false,
Expand Down Expand Up @@ -512,38 +480,6 @@ pub fn cast_with_options(
| Struct(_)
| Map(_, _)
| Dictionary(_, _),
)
| (
Boolean
| Int8
| UInt8
| Int16
| UInt16
| Int32
| UInt32
| Float32
| Date32
| Time32(_)
| Int64
| UInt64
| Float64
| Date64
| Timestamp(_, _)
| Time64(_)
| Duration(_)
| Interval(_)
| FixedSizeBinary(_)
| Binary
| Utf8
| LargeBinary
| LargeUtf8
| List(_)
| LargeList(_)
| FixedSizeList(_, _)
| Struct(_)
| Map(_, _)
| Dictionary(_, _),
Null,
) => Ok(new_null_array(to_type, array.len())),
(Struct(_), _) => Err(ArrowError::CastError(
"Cannot cast from struct to other types".to_string(),
Expand Down Expand Up @@ -4328,14 +4264,6 @@ mod tests {
assert_eq!(cast_array.data_type(), &cast_type);
assert_eq!(cast_array, &expected);
}
{
let array = Arc::new($ARR_TYPE::from(vec![None; 4])) as ArrayRef;
let expected = NullArray::new(4);
let cast_array = cast(&array, &DataType::Null).expect("cast failed");
let cast_array = as_null_array(&cast_array);
assert_eq!(cast_array.data_type(), &DataType::Null);
assert_eq!(cast_array, &expected);
}
}};
}

Expand All @@ -4354,17 +4282,7 @@ mod tests {
typed_test!(Date64Array, Date64, Date64Type);
}

fn cast_from_and_to_null(data_type: &DataType) {
// Cast from data_type to null
{
let array = new_null_array(data_type, 4);
assert_eq!(array.data_type(), data_type);
let cast_array = cast(&array, &DataType::Null).expect("cast failed");
assert_eq!(cast_array.data_type(), &DataType::Null);
for i in 0..4 {
assert!(cast_array.is_null(i));
}
}
fn cast_from_null_to_other(data_type: &DataType) {
// Cast from null to data_type
{
let array = new_null_array(&DataType::Null, 4);
Expand All @@ -4379,10 +4297,10 @@ mod tests {

#[test]
fn test_cast_null_from_and_to_variable_sized() {
cast_from_and_to_null(&DataType::Utf8);
cast_from_and_to_null(&DataType::LargeUtf8);
cast_from_and_to_null(&DataType::Binary);
cast_from_and_to_null(&DataType::LargeBinary);
cast_from_null_to_other(&DataType::Utf8);
cast_from_null_to_other(&DataType::LargeUtf8);
cast_from_null_to_other(&DataType::Binary);
cast_from_null_to_other(&DataType::LargeBinary);
}

#[test]
Expand All @@ -4399,32 +4317,32 @@ mod tests {
)),
false,
);
cast_from_and_to_null(&data_type);
cast_from_null_to_other(&data_type);

// Cast null from and to list
let data_type =
DataType::List(Box::new(Field::new("item", DataType::Int32, true)));
cast_from_and_to_null(&data_type);
cast_from_null_to_other(&data_type);
let data_type =
DataType::LargeList(Box::new(Field::new("item", DataType::Int32, true)));
cast_from_and_to_null(&data_type);
cast_from_null_to_other(&data_type);
let data_type = DataType::FixedSizeList(
Box::new(Field::new("item", DataType::Int32, true)),
4,
);
cast_from_and_to_null(&data_type);
cast_from_null_to_other(&data_type);

// Cast null from and to dictionary
let values = vec![None, None, None, None] as Vec<Option<&str>>;
let array: DictionaryArray<Int8Type> = values.into_iter().collect();
let array = Arc::new(array) as ArrayRef;
let data_type = array.data_type().to_owned();
cast_from_and_to_null(&data_type);
cast_from_null_to_other(&data_type);

// Cast null from and to struct
let data_type =
DataType::Struct(vec![Field::new("data", DataType::Int64, false)]);
cast_from_and_to_null(&data_type);
cast_from_null_to_other(&data_type);
}

/// Print the `DictionaryArray` `array` as a vector of strings
Expand Down

0 comments on commit ce035c0

Please sign in to comment.