Skip to content

Commit 8a0227e

Browse files
jatin510alamb
andauthored
Extend binary coercion rules to support Decimal arithmetic operations with integer(signed and unsigned) types (#16668)
* Extend binary coercion rules to support Decimal arithmetic operations with integer(signed and unsigned) types * added test for binary operation between decimal and integer types --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent c8bd776 commit 8a0227e

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

datafusion/expr-common/src/type_coercion/binary.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,25 @@ fn math_decimal_coercion(
307307
}
308308
// Unlike with comparison we don't coerce to a decimal in the case of floating point
309309
// numbers, instead falling back to floating point arithmetic instead
310-
(Decimal128(_, _), Int8 | Int16 | Int32 | Int64) => {
311-
Some((lhs_type.clone(), coerce_numeric_type_to_decimal(rhs_type)?))
312-
}
313-
(Int8 | Int16 | Int32 | Int64, Decimal128(_, _)) => {
314-
Some((coerce_numeric_type_to_decimal(lhs_type)?, rhs_type.clone()))
315-
}
316-
(Decimal256(_, _), Int8 | Int16 | Int32 | Int64) => Some((
310+
(
311+
Decimal128(_, _),
312+
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
313+
) => Some((lhs_type.clone(), coerce_numeric_type_to_decimal(rhs_type)?)),
314+
(
315+
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
316+
Decimal128(_, _),
317+
) => Some((coerce_numeric_type_to_decimal(lhs_type)?, rhs_type.clone())),
318+
(
319+
Decimal256(_, _),
320+
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
321+
) => Some((
317322
lhs_type.clone(),
318323
coerce_numeric_type_to_decimal256(rhs_type)?,
319324
)),
320-
(Int8 | Int16 | Int32 | Int64, Decimal256(_, _)) => Some((
325+
(
326+
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
327+
Decimal256(_, _),
328+
) => Some((
321329
coerce_numeric_type_to_decimal256(lhs_type)?,
322330
rhs_type.clone(),
323331
)),
@@ -2199,6 +2207,19 @@ mod tests {
21992207
DataType::Decimal128(10, 2),
22002208
);
22012209

2210+
test_math_decimal_coercion_rule(
2211+
DataType::UInt32,
2212+
DataType::Decimal128(10, 2),
2213+
DataType::Decimal128(10, 0),
2214+
DataType::Decimal128(10, 2),
2215+
);
2216+
test_math_decimal_coercion_rule(
2217+
DataType::Decimal128(10, 2),
2218+
DataType::UInt32,
2219+
DataType::Decimal128(10, 2),
2220+
DataType::Decimal128(10, 0),
2221+
);
2222+
22022223
Ok(())
22032224
}
22042225

datafusion/sqllogictest/test_files/decimal.slt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,31 @@ SELECT
747747
cast(cast('5.20' as decimal(4,2)) as decimal(3,2))
748748
----
749749
0 5.2
750+
751+
query RR
752+
SELECT
753+
arrow_cast(1.23,'Decimal128(3,2)') - arrow_cast(123, 'UInt64') as subtration_uint,
754+
arrow_cast(1.23,'Decimal128(3,2)') - arrow_cast(123, 'Int64') as subtration_int
755+
----
756+
-121.77 -121.77
757+
758+
query RR
759+
SELECT
760+
arrow_cast(1.23,'Decimal128(3,2)') + arrow_cast(123, 'UInt64') as addition_uint,
761+
arrow_cast(1.23,'Decimal128(3,2)') + arrow_cast(123, 'Int64') as addition_int
762+
----
763+
124.23 124.23
764+
765+
query RR
766+
SELECT
767+
arrow_cast(1.23,'Decimal128(3,2)') * arrow_cast(123, 'UInt64') as mulitplication_uint,
768+
arrow_cast(1.23,'Decimal128(3,2)') * arrow_cast(123, 'Int64') as multiplication_int
769+
----
770+
151.29 151.29
771+
772+
query RR
773+
SELECT
774+
arrow_cast(1.23,'Decimal128(3,2)') / arrow_cast(123, 'UInt64') as divison_uint,
775+
arrow_cast(1.23,'Decimal128(3,2)') / arrow_cast(123, 'Int64') as divison_int
776+
----
777+
0.01 0.01

0 commit comments

Comments
 (0)