Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Field::remove(), Schema::remove(), and RecordBatch::remove_column() APIs #4959

Merged
merged 8 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
34 changes: 34 additions & 0 deletions arrow-array/src/record_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,40 @@ impl RecordBatch {
&self.columns[..]
}

/// Remove column by index and return it.
///
/// Return the `ArrayRef` if the column is removed.
///
/// # Panics
///
/// Panics if `index`` out of bounds.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use arrow_array::{BooleanArray, Int32Array, RecordBatch};
/// use arrow_schema::{DataType, Field, Schema};
/// let id_array = Int32Array::from(vec![1, 2, 3, 4, 5]);
/// let bool_array = BooleanArray::from(vec![true, false, false, true, true]);
/// let schema = Schema::new(vec![
/// Field::new("id", DataType::Int32, false),
/// Field::new("bool", DataType::Boolean, false),
/// ]);
///
/// let mut batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(id_array), Arc::new(bool_array)]).unwrap();
///
/// let removed_column = batch.remove_column(0);
/// assert_eq!(removed_column.as_any().downcast_ref::<Int32Array>().unwrap(), &Int32Array::from(vec![1, 2, 3, 4, 5]));
/// assert_eq!(batch.num_columns(), 1);
/// ```
pub fn remove_column(&mut self, index: usize) -> ArrayRef {
let mut builder = SchemaBuilder::from(self.schema.fields());
builder.remove(index);
self.schema = Arc::new(builder.finish());
self.columns.remove(index)
}

/// Return a new RecordBatch where each column is sliced
/// according to `offset` and `length`
///
Expand Down
23 changes: 23 additions & 0 deletions arrow-schema/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ impl Fields {
.zip(other.iter())
.all(|(a, b)| Arc::ptr_eq(a, b) || a.contains(b))
}

/// Remove a field by index and return it.
/// ```
/// use arrow_schema::{DataType, Field, Fields};
/// let mut fields = Fields::from(vec![
/// Field::new("a", DataType::Boolean, false),
/// Field::new("b", DataType::Int8, false),
/// Field::new("c", DataType::Utf8, false),
/// ]);
/// assert_eq!(fields.len(), 3);
/// assert_eq!(fields.remove(1).unwrap(), Field::new("b", DataType::Int8, false).into());
/// assert_eq!(fields.len(), 2);
/// ```
pub fn remove(&mut self, index: usize) -> Option<FieldRef> {
Folyd marked this conversation as resolved.
Show resolved Hide resolved
if index >= self.len() {
return None;
}

let mut new_fields = self.0.iter().cloned().collect::<Vec<_>>();
let field = new_fields.remove(index);
self.0 = Arc::from(new_fields);
Some(field)
}
}

impl Default for Fields {
Expand Down
22 changes: 22 additions & 0 deletions arrow-schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,28 @@ impl Schema {
.iter()
.all(|(k, v1)| self.metadata.get(k).map(|v2| v1 == v2).unwrap_or_default())
}

/// Remove field by name and return it. Recommend to use [`SchemaBuilder`]
Folyd marked this conversation as resolved.
Show resolved Hide resolved
/// if you are looking to remove multiple columns, as this will save allocations.
///
/// ```
/// use arrow_schema::{DataType, Field, Schema};
/// let mut schema = Schema::new(vec![
/// Field::new("a", DataType::Boolean, false),
/// Field::new("b", DataType::Int8, false),
/// Field::new("c", DataType::Utf8, false),
/// ]);
/// assert_eq!(schema.fields.len(), 3);
/// assert_eq!(schema.remove_field_with_name("b").unwrap(), Field::new("b", DataType::Int8, false).into());
/// assert_eq!(schema.fields.len(), 2);
/// ```
pub fn remove_field_with_name(&mut self, name: &str) -> Option<FieldRef> {
Folyd marked this conversation as resolved.
Show resolved Hide resolved
if let Some((idx, _)) = self.fields.find(name) {
self.fields.remove(idx)
} else {
None
}
}
}

impl fmt::Display for Schema {
Expand Down
Loading