You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by al...@apache.org on 2025/07/13 12:43:11 UTC
(datafusion) branch main updated: Extend binary coercion rules to support Decimal arithmetic operations with integer(signed and unsigned) types (#16668)
This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 8a0227e5c2 Extend binary coercion rules to support Decimal arithmetic operations with integer(signed and unsigned) types (#16668)
8a0227e5c2 is described below
commit 8a0227e5c234a8e1ed06f2bc156a8aec22400e41
Author: Jagdish Parihar <ja...@gmail.com>
AuthorDate: Sun Jul 13 18:13:07 2025 +0530
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 <an...@nerdnetworks.org>
---
datafusion/expr-common/src/type_coercion/binary.rs | 37 +++++++++++++++++-----
datafusion/sqllogictest/test_files/decimal.slt | 28 ++++++++++++++++
2 files changed, 57 insertions(+), 8 deletions(-)
diff --git a/datafusion/expr-common/src/type_coercion/binary.rs b/datafusion/expr-common/src/type_coercion/binary.rs
index 955c28c42a..098a930842 100644
--- a/datafusion/expr-common/src/type_coercion/binary.rs
+++ b/datafusion/expr-common/src/type_coercion/binary.rs
@@ -307,17 +307,25 @@ fn math_decimal_coercion(
}
// Unlike with comparison we don't coerce to a decimal in the case of floating point
// numbers, instead falling back to floating point arithmetic instead
- (Decimal128(_, _), Int8 | Int16 | Int32 | Int64) => {
- Some((lhs_type.clone(), coerce_numeric_type_to_decimal(rhs_type)?))
- }
- (Int8 | Int16 | Int32 | Int64, Decimal128(_, _)) => {
- Some((coerce_numeric_type_to_decimal(lhs_type)?, rhs_type.clone()))
- }
- (Decimal256(_, _), Int8 | Int16 | Int32 | Int64) => Some((
+ (
+ Decimal128(_, _),
+ Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
+ ) => Some((lhs_type.clone(), coerce_numeric_type_to_decimal(rhs_type)?)),
+ (
+ Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
+ Decimal128(_, _),
+ ) => Some((coerce_numeric_type_to_decimal(lhs_type)?, rhs_type.clone())),
+ (
+ Decimal256(_, _),
+ Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
+ ) => Some((
lhs_type.clone(),
coerce_numeric_type_to_decimal256(rhs_type)?,
)),
- (Int8 | Int16 | Int32 | Int64, Decimal256(_, _)) => Some((
+ (
+ Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
+ Decimal256(_, _),
+ ) => Some((
coerce_numeric_type_to_decimal256(lhs_type)?,
rhs_type.clone(),
)),
@@ -2199,6 +2207,19 @@ mod tests {
DataType::Decimal128(10, 2),
);
+ test_math_decimal_coercion_rule(
+ DataType::UInt32,
+ DataType::Decimal128(10, 2),
+ DataType::Decimal128(10, 0),
+ DataType::Decimal128(10, 2),
+ );
+ test_math_decimal_coercion_rule(
+ DataType::Decimal128(10, 2),
+ DataType::UInt32,
+ DataType::Decimal128(10, 2),
+ DataType::Decimal128(10, 0),
+ );
+
Ok(())
}
diff --git a/datafusion/sqllogictest/test_files/decimal.slt b/datafusion/sqllogictest/test_files/decimal.slt
index 089910785a..2c91e82041 100644
--- a/datafusion/sqllogictest/test_files/decimal.slt
+++ b/datafusion/sqllogictest/test_files/decimal.slt
@@ -747,3 +747,31 @@ SELECT
cast(cast('5.20' as decimal(4,2)) as decimal(3,2))
----
0 5.2
+
+query RR
+SELECT
+ arrow_cast(1.23,'Decimal128(3,2)') - arrow_cast(123, 'UInt64') as subtration_uint,
+ arrow_cast(1.23,'Decimal128(3,2)') - arrow_cast(123, 'Int64') as subtration_int
+----
+-121.77 -121.77
+
+query RR
+SELECT
+ arrow_cast(1.23,'Decimal128(3,2)') + arrow_cast(123, 'UInt64') as addition_uint,
+ arrow_cast(1.23,'Decimal128(3,2)') + arrow_cast(123, 'Int64') as addition_int
+----
+124.23 124.23
+
+query RR
+SELECT
+ arrow_cast(1.23,'Decimal128(3,2)') * arrow_cast(123, 'UInt64') as mulitplication_uint,
+ arrow_cast(1.23,'Decimal128(3,2)') * arrow_cast(123, 'Int64') as multiplication_int
+----
+151.29 151.29
+
+query RR
+SELECT
+ arrow_cast(1.23,'Decimal128(3,2)') / arrow_cast(123, 'UInt64') as divison_uint,
+ arrow_cast(1.23,'Decimal128(3,2)') / arrow_cast(123, 'Int64') as divison_int
+----
+0.01 0.01
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org