From 74e001b70c246bc0d108e6053221df4aac190816 Mon Sep 17 00:00:00 2001 From: sundyli <543950155@qq.com> Date: Tue, 24 Oct 2023 22:01:22 -0700 Subject: [PATCH] fix(query):continue to fix decimal overflow check (#13438) --- src/query/functions/src/scalars/decimal.rs | 12 ++++++++++-- .../base/11_data_type/11_0006_data_type_decimal | 7 ++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/query/functions/src/scalars/decimal.rs b/src/query/functions/src/scalars/decimal.rs index c8fdfaf0411a5..a2f1759f7cabb 100644 --- a/src/query/functions/src/scalars/decimal.rs +++ b/src/query/functions/src/scalars/decimal.rs @@ -1309,6 +1309,8 @@ fn decimal_256_to_128( .collect() } else { let factor = i256::e((from_size.scale - dest_size.scale) as u32); + let source_factor = i256::e(from_size.scale as u32); + buffer .iter() .enumerate() @@ -1316,7 +1318,9 @@ fn decimal_256_to_128( let x = x * i128::one(); match x.checked_div(factor) { - Some(y) if (y <= max && y >= min) && !(y == 0 && x > 0) => *y.low(), + Some(y) if (y <= max && y >= min) && (y != 0 || x / source_factor == 0) => { + *y.low() + } _ => { ctx.set_error(row, concat!("Decimal overflow at line : ", line!())); i128::one() @@ -1348,6 +1352,8 @@ macro_rules! m_decimal_to_decimal { let factor = <$dest_type_name>::e(($from_size.scale - $dest_size.scale) as u32); let max = <$dest_type_name>::max_for_precision($dest_size.precision); let min = <$dest_type_name>::min_for_precision($dest_size.precision); + + let source_factor = <$from_type_name>::e($from_size.scale as u32); $buffer .iter() .enumerate() @@ -1355,7 +1361,9 @@ macro_rules! m_decimal_to_decimal { let x = x * <$dest_type_name>::one(); match x.checked_div(factor) { - Some(y) if y <= max && y >= min && !(y == 0 && x > 0) => { + Some(y) + if y <= max && y >= min && (y != 0 || x / source_factor == 0) => + { y as $dest_type_name } _ => { diff --git a/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal b/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal index b86ba5f5544b5..2e89211aa1a48 100644 --- a/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal +++ b/tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal @@ -905,10 +905,11 @@ select try_cast('34343' as Decimal(7,2)), try_cast(number::String as Decimal(7, 34343.00 1.000 34343.00 2.000 -query TT -select 1.234::DECIMAL(76,3)::DECIMAL(2,1), 1.234::DECIMAL(76,3)::DECIMAL(6,4) +query TTT +select 1.234::DECIMAL(76,3)::DECIMAL(2,1), 1.234::DECIMAL(76,3)::DECIMAL(6,4), cast(0.50 as decimal(10,0)) ---- -1.2 1.2340 +1.2 1.2340 0 + statement ok create table t(c1 decimal(28) not null)