-
Notifications
You must be signed in to change notification settings - Fork 1k
[Variant] Introduce new type over &str for ShortString #7718
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
use std::ops::Deref; | ||
|
||
// 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 | ||
|
@@ -29,6 +31,65 @@ mod list; | |
mod metadata; | ||
mod object; | ||
|
||
const MAX_SHORT_STRING_BYTES: usize = 0x3F; | ||
|
||
/// A Variant [`ShortString`] | ||
/// | ||
/// This implementation is a zero cost wrapper over `&str` that ensures | ||
/// the length of the underlying string is a valid Variant short string (63 bytes or less) | ||
#[derive(Debug, Clone, Copy, PartialEq)] | ||
pub struct ShortString<'a>(pub(crate) &'a str); | ||
|
||
impl<'a> ShortString<'a> { | ||
alamb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// Attempts to interpret `value` as a variant short string value. | ||
/// | ||
/// # Validation | ||
/// | ||
/// This constructor verifies that `value` is shorter than or equal to `MAX_SHORT_STRING_BYTES` | ||
pub fn try_new(value: &'a str) -> Result<Self, ArrowError> { | ||
if value.len() > MAX_SHORT_STRING_BYTES { | ||
return Err(ArrowError::InvalidArgumentError(format!( | ||
"value is larger than {MAX_SHORT_STRING_BYTES} bytes" | ||
))); | ||
} | ||
|
||
Ok(Self(value)) | ||
} | ||
|
||
/// Returns the underlying Variant short string as a &str | ||
pub fn as_str(&self) -> &'a str { | ||
self.0 | ||
} | ||
} | ||
|
||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
impl<'a> From<ShortString<'a>> for &'a str { | ||
fn from(value: ShortString<'a>) -> Self { | ||
Comment on lines
+65
to
+66
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. This seems a bit... unorthodox? Would 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. That makes a lot of sense |
||
value.0 | ||
} | ||
} | ||
|
||
impl<'a> TryFrom<&'a str> for ShortString<'a> { | ||
type Error = ArrowError; | ||
|
||
fn try_from(value: &'a str) -> Result<Self, Self::Error> { | ||
Self::try_new(value) | ||
} | ||
} | ||
|
||
impl<'a> AsRef<str> for ShortString<'a> { | ||
fn as_ref(&self) -> &str { | ||
self.0 | ||
} | ||
} | ||
|
||
impl<'a> Deref for ShortString<'a> { | ||
type Target = str; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.0 | ||
} | ||
} | ||
|
||
/// Represents a [Parquet Variant] | ||
/// | ||
/// The lifetimes `'m` and `'v` are for metadata and value buffers, respectively. | ||
|
@@ -85,7 +146,7 @@ mod object; | |
/// | ||
/// ## Creating `Variant` from Rust Types | ||
/// ``` | ||
/// # use parquet_variant::Variant; | ||
/// use parquet_variant::Variant; | ||
/// // variants can be directly constructed | ||
/// let variant = Variant::Int32(123); | ||
/// // or constructed via `From` impls | ||
|
@@ -98,7 +159,7 @@ mod object; | |
/// let value = [0x09, 0x48, 0x49]; | ||
/// // parse the header metadata | ||
/// assert_eq!( | ||
/// Variant::ShortString("HI"), | ||
/// Variant::from("HI"), | ||
/// Variant::try_new(&metadata, &value).unwrap() | ||
/// ); | ||
/// ``` | ||
|
@@ -152,7 +213,7 @@ pub enum Variant<'m, 'v> { | |
/// Primitive (type_id=1): STRING | ||
String(&'v str), | ||
/// Short String (type_id=2): STRING | ||
ShortString(&'v str), | ||
ShortString(ShortString<'v>), | ||
// need both metadata & value | ||
/// Object (type_id=3): N/A | ||
Object(VariantObject<'m, 'v>), | ||
|
@@ -165,12 +226,12 @@ impl<'m, 'v> Variant<'m, 'v> { | |
/// | ||
/// # Example | ||
/// ``` | ||
/// # use parquet_variant::{Variant, VariantMetadata}; | ||
/// use parquet_variant::{Variant, VariantMetadata}; | ||
/// let metadata = [0x01, 0x00, 0x00]; | ||
/// let value = [0x09, 0x48, 0x49]; | ||
/// // parse the header metadata | ||
/// assert_eq!( | ||
/// Variant::ShortString("HI"), | ||
/// Variant::from("HI"), | ||
/// Variant::try_new(&metadata, &value).unwrap() | ||
/// ); | ||
/// ``` | ||
|
@@ -189,7 +250,7 @@ impl<'m, 'v> Variant<'m, 'v> { | |
/// // parse the header metadata first | ||
/// let metadata = VariantMetadata::try_new(&metadata).unwrap(); | ||
/// assert_eq!( | ||
/// Variant::ShortString("HI"), | ||
/// Variant::from("HI"), | ||
/// Variant::try_new_with_metadata(metadata, &value).unwrap() | ||
/// ); | ||
/// ``` | ||
|
@@ -432,7 +493,7 @@ impl<'m, 'v> Variant<'m, 'v> { | |
/// | ||
/// // you can extract a string from string variants | ||
/// let s = "hello!"; | ||
/// let v1 = Variant::ShortString(s); | ||
/// let v1 = Variant::from(s); | ||
/// assert_eq!(v1.as_string(), Some(s)); | ||
/// | ||
/// // but not from other variants | ||
|
@@ -441,7 +502,7 @@ impl<'m, 'v> Variant<'m, 'v> { | |
/// ``` | ||
pub fn as_string(&'v self) -> Option<&'v str> { | ||
match self { | ||
Variant::String(s) | Variant::ShortString(s) => Some(s), | ||
Variant::String(s) | Variant::ShortString(ShortString(s)) => Some(s), | ||
_ => None, | ||
} | ||
} | ||
|
@@ -861,10 +922,25 @@ impl<'v> From<&'v [u8]> for Variant<'_, 'v> { | |
|
||
impl<'v> From<&'v str> for Variant<'_, 'v> { | ||
fn from(value: &'v str) -> Self { | ||
if value.len() < 64 { | ||
Variant::ShortString(value) | ||
} else { | ||
if value.len() > MAX_SHORT_STRING_BYTES { | ||
Variant::String(value) | ||
} else { | ||
Variant::ShortString(ShortString(value)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_construct_short_string() { | ||
let short_string = ShortString::try_new("norm").expect("should fit in short string"); | ||
assert_eq!(short_string.as_str(), "norm"); | ||
|
||
let long_string = "a".repeat(MAX_SHORT_STRING_BYTES + 1); | ||
let res = ShortString::try_new(&long_string); | ||
assert!(res.is_err()); | ||
} | ||
} |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.