tustvold commented on code in PR #3578:
URL: https://github.com/apache/arrow-rs/pull/3578#discussion_r1082668780
##########
parquet/src/util/bit_util.rs:
##########
@@ -17,76 +17,104 @@
use std::{cmp, mem::size_of};
-use crate::data_type::AsBytes;
+use crate::data_type::{AsBytes, ByteArray, FixedLenByteArray, Int96};
+use crate::errors::{ParquetError, Result};
use crate::util::bit_pack::{unpack16, unpack32, unpack64, unpack8};
use crate::util::memory::ByteBufferPtr;
#[inline]
-pub fn from_ne_slice<T: FromBytes>(bs: &[u8]) -> T {
- let mut b = T::Buffer::default();
- {
- let b = b.as_mut();
- let bs = &bs[..b.len()];
- b.copy_from_slice(bs);
- }
- T::from_ne_bytes(b)
+pub fn from_le_slice<T: FromBytes>(bs: &[u8]) -> T {
+ // TODO: propagate the error (#3577)
+ T::try_from_le_slice(bs).unwrap()
}
#[inline]
-pub fn from_le_slice<T: FromBytes>(bs: &[u8]) -> T {
- let mut b = T::Buffer::default();
- {
- let b = b.as_mut();
- let bs = &bs[..b.len()];
- b.copy_from_slice(bs);
+fn array_from_slice<const N: usize>(bs: &[u8]) -> Result<[u8; N]> {
+ // Need to slice as may be called with zero-padded values
+ match bs.get(..N) {
+ Some(b) => Ok(b.try_into().unwrap()),
+ None => Err(general_err!(
+ "error converting value, expected {} bytes got {}",
+ N,
+ bs.len()
+ )),
}
- T::from_le_bytes(b)
}
pub trait FromBytes: Sized {
type Buffer: AsMut<[u8]> + Default;
+ fn try_from_le_slice(b: &[u8]) -> Result<Self>;
fn from_le_bytes(bs: Self::Buffer) -> Self;
- fn from_be_bytes(bs: Self::Buffer) -> Self;
- fn from_ne_bytes(bs: Self::Buffer) -> Self;
}
macro_rules! from_le_bytes {
($($ty: ty),*) => {
$(
impl FromBytes for $ty {
type Buffer = [u8; size_of::<Self>()];
+ fn try_from_le_slice(b: &[u8]) -> Result<Self> {
+ Ok(Self::from_le_bytes(array_from_slice(b)?))
+ }
fn from_le_bytes(bs: Self::Buffer) -> Self {
<$ty>::from_le_bytes(bs)
}
- fn from_be_bytes(bs: Self::Buffer) -> Self {
- <$ty>::from_be_bytes(bs)
- }
- fn from_ne_bytes(bs: Self::Buffer) -> Self {
- <$ty>::from_ne_bytes(bs)
- }
Review Comment:
Data in parquet is always stored as little endian -
https://github.com/apache/parquet-format/blob/master/Encodings.md#plain-plain--0
The use of ne_bytes was actually incorrect
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]