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

feat: support cast between nested list and largelist #5121

Closed
wants to merge 2 commits into from
Closed
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
58 changes: 56 additions & 2 deletions arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,13 @@ pub fn cast_with_options(
cast_list_inner::<i64>(array, to, to_type, cast_options)
}
(List(list_from), LargeList(list_to)) => {
if list_to.data_type() != list_from.data_type() {
let to_type = list_to.data_type();
let from_type = list_from.data_type();

if to_type != from_type
&& !(matches!(to_type, List(_) | LargeList(_))
|| matches!(from_type, List(_) | LargeList(_)))
{
Err(ArrowError::CastError(
"cannot cast list to large-list with different child data".into(),
))
Expand All @@ -797,7 +803,13 @@ pub fn cast_with_options(
}
}
(LargeList(list_from), List(list_to)) => {
if list_to.data_type() != list_from.data_type() {
let to_type = list_to.data_type();
let from_type = list_from.data_type();

if to_type != from_type
&& !(matches!(to_type, List(_) | LargeList(_))
|| matches!(from_type, List(_) | LargeList(_)))
{
Err(ArrowError::CastError(
"cannot cast large-list to list with different child data".into(),
))
Expand Down Expand Up @@ -7459,6 +7471,48 @@ mod tests {
assert_eq!(&expected.value(2), &actual.value(2));
}

#[test]
fn test_cast_nested_list_container() {
// large-list to list
let array = Arc::new(make_large_list_array()) as ArrayRef;
let list_array = cast(
&array,
&DataType::List(Arc::new(Field::new(
"",
DataType::List(Arc::new(Field::new("", DataType::Int32, false))),
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT this test simply ignores the nested list here, and is returning a non-nested list. This is consistent with my reading of cast_list_container

false,
))),
)
.unwrap();
let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please use AsArray in tests, i.e. .as_list::<i32>() it is much easier to read

let expected = array.as_any().downcast_ref::<LargeListArray>().unwrap();

assert_eq!(&expected.value(0), &actual.value(0));
assert_eq!(&expected.value(1), &actual.value(1));
assert_eq!(&expected.value(2), &actual.value(2));

// list to large-list
let array = Arc::new(make_list_array()) as ArrayRef;
let large_list_array = cast(
&array,
&DataType::LargeList(Arc::new(Field::new(
"",
DataType::LargeList(Arc::new(Field::new("", DataType::Int32, false))),
false,
))),
)
.unwrap();
let actual = large_list_array
.as_any()
.downcast_ref::<LargeListArray>()
.unwrap();
let expected = array.as_any().downcast_ref::<ListArray>().unwrap();

assert_eq!(&expected.value(0), &actual.value(0));
assert_eq!(&expected.value(1), &actual.value(1));
assert_eq!(&expected.value(2), &actual.value(2));
}

#[test]
fn test_cast_list_to_fsl() {
// There four noteworthy cases we should handle:
Expand Down
Loading