Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error when internal multiplication overflowing in decimal division kernel #6833

Merged
merged 1 commit into from
Jul 6, 2023
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW this is what postgres does (treats it like a float, I think):

postgres=# create table foo (a DECIMAL(38, 20), b DECIMAL(38, 0));
CREATE TABLE
postgres=# insert into foo VALUES (1, 5);
INSERT 0 1
postgres=# select a / b from foo;
        ?column?
------------------------
 0.20000000000000000000
(1 row)

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