-
Notifications
You must be signed in to change notification settings - Fork 789
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
Add ByteView::try_new #5735
Add ByteView::try_new #5735
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow_buffer::Buffer; | ||
use arrow_buffer::{Buffer, ToByteSlice}; | ||
use arrow_schema::ArrowError; | ||
|
||
#[derive(Debug, Copy, Clone, Default)] | ||
|
@@ -32,6 +32,18 @@ pub struct ByteView { | |
} | ||
|
||
impl ByteView { | ||
/// Try to create a [`ByteView`] from the provided `v` | ||
/// | ||
/// If `v` instead contains the binary data inline, returns an `Err` containing it | ||
#[inline] | ||
pub fn try_new(v: &u128) -> Result<Self, &[u8]> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea would then be that we would remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what most confuses me about ByteView as a struct is that it doesn't represent in Rust anywhere the different layouts of the For example, if you look at the rust
This API seems to improve things (though I think if we went this way we should expand its docstring to explain the difference between the two types of byte views) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Correct, it represents the non-inlined case where you have a view, and not just a short inlined byte array. This is consistent with the terminology used in the docs - https://arrow.apache.org/docs/format/Columnar.html#variable-size-binary-view-layout And with the terminology for ListView and LargeListView, where a view represents a view into a separate buffer of data
What cases does it not encapsulate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I see -- in my mind the combination of The diference I am thinking about is two layouts shown here (described as "view structures") However, there is a single
One case is creating the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Right, but does this need to be its own type, or could it be a free function? I don't know the answer to this, yet, but I would always take no abstraction over a bad abstraction |
||
let len = *v as u32; | ||
match len <= 12 { | ||
true => Err(&v.to_byte_slice()[4..4 + len as usize]), | ||
false => Ok(Self::from(*v)), | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn as_u128(self) -> u128 { | ||
(self.length as u128) | ||
|
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.
We can see here how try_new encapsulates the logic for interpreting the u128