Skip to content

Commit

Permalink
feat: support datatype struct reorder
Browse files Browse the repository at this point in the history
Signed-off-by: fan <[email protected]>
  • Loading branch information
fansehep committed Oct 20, 2023
1 parent 7e134f4 commit d65f734
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
39 changes: 39 additions & 0 deletions arrow-schema/src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,4 +875,43 @@ mod tests {
UnionMode::Dense,
);
}

#[test]
fn test_struct_reorder() {
let mut datafields = DataType::Struct(Fields::from(vec![
Field::new("f1", DataType::Int32, false),
Field::new("f2", DataType::Utf8, false),
]));
let reverse_datafields = DataType::Struct(Fields::from(vec![
Field::new("f2", DataType::Utf8, false),
Field::new("f1", DataType::Int32, false),
]));
match datafields {
DataType::Struct(ref mut fields) => {
fields.reverse();
}
_ => {}
};
assert_eq!(datafields, reverse_datafields);
}

#[test]
fn test_struct_push() {
let mut datafields = DataType::Struct(Fields::from(vec![
Field::new("f1", DataType::Int32, false),
Field::new("f2", DataType::Utf8, false),
]));
match datafields {
DataType::Struct(ref mut fields) => {
fields.push(Field::new("f3", DataType::Boolean, false));
}
_ => {}
};
let expected_datafields = DataType::Struct(Fields::from(vec![
Field::new("f1", DataType::Int32, false),
Field::new("f2", DataType::Utf8, false),
Field::new("f3", DataType::Boolean, false),
]));
assert_eq!(datafields, expected_datafields);
}
}
24 changes: 19 additions & 5 deletions arrow-schema/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use std::sync::Arc;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Fields(Arc<[FieldRef]>);
pub struct Fields(Arc<Vec<FieldRef>>);

impl std::fmt::Debug for Fields {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -50,9 +50,13 @@ impl std::fmt::Debug for Fields {
}

impl Fields {
pub fn new(fields: Arc<Vec<FieldRef>>) -> Self {
Self(fields)
}

/// Returns a new empty [`Fields`]
pub fn empty() -> Self {
Self(Arc::new([]))
Self(Arc::new(vec![]))
}

/// Return size of this instance in bytes.
Expand Down Expand Up @@ -83,6 +87,16 @@ impl Fields {
.zip(other.iter())
.all(|(a, b)| Arc::ptr_eq(a, b) || a.contains(b))
}

pub fn reverse(&mut self) {
let new_fields: Vec<FieldRef> = self.iter().rev().map(|f| f.clone() as FieldRef).collect();
self.0 = Arc::new(new_fields);
}

pub fn push(&mut self, field: Field) {
let fields = Arc::make_mut(&mut self.0);
fields.push(Arc::new(field));
}
}

impl Default for Fields {
Expand All @@ -99,7 +113,7 @@ impl FromIterator<Field> for Fields {

impl FromIterator<FieldRef> for Fields {
fn from_iter<T: IntoIterator<Item = FieldRef>>(iter: T) -> Self {
Self(iter.into_iter().collect())
Self(Arc::new(iter.into_iter().map(|f| f as FieldRef).collect()))
}
}

Expand All @@ -117,13 +131,13 @@ impl From<Vec<FieldRef>> for Fields {

impl From<&[FieldRef]> for Fields {
fn from(value: &[FieldRef]) -> Self {
Self(value.into())
Self(Arc::new(value.to_vec()))
}
}

impl<const N: usize> From<[FieldRef; N]> for Fields {
fn from(value: [FieldRef; N]) -> Self {
Self(Arc::new(value))
Self(Arc::new(value.to_vec()))
}
}

Expand Down

0 comments on commit d65f734

Please sign in to comment.