This is an automated email from the ASF dual-hosted git repository.
alamb 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 42b690c95d Access `UnionFields` elements by index (#8959)
42b690c95d is described below
commit 42b690c95d6fa837b35b7fc2348107a8d73302ed
Author: Matthew Kim <[email protected]>
AuthorDate: Thu Dec 11 14:30:33 2025 -0500
Access `UnionFields` elements by index (#8959)
# Which issue does this PR close?
- Closes https://github.com/apache/arrow-rs/issues/8958
# Rationale for this change
This PR introduces 2 ways to access elements of `UnionFields` by index.
You can now use direct indexing (`fields[idx]`) or the safe accessor
`UnionFields::get(index: usize)` to avoid potential panics
Note, since we validate `UnionFields` contains only 128 elements upon
construction, we can forgo this check upon read
- https://github.com/apache/arrow-rs/pull/8891
---
arrow-schema/src/fields.rs | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/arrow-schema/src/fields.rs b/arrow-schema/src/fields.rs
index 0d351356b9..a488f88b6c 100644
--- a/arrow-schema/src/fields.rs
+++ b/arrow-schema/src/fields.rs
@@ -329,6 +329,22 @@ impl std::fmt::Debug for UnionFields {
}
}
+/// Allows direct indexing into [`UnionFields`] to access fields by position.
+///
+/// # Panics
+///
+/// Panics if the index is out of bounds. Note that [`UnionFields`] supports
+/// a maximum of 128 fields, as type IDs are represented as `i8` values.
+///
+/// For a non-panicking alternative, use [`UnionFields::get`].
+impl std::ops::Index<usize> for UnionFields {
+ type Output = (i8, FieldRef);
+
+ fn index(&self, index: usize) -> &Self::Output {
+ &self.0[index]
+ }
+}
+
impl UnionFields {
/// Create a new [`UnionFields`] with no fields
pub fn empty() -> Self {
@@ -396,6 +412,31 @@ impl UnionFields {
self.0.iter().map(|(id, f)| (*id, f))
}
+ /// Returns a reference to the field at the given index, or `None` if out
of bounds.
+ ///
+ /// This is a safe alternative to direct indexing via `[]`.
+ ///
+ /// # Example
+ ///
+ /// ```
+ /// use arrow_schema::{DataType, Field, UnionFields};
+ ///
+ /// let fields = UnionFields::new(
+ /// vec![1, 3],
+ /// vec![
+ /// Field::new("field1", DataType::UInt8, false),
+ /// Field::new("field3", DataType::Utf8, false),
+ /// ],
+ /// );
+ ///
+ /// assert!(fields.get(0).is_some());
+ /// assert!(fields.get(1).is_some());
+ /// assert!(fields.get(2).is_none());
+ /// ```
+ pub fn get(&self, index: usize) -> Option<&(i8, FieldRef)> {
+ self.0.get(index)
+ }
+
/// Searches for a field by its type id, returning the type id and field
reference if found.
/// Returns `None` if no field with the given type id exists.
pub fn find_by_type_id(&self, type_id: i8) -> Option<(i8, &FieldRef)> {