Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: out of bounds error in bitpacking #2692

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions rust/lance-encoding/src/encodings/physical/bitpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,7 @@ fn pack_bits(
// note that we don't need to do this if we wrote the full number of bits
// because source index would have been advanced by the inner loop above
if bit_len != num_bits {
let mut partial_bytes_written = num_bits / 8;

// if we didn't write the full byte for the last byte, increment by one because
// we wrote a partial byte. Also increment by one if it's a partial byte where
// the num bits is < 8
if bit_len % num_bits != 0 || partial_bytes_written == 0 {
partial_bytes_written += 1;
}
let partial_bytes_written = ceil(num_bits as usize, 8);

// we also want to the next location in src, unless we wrote something
// byte-aligned in which case the logic above would have already advanced
Expand All @@ -254,7 +247,7 @@ fn pack_bits(
to_next_byte = 0;
}

src_idx += (byte_len as u64 - partial_bytes_written + to_next_byte) as usize;
src_idx += byte_len - partial_bytes_written + to_next_byte;
}
}
}
Expand Down Expand Up @@ -468,16 +461,7 @@ impl PrimitivePageDecoder for BitpackedPageDecoder {
// note that we don't need to do this if we wrote the full number of bits
// because source index would have been advanced by the inner loop above
if self.uncompressed_bits_per_value != self.bits_per_value {
let mut partial_bytes_written = self.bits_per_value / 8;

// if we didn't write the full byte for the last byte, increment by one because
// we wrote a partial byte. Also increment by one if it's a partial byte written where
// num bits is less than 8
if self.uncompressed_bits_per_value % self.bits_per_value != 0
|| partial_bytes_written == 0
{
partial_bytes_written += 1;
}
let partial_bytes_written = ceil(self.bits_per_value as usize, 8);

// we also want to move one location to the next location in destination,
// unless we wrote something byte-aligned in which case the logic above
Expand All @@ -487,7 +471,7 @@ impl PrimitivePageDecoder for BitpackedPageDecoder {
to_next_byte = 0;
}
let next_dst_idx =
dst_idx + (byte_len - partial_bytes_written + to_next_byte) as usize;
dst_idx + byte_len as usize - partial_bytes_written + to_next_byte;

// pad remaining bytes with 1 for negative signed numbers
if self.signed && is_negative {
Expand Down
10 changes: 10 additions & 0 deletions rust/lance-encoding/src/encodings/physical/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,16 @@ pub(crate) mod tests {
),
),
),
// check byte aligned where the num bits doesn't divide evenly into the byte length
(
DataType::UInt64,
Box::new(
DistributionArrayGeneratorProvider::<UInt64Type, Uniform<u64>>::new(
// this range should always give 24 hits
Uniform::new(200 << 16, 250 << 16),
),
),
),
// check that we can still encode an all-0 array
(
DataType::UInt32,
Expand Down
Loading