-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Support FixedSizedListArray for length kernel
#4520
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
Merged
tustvold
merged 6 commits into
apache:master
from
Weijun-H:Support-FixedSizedListArray-for-length-kernel
Jul 19, 2023
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a9fbda4
feat: Support FixedSizedListArray for length kernel
Weijun-H b80afa0
fix clippy
Weijun-H 07e99ea
update comment
Weijun-H aaf29c1
avoid unsafe
Weijun-H 4c56e86
reduce useless trait
Weijun-H e76f7f5
remove T
Weijun-H File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,30 @@ where | |
| unary_offsets!(array, T::DATA_TYPE, |x| x) | ||
| } | ||
|
|
||
| fn length_list_fixed_size<T>(array: &dyn Array, length: i32) -> ArrayRef | ||
| where | ||
| T: ArrowPrimitiveType, | ||
| T::Native: OffsetSizeTrait, | ||
| { | ||
| let array = array.as_any().downcast_ref::<FixedSizeListArray>().unwrap(); | ||
| let null_bit_buffer = array.nulls().map(|b| b.inner().sliced()); | ||
| let length_list = array.len(); | ||
| let buffer = Buffer::from_vec(vec![length; length_list]); | ||
|
Contributor
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. This assumes T::Native is i32 |
||
|
|
||
| let data = unsafe { | ||
|
||
| ArrayData::new_unchecked( | ||
| T::DATA_TYPE, | ||
| length_list, | ||
| None, | ||
| null_bit_buffer, | ||
| 0, | ||
| vec![buffer], | ||
| vec![], | ||
| ) | ||
| }; | ||
| make_array(data) | ||
| } | ||
|
|
||
| fn length_binary<O, T>(array: &dyn Array) -> ArrayRef | ||
| where | ||
| O: OffsetSizeTrait, | ||
|
|
@@ -172,6 +196,9 @@ pub fn length(array: &dyn Array) -> Result<ArrayRef, ArrowError> { | |
| DataType::LargeUtf8 => Ok(length_string::<i64, Int64Type>(array)), | ||
| DataType::Binary => Ok(length_binary::<i32, Int32Type>(array)), | ||
| DataType::LargeBinary => Ok(length_binary::<i64, Int64Type>(array)), | ||
| DataType::FixedSizeList(_, len) => { | ||
| Ok(length_list_fixed_size::<Int32Type>(array, *len)) | ||
| } | ||
| other => Err(ArrowError::ComputeError(format!( | ||
| "length not supported for {other:?}" | ||
| ))), | ||
|
|
@@ -215,6 +242,8 @@ pub fn bit_length(array: &dyn Array) -> Result<ArrayRef, ArrowError> { | |
| mod tests { | ||
| use super::*; | ||
| use arrow_array::cast::AsArray; | ||
| use arrow_buffer::NullBuffer; | ||
| use arrow_schema::Field; | ||
|
|
||
| fn double_vec<T: Clone>(v: Vec<T>) -> Vec<T> { | ||
| [&v[..], &v[..]].concat() | ||
|
|
@@ -696,4 +725,34 @@ mod tests { | |
| assert_eq!(expected[i], actual[i],); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_fixed_size_list_length() { | ||
| // Construct a value array | ||
| let value_data = ArrayData::builder(DataType::Int32) | ||
| .len(9) | ||
| .add_buffer(Buffer::from_slice_ref([0, 1, 2, 3, 4, 5, 6, 7, 8])) | ||
| .build() | ||
| .unwrap(); | ||
| let list_data_type = DataType::FixedSizeList( | ||
| Arc::new(Field::new("item", DataType::Int32, false)), | ||
| 3, | ||
| ); | ||
| let nulls = NullBuffer::from(vec![true, false, true]); | ||
| let list_data = ArrayData::builder(list_data_type) | ||
| .len(3) | ||
| .add_child_data(value_data) | ||
| .nulls(Some(nulls)) | ||
| .build() | ||
| .unwrap(); | ||
| let list_array = FixedSizeListArray::from(list_data); | ||
|
|
||
| let lengths = length(&list_array).unwrap(); | ||
| let lengths = lengths.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
|
|
||
| assert_eq!(lengths.len(), 3); | ||
| assert_eq!(lengths.value(0), 3); | ||
| assert!(lengths.is_null(1)); | ||
| assert_eq!(lengths.value(2), 3); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.