Repository navigation
ARROW-11799: [Rust] fix len of string and binary arrays created from unbound iterator - #9588
yordan-pavlov wants to merge 2 commits into
Conversation
I think the biggest win with strings is if we could avoid |
|
@nevi-me this probably deserves its own discussion, but you are pretty close with your suggestion to avoid I have been doing quite a lot of profiling and benchmarking in the past few weeks on loading string arrays from parquet files, and yes, going through an intermediate I am happy to discuss this in more detail, but in short my benchmarks show that skipping the |
@yordan-pavlov we have observed very similar things while working with the Rust parquet reader in IOx -- I would be interesting in helping to support your efforts to improve the performance (either directly or with reviews, etc). Thank you for leading the charge |
alamb
left a comment
There was a problem hiding this comment.
Thank you very much @yordan-pavlov -- this seems uniformly better than the current master code. I do worry about issues with MutableBuffer.
I think @jorgecarleitao will be away for a few more weeks. I suggest we merge this PR in and handle any additional potential problems with MutableBuffer as a follow on.
| } | ||
|
|
||
| // calculate actual data_len, which may be different from the iterator's upper bound | ||
| let data_len = offsets.len() - 1; |
There was a problem hiding this comment.
I wonder if there is a problem of writing off the end of null_buf which was allocated to be only size of data_len?
There was a problem hiding this comment.
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 Iterator spec 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 Iterator spec);
There was a problem hiding this comment.
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_hint are 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, TrustedLen is still in unstable. For that reason, we have been exposing unsafe Buffer::from_trusted_len_iter and the like for those cases.
The issue here is that to correctly declare a FromIterator in this case, we must use BooleanBufferBuilder for the null bitmap, which is significantly slower than a MutableBuffer. This is why I added that panic! there, even though it is unsound. This PR is just surfacing this problem where the current implementation is unsound (because the iterator can be larger or smaller than upper_bound).
Since we have been seeing people prefer the FromIter over the Builder APIs, my suggestion is that we use BooleanBufferBuilder, like we are doing for PrimitiveArray, and offer (in a future PR), a from_trusted_len_iter for string arrays.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
It is not enforced that an iterator implementation yields the declared number of elements. A buggy iterator may yield less than the lower bound or more than the upper bound of elements.
size_hint() is primarily intended to be used for optimizations such as reserving space for the elements of the iterator, but must not be trusted to e.g., omit bounds checks in unsafe code. An incorrect implementation of size_hint() should not lead to memory safety violations.
I figured i would paste that into this PR as I had looked it up to satisfy my own curiosity
| 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); |
There was a problem hiding this comment.
I have the same question about the null buffer's length here as well.
jorgecarleitao
left a comment
There was a problem hiding this comment.
Left a comment that also fixes the case where the upper bound is unknown (because we can :P)
|
I reviewed this PR again, and also locally tested it against the latest apache master and everything still passes. I filed https://issues.apache.org/jira/browse/ARROW-11862 to track the lurking bug from iterators that report Thanks again @yordan-pavlov for this fix |
|
@yordan-pavlov And reading all the parquet files of the benchmark to memory (and running some queries) (left inclusive % instruction fetch, right self %).: |
|
@Dandandan I would be happy to collaborate on this;
I have managed to achieve about 10-15% improvement by replacing
All of these changes, I think, will propagate all the way to the low-level decoders and will take time, but when done should put us in a good position to transition to |
|
fwiw, I strongly agree with you, @yordan-pavlov on all those marks. I recently went through the |
|
@yordan-pavlov thanks a lot for detailed descriptions. I think that's a great overview of the current situation. I can help with reviewing your next PR (maybe the I will also have a look at using a sampling profiler ( I think from different queries the source of the "hot path" might be very different. For example, I didn't so far see that much in the |


While looking for a way to make loading array data from parquet files faster, I stumbled on an edge case where string and binary arrays are created with an incorrect length from an iterator with no upper bound.
Here is an example for such an iterator:
For even more details please see the new tests I have added in this PR.
Fortunately this is easy to fix by using the length of the child offset array.
@jorgecarleitao