Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions datafusion/expr/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ pub fn coercion_decimal_mathematics_type(
left_decimal_type,
right_decimal_type,
),
Operator::Multiply | Operator::Divide | Operator::Modulo => {
Operator::Divide | Operator::Modulo => {
get_wider_decimal_type(left_decimal_type, right_decimal_type)
}
_ => None,
Expand Down Expand Up @@ -946,7 +946,7 @@ mod tests {
&left_decimal_type,
&right_decimal_type,
);
assert_eq!(DataType::Decimal128(20, 4), result.unwrap());
assert_eq!(None, result);
let result =
decimal_op_mathematics_type(&op, &left_decimal_type, &right_decimal_type);
assert_eq!(DataType::Decimal128(31, 7), result.unwrap());
Expand Down Expand Up @@ -1232,7 +1232,7 @@ mod tests {
mathematics_op: Operator,
expected_lhs_type: Option<DataType>,
expected_rhs_type: Option<DataType>,
expected_coerced_type: DataType,
expected_coerced_type: Option<DataType>,
expected_output_type: DataType,
) {
// The coerced types for lhs and rhs, if any of them is not decimal
Expand All @@ -1245,8 +1245,7 @@ mod tests {

// The coerced type of decimal math expression, applied during expression evaluation
let coerced_type =
coercion_decimal_mathematics_type(&mathematics_op, &lhs_type, &rhs_type)
.unwrap();
coercion_decimal_mathematics_type(&mathematics_op, &lhs_type, &rhs_type);
assert_eq!(coerced_type, expected_coerced_type);

// The output type of decimal math expression
Expand All @@ -1263,7 +1262,7 @@ mod tests {
Operator::Plus,
None,
None,
DataType::Decimal128(11, 2),
Some(DataType::Decimal128(11, 2)),
DataType::Decimal128(11, 2),
);

Expand All @@ -1273,7 +1272,7 @@ mod tests {
Operator::Plus,
Some(DataType::Decimal128(10, 0)),
None,
DataType::Decimal128(13, 2),
Some(DataType::Decimal128(13, 2)),
DataType::Decimal128(13, 2),
);

Expand All @@ -1283,7 +1282,7 @@ mod tests {
Operator::Minus,
Some(DataType::Decimal128(10, 0)),
None,
DataType::Decimal128(13, 2),
Some(DataType::Decimal128(13, 2)),
DataType::Decimal128(13, 2),
);

Expand All @@ -1293,7 +1292,7 @@ mod tests {
Operator::Multiply,
Some(DataType::Decimal128(10, 0)),
None,
DataType::Decimal128(12, 2),
None,
DataType::Decimal128(21, 2),
);

Expand All @@ -1303,7 +1302,7 @@ mod tests {
Operator::Divide,
Some(DataType::Decimal128(10, 0)),
None,
DataType::Decimal128(12, 2),
Some(DataType::Decimal128(12, 2)),
DataType::Decimal128(23, 11),
);

Expand All @@ -1313,7 +1312,7 @@ mod tests {
Operator::Modulo,
Some(DataType::Decimal128(10, 0)),
None,
DataType::Decimal128(12, 2),
Some(DataType::Decimal128(12, 2)),
DataType::Decimal128(10, 2),
);

Expand Down
22 changes: 3 additions & 19 deletions datafusion/physical-expr/src/expressions/binary/kernels_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ use arrow_schema::DataType;
use datafusion_common::cast::{as_date32_array, as_date64_array, as_decimal128_array};
use datafusion_common::scalar::{date32_add, date64_add};
use datafusion_common::{DataFusionError, Result, ScalarValue};
use datafusion_expr::type_coercion::binary::decimal_op_mathematics_type;
use datafusion_expr::ColumnarValue;
use datafusion_expr::Operator;
use std::cmp::min;
use std::sync::Arc;

Expand Down Expand Up @@ -469,24 +467,8 @@ pub(crate) fn multiply_decimal_dyn_scalar(
result_type: &DataType,
) -> Result<ArrayRef> {
let (precision, scale) = get_precision_scale(result_type)?;

let op_type = decimal_op_mathematics_type(
&Operator::Multiply,
left.data_type(),
left.data_type(),
)
.unwrap();
let (_, op_scale) = get_precision_scale(&op_type)?;

let array = multiply_scalar_dyn::<Decimal128Type>(left, right)?;

if op_scale > scale {
let div = 10_i128.pow((op_scale - scale) as u32);
let array = divide_scalar_dyn::<Decimal128Type>(&array, div)?;
decimal_array_with_precision_scale(array, precision, scale)
} else {
decimal_array_with_precision_scale(array, precision, scale)
}
decimal_array_with_precision_scale(array, precision, scale)
}

pub(crate) fn divide_decimal_dyn_scalar(
Expand Down Expand Up @@ -703,6 +685,8 @@ pub(crate) fn modulus_decimal_dyn_scalar(
#[cfg(test)]
mod tests {
use super::*;
use datafusion_expr::type_coercion::binary::decimal_op_mathematics_type;
use datafusion_expr::Operator;

fn create_decimal_array(
array: &[Option<i128>],
Expand Down