-
Notifications
You must be signed in to change notification settings - Fork 4k
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) { | ||
|
||
| 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); | ||
|
||
| 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) { | ||
|
||
| 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)); | ||
|
||
| 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)); | ||
|
||
| 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. 💯