-
Notifications
You must be signed in to change notification settings - Fork 4.2k
ARROW-10136: [Rust]: Fix null handling in StringArray and BinaryArray filtering, add BinaryArray::from_opt_vec #8303
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -341,7 +341,7 @@ impl FilterContext { | |
| } | ||
| DataType::Binary => { | ||
| let input_array = array.as_any().downcast_ref::<BinaryArray>().unwrap(); | ||
| let mut values: Vec<&[u8]> = Vec::with_capacity(self.filtered_count); | ||
| let mut values: Vec<Option<&[u8]>> = Vec::with_capacity(self.filtered_count); | ||
| for i in 0..self.filter_u64.len() { | ||
| // foreach u64 batch | ||
| let filter_batch = self.filter_u64[i]; | ||
|
|
@@ -353,15 +353,19 @@ impl FilterContext { | |
| // foreach bit in batch: | ||
| if (filter_batch & self.filter_mask[j]) != 0 { | ||
| let data_index = (i * 64) + j; | ||
| values.push(input_array.value(data_index)); | ||
| if input_array.is_null(data_index) { | ||
|
Contributor
Author
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 is the same pattern as in the handling for primative array: https://github.com/apache/arrow/pull/8303/files#diff-d7b0b7cde1850e8744ceda458c6dea81R294-L298
Contributor
Author
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 is the code for BinaryArray that @nevi-me referred to in #8303 (comment)) |
||
| values.push(None) | ||
| } else { | ||
| values.push(Some(input_array.value(data_index))) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Ok(Arc::new(BinaryArray::from(values))) | ||
| } | ||
| DataType::Utf8 => { | ||
| let input_array = array.as_any().downcast_ref::<StringArray>().unwrap(); | ||
| let mut values: Vec<&str> = Vec::with_capacity(self.filtered_count); | ||
| let mut values: Vec<Option<&str>> = Vec::with_capacity(self.filtered_count); | ||
|
Contributor
Author
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. Note using an It would likely be possible to avoid this allocation entirely if we used the lower level I chose to follow the style of the rest of this module, though I would love opinions on trying to perf check this / optimize it (maybe a follow on JIRA ticket is enough)?
Member
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. Doesn't an IMO we should follow up on this: for kernels we have been using a mutable buffer with null masks as much as possible.
Contributor
Author
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.
Yes, I believe you are correct. This program: Produces the following on my machine: |
||
| for i in 0..self.filter_u64.len() { | ||
| // foreach u64 batch | ||
| let filter_batch = self.filter_u64[i]; | ||
|
|
@@ -373,7 +377,11 @@ impl FilterContext { | |
| // foreach bit in batch: | ||
| if (filter_batch & self.filter_mask[j]) != 0 { | ||
| let data_index = (i * 64) + j; | ||
| values.push(input_array.value(data_index)); | ||
| if input_array.is_null(data_index) { | ||
|
Contributor
Author
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. Likewise, this special case appears to miss the null check too |
||
| values.push(None) | ||
| } else { | ||
| values.push(Some(input_array.value(data_index))) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -666,7 +674,7 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| fn test_filter_array_with_null() { | ||
| fn test_filter_primative_array_with_null() { | ||
| let a = Int32Array::from(vec![Some(5), None]); | ||
| let b = BooleanArray::from(vec![false, true]); | ||
| let c = filter(&a, &b).unwrap(); | ||
|
|
@@ -675,6 +683,31 @@ mod tests { | |
| assert_eq!(true, d.is_null(0)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_filter_string_array_with_null() { | ||
| let a = StringArray::from(vec![Some("hello"), None, Some("world"), None]); | ||
| let b = BooleanArray::from(vec![true, false, false, true]); | ||
| let c = filter(&a, &b).unwrap(); | ||
| let d = c.as_ref().as_any().downcast_ref::<StringArray>().unwrap(); | ||
| assert_eq!(2, d.len()); | ||
| assert_eq!("hello", d.value(0)); | ||
|
Member
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. I suggest that we test the 3 quantities: let expected = StringArray::from(vec![Some("hello"), None]);
assert_eq!(d, expected); |
||
| assert_eq!(false, d.is_null(0)); | ||
| assert_eq!(true, d.is_null(1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_filter_binary_array_with_null() { | ||
| let data: Vec<Option<&[u8]>> = vec![Some(b"hello"), None, Some(b"world"), None]; | ||
| let a = BinaryArray::from(data); | ||
| let b = BooleanArray::from(vec![true, false, false, true]); | ||
| let c = filter(&a, &b).unwrap(); | ||
| let d = c.as_ref().as_any().downcast_ref::<BinaryArray>().unwrap(); | ||
| assert_eq!(2, d.len()); | ||
| assert_eq!(b"hello", d.value(0)); | ||
|
Member
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. same here. |
||
| assert_eq!(false, d.is_null(0)); | ||
| assert_eq!(true, d.is_null(1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_filter_array_slice_with_null() { | ||
| let a_slice = | ||
|
|
||
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.
is this code tested somewhere?
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.
It is tested (indirectly) in https://github.com/apache/arrow/pull/8303/files#diff-d7b0b7cde1850e8744ceda458c6dea81R700 -- but I think a more specific test would be valuable. I will add one.
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.
This turned out to be a great call @jorgecarleitao -- I found a bug in this implementation while writing a test. Thank you for the suggestion. 💯