Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Fixed error in slicing bitmap. (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao authored Jul 30, 2021
1 parent 3cd8cff commit 526fd1d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
7 changes: 1 addition & 6 deletions src/bitmap/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ impl Bitmap {
pub fn chunks<T: BitChunk>(&self) -> BitChunks<T> {
BitChunks::new(&self.bytes, self.offset, self.length)
}

#[inline]
pub(crate) fn bytes(&self) -> &[u8] {
&self.bytes
}
}

impl Bitmap {
Expand Down Expand Up @@ -217,9 +212,9 @@ impl Bitmap {
self.offset % 8
}

/// Returns the byte slice of this Bitmap.
#[inline]
pub(crate) fn as_slice(&self) -> &[u8] {
assert_eq!(self.offset % 8, 0); // slices only make sense when there is no offset
let start = self.offset / 8;
let len = self.length.saturating_add(7) / 8;
&self.bytes[start..start + len]
Expand Down
2 changes: 1 addition & 1 deletion src/bitmap/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl MutableBitmap {
/// Extends the [`MutableBitmap`] from a [`Bitmap`].
#[inline]
pub fn extend_from_bitmap(&mut self, bitmap: &Bitmap) {
self.extend_from_slice(bitmap.bytes(), bitmap.offset(), bitmap.len());
self.extend_from_slice(bitmap.as_slice(), bitmap.offset(), bitmap.len());
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/bitmap/utils/slice_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct SlicesIterator<'a> {
impl<'a> SlicesIterator<'a> {
pub fn new(values: &'a Bitmap) -> Self {
let offset = values.offset();
let buffer = &values.bytes()[offset / 8..];
let buffer = values.as_slice();
let mut iter = buffer.iter();

let (current_byte, state) = match iter.next() {
Expand Down Expand Up @@ -272,4 +272,16 @@ mod tests {

assert_eq!(count, total);
}

#[test]
fn sliced() {
let values = Bitmap::from_u8_slice(&[0b11111010, 0b11111011], 16);
let values = values.slice(8, 2);
let iter = SlicesIterator::new(&values);

let chunks = iter.collect::<Vec<_>>();

// the first "11" in the second byte
assert_eq!(chunks, vec![(0, 2)]);
}
}
4 changes: 2 additions & 2 deletions src/compute/aggregate/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::datatypes::{DataType, IntervalUnit};
use crate::types::days_ms;

fn validity_size(validity: &Option<Bitmap>) -> usize {
validity.as_ref().map(|b| b.bytes().len()).unwrap_or(0)
validity.as_ref().map(|b| b.as_slice().len()).unwrap_or(0)
}

macro_rules! dyn_primitive {
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn estimated_bytes_size(array: &dyn Array) -> usize {
Null => 0,
Boolean => {
let array = array.as_any().downcast_ref::<BooleanArray>().unwrap();
array.values().bytes().len() + validity_size(array.validity())
array.values().as_slice().len() + validity_size(array.validity())
}
Int8 => dyn_primitive!(array, i8),
Int16 => dyn_primitive!(array, i16),
Expand Down

0 comments on commit 526fd1d

Please sign in to comment.