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/06/23 23:48:35 UTC
[PR] [Variant] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
friendlymatthew opened a new pull request, #7757:
URL: https://github.com/apache/arrow-rs/pull/7757
# Which issue does this PR close?
Closes https://github.com/apache/arrow-rs/issues/7756
# Rationale for this change
Renames `VariantObject::get_field_by_name` to `VariantObject::get` for simplicity.
`VariantObject::get` and `VariantList::get` are refactored to return a `Option<Variant>`. The current return types were awkward to play with.
# Are there any user-facing changes?
New API (and docs with tests)
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2167737889
##########
parquet-variant/src/variant/object.rs:
##########
@@ -245,22 +255,35 @@ mod tests {
assert!(!variant_obj.is_empty());
// Test field access
- let active_field = variant_obj.field_by_name("active").unwrap();
+ let active_field = variant_obj.get("active");
assert!(active_field.is_some());
assert_eq!(active_field.unwrap().as_boolean(), Some(true));
- let age_field = variant_obj.field_by_name("age").unwrap();
+ let age_field = variant_obj.get("age");
assert!(age_field.is_some());
assert_eq!(age_field.unwrap().as_int8(), Some(42));
- let name_field = variant_obj.field_by_name("name").unwrap();
+ let name_field = variant_obj.get("name");
assert!(name_field.is_some());
assert_eq!(name_field.unwrap().as_string(), Some("hello"));
// Test non-existent field
- let missing_field = variant_obj.field_by_name("missing").unwrap();
+ let missing_field = variant_obj.get("missing");
assert!(missing_field.is_none());
+ // Fixme: This assertion will panic! That is not good
+ // let missing_field_name = variant_obj.field_name(3);
+ // assert!(missing_field_name.is_none());
Review Comment:
Here is the issue for this bug: https://github.com/apache/arrow-rs/issues/7784
This behavior doesn't occur when using a `VariantBuilder` (since the underlying `metadata` is slightly different), but it's still important to figure out why this test case is failing.
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#issuecomment-3006605871
Hi @alamb and @scovich,
This PR should be ready for review.
Some high level details about this PR:
- I commented out the failing tests and filed an issue https://github.com/apache/arrow-rs/issues/7784
- I added some comments to our getter methods about the panics. Since these panics are basically unreachable, I want to rethink validation: https://github.com/apache/arrow-rs/issues/7711
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2163472338
##########
parquet-variant/src/variant/object.rs:
##########
@@ -134,7 +134,12 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's value by index in `0..self.len()`
- pub fn field(&self, i: usize) -> Result<Variant<'m, 'v>, ArrowError> {
+ pub fn field(&self, i: usize) -> Option<Variant<'m, 'v>> {
+ self.field_err(i).ok()
Review Comment:
I worry that the `ok()` discards errors and will make it hard to debug
Also creating an `ArrowError` requires allocating a `String` so it is non trivial in terms of cost to create an Err and then discard it via `ok()`
What would you think about making errors (which can happen with invalid variant values) `panic`s ?
Something like
```suggestion
self.field_err(i).expect("Invalid variant value")
```
We may want to special case the check for out of bounds access to return None explicitly for performance reasons.
I think the validation strategy @scovich and I have been discussing is that the Variant would be validated during construction and then the `get` and other APIs would then assume that validation has already been done.
Eventually we envision there may be a way to disable the validation on construction for usecases where we know the variant is valid.
##########
parquet-variant/src/variant/object.rs:
##########
@@ -164,22 +174,22 @@ impl<'m, 'v> VariantObject<'m, 'v> {
fn iter_checked(
&self,
) -> impl Iterator<Item = Result<(&'m str, Variant<'m, 'v>), ArrowError>> + '_ {
- (0..self.num_elements).map(move |i| Ok((self.field_name(i)?, self.field(i)?)))
+ (0..self.num_elements).map(move |i| Ok((self.field_name_err(i)?, self.field_err(i)?)))
}
/// Returns the value of the field with the specified name, if any.
///
/// `Ok(None)` means the field does not exist; `Err` means the search encountered an error.
Review Comment:
likewise here it would be good to describe the error behavior in comments
##########
parquet-variant/src/variant/list.rs:
##########
@@ -123,7 +123,13 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.len() == 0
}
- pub fn get(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
+ /// Returns element by index in `0..self.len()`, if any
+ pub fn get(&self, index: usize) -> Option<Variant<'m, 'v>> {
+ self.get_err(index).ok()
Review Comment:
Same comment as below about documenting error behavior and not ignoring that error
However, for the case where `index` is out of bounds specifically, it might make sense to return None (but not ignore other errors)
##########
parquet-variant/src/builder.rs:
##########
@@ -112,11 +112,11 @@ fn make_room_for_header(buffer: &mut Vec<u8>, start_pos: usize, header_size: usi
/// panic!("unexpected variant type")
/// };
/// assert_eq!(
-/// variant_object.field_by_name("first_name").unwrap(),
+/// variant_object.get("first_name"),
Review Comment:
😍
##########
parquet-variant/src/variant/object.rs:
##########
@@ -245,22 +255,35 @@ mod tests {
assert!(!variant_obj.is_empty());
// Test field access
- let active_field = variant_obj.field_by_name("active").unwrap();
+ let active_field = variant_obj.get("active");
assert!(active_field.is_some());
assert_eq!(active_field.unwrap().as_boolean(), Some(true));
- let age_field = variant_obj.field_by_name("age").unwrap();
+ let age_field = variant_obj.get("age");
assert!(age_field.is_some());
assert_eq!(age_field.unwrap().as_int8(), Some(42));
- let name_field = variant_obj.field_by_name("name").unwrap();
+ let name_field = variant_obj.get("name");
assert!(name_field.is_some());
assert_eq!(name_field.unwrap().as_string(), Some("hello"));
// Test non-existent field
- let missing_field = variant_obj.field_by_name("missing").unwrap();
+ let missing_field = variant_obj.get("missing");
assert!(missing_field.is_none());
+ // Fixme: This assertion will panic! That is not good
+ // let missing_field_name = variant_obj.field_name(3);
+ // assert!(missing_field_name.is_none());
Review Comment:
I recommend we file a ticket
##########
parquet-variant/src/variant/object.rs:
##########
@@ -134,7 +134,12 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's value by index in `0..self.len()`
- pub fn field(&self, i: usize) -> Result<Variant<'m, 'v>, ArrowError> {
+ pub fn field(&self, i: usize) -> Option<Variant<'m, 'v>> {
+ self.field_err(i).ok()
+ }
+
+ /// Fallible version of `field`. Returns field value by index, capturing validation errors
+ fn field_err(&self, i: usize) -> Result<Variant<'m, 'v>, ArrowError> {
Review Comment:
What would you think about naming this `try_field` instead of `field_err` (and similarly for the other error variants?)
I think the `try_` prefix is somewhat more Rust "standard" though I do agree it is somewhat of an opinion
##########
parquet-variant/src/variant/object.rs:
##########
@@ -134,7 +134,12 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's value by index in `0..self.len()`
- pub fn field(&self, i: usize) -> Result<Variant<'m, 'v>, ArrowError> {
+ pub fn field(&self, i: usize) -> Option<Variant<'m, 'v>> {
Review Comment:
I think it would be good here to describe the error behavior in comments
Like
```rust
/// # Panics
// If the variant is invalid
```
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2162700336
##########
parquet-variant/src/variant/list.rs:
##########
@@ -123,7 +123,11 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.len() == 0
}
- pub fn get(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
+ pub fn get(&self, index: usize) -> Option<Variant<'m, 'v>> {
+ self.get_err(index).ok()
+ }
+
+ fn get_err(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
Review Comment:
I chose this path since `VariantList::get` was used in two separate code paths-- 1) to retrieve the element at that index and 2) to perform validation in `VariantList::try_new`.
Since the latter path requires an error message, I chose to private and rename the fallible `get` for internal usage
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2162702443
##########
parquet-variant/src/variant/list.rs:
##########
@@ -123,7 +123,11 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.len() == 0
}
- pub fn get(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
+ pub fn get(&self, index: usize) -> Option<Variant<'m, 'v>> {
+ self.get_err(index).ok()
+ }
+
+ fn get_err(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
Review Comment:
https://github.com/apache/arrow-rs/issues/7711 is an interesting/related issue to think about..
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2165218433
##########
parquet-variant/src/variant/object.rs:
##########
@@ -134,7 +134,19 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's value by index in `0..self.len()`
- pub fn field(&self, i: usize) -> Result<Variant<'m, 'v>, ArrowError> {
+ pub fn field(&self, i: usize) -> Option<Variant<'m, 'v>> {
+ if i >= self.num_elements {
+ return None;
+ }
+
+ match self.try_field(i) {
+ Ok(field) => Some(field),
+ Err(err) => panic!("validation error: {}", err),
+ }
Review Comment:
```suggestion
self.try_field(i).expect("validation error after construction")
```
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "scovich (via GitHub)" <gi...@apache.org>.
scovich commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2164969718
##########
parquet-variant/src/utils.rs:
##########
@@ -69,33 +69,33 @@ pub(crate) fn string_from_slice(slice: &[u8], range: Range<usize>) -> Result<&st
/// * `range` - The range to search in
/// * `target` - The target value to search for
/// * `key_extractor` - A function that extracts a comparable key from slice elements.
-/// This function can fail and return an error.
+/// This function can fail and return None.
///
/// # Returns
-/// * `Ok(Ok(index))` - Element found at the given index
-/// * `Ok(Err(index))` - Element not found, but would be inserted at the given index
-/// * `Err(e)` - Key extraction failed with error `e`
-pub(crate) fn try_binary_search_range_by<K, E, F>(
+/// * `Some(Ok(index))` - Element found at the given index
+/// * `Some(Err(index))` - Element not found, but would be inserted at the given index
+/// * `None` - Key extraction failed
+pub(crate) fn try_binary_search_range_by<K, F>(
range: Range<usize>,
target: &K,
- mut key_extractor: F,
-) -> Result<Result<usize, usize>, E>
+ key_extractor: F,
+) -> Option<Result<usize, usize>>
Review Comment:
This really needs [try_trait_v2](https://github.com/rust-lang/rust/issues/84277) feature to stabilize... then the passed in key extractor could decide the return type (`Option` vs. `Result`)
##########
parquet-variant/src/variant/object.rs:
##########
@@ -145,7 +157,19 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's name by index in `0..self.len()`
- pub fn field_name(&self, i: usize) -> Result<&'m str, ArrowError> {
+ pub fn field_name(&self, i: usize) -> Option<&'m str> {
+ if i >= self.num_elements {
+ return None;
+ }
+
+ match self.try_field_name(i) {
+ Ok(field_name) => Some(field_name),
+ Err(err) => panic!("validation error: {}", err),
Review Comment:
Given that this panic is ostensibly unreachable, would it be a good place for something like this?
```rust
let result = self.try_field_name(i);
debug_assert!(matches!(result, Ok(_)), "Unexpected validation failure: {result:?}");
result.ok()
```
##########
parquet-variant/src/variant/object.rs:
##########
@@ -145,7 +157,19 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's name by index in `0..self.len()`
- pub fn field_name(&self, i: usize) -> Result<&'m str, ArrowError> {
+ pub fn field_name(&self, i: usize) -> Option<&'m str> {
+ if i >= self.num_elements {
+ return None;
+ }
+
+ match self.try_field_name(i) {
+ Ok(field_name) => Some(field_name),
+ Err(err) => panic!("validation error: {}", err),
Review Comment:
I don't love panics... is there a reason out of bounds is None but any other problem is a panic? If anything, this is backward -- the caller could prevent an out of bounds panic by checking length before making this call. But they can't prevent a panic caused by invalid bytes in the buffer.
##########
parquet-variant/src/variant/list.rs:
##########
@@ -123,7 +123,11 @@ impl<'m, 'v> VariantList<'m, 'v> {
self.len() == 0
}
- pub fn get(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
+ pub fn get(&self, index: usize) -> Option<Variant<'m, 'v>> {
+ self.get_err(index).ok()
+ }
+
+ fn get_err(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
Review Comment:
Agree. And if we do make the constructor less thorough then we have to be more careful in our implementations of `get` et al, because they could be encountering bad bytes for the first time.
##########
parquet-variant/src/variant/object.rs:
##########
@@ -245,22 +255,35 @@ mod tests {
assert!(!variant_obj.is_empty());
// Test field access
- let active_field = variant_obj.field_by_name("active").unwrap();
+ let active_field = variant_obj.get("active");
assert!(active_field.is_some());
assert_eq!(active_field.unwrap().as_boolean(), Some(true));
- let age_field = variant_obj.field_by_name("age").unwrap();
+ let age_field = variant_obj.get("age");
assert!(age_field.is_some());
assert_eq!(age_field.unwrap().as_int8(), Some(42));
- let name_field = variant_obj.field_by_name("name").unwrap();
+ let name_field = variant_obj.get("name");
assert!(name_field.is_some());
assert_eq!(name_field.unwrap().as_string(), Some("hello"));
// Test non-existent field
- let missing_field = variant_obj.field_by_name("missing").unwrap();
+ let missing_field = variant_obj.get("missing");
assert!(missing_field.is_none());
+ // Fixme: This assertion will panic! That is not good
+ // let missing_field_name = variant_obj.field_name(3);
+ // assert!(missing_field_name.is_none());
Review Comment:
The assertion on L290 fails? Or the `field_name(3)` call on L289 panics?
Looking at the code, it seems like a missing bounds check in `field_name` which reads garbage from the wrong part of the slice, which I guess must somehow cause a panic inside the metadata dictionary lookup?
... but this PR adds the missing bounds checks, so I would expect the panic went away? If so, it would be good to triage the actual panic (in metadata dictionary code), since the bounds check here only masks the real problem.
##########
parquet-variant/src/variant/object.rs:
##########
@@ -145,7 +157,19 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's name by index in `0..self.len()`
- pub fn field_name(&self, i: usize) -> Result<&'m str, ArrowError> {
+ pub fn field_name(&self, i: usize) -> Option<&'m str> {
+ if i >= self.num_elements {
+ return None;
+ }
+
+ match self.try_field_name(i) {
+ Ok(field_name) => Some(field_name),
+ Err(err) => panic!("validation error: {}", err),
Review Comment:
Heh. And now I see outdated comments that recommended moving away from `ok()`...
##########
parquet-variant/src/variant/object.rs:
##########
@@ -145,7 +157,19 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's name by index in `0..self.len()`
- pub fn field_name(&self, i: usize) -> Result<&'m str, ArrowError> {
+ pub fn field_name(&self, i: usize) -> Option<&'m str> {
+ if i >= self.num_elements {
+ return None;
+ }
+
+ match self.try_field_name(i) {
+ Ok(field_name) => Some(field_name),
+ Err(err) => panic!("validation error: {}", err),
Review Comment:
I guess the constructor currently validates the bytes, so this should actually be unreachable code.
Still, `ok()` seems cleaner?
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2167243364
##########
parquet-variant/src/variant/object.rs:
##########
@@ -145,7 +157,19 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's name by index in `0..self.len()`
- pub fn field_name(&self, i: usize) -> Result<&'m str, ArrowError> {
+ pub fn field_name(&self, i: usize) -> Option<&'m str> {
+ if i >= self.num_elements {
+ return None;
+ }
+
+ match self.try_field_name(i) {
+ Ok(field_name) => Some(field_name),
+ Err(err) => panic!("validation error: {}", err),
Review Comment:
I used `.expect` but I agree with @scovich's comments about the err case being basically unreachable. But I imagine this code will change with https://github.com/apache/arrow-rs/issues/7711
##########
parquet-variant/src/variant/object.rs:
##########
@@ -145,7 +157,19 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's name by index in `0..self.len()`
- pub fn field_name(&self, i: usize) -> Result<&'m str, ArrowError> {
+ pub fn field_name(&self, i: usize) -> Option<&'m str> {
+ if i >= self.num_elements {
+ return None;
+ }
+
+ match self.try_field_name(i) {
+ Ok(field_name) => Some(field_name),
+ Err(err) => panic!("validation error: {}", err),
Review Comment:
I used `expect` and I agree with @scovich's comments about the err case being basically unreachable. But I imagine this code will change with https://github.com/apache/arrow-rs/issues/7711
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2162822572
##########
parquet-variant/src/variant/object.rs:
##########
@@ -245,22 +255,35 @@ mod tests {
assert!(!variant_obj.is_empty());
// Test field access
- let active_field = variant_obj.field_by_name("active").unwrap();
+ let active_field = variant_obj.get("active");
assert!(active_field.is_some());
assert_eq!(active_field.unwrap().as_boolean(), Some(true));
- let age_field = variant_obj.field_by_name("age").unwrap();
+ let age_field = variant_obj.get("age");
assert!(age_field.is_some());
assert_eq!(age_field.unwrap().as_int8(), Some(42));
- let name_field = variant_obj.field_by_name("name").unwrap();
+ let name_field = variant_obj.get("name");
assert!(name_field.is_some());
assert_eq!(name_field.unwrap().as_string(), Some("hello"));
// Test non-existent field
- let missing_field = variant_obj.field_by_name("missing").unwrap();
+ let missing_field = variant_obj.get("missing");
assert!(missing_field.is_none());
+ // Fixme: This assertion will panic! That is not good
+ // let missing_field_name = variant_obj.field_name(3);
+ // assert!(missing_field_name.is_none());
Review Comment:
A surprising bug arose! This assertion will also fail on main 👀
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "friendlymatthew (via GitHub)" <gi...@apache.org>.
friendlymatthew commented on PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#issuecomment-3000371105
I'm going to document these changes after work. Will revert to a draft for now!
--
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] Improve getter API for `VariantList` and `VariantObject` [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #7757:
URL: https://github.com/apache/arrow-rs/pull/7757#discussion_r2165217535
##########
parquet-variant/src/variant/object.rs:
##########
@@ -145,7 +157,19 @@ impl<'m, 'v> VariantObject<'m, 'v> {
}
/// Get a field's name by index in `0..self.len()`
- pub fn field_name(&self, i: usize) -> Result<&'m str, ArrowError> {
+ pub fn field_name(&self, i: usize) -> Option<&'m str> {
+ if i >= self.num_elements {
+ return None;
+ }
+
+ match self.try_field_name(i) {
+ Ok(field_name) => Some(field_name),
+ Err(err) => panic!("validation error: {}", err),
Review Comment:
maybe `expect` would be cleaner?
```rust
self.try_field_name(i)
.expect("Unexpected validation failure")
```
The `expect` will cause a panic but will include the message and the underlying error message
--
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