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

Disallow cast from other datatypes to NullType #1942

Merged
merged 1 commit into from Jun 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -514,38 +482,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 @@ -4347,14 +4283,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 @@ -4373,17 +4301,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 @@ -4398,10 +4316,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 @@ -4418,32 +4336,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