Skip to content

Commit

Permalink
Fix cast to decimal overflow bug (#3922) (#3942)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jan 27, 2022
1 parent 2748f10 commit 48a2afc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
24 changes: 15 additions & 9 deletions dbms/src/Functions/FunctionsTiDBConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -792,21 +792,27 @@ struct TiDBConvertToDecimal
using FromFieldType = typename FromDataType::FieldType;

template <typename T, typename U>
static U toTiDBDecimalInternal(T value, PrecType prec, ScaleType scale, const Context & context)
static U toTiDBDecimalInternal(T int_value, PrecType prec, ScaleType scale, const Context & context)
{
// int_value is the value that exposes to user. Such as cast(val to decimal), val is the int_value which used by user.
// And val * scale_mul is the scaled_value, which is stored in ColumnDecimal internally.
static_assert(std::is_integral_v<T>);
using UType = typename U::NativeType;
auto maxValue = DecimalMaxValue::Get(prec);
if (value > maxValue || value < -maxValue)
UType scale_mul = getScaleMultiplier<U>(scale);

Int256 scaled_value = static_cast<Int256>(int_value) * static_cast<Int256>(scale_mul);
Int256 scaled_max_value = DecimalMaxValue::Get(prec);

if (scaled_value > scaled_max_value || scaled_value < -scaled_max_value)
{
context.getDAGContext()->handleOverflowError("cast to decimal", Errors::Types::Truncated);
if (value > 0)
return static_cast<UType>(maxValue);
if (int_value > 0)
return static_cast<UType>(scaled_max_value);
else
return static_cast<UType>(-maxValue);
return static_cast<UType>(-scaled_max_value);
}
UType scale_mul = getScaleMultiplier<U>(scale);
U result = static_cast<UType>(value) * scale_mul;
return result;

return static_cast<UType>(scaled_value);
}

template <typename U>
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Storages/Page/PageUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void syncFile(WritableFilePtr & file)

#ifndef NDEBUG
void writeFile(
WritableFilePtr & file, UInt64 offset, char * data, size_t to_write, const RateLimiterPtr & rate_limiter, bool enable_failpoint)
WritableFilePtr & file, UInt64 offset, char * data, size_t to_write, const RateLimiterPtr & rate_limiter, bool enable_failpoint [[ maybe_unused ]])
#else
void writeFile(WritableFilePtr & file, UInt64 offset, char * data, size_t to_write, const RateLimiterPtr & rate_limiter)
#endif
Expand Down
17 changes: 17 additions & 0 deletions tests/fullstack-test/expr/cast_as_decimal.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mysql> drop table if exists test.t1;
mysql> create table test.t1(c1 int);
mysql> insert into test.t1 values(9999), (-9999), (99), (-99);
mysql> alter table test.t1 set tiflash replica 1;
func> wait_table test t1
mysql> set @@tidb_isolation_read_engines='tiflash'; select cast(c1 as decimal(4, 1)) from test.t1 order by 1;
cast(c1 as decimal(4, 1))
-999.9
-99.0
99.0
999.9
mysql> set @@tidb_isolation_read_engines='tiflash'; select cast(c1 as decimal(2, 2)) from test.t1 order by 1;
cast(c1 as decimal(2, 2))
-0.99
-0.99
0.99
0.99

0 comments on commit 48a2afc

Please sign in to comment.