Skip to content

Commit

Permalink
Return error when internal multiplication overflowing in decimal divi…
Browse files Browse the repository at this point in the history
…sion kernel (#6833)
  • Loading branch information
viirya authored Jul 6, 2023
1 parent f06185e commit eb03b4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/decimal.slt
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,12 @@ query R
select try_cast(1234567 as decimal(7,3));
----
NULL

statement ok
create table foo (a DECIMAL(38, 20), b DECIMAL(38, 0));

statement ok
insert into foo VALUES (1, 5);

query error DataFusion error: Arrow error: Compute error: Overflow happened on: 100000000000000000000 \* 100000000000000000000000000000000000000
select a / b from foo;
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

use arrow::compute::{
add_dyn, add_scalar_dyn, divide_dyn_checked, divide_scalar_dyn, modulus_dyn,
modulus_scalar_dyn, multiply_fixed_point, multiply_scalar_dyn, subtract_dyn,
subtract_scalar_dyn, try_unary,
modulus_scalar_dyn, multiply_fixed_point, multiply_scalar_checked_dyn,
multiply_scalar_dyn, subtract_dyn, subtract_scalar_dyn, try_unary,
};
use arrow::datatypes::{Date32Type, Date64Type, Decimal128Type};
use arrow::{array::*, datatypes::ArrowNumericType};
Expand Down Expand Up @@ -662,7 +662,7 @@ pub(crate) fn divide_decimal_dyn_scalar(
let (precision, scale) = get_precision_scale(result_type)?;

let mul = 10_i128.pow(scale as u32);
let array = multiply_scalar_dyn::<Decimal128Type>(left, mul)?;
let array = multiply_scalar_checked_dyn::<Decimal128Type>(left, mul)?;

let array = divide_scalar_dyn::<Decimal128Type>(&array, right)?;
decimal_array_with_precision_scale(array, precision, scale)
Expand Down Expand Up @@ -719,7 +719,7 @@ pub(crate) fn divide_dyn_checked_decimal(
let (precision, scale) = get_precision_scale(result_type)?;

let mul = 10_i128.pow(scale as u32);
let array = multiply_scalar_dyn::<Decimal128Type>(left, mul)?;
let array = multiply_scalar_checked_dyn::<Decimal128Type>(left, mul)?;

// Restore to original precision and scale (metadata only)
let (org_precision, org_scale) = get_precision_scale(right.data_type())?;
Expand Down

0 comments on commit eb03b4f

Please sign in to comment.