Repository navigation
ARROW-11799: [Rust] fix len of string and binary arrays created from unbound iterator #9588
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
Closed
yordan-pavlov
wants to merge
2
commits into
apache:master
from
yordan-pavlov:fix_array_len_from_unbound_iter
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -205,28 +205,30 @@ where | |
| let (_, data_len) = iter.size_hint(); | ||
| let data_len = data_len.expect("Iterator must be sized"); // panic if no upper bound. | ||
|
jorgecarleitao marked this conversation as resolved.
Resolved
|
||
|
|
||
| let mut offsets = | ||
| MutableBuffer::new((data_len + 1) * std::mem::size_of::<OffsetSize>()); | ||
| let offset_size = std::mem::size_of::<OffsetSize>(); | ||
| let mut offsets = MutableBuffer::new((data_len + 1) * offset_size); | ||
| let mut values = MutableBuffer::new(0); | ||
| let mut null_buf = MutableBuffer::new_null(data_len); | ||
|
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. I have the same question about the null buffer's length here as well. |
||
| let null_slice = null_buf.as_slice_mut(); | ||
| let mut length_so_far = OffsetSize::zero(); | ||
| offsets.push(length_so_far); | ||
|
|
||
| for (i, s) in iter.enumerate() { | ||
| if let Some(s) = s { | ||
| let s = s.as_ref(); | ||
| let value_bytes = if let Some(ref s) = s { | ||
| // set null bit | ||
| bit_util::set_bit(null_slice, i); | ||
|
|
||
| length_so_far += OffsetSize::from_usize(s.len()).unwrap(); | ||
| values.extend_from_slice(s.as_bytes()); | ||
| let s_bytes = s.as_ref().as_bytes(); | ||
| length_so_far += OffsetSize::from_usize(s_bytes.len()).unwrap(); | ||
| s_bytes | ||
| } else { | ||
| values.extend_from_slice(b""); | ||
| } | ||
| b"" | ||
| }; | ||
| values.extend_from_slice(value_bytes); | ||
| offsets.push(length_so_far); | ||
| } | ||
|
|
||
| // calculate actual data_len, which may be different from the iterator's upper bound | ||
| let data_len = (offsets.len() / offset_size) - 1; | ||
| let array_data = ArrayData::builder(OffsetSize::DATA_TYPE) | ||
| .len(data_len) | ||
| .add_buffer(offsets.into()) | ||
|
|
@@ -490,4 +492,28 @@ mod tests { | |
| assert_eq!(array1.value(0), "hello"); | ||
| assert_eq!(array1.value(1), "hello2"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_string_array_from_unbound_iter() { | ||
| // iterator that doesn't declare (upper) size bound | ||
| let string_iter = (0..) | ||
| .scan(0usize, |pos, i| { | ||
| if *pos < 10 { | ||
| *pos += 1; | ||
| Some(Some(format!("value {}", i))) | ||
| } else { | ||
| // actually returns up to 10 values | ||
| None | ||
| } | ||
| }) | ||
| // limited using take() | ||
| .take(100); | ||
|
|
||
| let (_, upper_size_bound) = string_iter.size_hint(); | ||
| // the upper bound, defined by take above, is 100 | ||
| assert_eq!(upper_size_bound, Some(100)); | ||
| let string_array: StringArray = string_iter.collect(); | ||
| // but the actual number of items in the array should be 10 | ||
| assert_eq!(string_array.len(), 10); | ||
| } | ||
| } | ||
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.
I wonder if there is a problem of writing off the end of
null_bufwhich was allocated to be only size ofdata_len?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.
writing beyond the allocated capacity would only happen if the iterator input returns more items than the declared upper bound - this would be against the
Iteratorspec and I think that would be unlikely ;this PR addresses the opposite case where an iterator returns less items than its declared upper bound (which is still within the
Iteratorspec);Uh oh!
There was an error while loading. Please reload this page.
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.
I think that @alamb is right (my comment is actually wrong). @yordan-pavlov , the Iterator spec recommends, does not require, that the iterator reports a correct length. Consumer that lead to undefined behavior from an incorrect
size_hintare the causers of said undefined behavior.The only case where consumers can trust the iterators' length is when the interator implement
unsafe trait TrustedLen. Unfortunately,TrustedLenis still in unstable. For that reason, we have been exposingunsafe Buffer::from_trusted_len_iterand the like for those cases.The issue here is that to correctly declare a FromIterator in this case, we must use
BooleanBufferBuilderfor the null bitmap, which is significantly slower than aMutableBuffer. This is why I added thatpanic!there, even though it is unsound. This PR is just surfacing this problem where the current implementation isunsound(because the iterator can be larger or smaller thanupper_bound).Since we have been seeing people prefer the
FromIterover theBuilderAPIs, my suggestion is that we useBooleanBufferBuilder, like we are doing forPrimitiveArray, and offer (in a future PR), afrom_trusted_len_iterfor string arrays.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.
you are right @jorgecarleitao, it is up to the iterator reader to ensure incorrectly declared bounds do not result in incorrect behavior and, sadly the current implementation could write out of allocated memory in the case of an iterator that produces more items than the declared upper bound;
but although I agree this should be fixed, I think it falls outside of the scope of this PR.
Uh oh!
There was an error while loading. Please reload this page.
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.
After some more study I agree (that further improvements could be made, but this PR makes the situation better so it is good as it is)
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.
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.size_hint says
I figured i would paste that into this PR as I had looked it up to satisfy my own curiosity