This is an automated email from the ASF dual-hosted git repository.
scovich 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 826b808b27 Add `StructArray::field_` APIs symmetric to
`StructArray::column_` ones (#10110)
826b808b27 is described below
commit 826b808b2792235c27a22ba917a7abe93cfbb221
Author: Konstantin Tarasov <[email protected]>
AuthorDate: Thu Jun 11 12:38:15 2026 -0400
Add `StructArray::field_` APIs symmetric to `StructArray::column_` ones
(#10110)
# Which issue does this PR close?
- Closes #10092.
# Rationale for this change
check issue
# What changes are included in this PR?
- Add two `field_` APIs symmetric to `column_` ones.
- Reuse `Fields::find` in `column_by_name` to avoid a `Vec` alloc.
- Fix doc pointing to an old Jira issue. Now points to #9205
- `MapArray::entries_fields` avoid a `Vec` alloc.
# Are these changes tested?
- Yes, unit tests
# Are there any user-facing changes?
New `StructArray` APIs.
---
arrow-array/src/array/map_array.rs | 9 ++++---
arrow-array/src/array/struct_array.rs | 44 +++++++++++++++++++++++++++++++----
2 files changed, 43 insertions(+), 10 deletions(-)
diff --git a/arrow-array/src/array/map_array.rs
b/arrow-array/src/array/map_array.rs
index 7a5fe0b468..e15318d67e 100644
--- a/arrow-array/src/array/map_array.rs
+++ b/arrow-array/src/array/map_array.rs
@@ -175,11 +175,10 @@ impl MapArray {
/// Returns a reference to the fields of the [`StructArray`] that backs
this map.
pub fn entries_fields(&self) -> (&Field, &Field) {
- let fields = self.entries.fields().iter().collect::<Vec<_>>();
- let fields = TryInto::<[&FieldRef; 2]>::try_into(fields)
- .expect("Every map has a key and value field");
-
- (fields[0].as_ref(), fields[1].as_ref())
+ (
+ self.entries.field(0).as_ref(),
+ self.entries.field(1).as_ref(),
+ )
}
/// Returns the data type of the map's keys.
diff --git a/arrow-array/src/array/struct_array.rs
b/arrow-array/src/array/struct_array.rs
index e7626b26d0..59513b9ff0 100644
--- a/arrow-array/src/array/struct_array.rs
+++ b/arrow-array/src/array/struct_array.rs
@@ -319,12 +319,25 @@ impl StructArray {
///
/// Note: A schema can currently have duplicate field names, in which case
/// the first field will always be selected.
- /// This issue will be addressed in
[ARROW-11178](https://issues.apache.org/jira/browse/ARROW-11178)
+ /// This issue will be addressed in
[#9205](https://github.com/apache/arrow-rs/issues/9205)
pub fn column_by_name(&self, column_name: &str) -> Option<&ArrayRef> {
- self.column_names()
- .iter()
- .position(|c| c == &column_name)
- .map(|pos| self.column(pos))
+ self.fields()
+ .find(column_name)
+ .map(|(pos, _)| self.column(pos))
+ }
+
+ /// Returns the [`FieldRef`] at `pos`.
+ pub fn field(&self, pos: usize) -> &FieldRef {
+ &self.fields()[pos]
+ }
+
+ /// Return the [`FieldRef`] whose name equals to `field_name`
+ ///
+ /// Note: A schema can currently have duplicate field names, in which case
+ /// the first field will always be selected.
+ /// This issue will be addressed in
[#9205](https://github.com/apache/arrow-rs/issues/9205)
+ pub fn field_by_name(&self, field_name: &str) -> Option<&FieldRef> {
+ self.fields().find(field_name).map(|(_, field)| field)
}
/// Returns a zero-copy slice of this array with the indicated offset and
length.
@@ -756,6 +769,27 @@ mod tests {
assert_eq!(struct_array["c"].as_ref(), int.as_ref());
}
+ #[test]
+ fn test_struct_array_field_access() {
+ let boolean = Arc::new(BooleanArray::from(vec![false, false, true,
true]));
+ let int = Arc::new(Int32Array::from(vec![42, 28, 19, 31]));
+
+ let b_field = Arc::new(Field::new("b", DataType::Boolean, false));
+ let c_field = Arc::new(Field::new("c", DataType::Int32, false));
+
+ let struct_array = StructArray::from(vec![
+ (b_field.clone(), boolean as ArrayRef),
+ (c_field.clone(), int as ArrayRef),
+ ]);
+
+ assert_eq!(struct_array.field(0), &b_field);
+ assert_eq!(struct_array.field(1), &c_field);
+
+ assert_eq!(struct_array.field_by_name("b"), Some(&b_field));
+ assert_eq!(struct_array.field_by_name("c"), Some(&c_field));
+ assert_eq!(struct_array.field_by_name("d"), None);
+ }
+
/// validates that the in-memory representation follows [the
spec](https://arrow.apache.org/docs/format/Columnar.html#struct-layout)
#[test]
fn test_struct_array_from_vec() {