You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "friendlymatthew (via GitHub)" <gi...@apache.org> on 2025/07/11 15:44:02 UTC
[PR] [Variant] Avoid superflous checks [arrow-rs]
friendlymatthew opened a new pull request, #7906:
URL: https://github.com/apache/arrow-rs/pull/7906
# Which issue does this PR close?
- Closes https://github.com/apache/arrow-rs/issues/7900
# Rational for this change
We can avoid certain checks in the validation code since other checks already guarantee these invariants for us
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201810926
##########
parquet-variant/src/variant/object.rs:
##########
@@ -242,6 +242,8 @@ impl<'m, 'v> VariantObject<'m, 'v> {
} else {
// The metadata dictionary can't guarantee uniqueness or sortedness, so we have to parse out the corresponding field names
// to check lexicographical order
+ //
+ // Since we are probing the metadata dictionary by field id, this also verifies field ids are in-bounds
let are_field_names_sorted = field_ids
.iter()
Review Comment:
> We only make a single pass now, so we no longer need to collect field ids into a vec. The only non-trivial tweak is to request the last field id specifically for the field id bounds check -- O(1) cost, so no need to materialize a whole just vec for that.
Hm, I'm a bit confused why we'd need to request the last field id? The bounds check occurs when mapping through field ids and requesting the field name via the metadata dictionary by id.
Since `metadata.get(i)` returns a `Result<&str>`, I'll need to do a try_fold here. Might make it a bit harder to reason through, but I'll push something up
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201823132
##########
parquet-variant/src/variant/metadata.rs:
##########
@@ -237,22 +237,15 @@ impl<'m> VariantMetadata<'m> {
let offsets =
map_bytes_to_offsets(offset_bytes, self.header.offset_size).collect::<Vec<_>>();
Review Comment:
We are tracking https://github.com/apache/arrow-rs/issues/7901
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201728155
##########
parquet-variant/src/variant/metadata.rs:
##########
@@ -268,6 +261,16 @@ impl<'m> VariantMetadata<'m> {
"dictionary values are not unique and ordered".to_string(),
));
}
+ } else {
+ // Validate offsets are in-bounds and monotonically increasing.
+ // Since shallow validation ensures the first and last offsets are in bounds, we can also verify all offsets
+ // are in-bounds by checking if offsets are monotonically increasing.
+ let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
+ if !are_offsets_monotonic {
Review Comment:
not sure the extra `let` is helpful?
```suggestion
if !offsets.is_sorted_by(|a, b| a < b) {
```
##########
parquet-variant/src/variant/metadata.rs:
##########
@@ -237,22 +237,15 @@ impl<'m> VariantMetadata<'m> {
let offsets =
map_bytes_to_offsets(offset_bytes, self.header.offset_size).collect::<Vec<_>>();
Review Comment:
Are we still tracking a TODO to eliminate this materialization?
Once the comment below is addressed, I _think_ it's the only one left.
##########
parquet-variant/src/variant/object.rs:
##########
@@ -242,6 +242,8 @@ impl<'m, 'v> VariantObject<'m, 'v> {
} else {
// The metadata dictionary can't guarantee uniqueness or sortedness, so we have to parse out the corresponding field names
// to check lexicographical order
+ //
+ // Since we are probing the metadata dictionary by field id, this also verifies field ids are in-bounds
let are_field_names_sorted = field_ids
.iter()
Review Comment:
We only make a single pass now, so we no longer need to collect field ids into a vec. The only non-trivial tweak is to request the last field id specifically for the field id bounds check -- O(1) cost, so no need to materialize a whole just vec for that.
While you're at it, consider replacing the `collect` + `is_sorted` pair just below with just `Iterator::is_sorted`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201810926
##########
parquet-variant/src/variant/object.rs:
##########
@@ -242,6 +242,8 @@ impl<'m, 'v> VariantObject<'m, 'v> {
} else {
// The metadata dictionary can't guarantee uniqueness or sortedness, so we have to parse out the corresponding field names
// to check lexicographical order
+ //
+ // Since we are probing the metadata dictionary by field id, this also verifies field ids are in-bounds
let are_field_names_sorted = field_ids
.iter()
Review Comment:
> We only make a single pass now, so we no longer need to collect field ids into a vec. The only non-trivial tweak is to request the last field id specifically for the field id bounds check -- O(1) cost, so no need to materialize a whole just vec for that.
Hm, I'm a bit confused why we'd need to request the last field id? The bounds check occurs when mapping through field ids and requesting the field name via the metadata dictionary by id.
Since `metadata.get(i)` returns a `Result<&str>`, I'll need to do a try_fold here. Might make it a bit harder to reason through, but I'll push something up
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#issuecomment-3063262063
cc @scovich @alamb
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#issuecomment-3065969207
Thanks again @friendlymatthew and @scovich
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201515136
##########
parquet-variant/src/variant/list.rs:
##########
@@ -216,28 +216,19 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.header.first_offset_byte() as _..self.first_value_byte as _,
)?;
- let offsets =
- map_bytes_to_offsets(offset_buffer, self.header.offset_size).collect::<Vec<_>>();
-
- // Validate offsets are in-bounds and monotonically increasing.
- // Since shallow verification checks whether the first and last offsets are in-bounds,
- // we can also verify all offsets are in-bounds by checking if offsets are monotonically increasing.
- let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
- if !are_offsets_monotonic {
- return Err(ArrowError::InvalidArgumentError(
- "offsets are not monotonically increasing".to_string(),
- ));
- }
-
let value_buffer = slice_from_slice(self.value, self.first_value_byte as _..)?;
// Validate whether values are valid variant objects
- for i in 1..offsets.len() {
- let start_offset = offsets[i - 1];
- let end_offset = offsets[i];
-
- let value_bytes = slice_from_slice(value_buffer, start_offset..end_offset)?;
+ //
+ // Since we use offsets to slice into the value buffer, we can also verify all offsets are in-bounds
+ // and monotonically increasing
Review Comment:
Yes, this was my thinking
`slice_from_slice` will err if the slice range is invalid. A slice range is invalid when the start offset is greater than the end offset.
So when we iterate through the offsets, we build our slice range `offsets[i]..offsets[i + 1]`. If every slice attempt is successful, we can guarantee offsets are non-decreasing.
We still need to check if `offsets[i] == offsets[i + 1]` for any `i`. This is still a valid range and `slice_from_slice` will return a valid slice (empty bytes).
This is when the `Variant::try_new_with_metadata` will err.
```rs
#[test]
fn foo() {
let metadata = VariantMetadata::try_new(&[]).unwrap();
// the following will panic:
// called `Result::unwrap()` on an `Err` value: InvalidArgumentError("Received empty bytes")
let v = Variant::try_new_with_metadata(metadata, &[]).unwrap();
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201528953
##########
parquet-variant/src/variant/list.rs:
##########
@@ -216,28 +216,19 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.header.first_offset_byte() as _..self.first_value_byte as _,
)?;
- let offsets =
- map_bytes_to_offsets(offset_buffer, self.header.offset_size).collect::<Vec<_>>();
-
- // Validate offsets are in-bounds and monotonically increasing.
- // Since shallow verification checks whether the first and last offsets are in-bounds,
- // we can also verify all offsets are in-bounds by checking if offsets are monotonically increasing.
- let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
- if !are_offsets_monotonic {
- return Err(ArrowError::InvalidArgumentError(
- "offsets are not monotonically increasing".to_string(),
- ));
- }
-
let value_buffer = slice_from_slice(self.value, self.first_value_byte as _..)?;
// Validate whether values are valid variant objects
- for i in 1..offsets.len() {
- let start_offset = offsets[i - 1];
- let end_offset = offsets[i];
-
- let value_bytes = slice_from_slice(value_buffer, start_offset..end_offset)?;
+ //
+ // Since we use offsets to slice into the value buffer, we can also verify all offsets are in-bounds
+ // and monotonically increasing
Review Comment:
Added the example above as a test case in a5a5e45
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201478740
##########
parquet-variant/src/variant/object.rs:
##########
@@ -242,6 +242,8 @@ impl<'m, 'v> VariantObject<'m, 'v> {
} else {
// The metadata dictionary can't guarantee uniqueness or sortedness, so we have to parse out the corresponding field names
// to check lexicographical order
+ //
+ // Since we are probing the metadata dictionary by field id, we can also verify field ids are in-bounds
Review Comment:
```suggestion
// Since we are probing the metadata dictionary by field id, this also verifies field ids are in-bounds
```
##########
parquet-variant/src/variant/list.rs:
##########
@@ -216,28 +216,19 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.header.first_offset_byte() as _..self.first_value_byte as _,
)?;
- let offsets =
- map_bytes_to_offsets(offset_buffer, self.header.offset_size).collect::<Vec<_>>();
-
- // Validate offsets are in-bounds and monotonically increasing.
- // Since shallow verification checks whether the first and last offsets are in-bounds,
- // we can also verify all offsets are in-bounds by checking if offsets are monotonically increasing.
- let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
- if !are_offsets_monotonic {
- return Err(ArrowError::InvalidArgumentError(
- "offsets are not monotonically increasing".to_string(),
- ));
- }
-
let value_buffer = slice_from_slice(self.value, self.first_value_byte as _..)?;
// Validate whether values are valid variant objects
- for i in 1..offsets.len() {
- let start_offset = offsets[i - 1];
- let end_offset = offsets[i];
-
- let value_bytes = slice_from_slice(value_buffer, start_offset..end_offset)?;
+ //
+ // Since we use offsets to slice into the value buffer, we can also verify all offsets are in-bounds
Review Comment:
```suggestion
// Since we use offsets to slice into the value buffer, this also verify all offsets are in-bounds
```
##########
parquet-variant/src/variant/metadata.rs:
##########
@@ -237,22 +237,15 @@ impl<'m> VariantMetadata<'m> {
let offsets =
map_bytes_to_offsets(offset_bytes, self.header.offset_size).collect::<Vec<_>>();
- // Validate offsets are in-bounds and monotonically increasing.
- // Since shallow validation ensures the first and last offsets are in bounds, we can also verify all offsets
- // are in-bounds by checking if offsets are monotonically increasing.
- let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
- if !are_offsets_monotonic {
- return Err(ArrowError::InvalidArgumentError(
- "offsets not monotonically increasing".to_string(),
- ));
- }
-
// Verify the string values in the dictionary are UTF-8 encoded strings.
let value_buffer =
string_from_slice(self.bytes, 0, self.first_value_byte as _..self.bytes.len())?;
if self.header.is_sorted {
// Validate the dictionary values are unique and lexicographically sorted
+ //
+ // Since we use the offsets to access dictionary values, we can also validate
Review Comment:
```suggestion
// Since we use the offsets to access dictionary values, this also validates
```
##########
parquet-variant/src/variant/list.rs:
##########
@@ -216,28 +216,19 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.header.first_offset_byte() as _..self.first_value_byte as _,
)?;
- let offsets =
- map_bytes_to_offsets(offset_buffer, self.header.offset_size).collect::<Vec<_>>();
-
- // Validate offsets are in-bounds and monotonically increasing.
- // Since shallow verification checks whether the first and last offsets are in-bounds,
- // we can also verify all offsets are in-bounds by checking if offsets are monotonically increasing.
- let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
- if !are_offsets_monotonic {
- return Err(ArrowError::InvalidArgumentError(
- "offsets are not monotonically increasing".to_string(),
- ));
- }
-
let value_buffer = slice_from_slice(self.value, self.first_value_byte as _..)?;
// Validate whether values are valid variant objects
- for i in 1..offsets.len() {
- let start_offset = offsets[i - 1];
- let end_offset = offsets[i];
-
- let value_bytes = slice_from_slice(value_buffer, start_offset..end_offset)?;
+ //
+ // Since we use offsets to slice into the value buffer, we can also verify all offsets are in-bounds
+ // and monotonically increasing
Review Comment:
I don't quite follow how this check ensures the offsets are monotonically increasing
Is it because slice_from_slice requires the bounds to be increasing?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201846629
##########
parquet-variant/src/variant/object.rs:
##########
@@ -242,6 +242,8 @@ impl<'m, 'v> VariantObject<'m, 'v> {
} else {
// The metadata dictionary can't guarantee uniqueness or sortedness, so we have to parse out the corresponding field names
// to check lexicographical order
+ //
+ // Since we are probing the metadata dictionary by field id, this also verifies field ids are in-bounds
let are_field_names_sorted = field_ids
.iter()
Review Comment:
Since the `map` returns a `Result<T>`, I needed to manually loop through the iterator
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201515136
##########
parquet-variant/src/variant/list.rs:
##########
@@ -216,28 +216,19 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.header.first_offset_byte() as _..self.first_value_byte as _,
)?;
- let offsets =
- map_bytes_to_offsets(offset_buffer, self.header.offset_size).collect::<Vec<_>>();
-
- // Validate offsets are in-bounds and monotonically increasing.
- // Since shallow verification checks whether the first and last offsets are in-bounds,
- // we can also verify all offsets are in-bounds by checking if offsets are monotonically increasing.
- let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
- if !are_offsets_monotonic {
- return Err(ArrowError::InvalidArgumentError(
- "offsets are not monotonically increasing".to_string(),
- ));
- }
-
let value_buffer = slice_from_slice(self.value, self.first_value_byte as _..)?;
// Validate whether values are valid variant objects
- for i in 1..offsets.len() {
- let start_offset = offsets[i - 1];
- let end_offset = offsets[i];
-
- let value_bytes = slice_from_slice(value_buffer, start_offset..end_offset)?;
+ //
+ // Since we use offsets to slice into the value buffer, we can also verify all offsets are in-bounds
+ // and monotonically increasing
Review Comment:
Yes, this was my thinking
`slice_from_slice` will err if the slice range is invalid. A slice range is invalid when the start offset is greater than the end offset.
So when we iterate through the offsets, we build our slice range `offsets[i]..offsets[i + 1]`. If every slice attempt is successful, we can guarantee offsets are non-decreasing.
We still need to check if `offsets[i] != offsets[i + 1]` for any `i`. This is still a valid range and `slice_from_slice` will return a valid slice (empty bytes).
This is when the `Variant::try_new_with_metadata` will err.
```rs
#[test]
fn foo() {
let metadata = VariantMetadata::try_new(&[]).unwrap();
// the following will panic:
// called `Result::unwrap()` on an `Err` value: InvalidArgumentError("Received empty bytes")
let v = Variant::try_new_with_metadata(metadata, &[]).unwrap();
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201515136
##########
parquet-variant/src/variant/list.rs:
##########
@@ -216,28 +216,19 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.header.first_offset_byte() as _..self.first_value_byte as _,
)?;
- let offsets =
- map_bytes_to_offsets(offset_buffer, self.header.offset_size).collect::<Vec<_>>();
-
- // Validate offsets are in-bounds and monotonically increasing.
- // Since shallow verification checks whether the first and last offsets are in-bounds,
- // we can also verify all offsets are in-bounds by checking if offsets are monotonically increasing.
- let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
- if !are_offsets_monotonic {
- return Err(ArrowError::InvalidArgumentError(
- "offsets are not monotonically increasing".to_string(),
- ));
- }
-
let value_buffer = slice_from_slice(self.value, self.first_value_byte as _..)?;
// Validate whether values are valid variant objects
- for i in 1..offsets.len() {
- let start_offset = offsets[i - 1];
- let end_offset = offsets[i];
-
- let value_bytes = slice_from_slice(value_buffer, start_offset..end_offset)?;
+ //
+ // Since we use offsets to slice into the value buffer, we can also verify all offsets are in-bounds
+ // and monotonically increasing
Review Comment:
Yes, this was my thinking
`slice_from_slice` will err if the slice range is invalid. A slice range is invalid when the start offset is greater than the end offset.
So when we iterate through the offsets, we build our slice range `offsets[i]..offsets[i + 1]`. If every slice attempt is successful, we can guarantee offsets are non-decreasing.
We still need to check if `offsets[i] == offsets[i + 1]` for any `i`. This is still a valid range and `slice_from_slice` will return a valid slice (empty bytes).
This is when the `Variant::try_new_with_metadata` will err.
```rs
#[test]
fn foo() {
let metadata = VariantMetadata::try_new(&[1, 0, 0]).unwrap();
// the following will panic:
// called `Result::unwrap()` on an `Err` value: InvalidArgumentError("Received empty bytes")
let v = Variant::try_new_with_metadata(metadata, &[]).unwrap();
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201964427
##########
parquet-variant/src/variant/object.rs:
##########
@@ -242,6 +242,8 @@ impl<'m, 'v> VariantObject<'m, 'v> {
} else {
// The metadata dictionary can't guarantee uniqueness or sortedness, so we have to parse out the corresponding field names
// to check lexicographical order
+ //
+ // Since we are probing the metadata dictionary by field id, this also verifies field ids are in-bounds
let are_field_names_sorted = field_ids
.iter()
Review Comment:
Hi, after we create an iterator of offsets, we immediately split into two different validation paths. I'm a bit unsure how to best handle this while avoiding the allocation.
I opened #7901 to track areas where we can avoid materialization.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201726616
##########
parquet-variant/src/variant/metadata.rs:
##########
@@ -268,6 +261,16 @@ impl<'m> VariantMetadata<'m> {
"dictionary values are not unique and ordered".to_string(),
));
}
+ } else {
+ // Validate offsets are in-bounds and monotonically increasing.
+ // Since shallow validation ensures the first and last offsets are in bounds, we can also verify all offsets
Review Comment:
nit: strangely long line?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201515136
##########
parquet-variant/src/variant/list.rs:
##########
@@ -216,28 +216,19 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.header.first_offset_byte() as _..self.first_value_byte as _,
)?;
- let offsets =
- map_bytes_to_offsets(offset_buffer, self.header.offset_size).collect::<Vec<_>>();
-
- // Validate offsets are in-bounds and monotonically increasing.
- // Since shallow verification checks whether the first and last offsets are in-bounds,
- // we can also verify all offsets are in-bounds by checking if offsets are monotonically increasing.
- let are_offsets_monotonic = offsets.is_sorted_by(|a, b| a < b);
- if !are_offsets_monotonic {
- return Err(ArrowError::InvalidArgumentError(
- "offsets are not monotonically increasing".to_string(),
- ));
- }
-
let value_buffer = slice_from_slice(self.value, self.first_value_byte as _..)?;
// Validate whether values are valid variant objects
- for i in 1..offsets.len() {
- let start_offset = offsets[i - 1];
- let end_offset = offsets[i];
-
- let value_bytes = slice_from_slice(value_buffer, start_offset..end_offset)?;
+ //
+ // Since we use offsets to slice into the value buffer, we can also verify all offsets are in-bounds
+ // and monotonically increasing
Review Comment:
Yes, here was how I thought about it.
`slice_from_slice` will err if the slice range is invalid. A slice range is invalid when the start offset is greater than the end offset.
So when we iterate through the offsets, we build our slice range `offsets[i]..offsets[i + 1]`. If every slice attempt is successful, we can guarantee offsets are non-decreasing.
We still need to check if `offsets[i] != offsets[i + 1]` for any `i`. This is still a valid range and `slice_from_slice` will return a valid slice (empty bytes).
This is when the `Variant::try_new_with_metadata` will err.
```rs
#[test]
fn foo() {
let metadata = VariantMetadata::try_new(&[]).unwrap();
// the following will panic:
// called `Result::unwrap()` on an `Err` value: InvalidArgumentError("Received empty bytes")
let v = Variant::try_new_with_metadata(metadata, &[]).unwrap();
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Variant] Avoid superflous validation checks [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7906:
URL: https://github.com/apache/arrow-rs/pull/7906#discussion_r2201846629
##########
parquet-variant/src/variant/object.rs:
##########
@@ -242,6 +242,8 @@ impl<'m, 'v> VariantObject<'m, 'v> {
} else {
// The metadata dictionary can't guarantee uniqueness or sortedness, so we have to parse out the corresponding field names
// to check lexicographical order
+ //
+ // Since we are probing the metadata dictionary by field id, this also verifies field ids are in-bounds
let are_field_names_sorted = field_ids
.iter()
Review Comment:
Since the `map` returns a `Result<T>`, I needed to manually loop through the iterator
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org