Skip to content

Commit

Permalink
fix: panic when read column of type array<float>
Browse files Browse the repository at this point in the history
  • Loading branch information
youngsofun committed Jul 2, 2024
1 parent a57abda commit 81f800f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/array_decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub fn array_decoder_factory(
}
);
let iter = stripe.stream_map().get(column, Kind::Data);
let iter = Box::new(FloatIter::new(iter, stripe.number_of_rows()));
let iter = Box::new(FloatIter::new(iter));
let present = get_present_vec(column, stripe)?
.map(|iter| Box::new(iter.into_iter()) as Box<dyn Iterator<Item = bool> + Send>);
Box::new(Float32ArrayDecoder::new(iter, present))
Expand All @@ -418,7 +418,7 @@ pub fn array_decoder_factory(
}
);
let iter = stripe.stream_map().get(column, Kind::Data);
let iter = Box::new(FloatIter::new(iter, stripe.number_of_rows()));
let iter = Box::new(FloatIter::new(iter));
let present = get_present_vec(column, stripe)?
.map(|iter| Box::new(iter.into_iter()) as Box<dyn Iterator<Item = bool> + Send>);
Box::new(Float64ArrayDecoder::new(iter, present))
Expand Down
39 changes: 14 additions & 25 deletions src/reader/decode/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,53 +32,42 @@ impl Float for f64 {
/// An iterator
pub struct FloatIter<T: Float, R: std::io::Read> {
reader: R,
remaining: usize,
phantom: std::marker::PhantomData<T>,
}

impl<T: Float, R: std::io::Read> FloatIter<T, R> {
/// Returns a new [`FloatIter`]
#[inline]
pub fn new(reader: R, length: usize) -> Self {
pub fn new(reader: R) -> Self {
Self {
reader,
remaining: length,
phantom: Default::default(),
}
}

/// The number of items remaining
#[inline]
pub fn len(&self) -> usize {
self.remaining
}
}

impl<T: Float, R: std::io::Read> Iterator for FloatIter<T, R> {
type Item = Result<T>;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.remaining == 0 {
return None;
}
let mut chunk: T::OBytes = Default::default();
if let Err(err) = self
match self
.reader
.read_exact(chunk.as_mut())
.read(chunk.as_mut())
.context(error::DecodeFloatSnafu)
{
return Some(Err(err));
Err(err) => {
return Some(Err(err));
}
Ok(n) => {
if n == 0 {
return None;
}
}
};
self.remaining -= 1;
Some(Ok(T::from_le_bytes(chunk)))
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.len();
(remaining, Some(remaining))
}
}

#[cfg(test)]
Expand All @@ -100,7 +89,7 @@ mod tests {
let bytes = float_to_bytes(&input);
let bytes = Cursor::new(bytes);

let iter = FloatIter::<F, _>::new(bytes, input.len());
let iter = FloatIter::<F, _>::new(bytes);
let actual = iter.collect::<Result<Vec<_>>>().unwrap();

assert_eq!(input, actual);
Expand Down Expand Up @@ -131,7 +120,7 @@ mod tests {
let bytes = float_to_bytes(&[f32::NAN]);
let bytes = Cursor::new(bytes);

let iter = FloatIter::<f32, _>::new(bytes, 1);
let iter = FloatIter::<f32, _>::new(bytes);
let actual = iter.collect::<Result<Vec<_>>>().unwrap();
assert_eq!(actual.len(), 1);
assert!(actual[0].is_nan());
Expand All @@ -142,7 +131,7 @@ mod tests {
let bytes = float_to_bytes(&[f64::NAN]);
let bytes = Cursor::new(bytes);

let iter = FloatIter::<f64, _>::new(bytes, 1);
let iter = FloatIter::<f64, _>::new(bytes);
let actual = iter.collect::<Result<Vec<_>>>().unwrap();
assert_eq!(actual.len(), 1);
assert!(actual[0].is_nan());
Expand Down

0 comments on commit 81f800f

Please sign in to comment.