This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 9f87453338 refactor(variant): simplify the offset and UTF-8 decoding
helpers (#12308)
9f87453338 is described below
commit 9f87453338091a0cf078300f01b8eb0a84a99f10
Author: Peter L <[email protected]>
AuthorDate: Thu Nov 19 15:31:34 2026 +0000
refactor(variant): simplify the offset and UTF-8 decoding helpers (#12308)
# Which issue does this PR close?
- Part of #11511
This is the first of a stack of PRs that split #11511 into smaller
pieces, as its reviewers asked.
# Rationale for this change
#11511 adds header-only views of encoded variant bytes. The views read
offsets and field names straight from the encoded buffer, and these
helpers are in the way of that. This PR makes the helper changes on
their own, so that the later PRs only show the views.
# What changes are included in this PR?
- `OffsetSizeBytes::unpack_u32_at_offset` takes `self` by value.
`OffsetSizeBytes` is `Copy`.
- `OffsetSizeBytes::unpack_u32` is removed. It was
`unpack_u32_at_offset` with a byte offset of 0, and its callers now call
that.
- `str_from_bytes` is extracted from `string_from_slice`, which now
calls it.
All of these items are crate-private.
# Are these changes tested?
The existing tests cover the changed code. The `unpack_u32` tests now
call `unpack_u32_at_offset`.
# Are there any user-facing changes?
No.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Fable 5.2 <[email protected]>
---
parquet-variant/src/decoder.rs | 66 +++++++++++++++++++++------------
parquet-variant/src/utils.rs | 12 ++++--
parquet-variant/src/variant/list.rs | 4 +-
parquet-variant/src/variant/metadata.rs | 8 ++--
parquet-variant/src/variant/object.rs | 9 ++++-
5 files changed, 66 insertions(+), 33 deletions(-)
diff --git a/parquet-variant/src/decoder.rs b/parquet-variant/src/decoder.rs
index 7d95a8d685..e99005e5c8 100644
--- a/parquet-variant/src/decoder.rs
+++ b/parquet-variant/src/decoder.rs
@@ -137,16 +137,6 @@ impl OffsetSizeBytes {
Ok(result)
}
- /// Return one unsigned little-endian value from `bytes`.
- ///
- /// * `bytes` – the byte buffer to index
- /// * `index` – 0-based index into the buffer
- ///
- /// Each value is `self as u32` bytes wide (1, 2, 3 or 4), zero-extended
to 32 bits as needed.
- pub(crate) fn unpack_u32(&self, bytes: &[u8], index: usize) -> Result<u32,
ArrowError> {
- self.unpack_u32_at_offset(bytes, 0, index)
- }
-
/// Return one unsigned little-endian value from `bytes`.
///
/// * `bytes` – the byte buffer to index
@@ -157,7 +147,7 @@ impl OffsetSizeBytes {
///
/// Each value is `self as u32` bytes wide (1, 2, 3 or 4), zero-extended
to 32 bits as needed.
pub(crate) fn unpack_u32_at_offset(
- &self,
+ self,
bytes: &[u8],
byte_offset: usize, // how many bytes to skip
offset_index: usize, // which offset in an array of offsets
@@ -165,9 +155,9 @@ impl OffsetSizeBytes {
use OffsetSizeBytes::*;
// Index into the byte array:
- // byte_offset + (*self as usize) * offset_index
+ // byte_offset + (self as usize) * offset_index
let offset = offset_index
- .checked_mul(*self as usize)
+ .checked_mul(self as usize)
.and_then(|n| n.checked_add(byte_offset))
.ok_or_else(|| overflow_error("unpacking offset array value"))?;
let value = match self {
@@ -637,39 +627,61 @@ mod tests {
fn unpack_u32_all_widths() {
// One-byte offsets
let buf_one = [0x01u8, 0xAB, 0xCD];
- assert_eq!(OffsetSizeBytes::One.unpack_u32(&buf_one, 0).unwrap(),
0x01);
- assert_eq!(OffsetSizeBytes::One.unpack_u32(&buf_one, 2).unwrap(),
0xCD);
+ assert_eq!(
+ OffsetSizeBytes::One
+ .unpack_u32_at_offset(&buf_one, 0, 0)
+ .unwrap(),
+ 0x01
+ );
+ assert_eq!(
+ OffsetSizeBytes::One
+ .unpack_u32_at_offset(&buf_one, 0, 2)
+ .unwrap(),
+ 0xCD
+ );
// Two-byte offsets (little-endian 0x1234, 0x5678)
let buf_two = [0x34, 0x12, 0x78, 0x56];
assert_eq!(
- OffsetSizeBytes::Two.unpack_u32(&buf_two, 0).unwrap(),
+ OffsetSizeBytes::Two
+ .unpack_u32_at_offset(&buf_two, 0, 0)
+ .unwrap(),
0x1234
);
assert_eq!(
- OffsetSizeBytes::Two.unpack_u32(&buf_two, 1).unwrap(),
+ OffsetSizeBytes::Two
+ .unpack_u32_at_offset(&buf_two, 0, 1)
+ .unwrap(),
0x5678
);
// Three-byte offsets (0x030201 and 0x0000FF)
let buf_three = [0x01, 0x02, 0x03, 0xFF, 0x00, 0x00];
assert_eq!(
- OffsetSizeBytes::Three.unpack_u32(&buf_three, 0).unwrap(),
+ OffsetSizeBytes::Three
+ .unpack_u32_at_offset(&buf_three, 0, 0)
+ .unwrap(),
0x030201
);
assert_eq!(
- OffsetSizeBytes::Three.unpack_u32(&buf_three, 1).unwrap(),
+ OffsetSizeBytes::Three
+ .unpack_u32_at_offset(&buf_three, 0, 1)
+ .unwrap(),
0x0000FF
);
// Four-byte offsets (0x12345678, 0x90ABCDEF)
let buf_four = [0x78, 0x56, 0x34, 0x12, 0xEF, 0xCD, 0xAB, 0x90];
assert_eq!(
- OffsetSizeBytes::Four.unpack_u32(&buf_four, 0).unwrap(),
+ OffsetSizeBytes::Four
+ .unpack_u32_at_offset(&buf_four, 0, 0)
+ .unwrap(),
0x1234_5678
);
assert_eq!(
- OffsetSizeBytes::Four.unpack_u32(&buf_four, 1).unwrap(),
+ OffsetSizeBytes::Four
+ .unpack_u32_at_offset(&buf_four, 0, 1)
+ .unwrap(),
0x90AB_CDEF
);
}
@@ -677,8 +689,16 @@ mod tests {
#[test]
fn unpack_u32_out_of_bounds() {
let tiny = [0x00u8]; // deliberately too short
- assert!(OffsetSizeBytes::Two.unpack_u32(&tiny, 0).is_err());
- assert!(OffsetSizeBytes::Three.unpack_u32(&tiny, 0).is_err());
+ assert!(
+ OffsetSizeBytes::Two
+ .unpack_u32_at_offset(&tiny, 0, 0)
+ .is_err()
+ );
+ assert!(
+ OffsetSizeBytes::Three
+ .unpack_u32_at_offset(&tiny, 0, 0)
+ .is_err()
+ );
}
#[test]
diff --git a/parquet-variant/src/utils.rs b/parquet-variant/src/utils.rs
index d950dcfdf1..7c9ca8ef1f 100644
--- a/parquet-variant/src/utils.rs
+++ b/parquet-variant/src/utils.rs
@@ -71,21 +71,25 @@ pub(crate) fn string_from_slice(
offset: usize,
range: Range<usize>,
) -> Result<&str, ArrowError> {
- let offset_buffer = slice_from_slice_at_offset(slice, offset, range)?;
+ str_from_bytes(slice_from_slice_at_offset(slice, offset, range)?)
+}
+/// Interprets `bytes` as a UTF-8 string.
+#[inline]
+pub(crate) fn str_from_bytes(bytes: &[u8]) -> Result<&str, ArrowError> {
//Use simdutf8 by default
#[cfg(feature = "simdutf8")]
{
- simdutf8::basic::from_utf8(offset_buffer).map_err(|_| {
+ simdutf8::basic::from_utf8(bytes).map_err(|_| {
// Use simdutf8::compat to return details about the decoding error
- let e = simdutf8::compat::from_utf8(offset_buffer).unwrap_err();
+ let e = simdutf8::compat::from_utf8(bytes).unwrap_err();
ArrowError::InvalidArgumentError(format!("encountered non UTF-8
data: {e}"))
})
}
//Use std::str if simdutf8 is not enabled
#[cfg(not(feature = "simdutf8"))]
- str::from_utf8(offset_buffer)
+ str::from_utf8(bytes)
.map_err(|_| ArrowError::InvalidArgumentError("invalid UTF-8
string".to_string()))
}
diff --git a/parquet-variant/src/variant/list.rs
b/parquet-variant/src/variant/list.rs
index 4172ac1d25..b51e79c5d9 100644
--- a/parquet-variant/src/variant/list.rs
+++ b/parquet-variant/src/variant/list.rs
@@ -327,7 +327,9 @@ impl<'m, 'v> VariantList<'m, 'v> {
fn get_offset(&self, index: usize) -> Result<u32, ArrowError> {
let byte_range = self.header.first_offset_byte() as
_..self.first_value_byte as _;
let offset_bytes = slice_from_slice(self.value, byte_range)?;
- self.header.offset_size.unpack_u32(offset_bytes, index)
+ self.header
+ .offset_size
+ .unpack_u32_at_offset(offset_bytes, 0, index)
}
}
diff --git a/parquet-variant/src/variant/metadata.rs
b/parquet-variant/src/variant/metadata.rs
index b51b4ffbd9..0974318e8a 100644
--- a/parquet-variant/src/variant/metadata.rs
+++ b/parquet-variant/src/variant/metadata.rs
@@ -341,7 +341,7 @@ impl<'m> VariantMetadata<'m> {
fn get_offset(&self, i: usize) -> Result<u32, ArrowError> {
let offset_byte_range = self.header.first_offset_byte() as
_..self.first_value_byte as _;
let bytes = slice_from_slice(self.bytes, offset_byte_range)?;
- self.header.offset_size.unpack_u32(bytes, i)
+ self.header.offset_size.unpack_u32_at_offset(bytes, 0, i)
}
/// Returns the total size, in bytes, of the metadata.
@@ -441,7 +441,7 @@ impl<'m> VariantMetadata<'m> {
let cmp = |i| {
Some(
offset_size
- .unpack_u32(offsets, i)
+ .unpack_u32_at_offset(offsets, 0, i)
.ok()?
.cmp(&field_name_offset),
)
@@ -449,7 +449,9 @@ impl<'m> VariantMetadata<'m> {
let field_id = try_binary_search_range_by(0..self.len(), cmp)?.ok()?;
// Verify that this entry has exactly `field_name`'s bytes; see
"Correctness" above.
- let entry_end = offset_size.unpack_u32(offsets, field_id + 1).ok()?;
+ let entry_end = offset_size
+ .unpack_u32_at_offset(offsets, 0, field_id + 1)
+ .ok()?;
let entry_len = entry_end.checked_sub(field_name_offset)?;
(entry_len as usize == field_name.len()).then_some(field_id as u32)
}
diff --git a/parquet-variant/src/variant/object.rs
b/parquet-variant/src/variant/object.rs
index 3c21f12e60..acb568aa99 100644
--- a/parquet-variant/src/variant/object.rs
+++ b/parquet-variant/src/variant/object.rs
@@ -360,7 +360,9 @@ impl<'m, 'v> VariantObject<'m, 'v> {
fn get_offset(&self, i: usize) -> Result<u32, ArrowError> {
let byte_range = self.first_field_offset_byte as
_..self.first_value_byte as _;
let field_offsets = slice_from_slice(self.value, byte_range)?;
- self.header.field_offset_size.unpack_u32(field_offsets, i)
+ self.header
+ .field_offset_size
+ .unpack_u32_at_offset(field_offsets, 0, i)
}
/// Get a field's name by index in `0..self.len()`
@@ -379,7 +381,10 @@ impl<'m, 'v> VariantObject<'m, 'v> {
fn try_field_name(&self, i: usize) -> Result<&'m str, ArrowError> {
let byte_range = self.header.field_ids_start_byte() as
_..self.first_field_offset_byte as _;
let field_id_bytes = slice_from_slice(self.value, byte_range)?;
- let field_id = self.header.field_id_size.unpack_u32(field_id_bytes,
i)?;
+ let field_id = self
+ .header
+ .field_id_size
+ .unpack_u32_at_offset(field_id_bytes, 0, i)?;
self.metadata.get(field_id as _)
}