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

ISSUE-4374: Simd selected for BooleanColumn #4484

Merged
merged 3 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 28 additions & 8 deletions common/datavalues/src/columns/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,37 @@ impl Column for BooleanColumn {
}

fn filter(&self, filter: &BooleanColumn) -> ColumnRef {
if filter.values().null_count() == 0 {
let selected = filter.values().len() - filter.values().null_count();
if selected == self.len() {
return Arc::new(self.clone());
}
let iter = self
.values()
.iter()
.zip(filter.values().iter())
.filter(|(_, b)| *b)
.map(|(a, _)| a);
let mut bitmap = MutableBitmap::with_capacity(selected);
let mut chunks = self.values().chunks::<u64>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should follow the way of primitive::filter.

Iterator of filter.values().chunks::<u64> may cause too much u64 merges.

let mut filter_chunks = filter.values().chunks::<u64>();

chunks
.by_ref()
.zip(filter_chunks.by_ref())
.for_each(|(chunk, mut mask)| {
while mask != 0 {
let n = mask.trailing_zeros() as usize;
let value: bool = chunk & (1 << n) != 0;
bitmap.push(value);
mask = mask & (mask - 1);
}
});

let col = Self::from_iterator(iter);
let chunk = chunks.remainder();
let mut mask = filter_chunks.remainder();
while mask != 0 {
let n = mask.trailing_zeros() as usize;
let value: bool = chunk & (1 << n) != 0;
bitmap.push(value);
mask = mask & (mask - 1);
}
let col = BooleanColumn {
values: bitmap.into(),
};
Arc::new(col)
}

Expand Down
49 changes: 49 additions & 0 deletions common/datavalues/tests/it/columns/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common_arrow::arrow::bitmap::Bitmap;
use common_arrow::arrow::bitmap::MutableBitmap;
use common_datavalues::prelude::*;

#[test]
Expand Down Expand Up @@ -49,3 +51,50 @@ fn test_boolean_column() {
let slice = data_column.slice(0, N / 2);
assert!(slice.len() == N / 2);
}

#[test]
fn test_filter_column() {
const N: usize = 1000;
let data_column =
BooleanColumn::from_iterator((0..N).map(|e| if e % 2 == 0 { true } else { false }));

struct Test {
filter: BooleanColumn,
expect: Vec<bool>,
}

let tests: Vec<Test> = vec![
Test {
filter: BooleanColumn::from_iterator((0..N).map(|_| true)),
expect: (0..N)
.map(|e| if e % 2 == 0 { true } else { false })
.collect(),
},
Test {
filter: BooleanColumn::from_iterator((0..N).map(|_| false)),
expect: vec![],
},
Test {
filter: BooleanColumn::from_iterator((0..N).map(|i| i % 3 == 0)),
expect: (0..N)
.map(|e| if e % 2 == 0 { true } else { false })
.enumerate()
.filter(|(i, _)| i % 3 == 0)
.map(|(_, e)| e)
.collect(),
},
];

for test in tests {
let res = data_column.filter(&test.filter);
let iter = test.expect.into_iter();
let bitmap: Bitmap = MutableBitmap::from_iter(iter).into();
assert_eq!(
res.as_any()
.downcast_ref::<BooleanColumn>()
.unwrap()
.values(),
&bitmap
);
}
}