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

Minor: support cast values to fixedsizelist #5340

Merged
merged 6 commits into from
Feb 4, 2024
Merged
Changes from 2 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
47 changes: 47 additions & 0 deletions arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
}
(_, List(list_to)) => can_cast_types(from_type, list_to.data_type()),
(_, LargeList(list_to)) => can_cast_types(from_type, list_to.data_type()),
(_, FixedSizeList(list_to,size)) if size.eq(&1_i32) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
(_, FixedSizeList(list_to,size)) if size.eq(&1_i32) => {
(_, FixedSizeList(list_to,size)) if *size == 1 => {

can_cast_types(from_type, list_to.data_type())},
// cast one decimal type to another decimal type
(Decimal128(_, _), Decimal128(_, _)) => true,
(Decimal256(_, _), Decimal256(_, _)) => true,
Expand Down Expand Up @@ -802,6 +804,9 @@ pub fn cast_with_options(
}
(_, List(ref to)) => cast_values_to_list::<i32>(array, to, cast_options),
(_, LargeList(ref to)) => cast_values_to_list::<i64>(array, to, cast_options),
(_, FixedSizeList(ref to, size)) if size.eq(&1_i32) => {
cast_values_to_fixed_size_list(array, to, size, cast_options)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
cast_values_to_fixed_size_list(array, to, size, cast_options)
cast_values_to_fixed_size_list(array, to, *size, cast_options)

}
(Decimal128(_, s1), Decimal128(p2, s2)) => {
cast_decimal_to_decimal_same_type::<Decimal128Type>(
array.as_primitive(),
Expand Down Expand Up @@ -3040,6 +3045,18 @@ fn cast_values_to_list<O: OffsetSizeTrait>(
Ok(Arc::new(list))
}

/// Helper function that takes a primitive array and casts to a fixed size list array.
fn cast_values_to_fixed_size_list(
array: &dyn Array,
to: &FieldRef,
size: &i32,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
size: &i32,
size: i32,

cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError> {
let values = cast_with_options(array, to.data_type(), cast_options)?;
let list = FixedSizeListArray::new(to.clone(), *size, values, None);
Ok(Arc::new(list))
}

/// A specified helper to cast from `GenericBinaryArray` to `GenericStringArray` when they have same
/// offset size so re-encoding offset is unnecessary.
fn cast_binary_to_string<O: OffsetSizeTrait>(
Expand Down Expand Up @@ -7609,6 +7626,36 @@ mod tests {
assert_eq!(expected.values(), actual.values());
}

#[test]
fn test_cast_utf8_to_list() {
// DataType::List
let array = Arc::new(StringArray::from(vec!["5"])) as ArrayRef;
let field = Arc::new(Field::new("", DataType::Int32, false));
let list_array = cast(&array, &DataType::List(field.clone())).unwrap();
let actual = list_array.as_any().downcast_ref::<ListArray>().unwrap();
let expect = ListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
assert_eq!(&expect.value(0), &actual.value(0));

// DataType::LargeList
let list_array = cast(&array, &DataType::LargeList(field.clone())).unwrap();
let actual = list_array
.as_any()
.downcast_ref::<LargeListArray>()
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 use AsArray please

.unwrap();
let expect = LargeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])]);
assert_eq!(&expect.value(0), &actual.value(0));

// DataType::FixedSizeList
let list_array = cast(&array, &DataType::FixedSizeList(field.clone(), 1)).unwrap();
let actual = list_array
.as_any()
.downcast_ref::<FixedSizeListArray>()
.unwrap();
let expect =
FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>([Some([Some(5)])], 1);
assert_eq!(&expect.value(0), &actual.value(0));
}

#[test]
fn test_cast_list_containers() {
// large-list to list
Expand Down
Loading