From b333d969dcc51126b9763a44a87a55ca7204561a Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Fri, 20 Oct 2023 13:53:07 +0100 Subject: [PATCH] Add SchemaBuilder::remove (#4952) --- arrow-schema/src/fields.rs | 17 ++++++++++++++++- arrow-schema/src/schema.rs | 9 +++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/arrow-schema/src/fields.rs b/arrow-schema/src/fields.rs index 07e9abeee56a..368ecabbf3ef 100644 --- a/arrow-schema/src/fields.rs +++ b/arrow-schema/src/fields.rs @@ -27,7 +27,7 @@ use std::sync::Arc; /// /// ``` /// # use std::sync::Arc; -/// # use arrow_schema::{DataType, Field, Fields}; +/// # use arrow_schema::{DataType, Field, Fields, SchemaBuilder}; /// // Can be constructed from Vec /// Fields::from(vec![Field::new("a", DataType::Boolean, false)]); /// // Can be constructed from Vec @@ -38,6 +38,21 @@ use std::sync::Arc; /// std::iter::once(Arc::new(Field::new("a", DataType::Boolean, false))).collect::(); /// ``` /// +/// See [`SchemaBuilder`] for mutating or updating [`Fields`] +/// +/// ``` +/// # use arrow_schema::{DataType, Field, SchemaBuilder}; +/// let mut builder = SchemaBuilder::new(); +/// builder.push(Field::new("a", DataType::Boolean, false)); +/// builder.push(Field::new("b", DataType::Boolean, false)); +/// let fields = builder.finish().fields; +/// +/// let mut builder = SchemaBuilder::from(&fields); +/// builder.remove(0); +/// let new = builder.finish().fields; +/// ``` +/// +/// [`SchemaBuilder`]: crate::SchemaBuilder #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(transparent))] diff --git a/arrow-schema/src/schema.rs b/arrow-schema/src/schema.rs index a00e8a588757..4cba873bdaf2 100644 --- a/arrow-schema/src/schema.rs +++ b/arrow-schema/src/schema.rs @@ -48,6 +48,15 @@ impl SchemaBuilder { self.fields.push(field.into()) } + /// Removes and returns the [`FieldRef`] as index `idx` + /// + /// # Panics + /// + /// Panics if index out of bounds + pub fn remove(&mut self, idx: usize) -> FieldRef { + self.fields.remove(idx) + } + /// Appends a [`FieldRef`] to this [`SchemaBuilder`] checking for collision /// /// If an existing field exists with the same name, calls [`Field::try_merge`]