Skip to content

Commit

Permalink
Cleanup get_vlen_bytes_and_offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Jul 25, 2024
1 parent b3110ea commit 77e9e4e
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/array/codec/array_to_bytes/vlen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,27 @@ fn get_vlen_bytes_and_offsets(
}
_ => unreachable!("other data types are not part of VlenIndexDataType"),
};
let data_len_expected = *index.last().unwrap();

// Get the data length
let Some(&data_len_expected) = index.last() else {
return Err(CodecError::Other(
"Index is empty? It should have at least one element".to_string(),
));
};

// Decode the data
let data = &bytes[data_start..data_start + data_compressed_len];
let data = if let Ok(data_len_expected) = NonZeroU64::try_from(data_len_expected as u64) {
data_codecs.decode(
data.into(),
&ChunkRepresentation::new(
vec![data_len_expected],
DataType::UInt8,
FillValue::from(0u8),
)
.unwrap(),
&unsafe {
// SAFETY: data type and fill value are compatible
ChunkRepresentation::new_unchecked(
vec![data_len_expected],
DataType::UInt8,
FillValue::from(0u8),
)
},
options,
)?
} else {
Expand All @@ -114,12 +122,14 @@ fn get_vlen_bytes_and_offsets(
.into_owned();
let data_len = data.len();

// Validate the offsets
// Check the data length is as expected
if data_len != data_len_expected {
return Err(CodecError::Other(format!(
"Expected data length {data_len_expected} does not match data length {data_len}"
)));
}

// Validate the offsets
for (curr, next) in index.iter().tuple_windows() {
if next < curr || *next > data_len {
return Err(CodecError::Other(
Expand Down

0 comments on commit 77e9e4e

Please sign in to comment.