Apache DataFusion SQL Query Engine
Extend binary coercion rules to support Decimal arithmetic operations with integer(signed and unsigned) types (#16668)
Jagdish Parihar8a0227e5c234
diff --git a/datafusion/expr-common/src/type_coercion/binary.rs b/datafusion/expr-common/src/type_coercion/binary.rsindex 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.sltindex 089910785a..2c91e82041 100644--- a/datafusion/sqllogictest/test_files/decimal.slt+++ b/datafusion/sqllogictest/test_files/decimal.slt@@ -747,3 +747,31 @@ SELECTcast(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