Skip to content

Commit

Permalink
Adding is_null datatype shortcut method (apache#5157)
Browse files Browse the repository at this point in the history
  • Loading branch information
comphead authored Dec 5, 2023
1 parent 5788c69 commit 9efaf06
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions arrow-schema/src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,23 +350,27 @@ impl DataType {
}

/// Returns true if this type is floating: (Float*).
#[inline]
pub fn is_floating(&self) -> bool {
use DataType::*;
matches!(self, Float16 | Float32 | Float64)
}

/// Returns true if this type is integer: (Int*, UInt*).
#[inline]
pub fn is_integer(&self) -> bool {
self.is_signed_integer() || self.is_unsigned_integer()
}

/// Returns true if this type is signed integer: (Int*).
#[inline]
pub fn is_signed_integer(&self) -> bool {
use DataType::*;
matches!(self, Int8 | Int16 | Int32 | Int64)
}

/// Returns true if this type is unsigned integer: (UInt*).
#[inline]
pub fn is_unsigned_integer(&self) -> bool {
use DataType::*;
matches!(self, UInt8 | UInt16 | UInt32 | UInt64)
Expand All @@ -387,6 +391,7 @@ impl DataType {

/// Returns true if this type is nested (List, FixedSizeList, LargeList, Struct, Union,
/// or Map), or a dictionary of a nested type
#[inline]
pub fn is_nested(&self) -> bool {
use DataType::*;
match self {
Expand All @@ -398,6 +403,13 @@ impl DataType {
}
}

/// Returns true if this type is DataType::Null.
#[inline]
pub fn is_null(&self) -> bool {
use DataType::*;
matches!(self, Null)
}

/// Compares the datatype with another, ignoring nested field names
/// and metadata.
pub fn equals_datatype(&self, other: &DataType) -> bool {
Expand Down Expand Up @@ -855,6 +867,12 @@ mod tests {
assert!(!DataType::is_floating(&DataType::Int32));
}

#[test]
fn test_datatype_is_null() {
assert!(DataType::is_null(&DataType::Null));
assert!(!DataType::is_null(&DataType::Int32));
}

#[test]
fn size_should_not_regress() {
assert_eq!(std::mem::size_of::<DataType>(), 24);
Expand Down

0 comments on commit 9efaf06

Please sign in to comment.