Skip to content

Commit 44cd972

Browse files
AdamGSalamb
andauthored
Support Decimal32/64 types (#17501)
* Support Decimal32/64 types * Fix bugs, tests, handle more aggregate functions and schema * Fill out more parts in expr,common and expr-common * Some stragglers and overlooked corners * Actually commit the avg_distinct support --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent 7125e97 commit 44cd972

File tree

24 files changed

+1188
-138
lines changed

24 files changed

+1188
-138
lines changed

datafusion/common/src/cast.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
2323
use crate::{downcast_value, Result};
2424
use arrow::array::{
25-
BinaryViewArray, DurationMicrosecondArray, DurationMillisecondArray,
26-
DurationNanosecondArray, DurationSecondArray, Float16Array, Int16Array, Int8Array,
27-
LargeBinaryArray, LargeStringArray, StringViewArray, UInt16Array,
25+
BinaryViewArray, Decimal32Array, Decimal64Array, DurationMicrosecondArray,
26+
DurationMillisecondArray, DurationNanosecondArray, DurationSecondArray, Float16Array,
27+
Int16Array, Int8Array, LargeBinaryArray, LargeStringArray, StringViewArray,
28+
UInt16Array,
2829
};
2930
use arrow::{
3031
array::{
@@ -97,6 +98,16 @@ pub fn as_uint64_array(array: &dyn Array) -> Result<&UInt64Array> {
9798
Ok(downcast_value!(array, UInt64Array))
9899
}
99100

101+
// Downcast Array to Decimal32Array
102+
pub fn as_decimal32_array(array: &dyn Array) -> Result<&Decimal32Array> {
103+
Ok(downcast_value!(array, Decimal32Array))
104+
}
105+
106+
// Downcast Array to Decimal64Array
107+
pub fn as_decimal64_array(array: &dyn Array) -> Result<&Decimal64Array> {
108+
Ok(downcast_value!(array, Decimal64Array))
109+
}
110+
100111
// Downcast Array to Decimal128Array
101112
pub fn as_decimal128_array(array: &dyn Array) -> Result<&Decimal128Array> {
102113
Ok(downcast_value!(array, Decimal128Array))

datafusion/common/src/dfschema.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,14 @@ impl DFSchema {
798798
.zip(iter2)
799799
.all(|((t1, f1), (t2, f2))| t1 == t2 && Self::field_is_semantically_equal(f1, f2))
800800
}
801+
(
802+
DataType::Decimal32(_l_precision, _l_scale),
803+
DataType::Decimal32(_r_precision, _r_scale),
804+
) => true,
805+
(
806+
DataType::Decimal64(_l_precision, _l_scale),
807+
DataType::Decimal64(_r_precision, _r_scale),
808+
) => true,
801809
(
802810
DataType::Decimal128(_l_precision, _l_scale),
803811
DataType::Decimal128(_r_precision, _r_scale),
@@ -1056,6 +1064,12 @@ fn format_simple_data_type(data_type: &DataType) -> String {
10561064
DataType::Dictionary(_, value_type) => {
10571065
format_simple_data_type(value_type.as_ref())
10581066
}
1067+
DataType::Decimal32(precision, scale) => {
1068+
format!("decimal32({precision}, {scale})")
1069+
}
1070+
DataType::Decimal64(precision, scale) => {
1071+
format!("decimal64({precision}, {scale})")
1072+
}
10591073
DataType::Decimal128(precision, scale) => {
10601074
format!("decimal128({precision}, {scale})")
10611075
}
@@ -1794,6 +1808,27 @@ mod tests {
17941808
&DataType::Int16
17951809
));
17961810

1811+
// Succeeds if decimal precision and scale are different
1812+
assert!(DFSchema::datatype_is_semantically_equal(
1813+
&DataType::Decimal32(1, 2),
1814+
&DataType::Decimal32(2, 1),
1815+
));
1816+
1817+
assert!(DFSchema::datatype_is_semantically_equal(
1818+
&DataType::Decimal64(1, 2),
1819+
&DataType::Decimal64(2, 1),
1820+
));
1821+
1822+
assert!(DFSchema::datatype_is_semantically_equal(
1823+
&DataType::Decimal128(1, 2),
1824+
&DataType::Decimal128(2, 1),
1825+
));
1826+
1827+
assert!(DFSchema::datatype_is_semantically_equal(
1828+
&DataType::Decimal256(1, 2),
1829+
&DataType::Decimal256(2, 1),
1830+
));
1831+
17971832
// Test lists
17981833

17991834
// Succeeds if both have the same element type, disregards names and nullability
@@ -2377,6 +2412,8 @@ mod tests {
23772412
),
23782413
false,
23792414
),
2415+
Field::new("decimal32", DataType::Decimal32(9, 4), true),
2416+
Field::new("decimal64", DataType::Decimal64(9, 4), true),
23802417
Field::new("decimal128", DataType::Decimal128(18, 4), true),
23812418
Field::new("decimal256", DataType::Decimal256(38, 10), false),
23822419
Field::new("date32", DataType::Date32, true),
@@ -2408,6 +2445,8 @@ mod tests {
24082445
|-- fixed_size_binary: fixed_size_binary (nullable = true)
24092446
|-- fixed_size_list: fixed size list (nullable = false)
24102447
| |-- item: int32 (nullable = true)
2448+
|-- decimal32: decimal32(9, 4) (nullable = true)
2449+
|-- decimal64: decimal64(9, 4) (nullable = true)
24112450
|-- decimal128: decimal128(18, 4) (nullable = true)
24122451
|-- decimal256: decimal256(38, 10) (nullable = false)
24132452
|-- date32: date32 (nullable = true)

0 commit comments

Comments
 (0)