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

feat: support schema change by idx and reverse #4985

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions arrow-schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldRef>) {
self.fields[idx] = field.into()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Change the FieldRef as index `idx`
/// if index out of bounds, will panic
pub fn change_field(&mut self, idx: usize, field: impl Into<FieldRef>) {
self.fields[idx] = field.into()
/// Returns a mutable reference to the field at `idx`
pub fn field_mut(&mut self, idx: usize) -> &mut FieldRef {
&mut self.fields[idx]

What do you think about this instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's good!!! 😄

}

/// 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`]
Expand Down Expand Up @@ -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))
]
);
}
}
Loading