Skip to content

Commit

Permalink
fix: throw err when cast Decimal overflow (#14489)
Browse files Browse the repository at this point in the history
fix: throw arr when cast Decimal overflow
  • Loading branch information
guojidan authored Jan 27, 2024
1 parent ca3d135 commit 8c55e07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/query/functions/src/scalars/decimal/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,16 +500,22 @@ where
let max_for_precision = T::max_for_precision(size.precision);

let f = |x: S::ScalarRef<'_>, builder: &mut Vec<T>, ctx: &mut EvalContext| {
let x = T::from_i128(x.as_()) * multiplier;

if x > max_for_precision || x < min_for_precision {
if let Some(x) = T::from_i128(x.as_()).checked_mul(multiplier) {
if x > max_for_precision || x < min_for_precision {
ctx.set_error(
builder.len(),
concat!("Decimal overflow at line : ", line!()),
);
builder.push(T::one());
} else {
builder.push(x);
}
} else {
ctx.set_error(
builder.len(),
concat!("Decimal overflow at line : ", line!()),
);
builder.push(T::one());
} else {
builder.push(x);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ select 1::Decimal(41, 3)::Float64, 2::Decimal(42, 4)::Float64, 3::Decimal(55, 5)
----
1.0 2.0 3.0 10.0

# cast overflow
statement error 1006
select CAST(1754842 AS Decimal(69, 63))

statement error 1006
select CAST(17548423738721552635 AS Decimal(69, 63))

## to_decimal(decimal) overflow
statement error 1006
select 10000::decimal(5,0)::decimal(3,0);
Expand Down

0 comments on commit 8c55e07

Please sign in to comment.