-
Notifications
You must be signed in to change notification settings - Fork 855
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 ByteArray constructors (#3879) #4081
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
db5b485
Add ByteArray constructors (#3879)
tustvold bdb8308
Clippy
tustvold c6c9900
Make ListArray error message consistent
tustvold c5b2a16
Merge remote-tracking branch 'upstream/master' into add-byte-array-co…
tustvold 2d703d5
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
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
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,7 +19,7 @@ | |||||||||
|
||||||||||
use crate::delta::shift_months; | ||||||||||
use crate::{ArrowNativeTypeOp, OffsetSizeTrait}; | ||||||||||
use arrow_buffer::i256; | ||||||||||
use arrow_buffer::{i256, Buffer, OffsetBuffer}; | ||||||||||
use arrow_data::decimal::{validate_decimal256_precision, validate_decimal_precision}; | ||||||||||
use arrow_schema::{ | ||||||||||
ArrowError, DataType, IntervalUnit, TimeUnit, DECIMAL128_MAX_PRECISION, | ||||||||||
|
@@ -1526,10 +1526,18 @@ pub trait ByteArrayType: 'static + Send + Sync + bytes::ByteArrayTypeSealed { | |||||||||
/// Utf8Array will have native type has &str | ||||||||||
/// BinaryArray will have type as [u8] | ||||||||||
type Native: bytes::ByteArrayNativeType + AsRef<Self::Native> + AsRef<[u8]> + ?Sized; | ||||||||||
|
||||||||||
/// "Binary" or "String", for use in error messages | ||||||||||
const PREFIX: &'static str; | ||||||||||
|
||||||||||
/// Datatype of array elements | ||||||||||
const DATA_TYPE: DataType; | ||||||||||
|
||||||||||
/// Verifies that every consecutive pair of `offsets` denotes a valid slice of `values` | ||||||||||
fn validate( | ||||||||||
offsets: &OffsetBuffer<Self::Offset>, | ||||||||||
values: &Buffer, | ||||||||||
) -> Result<(), ArrowError>; | ||||||||||
} | ||||||||||
|
||||||||||
/// [`ByteArrayType`] for string arrays | ||||||||||
|
@@ -1547,6 +1555,33 @@ impl<O: OffsetSizeTrait> ByteArrayType for GenericStringType<O> { | |||||||||
} else { | ||||||||||
DataType::Utf8 | ||||||||||
}; | ||||||||||
|
||||||||||
fn validate( | ||||||||||
offsets: &OffsetBuffer<Self::Offset>, | ||||||||||
values: &Buffer, | ||||||||||
) -> Result<(), ArrowError> { | ||||||||||
// Verify that the slice as a whole is valid UTF-8 | ||||||||||
let validated = std::str::from_utf8(values).map_err(|e| { | ||||||||||
ArrowError::InvalidArgumentError(format!("Encountered non UTF-8 data: {e}")) | ||||||||||
})?; | ||||||||||
|
||||||||||
// Verify each offset is at a valid character boundary in this UTF-8 array | ||||||||||
for offset in offsets.iter() { | ||||||||||
let o = offset.as_usize(); | ||||||||||
if !validated.is_char_boundary(o) { | ||||||||||
if o < validated.len() { | ||||||||||
return Err(ArrowError::InvalidArgumentError(format!( | ||||||||||
"Split UTF-8 codepoint at offset {o}" | ||||||||||
))); | ||||||||||
} | ||||||||||
return Err(ArrowError::InvalidArgumentError(format!( | ||||||||||
"Offset of {o} exceeds length of values {}", | ||||||||||
validated.len() | ||||||||||
))); | ||||||||||
} | ||||||||||
} | ||||||||||
Ok(()) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/// An arrow utf8 array with i32 offsets | ||||||||||
|
@@ -1569,6 +1604,21 @@ impl<O: OffsetSizeTrait> ByteArrayType for GenericBinaryType<O> { | |||||||||
} else { | ||||||||||
DataType::Binary | ||||||||||
}; | ||||||||||
|
||||||||||
fn validate( | ||||||||||
offsets: &OffsetBuffer<Self::Offset>, | ||||||||||
values: &Buffer, | ||||||||||
) -> Result<(), ArrowError> { | ||||||||||
// offsets are guaranteed to be monotonically increasing and non-empty | ||||||||||
let max_offset = offsets.last().unwrap().as_usize(); | ||||||||||
if values.len() < max_offset { | ||||||||||
return Err(ArrowError::InvalidArgumentError(format!( | ||||||||||
"Maximum offset of {max_offset} is larger than values of length {}", | ||||||||||
values.len() | ||||||||||
Comment on lines
+1616
to
+1617
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.
Suggested change
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 the phrasing before is grammatically more correct |
||||||||||
))); | ||||||||||
} | ||||||||||
Ok(()) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/// An arrow binary array with i32 offsets | ||||||||||
|
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.
Don't we need to also check the length of values buffer?
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 is covered by
T::validate
, will add a comment