Skip to content

Commit

Permalink
fix(query):continue to fix decimal overflow check (databendlabs#13438)
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li authored and andylokandy committed Nov 27, 2023
1 parent cb48eb4 commit 74e001b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/query/functions/src/scalars/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,14 +1309,18 @@ 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()
.map(|(row, x)| {
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()
Expand Down Expand Up @@ -1348,14 +1352,18 @@ 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()
.map(|(row, x)| {
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
}
_ => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 74e001b

Please sign in to comment.