-
Notifications
You must be signed in to change notification settings - Fork 221
Get StructArray
's child array by column name
#416
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ use crate::{ | |
}; | ||
|
||
use super::{ffi::ToFfi, new_empty_array, new_null_array, Array, FromFfi}; | ||
use crate::array::ArrayRef; | ||
|
||
/// A [`StructArray`] is a nested [`Array`] with an optional validity representing | ||
/// multiple [`Array`] with the same number of rows. | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This operation is |
||
self.fields().iter().map(|f| f.name.as_str()).collect() | ||
} | ||
|
||
/// Return child array whose field name equals to column_name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment.
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.