use std::sync::Arc;
use arrow_array::{Array, ArrayRef, FixedSizeListArray};
use arrow_cast::CastOptions;
use arrow_schema::{ArrowError, DataType};
pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
use DataType::*;
match (from_type, to_type) {
(FixedSizeList(from_field, size_from), FixedSizeList(to_field, size_to)) => {
size_from == size_to && can_cast_types(from_field.data_type(), to_field.data_type())
}
_ => arrow_cast::can_cast_types(from_type, to_type),
}
}
pub fn cast_with_options(
array: &dyn Array,
to_type: &DataType,
cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError> {
use DataType::*;
match (array.data_type(), to_type) {
(FixedSizeList(_, size_from), FixedSizeList(to_field, size_to)) if size_from == size_to => {
let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
let values = cast_with_options(array.values(), to_field.data_type(), cast_options)?;
Ok(Arc::new(FixedSizeListArray::try_new(
to_field.clone(),
*size_from,
values,
array.nulls().cloned(),
)?))
}
_ => arrow_cast::cast_with_options(array, to_type, cast_options),
}
}