Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Get StructArray's child array by column name #416

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/array/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};

use super::{ffi::ToFfi, new_empty_array, new_null_array, Array, FromFfi};
use crate::array::ArrayRef;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use Arc<dyn Array>? ArrayRef is only kept in the public API for compatiblity with DataFusion. For people not used to it, this adds an extra layer of indirection in reading code that is not necessary.


/// A [`StructArray`] is a nested [`Array`] with an optional validity representing
/// multiple [`Array`] with the same number of rows.
Expand Down Expand Up @@ -134,10 +135,37 @@ impl StructArray {
&self.values
}

/// Returns the field at `pos`.
pub fn value(&self, pos: usize) -> &ArrayRef {
&self.values[pos]
}

/// Return the number of fields in this struct array
pub fn num_columns(&self) -> usize {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe num_fields for consistency, since Structs have fields.

self.values.len()
}

/// Returns the fields of this [`StructArray`].
pub fn fields(&self) -> &[Field] {
Self::get_fields(&self.data_type)
}

/// Return field names in this struct array
pub fn column_names(&self) -> Vec<&str> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This operation is O(F) where F is the number of fields and requires an allocation. Generally, it is more efficient to use self.fields().iter().

self.fields().iter().map(|f| f.name.as_str()).collect()
}

/// Return child array whose field name equals to column_name
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is susceptible of errors: it returns the first field, which for structs with duplicated names, can cause hard-to-debug errors. I would prefer to leave this to whoever enforces unique names (or is ok with this semantic).

///
/// 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)
pub fn column_by_name(&self, column_name: &str) -> Option<&ArrayRef> {
self.column_names()
.iter()
.position(|c| c == &column_name)
.map(|pos| self.value(pos))
}
}

impl StructArray {
Expand Down
6 changes: 5 additions & 1 deletion tests/it/array/growable/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ fn basic() {
vec![values[0].slice(1, 2).into(), values[1].slice(1, 2).into()],
None,
);
assert_eq!(result, expected)
assert_eq!(result, expected);
assert_eq!(result.column_names(), expected.column_names());
assert_eq!(result.num_columns(), expected.num_columns());
assert_eq!(result.column_by_name("f1"), expected.column_by_name("f1"));
assert_eq!(result.column_by_name("f2"), expected.column_by_name("f2"));
}

#[test]
Expand Down