-
Notifications
You must be signed in to change notification settings - Fork 810
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
ArrayData Enumeration for Primitive, Binary and UTF8 #3749
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bab9d22
Add BooleanBuffer
tustvold 332931e
Add NullBuffer
tustvold b103522
Add PrimitiveArrayData
tustvold 6ae6b84
Add BytesArrayData
tustvold bd93c15
Move module
tustvold 76f55ab
Make private for now
tustvold a7cb0b0
Move NullBuffer to arrow-buffer
tustvold e144dc6
Format
tustvold 7653fd6
More docs
tustvold 68f27f5
Seal traits
tustvold c1527b5
Doc
tustvold 85cc99e
Review feedback
tustvold 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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 crate::{bit_util, Buffer}; | ||
|
||
/// A slice-able [`Buffer`] containing bit-packed booleans | ||
#[derive(Debug, Clone)] | ||
pub struct BooleanBuffer { | ||
buffer: Buffer, | ||
offset: usize, | ||
len: usize, | ||
} | ||
|
||
impl BooleanBuffer { | ||
/// Create a new [`BooleanBuffer`] from a [`Buffer`], an `offset` and `length` in bits | ||
/// | ||
/// # Panics | ||
/// | ||
/// This method will panic if `buffer` is not large enough | ||
pub fn new(buffer: Buffer, offset: usize, len: usize) -> Self { | ||
let total_len = offset.saturating_add(len); | ||
let bit_len = buffer.len().saturating_mul(8); | ||
assert!(total_len <= bit_len); | ||
Self { | ||
buffer, | ||
offset, | ||
len, | ||
} | ||
} | ||
|
||
/// Returns the number of set bits in this buffer | ||
pub fn count_set_bits(&self) -> usize { | ||
self.buffer.count_set_bits_offset(self.offset, self.len) | ||
} | ||
|
||
/// Returns `true` if the bit at index `i` is set | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `i >= self.len()` | ||
#[inline] | ||
pub fn is_set(&self, i: usize) -> bool { | ||
assert!(i < self.len); | ||
unsafe { bit_util::get_bit_raw(self.buffer.as_ptr(), i + self.offset) } | ||
} | ||
|
||
/// Returns the offset of this [`BooleanBuffer`] in bits | ||
#[inline] | ||
pub fn offset(&self) -> usize { | ||
self.offset | ||
} | ||
|
||
/// Returns the length of this [`BooleanBuffer`] in bits | ||
#[inline] | ||
pub fn len(&self) -> usize { | ||
self.len | ||
} | ||
|
||
/// Returns true if this [`BooleanBuffer`] is empty | ||
#[inline] | ||
pub fn is_empty(&self) -> bool { | ||
self.len == 0 | ||
} | ||
|
||
/// Returns the packed values of this [`BooleanBuffer`] not including any offset | ||
#[inline] | ||
pub fn values(&self) -> &[u8] { | ||
&self.buffer | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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 crate::buffer::BooleanBuffer; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NullBuffer { | ||
buffer: BooleanBuffer, | ||
null_count: usize, | ||
} | ||
|
||
impl NullBuffer { | ||
/// Create a new [`NullBuffer`] computing the null count | ||
pub fn new(buffer: BooleanBuffer) -> Self { | ||
let null_count = buffer.len() - buffer.count_set_bits(); | ||
Self { buffer, null_count } | ||
} | ||
|
||
/// Create a new [`NullBuffer`] with the provided `buffer` and `null_count` | ||
/// | ||
/// # Safety | ||
/// | ||
/// `buffer` must contain `null_count` `0` bits | ||
pub unsafe fn new_unchecked(buffer: BooleanBuffer, null_count: usize) -> Self { | ||
Self { buffer, null_count } | ||
} | ||
|
||
/// Returns the length of this [`NullBuffer`] | ||
#[inline] | ||
pub fn len(&self) -> usize { | ||
self.buffer.len() | ||
} | ||
|
||
/// Returns true if this [`NullBuffer`] is empty | ||
#[inline] | ||
pub fn is_empty(&self) -> bool { | ||
self.buffer.is_empty() | ||
} | ||
|
||
/// Returns the null count for this [`NullBuffer`] | ||
#[inline] | ||
pub fn null_count(&self) -> usize { | ||
self.null_count | ||
} | ||
|
||
/// Returns `true` if the value at `idx` is not null | ||
#[inline] | ||
pub fn is_valid(&self, idx: usize) -> bool { | ||
self.buffer.is_set(idx) | ||
} | ||
|
||
/// Returns `true` if the value at `idx` is null | ||
#[inline] | ||
pub fn is_null(&self, idx: usize) -> bool { | ||
!self.is_valid(idx) | ||
} | ||
|
||
/// Returns the inner buffer | ||
#[inline] | ||
pub fn inner(&self) -> &BooleanBuffer { | ||
&self.buffer | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[test] | ||
fn test_size() { | ||
// This tests that the niche optimisation eliminates the overhead of an option | ||
assert_eq!( | ||
std::mem::size_of::<NullBuffer>(), | ||
std::mem::size_of::<Option<NullBuffer>>() | ||
); | ||
} | ||
} |
Oops, something went wrong.
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.
This closes #1802 as discussed in #3700 this needs to be a new type to avoid breaking existing workloads