From 468adb357c7323f727622a650639dfaf0821c275 Mon Sep 17 00:00:00 2001 From: fan Date: Tue, 24 Oct 2023 21:53:07 +0800 Subject: [PATCH 1/2] feat: support schema change by idx and reverse Signed-off-by: fan --- arrow-schema/src/schema.rs | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/arrow-schema/src/schema.rs b/arrow-schema/src/schema.rs index 43bbffd06523..dbf343a7baf4 100644 --- a/arrow-schema/src/schema.rs +++ b/arrow-schema/src/schema.rs @@ -57,6 +57,17 @@ impl SchemaBuilder { self.fields.remove(idx) } + /// Change the FieldRef as index `idx` + /// if index out of bounds, will panic + pub fn change_field(&mut self, idx: usize, field: impl Into) { + self.fields[idx] = field.into() + } + + /// Reverse the fileds + pub fn reverse(&mut self) { + self.fields.reverse(); + } + /// Appends a [`FieldRef`] to this [`SchemaBuilder`] checking for collision /// /// If an existing field exists with the same name, calls [`Field::try_merge`] @@ -837,4 +848,34 @@ mod tests { "Could not find expected string '{expected}' in '{res}'" ); } + + #[test] + fn test_schemabuilder_change_field() { + let mut builder = SchemaBuilder::new(); + builder.push(Field::new("a", DataType::Int32, false)); + builder.push(Field::new("b", DataType::Utf8, false)); + builder.change_field(1, Field::new("c", DataType::Int32, false)); + assert_eq!( + builder.fields, + vec![ + Arc::new(Field::new("a", DataType::Int32, false)), + Arc::new(Field::new("c", DataType::Int32, false)) + ] + ); + } + + #[test] + fn test_schemabuilder_reverse() { + let mut builder = SchemaBuilder::new(); + builder.push(Field::new("a", DataType::Int32, false)); + builder.push(Field::new("b", DataType::Utf8, true)); + builder.reverse(); + assert_eq!( + builder.fields, + vec![ + Arc::new(Field::new("b", DataType::Utf8, true)), + Arc::new(Field::new("a", DataType::Int32, false)) + ] + ); + } } From a176b41a8c60f96517e6e91a826ae92689f03100 Mon Sep 17 00:00:00 2001 From: fan Date: Wed, 25 Oct 2023 12:32:15 +0800 Subject: [PATCH 2/2] follow reviews Signed-off-by: fan --- arrow-schema/src/schema.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arrow-schema/src/schema.rs b/arrow-schema/src/schema.rs index dbf343a7baf4..b05cfbe3d950 100644 --- a/arrow-schema/src/schema.rs +++ b/arrow-schema/src/schema.rs @@ -57,10 +57,10 @@ impl SchemaBuilder { self.fields.remove(idx) } - /// Change the FieldRef as index `idx` + /// Get mut FieldRef as index `idx` /// if index out of bounds, will panic - pub fn change_field(&mut self, idx: usize, field: impl Into) { - self.fields[idx] = field.into() + pub fn field_mut(&mut self, idx: usize) -> &mut FieldRef { + &mut self.fields[idx] } /// Reverse the fileds @@ -854,7 +854,7 @@ mod tests { let mut builder = SchemaBuilder::new(); builder.push(Field::new("a", DataType::Int32, false)); builder.push(Field::new("b", DataType::Utf8, false)); - builder.change_field(1, Field::new("c", DataType::Int32, false)); + *builder.field_mut(1) = Arc::new(Field::new("c", DataType::Int32, false)); assert_eq!( builder.fields, vec![