-
Notifications
You must be signed in to change notification settings - Fork 875
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
Changes from 2 commits
ef9a0d5
2dd2298
405753f
5ff47cc
e03bef1
39511d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) => { | ||||||
can_cast_types(from_type, list_to.data_type())}, | ||||||
// cast one decimal type to another decimal type | ||||||
(Decimal128(_, _), Decimal128(_, _)) => true, | ||||||
(Decimal256(_, _), Decimal256(_, _)) => true, | ||||||
|
@@ -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) => { | ||||||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
cast_values_to_fixed_size_list(array, to, size, cast_options) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
(Decimal128(_, s1), Decimal128(p2, s2)) => { | ||||||
cast_decimal_to_decimal_same_type::<Decimal128Type>( | ||||||
array.as_primitive(), | ||||||
|
@@ -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, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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>( | ||||||
|
@@ -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>() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||||||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
// 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)); | ||||||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_cast_list_containers() { | ||||||
// large-list to list | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.