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

Casting fixedsizelist to list/largelist #4433

Merged
merged 5 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions arrow-array/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,15 @@ pub trait AsArray: private::Sealed {
self.as_list_opt().expect("list array")
}

/// Downcast this to a [`FixedSizeListArray`] returning `None` if not possible
fn as_fixed_size_list_opt(&self) -> Option<&FixedSizeListArray>;

/// Downcast this to a [`FixedSizeListArray`] panicking if not possible
fn as_fixed_size_list(&self) -> &FixedSizeListArray {
self.as_fixed_size_list_opt()
.expect("fixed size list array")
}

/// Downcast this to a [`MapArray`] returning `None` if not possible
fn as_map_opt(&self) -> Option<&MapArray>;

Expand Down Expand Up @@ -839,6 +848,10 @@ impl AsArray for dyn Array + '_ {
self.as_any().downcast_ref()
}

fn as_fixed_size_list_opt(&self) -> Option<&FixedSizeListArray> {
self.as_any().downcast_ref()
}

fn as_map_opt(&self) -> Option<&MapArray> {
self.as_any().downcast_ref()
}
Expand Down Expand Up @@ -872,6 +885,10 @@ impl AsArray for ArrayRef {
self.as_ref().as_list_opt()
}

fn as_fixed_size_list_opt(&self) -> Option<&FixedSizeListArray> {
self.as_ref().as_fixed_size_list_opt()
}

fn as_map_opt(&self) -> Option<&MapArray> {
self.as_any().downcast_ref()
}
Expand Down
141 changes: 141 additions & 0 deletions arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
can_cast_types(list_from.data_type(), to_type)
}
(List(_), _) => false,
(FixedSizeList(list_from,_), List(list_to)) => {
list_from.data_type() == list_to.data_type()
}
(FixedSizeList(list_from,_), LargeList(list_to)) => {
list_from.data_type() == list_to.data_type()
}
(_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
(_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
// cast one decimal type to another decimal type
Expand Down Expand Up @@ -824,6 +830,25 @@ pub fn cast_with_options(
"Cannot cast list to non-list data types".to_string(),
)),
},
(FixedSizeList(list_from, _), List(list_to)) => {
if list_to.data_type() != list_from.data_type() {
Err(ArrowError::CastError(
"cannot cast fixed-size-list to list with different child data".into(),
))
} else {
cast_fixed_size_list_to_list::<i32>(array)
}
}
(FixedSizeList(list_from, _), LargeList(list_to)) => {
if list_to.data_type() != list_from.data_type() {
Err(ArrowError::CastError(
"cannot cast fixed-size-list to largelist with different child data".into(),
))
} else {
cast_fixed_size_list_to_list::<i64>(array)
}
}

(_, List(ref to)) => {
cast_primitive_to_list::<i32>(array, to, to_type, cast_options)
}
Expand Down Expand Up @@ -3822,6 +3847,17 @@ where
Ok(Arc::new(GenericByteArray::<TO>::from(array_data)))
}

fn cast_fixed_size_list_to_list<OffsetSize>(
array: &dyn Array,
) -> Result<ArrayRef, ArrowError>
where
OffsetSize: OffsetSizeTrait,
{
let fixed_size_list: &FixedSizeListArray = array.as_fixed_size_list();
let list: GenericListArray<OffsetSize> = fixed_size_list.clone().into();
Ok(Arc::new(list))
}

/// Cast the container type of List/Largelist array but not the inner types.
/// This function can leave the value data intact and only has to cast the offset dtypes.
fn cast_list_container<OffsetSizeFrom, OffsetSizeTo>(
Expand Down Expand Up @@ -7847,6 +7883,71 @@ mod tests {
assert!(!c.is_valid(5)); // "2000-01-01"
}

#[test]
fn test_can_cast_types_fixed_size_list_to_list() {
// DataType::List
let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
assert!(can_cast_types(
array1.data_type(),
&DataType::List(Arc::new(Field::new("", DataType::Int32, false)))
));

// DataType::LargeList
let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
assert!(can_cast_types(
array2.data_type(),
&DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false)))
));
}

#[test]
fn test_cast_fixed_size_list_to_list() {
// DataType::List
let array1 = Arc::new(make_fixed_size_list_array()) as ArrayRef;
let list_array1 = cast(
&array1,
&DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
)
.unwrap();
let actual = list_array1.as_any().downcast_ref::<ListArray>().unwrap();
let expected = array1
.as_any()
.downcast_ref::<FixedSizeListArray>()
.unwrap();

assert_eq!(expected.values(), actual.values());
assert_eq!(expected.len(), actual.len());

// DataType::LargeList
let array2 = Arc::new(make_fixed_size_list_array_for_large_list()) as ArrayRef;
let list_array2 = cast(
&array2,
&DataType::LargeList(Arc::new(Field::new("", DataType::Int64, false))),
)
.unwrap();
let actual = list_array2
.as_any()
.downcast_ref::<LargeListArray>()
.unwrap();
let expected = array2
.as_any()
.downcast_ref::<FixedSizeListArray>()
.unwrap();
assert_eq!(expected.values(), actual.values());
assert_eq!(expected.len(), actual.len());

// Cast previous LargeList to List
let array3 = Arc::new(actual.clone()) as ArrayRef;
let list_array3 = cast(
&array3,
&DataType::List(Arc::new(Field::new("", DataType::Int64, false))),
)
.unwrap();
let actual = list_array3.as_any().downcast_ref::<ListArray>().unwrap();
let expected = array3.as_any().downcast_ref::<LargeListArray>().unwrap();
assert_eq!(expected.values(), actual.values());
}

#[test]
fn test_cast_list_containers() {
// large-list to list
Expand Down Expand Up @@ -7929,6 +8030,46 @@ mod tests {
LargeListArray::from(list_data)
}

fn make_fixed_size_list_array() -> FixedSizeListArray {
// Construct a value array
let value_data = ArrayData::builder(DataType::Int32)
.len(8)
.add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7]))
.build()
.unwrap();

let list_data_type = DataType::FixedSizeList(
Arc::new(Field::new("item", DataType::Int32, true)),
4,
);
let list_data = ArrayData::builder(list_data_type)
.len(2)
.add_child_data(value_data)
.build()
.unwrap();
FixedSizeListArray::from(list_data)
}

fn make_fixed_size_list_array_for_large_list() -> FixedSizeListArray {
// Construct a value array
let value_data = ArrayData::builder(DataType::Int64)
.len(8)
.add_buffer(Buffer::from_slice_ref([0i64, 1, 2, 3, 4, 5, 6, 7]))
.build()
.unwrap();

let list_data_type = DataType::FixedSizeList(
Arc::new(Field::new("item", DataType::Int64, true)),
4,
);
let list_data = ArrayData::builder(list_data_type)
.len(2)
.add_child_data(value_data)
.build()
.unwrap();
FixedSizeListArray::from(list_data)
}

#[test]
fn test_utf8_cast_offsets() {
// test if offset of the array is taken into account during cast
Expand Down