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: {Field,DataType}::size #3149

Merged
merged 1 commit into from Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions arrow-schema/src/datatype.rs
Expand Up @@ -372,6 +372,56 @@ impl DataType {
_ => self == other,
}
}

/// Return size of this instance in bytes.
///
/// Includes the size of `Self`.
pub fn size(&self) -> usize {
std::mem::size_of_val(self)
+ match self {
DataType::Null
| DataType::Boolean
| DataType::Int8
| DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::UInt8
| DataType::UInt16
| DataType::UInt32
| DataType::UInt64
| DataType::Float16
| DataType::Float32
| DataType::Float64
| DataType::Date32
| DataType::Date64
| DataType::Time32(_)
| DataType::Time64(_)
| DataType::Duration(_)
| DataType::Interval(_)
| DataType::Binary
| DataType::FixedSizeBinary(_)
| DataType::LargeBinary
| DataType::Utf8
| DataType::LargeUtf8
| DataType::Decimal128(_, _)
| DataType::Decimal256(_, _) => 0,
DataType::Timestamp(_, s) => {
s.as_ref().map(|s| s.capacity()).unwrap_or_default()
}
DataType::List(field)
| DataType::FixedSizeList(field, _)
| DataType::LargeList(field)
| DataType::Map(field, _) => field.size(),
DataType::Struct(fields) | DataType::Union(fields, _, _) => {
fields
.iter()
.map(|field| field.size() - std::mem::size_of_val(field))
.sum::<usize>()
+ (std::mem::size_of::<Field>() * fields.capacity())
}
DataType::Dictionary(dt1, dt2) => dt1.size() + dt2.size(),
}
}
}

#[cfg(test)]
Expand Down
15 changes: 15 additions & 0 deletions arrow-schema/src/field.rs
Expand Up @@ -459,6 +459,21 @@ impl Field {
}
}
}

/// Return size of this instance in bytes.
///
/// Includes the size of `Self`.
pub fn size(&self) -> usize {
std::mem::size_of_val(self) - std::mem::size_of_val(&self.data_type)
+ self.data_type.size()
+ self.name.capacity()
+ (std::mem::size_of::<(String, String)>() * self.metadata.capacity())
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure how accurate this approximation is, but I imagine probably good enough to a first order

+ self
.metadata
.iter()
.map(|(k, v)| k.capacity() + v.capacity())
.sum::<usize>()
}
}

// TODO: improve display with crate https://crates.io/crates/derive_more ?
Expand Down