Skip to content

Commit

Permalink
fix: iterator bad implement (#2760)
Browse files Browse the repository at this point in the history
* fix: iter_data() doesn't stop

* chore: Update src/datatypes/src/vectors/decimal.rs

---------

Co-authored-by: Yingwen <[email protected]>
  • Loading branch information
QuenKar and evenyag authored Nov 16, 2023
1 parent ae27fbc commit d2cf72e
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/datatypes/src/vectors/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,9 @@ impl<'a> Iterator for Decimal128Iter<'a> {
type Item = Option<Decimal128>;

fn next(&mut self) -> Option<Self::Item> {
Some(
self.iter
.next()
.and_then(|v| v.map(|v| Decimal128::new(v, self.precision, self.scale))),
)
self.iter
.next()
.map(|item| item.map(|v| Decimal128::new(v, self.precision, self.scale)))
}

fn size_hint(&self) -> (usize, Option<usize>) {
Expand Down Expand Up @@ -519,3 +517,22 @@ pub mod tests {
assert!(array.is_null(4));
}
}

#[test]
fn test_decimal28_vector_iter_data() {
let vector = Decimal128Vector::from_values(vec![1, 2, 3, 4])
.with_precision_and_scale(3, 1)
.unwrap();
let mut iter = vector.iter_data();
assert_eq!(iter.next(), Some(Some(Decimal128::new(1, 3, 1))));
assert_eq!(iter.next(), Some(Some(Decimal128::new(2, 3, 1))));
assert_eq!(iter.next(), Some(Some(Decimal128::new(3, 3, 1))));
assert_eq!(iter.next(), Some(Some(Decimal128::new(4, 3, 1))));
assert_eq!(iter.next(), None);

let values = vector
.iter_data()
.filter_map(|v| v.map(|x| x.val() * 2))
.collect::<Vec<_>>();
assert_eq!(values, vec![2, 4, 6, 8]);
}

0 comments on commit d2cf72e

Please sign in to comment.