-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Refactor PrimitiveGroupValueBuilder to use MaybeNullBufferBuilder
#12623
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e11f89c
Refactor PrimitiveGroupValueBuilder to use BooleanBuilder
alamb 72bea9d
Merge remote-tracking branch 'apache/main' into alamb/improve_boolean…
alamb 126bb74
Merge remote-tracking branch 'apache/main' into alamb/improve_boolean…
alamb 20f1ce5
Refactor boolean buffer builder out
alamb ee7ed9d
tweaks
alamb 9f87955
tweak
alamb 5ef1038
simplify
alamb 36a2003
Add specializations for null / non null
alamb c985303
Merge remote-tracking branch 'apache/main' into alamb/improve_boolean…
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
datafusion/physical-plan/src/aggregates/group_values/null_builder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow_buffer::{BooleanBufferBuilder, NullBuffer}; | ||
|
||
/// Builder for an (optional) null mask | ||
/// | ||
/// Optimized for avoid creating the bitmask when all values are non-null | ||
#[derive(Debug)] | ||
pub(crate) enum MaybeNullBufferBuilder { | ||
/// seen `row_count` rows but no nulls yet | ||
NoNulls { row_count: usize }, | ||
/// have at least one null value | ||
/// | ||
/// Note this is an Arrow *VALIDITY* buffer (so it is false for nulls, true | ||
/// for non-nulls) | ||
Nulls(BooleanBufferBuilder), | ||
} | ||
|
||
impl MaybeNullBufferBuilder { | ||
/// Create a new builder | ||
pub fn new() -> Self { | ||
Self::NoNulls { row_count: 0 } | ||
} | ||
|
||
/// Return true if the row at index `row` is null | ||
pub fn is_null(&self, row: usize) -> bool { | ||
match self { | ||
Self::NoNulls { .. } => false, | ||
// validity mask means a unset bit is NULL | ||
Self::Nulls(builder) => !builder.get_bit(row), | ||
} | ||
} | ||
|
||
/// Set the nullness of the next row to `is_null` | ||
/// | ||
/// num_values is the current length of the rows being tracked | ||
/// | ||
/// If `value` is true, the row is null. | ||
/// If `value` is false, the row is non null | ||
pub fn append(&mut self, is_null: bool) { | ||
match self { | ||
Self::NoNulls { row_count } if is_null => { | ||
// have seen no nulls so far, this is the first null, | ||
// need to create the nulls buffer for all currently valid values | ||
// alloc 2x the need given we push a new but immediately | ||
let mut nulls = BooleanBufferBuilder::new(*row_count * 2); | ||
nulls.append_n(*row_count, true); | ||
nulls.append(false); | ||
*self = Self::Nulls(nulls); | ||
} | ||
Self::NoNulls { row_count } => { | ||
*row_count += 1; | ||
} | ||
Self::Nulls(builder) => builder.append(!is_null), | ||
} | ||
} | ||
|
||
/// return the number of heap allocated bytes used by this structure to store boolean values | ||
pub fn allocated_size(&self) -> usize { | ||
match self { | ||
Self::NoNulls { .. } => 0, | ||
// BooleanBufferBuilder builder::capacity returns capacity in bits (not bytes) | ||
Self::Nulls(builder) => builder.capacity() / 8, | ||
} | ||
} | ||
|
||
/// Return a NullBuffer representing the accumulated nulls so far | ||
pub fn build(self) -> Option<NullBuffer> { | ||
match self { | ||
Self::NoNulls { .. } => None, | ||
Self::Nulls(mut builder) => Some(NullBuffer::from(builder.finish())), | ||
} | ||
} | ||
|
||
/// Returns a NullBuffer representing the first `n` rows accumulated so far | ||
/// shifting any remaining down by `n` | ||
pub fn take_n(&mut self, n: usize) -> Option<NullBuffer> { | ||
match self { | ||
Self::NoNulls { row_count } => { | ||
*row_count -= n; | ||
None | ||
} | ||
Self::Nulls(builder) => { | ||
// Copy over the values at n..len-1 values to the start of a | ||
// new builder and leave it in self | ||
// | ||
// TODO: it would be great to use something like `set_bits` from arrow here. | ||
let mut new_builder = BooleanBufferBuilder::new(builder.len()); | ||
for i in n..builder.len() { | ||
new_builder.append(builder.get_bit(i)); | ||
} | ||
std::mem::swap(&mut new_builder, builder); | ||
|
||
// take only first n values from the original builder | ||
new_builder.truncate(n); | ||
Some(NullBuffer::from(new_builder.finish())) | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I collapsed
nullable
andhas_nulls
into anOption
and anEnum
which I think makes the intent much clearer (and harder to mess up thenullable
optimizationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My benchmark shows a slowdown compared to the main branch. I experienced a similar slowdown before due to the use of Option, and I suspect that might be the case again with this change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bencmarks seem to show a speedup, but I did not compare to just a bool. I'll make that change and rerun