Skip to content

Commit 068a7e4

Browse files
authored
Add DataType::is_decimal (#9100)
# Which issue does this PR close? - Closes #5163 # Rationale for this change I've implemented this function at least twice in other codebases, and `arrow-rs` now has 4 variants. # What changes are included in this PR? New public function to test if a `DataType` is any decimal variant. # Are these changes tested? Yes # Are there any user-facing changes? New function including docs.
1 parent b1dfb69 commit 068a7e4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

arrow-schema/src/datatype.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,16 @@ impl DataType {
591591
matches!(self, UInt8 | UInt16 | UInt32 | UInt64)
592592
}
593593

594+
/// Returns true if this type is decimal: (Decimal*).
595+
#[inline]
596+
pub fn is_decimal(&self) -> bool {
597+
use DataType::*;
598+
matches!(
599+
self,
600+
Decimal32(..) | Decimal64(..) | Decimal128(..) | Decimal256(..)
601+
)
602+
}
603+
594604
/// Returns true if this type is valid as a dictionary key
595605
#[inline]
596606
pub fn is_dictionary_key_type(&self) -> bool {
@@ -1168,6 +1178,15 @@ mod tests {
11681178
assert!(!DataType::is_floating(&DataType::Int32));
11691179
}
11701180

1181+
#[test]
1182+
fn test_decimal() {
1183+
assert!(DataType::is_decimal(&DataType::Decimal32(4, 2)));
1184+
assert!(DataType::is_decimal(&DataType::Decimal64(4, 2)));
1185+
assert!(DataType::is_decimal(&DataType::Decimal128(4, 2)));
1186+
assert!(DataType::is_decimal(&DataType::Decimal256(4, 2)));
1187+
assert!(!DataType::is_decimal(&DataType::Float16));
1188+
}
1189+
11711190
#[test]
11721191
fn test_datatype_is_null() {
11731192
assert!(DataType::is_null(&DataType::Null));

0 commit comments

Comments
 (0)