You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Undefined behavior for GenericStringArray::from_iter_values if reported iterator upper bound is incorrect #1144
Describe the bug
While reviewing #197 I was able to create a reproducer that indeed caused undefined behavior
To Reproduce
use arrow::array::StringArray;// Make an iterator that is untruthful about its actual length (say it is shorter than it really is):#[derive(Debug,Clone)]structBadIterator<T>{/// where the iterator currently iscur:u64,/// How many items will this iterator *actually* makelimit:u64,/// How many items this iterator claims it will makeclaimed:u64,/// What type of item to returnpubitem:T,}impl<T>BadIterator<T>{/// Create a new iterator for <limit> items, but that reports to/// produce <claimed> itemsfnnew(limit:u64,claimed:u64,item:T) -> Self{Self{cur:0,
limit,
claimed,
item
}}}impl<T:Clone>IteratorforBadIterator<T>{typeItem = T;fnnext(&mutself) -> Option<Self::Item>{ifself.cur < self.limit{self.cur += 1;Some(self.item.clone())}else{None}}/// report whatever the iterator says tofnsize_hint(&self) -> (usize,Option<usize>){(0,Some(self.claimedasusize))}}fnfrom_vec(v:Vec<&str>) -> StringArray{
v.into_iter().map(Some).collect()}// This definitely causes a problemfnuse_iter_values(){// correctly report size:let array = StringArray::from_iter_values(BadIterator::new(3,3,"foo"));assert_eq!(array, from_vec(vec!["foo","foo","foo"]));// incorrectly correctly report size to be 10let array = StringArray::from_iter_values(BadIterator::new(3,10,"foo"));assert_eq!(array, from_vec(vec!["foo","foo","foo"]));// incorrectly correctly report size to be 1 (too small)let array = StringArray::from_iter_values(BadIterator::new(3,1,"foo"));assert_eq!(array, from_vec(vec!["foo","foo","foo"]));}fnmain(){println!("Starting...");use_iter_values();println!("Done...");}
Results in
Starting...
Use collect
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /Users/alamb/Software/arrow-rs/arrow/src/array/array_string.rs:101:40
stack backtrace:
And undefined behavior in MIRI
Expected behavior
Success and no undefined behavior
Additional context
Add any other context about the problem here.
Strangely, I don't understand the usecase of from_iter_values compared to FromIterator
I think that from_iter_values leverages the invariant that there are no nulls in the iterator (AsRef<str>), while FromIterator accepts nulls / None. The two APIs are motivated by a significant performance penalty in creating the bitmap from an iterator of options (compared to the cost of an iterator of non-nullable values)
changed the title [-]Undefined behavior for GenericStringArray::from_iter_values if reported iterator upper bound is incorrect[/-][+]Undefined behavior for `GenericStringArray::from_iter_values` if reported iterator upper bound is incorrect[/+]on Jan 20, 2022
Activity
alamb commentedon Jan 8, 2022
Strangely, I don't understand the usecase of from_iter_values compared to
FromIteratoralamb commentedon Jan 8, 2022
It looks like
DataFusionusesfrom_iter_valuesin several places (both for StringArrays and primitive arrays)https://github.com/apache/arrow-datafusion/search?q=from_iter_values
However, when I was looking at the implementations of
FromIteratorandfrom_iter_valuesI didn't see the need for both.jorgecarleitao commentedon Jan 8, 2022
I think that
from_iter_valuesleverages the invariant that there are no nulls in the iterator (AsRef<str>), whileFromIteratoraccepts nulls / None. The two APIs are motivated by a significant performance penalty in creating the bitmap from an iterator of options (compared to the cost of an iterator of non-nullable values)[-]Undefined behavior for GenericStringArray::from_iter_values if reported iterator upper bound is incorrect[/-][+]Undefined behavior for `GenericStringArray::from_iter_values` if reported iterator upper bound is incorrect[/+]